Topic Links:
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=1754
Exercises
Single point update, query into segments.
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #defineLson o<<16 #defineRson (o<<1) |7 #defineM L + (r-l)/28 using namespacestd;9 Ten Const intMAXN = 2e5 +Ten; One A intN, M; - intmaxv[maxn<<2]; - the voidBuildintOintLintr) { - if(L = =r) { -scanf"%d", MAXV +o); - } + Else { - Build (Lson, L, M); +Build (Rson, M +1, R); AMaxv[o] =Max (Maxv[lson], Maxv[rson]); at } - } - - int_po, _v; - voidUpdateintOintLintr) { - if(L = =r) { inMaxv[o] =_v; - } to Else { + if(_po <=m) Update (Lson, L, m); - ElseUpdate (Rson, M +1, R); theMaxv[o] =Max (Maxv[lson], Maxv[rson]); * } $ }Panax Notoginseng - intQL,QR; the int_max; + voidQueryintOintLintr) { A if(QL <= L&&r <=qr) { the //printf ("(%d,%d):%d\n", L, R,maxv[o]); +_max =Max (_max, Maxv[o]); - } $ Else { $ //printf ("(%d,%d):%d\n", L, R, Maxv[o]); - if(QL <=m) query (Lson, L, m); - if(QR > M) query (Rson, M +1, R); the } - }Wuyi the intMain () { - while(SCANF ("%d%d", &n, &m) = =2) { WuBuild1,1, n); - Charcmd[ One]; About while(m--) { $scanf"%s", cmd); - if(cmd[0] =='Q') { -_max =-1; -scanf"%d%d", &ql,&qr); AQuery1,1, n); +printf"%d\n", _max); the } - Else if(cmd[0] =='U') { $scanf"%d%d", &_po, &_v); theUpdate1,1, n); the } the } the } - return 0; in}
HDU 1754 I Hate It segment Tree (single point update, segmented query)