Topic Links:
https://cn.vjudge.net/problem/ZOJ-3279
Main topic:
There are 1 to n that level each level has a[i] only ants
Two types of operations
p a B change the number of ants at level A to B
Q A queries which level a ant is in.
Problem Solving Ideas:
Dynamic maintenance of prefixes and, in a tree-like array, two points to find the first ant
Use printf, or you'll time out.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = 1e5 +Ten;4 intTREE[MAXN], N, A[MAXN];5 intLowbit (intx)6 {7 return(X & (-x));8 }9 voidAddintXintD//Add the value of X to the DTen { One while(x <=N) A { -TREE[X] + =D; -X + =lowbit (x); the } - } - intSumintx) - { + inttot =0; - while(x) + { ATot + =Tree[x]; atX-=lowbit (x); - } - returntot; - } - - intMain () in { - while(SCANF ("%d", &n)! =EOF) to { +Memset (A,0,sizeof(a)); -memset (Tree,0,sizeof(tree)); the for(inti =1; I <= N; i++) * { $scanf"%d", &a[i]);Panax Notoginseng Add (i, a[i]); - } the intm, x, y; +scanf"%d", &m); A Chars[2]; the while(m--) + { -scanf"%s", s); $ if(s[0] =='P') $ { -scanf"%d%d", &x, &y); -Add (x,-a[x]);//single-point modification the Add (x, y); -A[X] =y;Wuyi } the Else if(s[0] =='Q') - { Wuscanf"%d", &x); - intL =1, r =N, ans; About while(L <=R) $ { - intm = (L + r)/2; - if(SUM (m) >= x) ans = m, r = M-1; - ElseL = m +1; A } +printf"%d\n", ans); the } - } $ } the return 0; the}
ZOJ-3279 Ants tree-shaped array + two points