fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. struct data {
  5. int sum, pref, suff, ans;
  6. };
  7. data combine (data l, data r) {
  8. data res;
  9. res.sum = l.sum + r.sum;
  10. res.pref = max (l.pref, l.sum + r.pref);
  11. res.suff = max (r.suff, r.sum + l.suff);
  12. res.ans = max (max (l.ans, r.ans), l.suff + r.pref);
  13. return res;
  14. }
  15. data make_data (int val) {
  16. data res;
  17. res.sum = val;
  18. res.pref = res.suff = res.ans = val;
  19. return res;
  20. }
  21. void build (int a[], int v, int tl, int tr, data t[]) {
  22. if (tl == tr)
  23. t[v] = make_data (a[tl]);
  24. else {
  25. int tm = (tl + tr) / 2;
  26. build (a, v*2, tl, tm, t);
  27. build (a, v*2+1, tm+1, tr, t);
  28. t[v] = combine (t[v*2], t[v*2+1]);
  29. }
  30. }
  31. void update (int v, int tl, int tr, int pos, int new_val, data t[]) {
  32. if (tl == tr)
  33. t[v] = make_data (new_val);
  34. else {
  35. int tm = (tl + tr) / 2;
  36. if (pos <= tm)
  37. update (v*2, tl, tm, pos, new_val, t);
  38. else
  39. update (v*2+1, tm+1, tr, pos, new_val, t);
  40. t[v] = combine (t[v*2], t[v*2+1]);
  41. }
  42. }
  43. data query (int v, int tl, int tr, int l, int r, data t[]) {
  44. if (l == tl && tr == r)
  45. return t[v];
  46. int tm = (tl + tr) / 2;
  47. if (r <= tm)
  48. return query (v*2, tl, tm, l, r, t);
  49. if (l > tm)
  50. return query (v*2+1, tm+1, tr, l, r, t);
  51. return combine (
  52. query (v*2, tl, tm, l, tm, t),
  53. query (v*2+1, tm+1, tr, tm+1, r, t)
  54. );
  55. }
  56.  
  57. int main() {
  58. int n, m, x, y;
  59. cin >> n;
  60. int* a = new int[n];
  61. for(int i = 0; i < n; i++) {
  62. cin >> a[i];
  63. }
  64. data* t = new data[4*n];
  65. build(a, 1, 0, n-1, t);
  66. cin >> m ;
  67. bool oper;
  68. for(int i = 0; i < m; i++) {
  69. cin >> oper >> x >> y;
  70. if(!oper) {
  71. update (1, 0, n-1, x-1, y, t);
  72. } else if(oper) {
  73. cout << query (1, 0, n-1, x-1, y-1, t).ans << endl;
  74.  
  75. }
  76. }
  77. return 0;
  78. }
Success #stdin #stdout 0s 3464KB
stdin
4
1 2 3 4
4
1 1 3
0 3 -3
1 2 4
1 3 3
stdout
6
4
-3