Guest User

binary indexed trees

a guest
Jan 21st, 2014
1,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAXN 10
  3. #define LL long long
  4. LL tree[MAXN];
  5.  
  6. // S 1 5 1  means  add 1 to heap 1 to 5
  7. // Q 3  means find how many items in heap number 3
  8.  
  9.  
  10. LL read(int idx) {
  11. LL sum = 0;
  12. while (idx > 0) {
  13. sum += tree[idx];
  14. idx -= (idx & -idx);
  15. }
  16. return sum;
  17. }
  18.  
  19. void update(int idx, LL val) {
  20. while (idx <= MAXN) {
  21. tree[idx] += val;
  22. idx += (idx & -idx);
  23. }
  24. }
  25.  
  26. inline void scan(int *a) {
  27. register char c=0;
  28. while (c < 33)
  29. c = getchar_unlocked();
  30. *a = 0;
  31. while (c > 33) {
  32. *a = *a * 10 + c - '0';
  33. c = getchar_unlocked();
  34. }
  35. }
  36.  
  37. LL in(){LL r=0,c;for(c=getchar_unlocked();c<=32;c=getchar_unlocked());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar_unlocked());return r;}
  38.  
  39. main()
  40. {
  41. int n, m,i;
  42. LL c;
  43. char ch;
  44. scan(&n);
  45. scan(&m);
  46. c = in();
  47. update(1, c);
  48.  printf("freq. : ");
  49. for(i=1;i<=7;i++)printf("%d ",read(i)-read(i-1));printf("\n");
  50.  printf("cumulative. : ");
  51. for(i=1;i<=7;i++)printf("%d ",read(i));printf("\n");
  52. printf("tree. : ");
  53. for(i=1;i<=7;i++)printf("%d ",tree[i]);printf("\n");
  54. while (m--)
  55. {
  56.  ch = getchar();
  57.  while (ch != 'Q' && ch != 'S' && ch != 'q' && ch != 's')
  58.   ch = getchar();
  59.  
  60.   if (ch == 'Q' || ch == 'q')
  61.   {
  62.   int p;
  63.   scan(&p);
  64.   printf("%lld\n", read(p));
  65.   }
  66. else
  67.  {
  68.   int u, v;
  69.   LL k;
  70.   scan(&u);
  71.   scan(&v);
  72.   k = in();
  73.   update(u, k);///update from u to MAXVAL but we want from
  74. /// u till v ...so subtract from v+1 till MAXVAL
  75.   update(v+1, -k);
  76.  }
  77.  printf("freq. : ");
  78. for(i=1;i<=7;i++)printf("%d ",read(i)-read(i-1));printf("\n");
  79.  printf("cumulative. : ");
  80. for(i=1;i<=7;i++)printf("%d ",read(i));printf("\n");
  81. printf("tree. : ");
  82. for(i=1;i<=7;i++)printf("%d ",tree[i]);printf("\n");
  83. }
  84. return 0;
  85. }
  86.  
  87.  
  88.  
  89. INPUT :
  90. 7 5 0
  91. S 2 5 1
  92. S 1 3 1
  93. S 3 6 1
  94. S 1 5 1
  95. Q 3
  96.  
  97.  
  98. OUTPUT :
  99. freq. : 0 0 0 0 0 0 0
  100. cumulative. : 0 0 0 0 0 0 0
  101. tree. : 0 0 0 0 0 0 0
  102. freq. : 0 1 0 0 0 -1 0
  103. cumulative. : 0 1 1 1 1 0 0
  104. tree. : 0 1 0 1 0 -1 0
  105. freq. : 1 1 0 -1 0 -1 0
  106. cumulative. : 1 2 2 1 1 0 0
  107. tree. : 1 2 0 1 0 -1 0
  108. freq. : 1 1 1 -1 0 -1 -1
  109. cumulative. : 1 2 3 2 2 1 0
  110. tree. : 1 2 1 2 0 -1 -1
  111. freq. : 2 1 1 -1 0 -2 -1
  112. cumulative. : 2 3 4 3 3 1 0
  113. tree. : 2 3 1 3 0 -2 -1
  114. 4
  115. freq. : 2 1 1 -1 0 -2 -1
  116. cumulative. : 2 3 4 3 3 1 0
  117. tree. : 2 3 1 3 0 -2 -1
Advertisement
Add Comment
Please, Sign In to add comment