DSA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.2 KiB

  1. /*
  2. #pragma GCC target ("avx2")
  3. #pragma GCC optimize ("Ofast")
  4. #pragma GCC optimize ("unroll-loops")
  5. */
  6. #include "bits/stdc++.h"
  7. #define iosb ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  8. #define BIT(x) __builtin_popcount(x)
  9. #define all(x) x.begin() , x.end()
  10. #define F first
  11. #define S second
  12. #define pb push_back
  13. using namespace std ;
  14. typedef unsigned long long UL ;
  15. typedef long long L ;
  16. typedef string T ;
  17. typedef int I ;
  18. const I MaxN = 1e5+1 ;
  19. const I MOD = 1e9+7 ;
  20. const I inf = 2e9+7 ;
  21. const L INF = 2e18+7 ;
  22. L a[MaxN], n, m ;
  23. struct{
  24. L pref = -INF, suff = -INF, sum = -INF, ans = -INF ;
  25. } t[MaxN*4] ;
  26. void build(int v, int tl, int tr)
  27. {
  28. if(tl == tr)
  29. {
  30. t[v].sum = a[tl] ;
  31. t[v].ans = a[tl] ;
  32. t[v].pref = a[tl] ;
  33. t[v].suff = a[tl] ;
  34. return ;
  35. }
  36. int tm = (tl + tr) >> 1 ;
  37. build(v*2, tl, tm) ;
  38. build(v*2+1, tm+1, tr) ;
  39. t[v].ans = max(max(t[v*2].ans, t[v*2+1].ans), t[v*2].suff + t[v*2+1].pref) ;
  40. t[v].sum = t[v*2].sum + t[v*2+1].sum ;
  41. t[v].pref = max(t[v*2].pref, t[v*2].sum + t[v*2+1].pref) ;
  42. t[v].suff = max(t[v*2+1].suff, t[v*2].suff + t[v*2+1].sum) ;
  43. }
  44. void update(int v, int tl, int tr, int l, int x)
  45. {
  46. if(tl == tr && tl == l)
  47. {
  48. t[v].sum = x ;
  49. t[v].ans = x ;
  50. t[v].pref = x ;
  51. t[v].suff = x ;
  52. return ;
  53. }
  54. if(tr < l || l < tl)
  55. return ;
  56. int tm = (tl + tr) >> 1 ;
  57. update(v*2, tl, tm, l, x) ;
  58. update(v*2+1, tm+1, tr, l, x) ;
  59. t[v].ans = max(max(t[v*2].ans, t[v*2+1].ans), t[v*2].suff + t[v*2+1].pref) ;
  60. t[v].sum = t[v*2].sum + t[v*2+1].sum ;
  61. t[v].pref = max(t[v*2].pref, t[v*2].sum + t[v*2+1].pref) ;
  62. t[v].suff = max(t[v*2+1].suff, t[v*2].suff + t[v*2+1].sum) ;
  63. }
  64. int32_t main(int argc, char *argv[])
  65. {
  66. if(argc > 1)
  67. freopen(argv[1], "r", stdin) ;
  68. //freopen(".in" , "r" , stdin) ;
  69. //freopen(".out" , "w" , stdout) ;
  70. cin >> n >> m ;
  71. for(int i = 0 ; i < n ; ++i)
  72. cin >> a[i] ;
  73. build(1, 0, n-1) ;
  74. cout << max(0ll, t[1].ans) << '\n' ;
  75. while(m--)
  76. {
  77. int l, x ;
  78. cin >> l >> x ;
  79. update(1, 0, n-1, l, x) ;
  80. cout << max(0ll, t[1].ans) << '\n' ;
  81. }
  82. }