fork download
  1. #include <iostream>
  2. using namespace std;
  3. #define int long long int
  4. const int N = 2e5+10;
  5.  
  6. int arr[N];
  7. int segTree[4*N+1];
  8.  
  9. void buildSegTree(int idx, int low, int high){
  10. if(low == high){
  11. segTree[idx] = 0;
  12. return;
  13. }
  14. int mid = (low + high) >> 1;
  15. buildSegTree(2*idx, low, mid);
  16. buildSegTree(2*idx+1, mid+1, high);
  17. segTree[idx] = segTree[2*idx]+segTree[2*idx+1];
  18. }
  19.  
  20. void updateSegTreePoint(int idx, int low, int high, int index, int val){
  21. if(index<low or index>high){
  22. return;
  23. }
  24. if(low == high){
  25. segTree[idx] += val;
  26. return;
  27. }
  28.  
  29. int mid = (low+high)>>1;
  30.  
  31. if(index <= mid){
  32. updateSegTreePoint(2*idx, low, mid, index, val);
  33. } else {
  34. updateSegTreePoint(2*idx+1, mid+1, high, index, val);
  35. }
  36. segTree[idx] = segTree[2*idx]+segTree[2*idx+1];
  37. }
  38.  
  39. int querySegTree(int idx, int low, int high, int qs, int qe){
  40. if(qs <= low and high <= qe){
  41. return segTree[idx];
  42. }
  43.  
  44. if(qe<low or qs>high or low>high){
  45. return 0;
  46. }
  47. int mid = (low+high)>>1;
  48. int left = querySegTree(2*idx, low, mid, qs, qe);
  49. int right = querySegTree(2*idx+1, mid+1, high, qs, qe);
  50. return left+right;
  51. }
  52.  
  53. int32_t main() {
  54. int n, q;
  55. cin >> n >> q;
  56. for(int i=0; i<n; i++){
  57. cin>>arr[i];
  58. }
  59. buildSegTree(1, 0, n-1);
  60. while(q--){
  61. int t;
  62. cin>>t;
  63. if(t==1){
  64. int a,b,u;
  65. cin>>a>>b>>u;
  66. updateSegTreePoint(1, 0, n-1, a-1, u);
  67. updateSegTreePoint(1, 0, n-1, b, -u);
  68. } else {
  69. int k;
  70. cin>>k;
  71. cout << querySegTree(1, 0, n-1, 0, k-1)+arr[k-1] << endl;
  72. }
  73. }
  74. return 0;
  75. }
Success #stdin #stdout 0.01s 5280KB
stdin
8 3
3 2 4 5 1 1 5 3
2 4
1 2 5 1
2 4
stdout
5
6