BZOJ2333[SCOI2011] Tricky operation

Source: Internet
Author: User

BZOJ2333[SCOI2011] Tricky operation

Test instructions

There are n nodes, M operations: Connect two nodes, the weight of a single node increases V, the weights of all nodes of the connected block are increased by V, the weights of all nodes are increased by V, the weights of the nodes are queried, the weights of the nodes in the connected block where the nodes are queried, and the weights of the nodes with the highest weights in all nodes are queried. n,m≤300000

Exercises

Can and heap, although I heard that the pairing heap very fast, but the tutorial is too little to write, so to learn the oblique heap, relatively good writing. The oblique heap is actually a binary tree, the core is the merge operation, which is a recursive process, a bit like the treap delete operation. The method of ensuring the complexity of the inclined heap is to combine the right node with each recursive merge, and then exchange the left and then nodes, so that the whole tree and splay can be "automatically" balanced and metaphysical. To modify the entire connected block, mark the line. The special point of this problem is to ask for the maximum value of all nodes, you can use the STL set to maintain the root node of all connected blocks, when the edge and modify the weight value if the root node is modified to maintain the set.

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <Set>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMAXN 3001007 #defineINF 0X3FFFFFFF8 using namespacestd;9 Ten intfa[maxn],ch[maxn][2],tg[maxn],v[maxn],n,m,add; OneMultiset <int>St; A voidPushdown (intx) { -     if(Tg[x]) { -         if(ch[x][0]) tg[ch[x][0]]+=tg[x],v[ch[x][0]]+=Tg[x]; the         if(ch[x][1]) tg[ch[x][1]]+=tg[x],v[ch[x][1]]+=Tg[x]; -tg[x]=0; -     } - } + intDt[maxn],dts; - intFindintx) { +dt[dts=1]=x; while(Fa[x]) x=fa[x],dt[++dts]=x; A      for(inti=dts;i>=1; i--) Pushdown (Dt[i]);returnx; at } - intMergeintXinty) { -     if(!x| |! YreturnX+y;if(v[x]<V[y]) swap (x, y); Pushdown (x); -ch[x][1]=merge (ch[x][1],y); fa[ch[x][1]]=x; Swap (ch[x][0],ch[x][1]);returnx; - } - intDelintx) { in     intT=merge (ch[x][0],ch[x][1]), f=fa[x]; fa[x]=ch[x][0]=ch[x][1]=0; -Fa[t]=f;if(f) ch[f][ch[f][1]==x]=t;returnT; to } + voidUpdate1 (intXintval) { -     intY=find (x);intT=del (x); v[x]+=Val; the     if(y!=x) { *         intz=merge (y,x); St.erase (St.find (v[y)); St.insert (V[z]); $}Else{Panax Notoginseng         if(t) { -             intZ=merge (T,X); St.erase (St.find (v[x]-val)), St.insert (V[z]); the}ElseSt.erase (St.find (v[x]-val)), St.insert (V[x]); +     } A } the voidUpdate2 (intXintval) { +X=find (x); Tg[x]+=val; V[x]+=val;if(!fa[x]) st.erase (St.find (v[x]-val)), St.insert (V[x]); - } $ voidUpdate3 (intVal) {add+=Val;} $ intQuery1 (intx) {find (x);returnv[x];} - intQuery2 (intx) {intY=find (x);returnv[y];} - intQuery3 () {return* --St.find (INF);} the voidConnectintXinty) { -     intXx=find (x), Yy=find (y);if(XX==YY)return;intz=merge (XX,YY);Wuyi     if(z==xx) St.erase (St.find (V[yy]));Elsest.erase (St.find (v[xx)); the } - Charopt[3]; Wu intMain () { -     //freopen ("Test.txt", "R", stdin); Aboutscanf"%d", &n); Add=0; St.clear (); $Inc (I,1, N) { -scanf"%d", &v[i]); St.insert (V[i]); fa[i]=ch[i][0]=ch[i][1]=tg[i]=0; -     } -scanf"%d",&m); AInc (I,1, M) { +scanf"%s", opt);intx, y; the         if(opt[0]=='U') scanf ("%d%d",&x,&y), connect (x, y); -         if(opt[0]=='A'){ $             if(opt[1]=='1') scanf ("%d%d",&x,&y), update1 (x, y); the             if(opt[1]=='2') scanf ("%d%d",&x,&y), Update2 (x, y); the             if(opt[1]=='3') scanf ("%d",&x), Update3 (x); the         } the         if(opt[0]=='F'){ -             if(opt[1]=='1') scanf ("%d", &x), printf ("%d\n", Query1 (x) +add); in             if(opt[1]=='2') scanf ("%d", &x), printf ("%d\n", Query2 (x) +add); the             if(opt[1]=='3') printf ("%d\n", Query3 () +add); the         } About         //if (i==2) break; the     } the     return 0; the}

20160530

BZOJ2333[SCOI2011] Tricky operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.