fork(1) download
  1.  
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define ll long long
  5.  
  6. //Build Segment Tree
  7. void build_tree(ll int *a, ll int s,ll int e, ll int *tree, ll int index)
  8. {
  9. if(s==e)
  10. {
  11. tree[index] = a[s];
  12. return;
  13. }
  14. ll int mid = (s+e)/2;
  15. build_tree(a,s,mid,tree,2*index);
  16. build_tree(a,mid+1,e,tree,2*index+1);
  17. tree[index] += tree[2*index]+tree[2*index+1];
  18. return;
  19. }
  20.  
  21. //Query
  22. ll query(ll *tree,ll ss,ll se,ll qs,ll qe ,ll index)
  23. {
  24. //complete overlap
  25. if(ss>=qs and se<=qe)
  26. {
  27. return tree[index];
  28. }
  29. //No Overlap
  30. if(qe<ss || qs>se)
  31. return 0;
  32.  
  33. //partial overlap
  34. ll int mid = (ss + se)/2;
  35. ll int left = query(tree,ss,mid,qs,qe,2*index);
  36. ll int right = query(tree,mid+1,se,qs,qe,2*index+1);
  37. return left+right;
  38. }
  39.  
  40. //point update
  41. void point_update(ll *tree, ll ss,ll se, ll i,ll inc,ll index)
  42. {
  43. if(i>se || i<ss)
  44. return;
  45. if(ss == se)
  46. {
  47. tree[index] = inc;
  48. return;
  49. }
  50. ll int mid = (ss + se)/2;
  51. point_update(tree,ss,mid,i,inc,2*index);
  52. point_update(tree,mid+1,se,i,inc,2*index+1);
  53. tree[index] = tree[2*index]+tree[2*index+1];
  54. return;
  55. }
  56.  
  57. void solve()
  58. {
  59. int n,q;
  60. cin>>n>>q;
  61. ll a[n];
  62. for(int i=0;i<n;i++)
  63. cin>>a[i];
  64. ll tree[4*n+1]={0};
  65. build_tree(a,0,n-1,tree,1);
  66. // for(int i=1;i<=4*n;i++)
  67. // cout<<tree[i]<<endl;
  68.  
  69. while(q--)
  70. {
  71. ll num,l,r;
  72. cin>>num>>l>>r;
  73. if(num==1)
  74. {
  75. point_update(tree,0,n-1,l,r,1);
  76.  
  77. }
  78. else
  79. cout<<query(tree,0,n-1,l,r-1,1)<<endl;
  80.  
  81. }
  82.  
  83. }
  84. int main()
  85. {
  86. ios_base::sync_with_stdio(false);
  87. cin.tie(NULL);
  88.  
  89. ll int t=1;
  90. // cin>>t;
  91. while(t--)
  92. {
  93. solve();
  94. }
  95.  
  96. return 0;
  97. }
  98.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:64:10: error: variable-sized object may not be initialized
        ll tree[4*n+1]={0};
                ^~~~~
1 error generated.
stdout
Standard output is empty