Advertisement
limimage

seq tree without seq update

May 3rd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3.  
  4. using namespace std;
  5. using ll = long long;
  6. using pii = pair<int, int>;
  7.  
  8. constexpr int N = 5e6+1;
  9. constexpr int MOD = 1e9+7;
  10. constexpr int INF = 1e9+9;
  11.  
  12. int tr[4*N], n, arr[N], q, ql, qr;
  13. char type;
  14.  
  15. void build(int id = 0, int l = 0, int r = n)
  16. {
  17. if (l==r)
  18. return;
  19. if (l+1==r)
  20. {
  21. tr[id]=arr[l];
  22. return;
  23. }
  24. int mid = (r+l)/2;
  25. build(2*id+1, l, mid);
  26. build(2*id+2, mid, r);
  27. tr[id]+=tr[2*id+1]+tr[2*id+2];
  28. }
  29.  
  30. void upd(int val, int i, int id = 0, int l = 0, int r = n)
  31. {
  32. if (l==r)
  33. return;
  34. if (l+1==r)
  35. {
  36. tr[id]+=val;
  37. return;
  38. }
  39. tr[id]+=val;
  40. int mid = (l+r)/2;
  41. if (i<mid)
  42. upd(val, i, 2*id+1, l, mid);
  43. else upd(val, i, 2*id+2, mid, r);
  44. }
  45.  
  46. void update(int i, int val)
  47. {
  48. i--;
  49. int temp = val-arr[i];
  50. arr[i]=val;
  51. val = temp;
  52. upd(val, i);
  53. }
  54.  
  55. int get(int l, int r, int id = 0, int cl = 0, int cr = n)
  56. {
  57. if (l>cr||r<cl||l==r)
  58. return 0;
  59. if (l==cl&&r==cr)
  60. {
  61. return tr[id];
  62. }
  63. int mid = (cl+cr)/2;
  64. return get(max(l, cl), min(r, mid), 2*id+1, cl, mid)+
  65. get(max(l, mid), min(r, cr), 2*id+2, mid, cr);
  66. }
  67.  
  68. int get_sum(int l, int r)
  69. {
  70. l--;
  71. return get(l, r);
  72. }
  73.  
  74. int close(int n)
  75. {
  76. int res = 1;
  77. while(res<n)
  78. {
  79. res<<=1;
  80. }
  81. return res;
  82. }
  83. void Solve()
  84. {
  85. cin >> n >> q;
  86. for (int i = 0; i < n; i++)
  87. cin >> arr[i];
  88. n=close(n);
  89. build();
  90. while(q--)
  91. {
  92. cin >> type >> ql >> qr;
  93. if (type=='=')
  94. update(ql, qr);
  95. else cout << get_sum(ql, qr) << endl;
  96. }
  97. }
  98.  
  99. int main()
  100. {
  101. ios_base::sync_with_stdio(false);
  102. cin.tie(nullptr);
  103. cout.tie(nullptr);
  104. Solve();
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement