Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAXN 10
- #define LL long long
- LL tree[MAXN];
- // S 1 5 1 means add 1 to heap 1 to 5
- // Q 3 means find how many items in heap number 3
- LL read(int idx) {
- LL sum = 0;
- while (idx > 0) {
- sum += tree[idx];
- idx -= (idx & -idx);
- }
- return sum;
- }
- void update(int idx, LL val) {
- while (idx <= MAXN) {
- tree[idx] += val;
- idx += (idx & -idx);
- }
- }
- inline void scan(int *a) {
- register char c=0;
- while (c < 33)
- c = getchar_unlocked();
- *a = 0;
- while (c > 33) {
- *a = *a * 10 + c - '0';
- c = getchar_unlocked();
- }
- }
- 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;}
- main()
- {
- int n, m,i;
- LL c;
- char ch;
- scan(&n);
- scan(&m);
- c = in();
- update(1, c);
- printf("freq. : ");
- for(i=1;i<=7;i++)printf("%d ",read(i)-read(i-1));printf("\n");
- printf("cumulative. : ");
- for(i=1;i<=7;i++)printf("%d ",read(i));printf("\n");
- printf("tree. : ");
- for(i=1;i<=7;i++)printf("%d ",tree[i]);printf("\n");
- while (m--)
- {
- ch = getchar();
- while (ch != 'Q' && ch != 'S' && ch != 'q' && ch != 's')
- ch = getchar();
- if (ch == 'Q' || ch == 'q')
- {
- int p;
- scan(&p);
- printf("%lld\n", read(p));
- }
- else
- {
- int u, v;
- LL k;
- scan(&u);
- scan(&v);
- k = in();
- update(u, k);///update from u to MAXVAL but we want from
- /// u till v ...so subtract from v+1 till MAXVAL
- update(v+1, -k);
- }
- printf("freq. : ");
- for(i=1;i<=7;i++)printf("%d ",read(i)-read(i-1));printf("\n");
- printf("cumulative. : ");
- for(i=1;i<=7;i++)printf("%d ",read(i));printf("\n");
- printf("tree. : ");
- for(i=1;i<=7;i++)printf("%d ",tree[i]);printf("\n");
- }
- return 0;
- }
- INPUT :
- 7 5 0
- S 2 5 1
- S 1 3 1
- S 3 6 1
- S 1 5 1
- Q 3
- OUTPUT :
- freq. : 0 0 0 0 0 0 0
- cumulative. : 0 0 0 0 0 0 0
- tree. : 0 0 0 0 0 0 0
- freq. : 0 1 0 0 0 -1 0
- cumulative. : 0 1 1 1 1 0 0
- tree. : 0 1 0 1 0 -1 0
- freq. : 1 1 0 -1 0 -1 0
- cumulative. : 1 2 2 1 1 0 0
- tree. : 1 2 0 1 0 -1 0
- freq. : 1 1 1 -1 0 -1 -1
- cumulative. : 1 2 3 2 2 1 0
- tree. : 1 2 1 2 0 -1 -1
- freq. : 2 1 1 -1 0 -2 -1
- cumulative. : 2 3 4 3 3 1 0
- tree. : 2 3 1 3 0 -2 -1
- 4
- freq. : 2 1 1 -1 0 -2 -1
- cumulative. : 2 3 4 3 3 1 0
- tree. : 2 3 1 3 0 -2 -1
Advertisement
Add Comment
Please, Sign In to add comment