fork download
  1. #include<bits/stdc++.h>
  2. using namespace::std;
  3.  
  4. const long long inf = 1e18;
  5.  
  6. struct node{
  7. long long ans;
  8. long long total;
  9. long long prefix;
  10. long long suffix;
  11. node(){
  12. ans = prefix = suffix = -inf;
  13. total = 0;
  14. }
  15. node(int x){
  16. ans = total = prefix = suffix = x;
  17. }
  18.  
  19. node operator + (const node R){
  20. node q;
  21. q.ans = max(ans, max(R.ans, suffix + R.prefix));
  22. q.suffix = max(R.suffix, R.total + suffix);
  23. q.prefix = max(prefix, total + R.prefix);
  24. q.total = total + R.total;
  25. return q;
  26. }
  27. };
  28.  
  29. template<class T>
  30. struct SegTree{
  31. int n;
  32. vector<T> st;
  33.  
  34. void set(vector<T> &a){
  35. n = a.size();
  36. st.assign(4 * n, 0);
  37. for(int i = 0; i < n; i++) update(i, a[i]);
  38. }
  39.  
  40. void update(int x, T y, int pos, int l, int r){
  41. if(l == r){
  42. st[pos] = y;
  43. return;
  44. }
  45. int mi = (l + r) / 2;
  46. if(x <= mi) update(x, y, 2 * pos, l, mi);
  47. else update(x, y, 2 * pos + 1, mi + 1, r);
  48. st[pos] = st[2 * pos] + st[2 * pos + 1];
  49. }
  50.  
  51. T query(int x, int y, int pos, int l, int r){
  52. if(y < l or r < x) return T();
  53. if(x <= l and r <= y) return st[pos];
  54. int mi = (l + r) / 2;
  55. return query(x, y, 2 * pos, l, mi) + query(x, y, 2 * pos + 1, mi + 1, r);
  56. }
  57.  
  58. void update(int x, T y){
  59. update(x, y, 1, 0, n - 1);
  60. }
  61.  
  62. T query(int x, int y){
  63. return query(x, y, 1, 0, n - 1);
  64. }
  65. };
  66.  
  67. int main(){
  68. int n, q;
  69. scanf("%d %d", &n, &q);
  70. vector<node> a(n);
  71. for(int i = 0; i < n; i++){
  72. int x;
  73. scanf("%d", &x);
  74. a[i] = node(x);
  75. }
  76. SegTree<node> st;
  77. st.set(a);
  78. printf("%lld\n", max(0ll, st.st[1].ans));
  79. while(q--){
  80. int i, v;
  81. scanf("%d %d", &i, &v);
  82. st.update(i, node(v));
  83. printf("%lld\n", max(0ll, st.st[1].ans));
  84. }
  85. return 0;
  86. }
Success #stdin #stdout 0.01s 5424KB
stdin
5 2
5 -4 4 3 -5
4 3
3 -1
stdout
8
11
7