fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct wezel
  5. {
  6. int dod;
  7. int a;
  8. int b;
  9. };
  10. wezel poj[4000005];
  11. int t[4000005];
  12.  
  13. void dodaj(int v, int tl, int tr, int l, int r, int add) //dodawanie do przedziału l,r
  14. {
  15. // cout<<v<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<" "<<add<<endl;
  16. if(l>r)
  17. return;
  18. if(tr==r && tl==l) //wezel zawiera cale poddrzewo ciagu
  19. {
  20. poj[v].dod+=add;
  21. return;
  22. }
  23. else //a co teraz
  24. {
  25. int tm = (tl + tr) / 2;
  26. //lewe poddrzewo tl-tm, prawe poddrzewo tm+1-tr;
  27. if(r<tm)//tylko lewe podrzewo
  28. {
  29. dodaj (v*2, tl, tm, l, r , add);
  30. }
  31. else
  32. if(l>tm) //tylko prawe poddrzewo
  33. dodaj(v*2+1,tm+1,tr,l,r,add);
  34. else //oba poddrzewa
  35. {
  36. dodaj (v*2, tl, tm, l, tm, add); //lewe
  37. dodaj(v*2+1,tm+1,tr,tm+1,r,add);
  38. }
  39. }
  40. }
  41.  
  42. int get (int v, int tl, int tr, int pos) {
  43. if (tl == tr)
  44. return poj[v].dod;
  45. int tm = (tl + tr) / 2;
  46. if (pos <= tm)
  47. return poj[v].dod + get (v*2, tl, tm, pos);
  48. else
  49. return poj[v].dod + get (v*2+1, tm+1, tr, pos);
  50. }
  51. int main() {
  52. int n, m;
  53. int a,b,ile,p,q;
  54. cin >> n >> m; // Wczytanie liczby pojemników i liczby czynności
  55.  
  56. for (int i = 0; i < m; ++i) {
  57. cin>>q;
  58. if(q==0)
  59. {
  60. cin>>a>>b>>ile;
  61. dodaj(1,1,n,a,b,ile);
  62. }
  63. else
  64. {
  65. cin>>p;
  66. cout<<get(1,1,n,p)<<endl;
  67. }
  68. }
  69.  
  70. // countStones(n,p);
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5492KB
stdin
4 6
0 1 4 2
0 2 2 1
1 2
0 1 4 3
1 2
1 3
stdout
3
6
5