"BZOJ-3786" Galaxy explore Splay + DFS sequence

Source: Internet
Author: User

3786: Galaxy Quest Time limit:40 Sec Memory limit:256 MB
submit:647 solved:212
[Submit] [Status] [Discuss] Description

The study of physicists ' small c is encountering a bottleneck.

He is studying a galaxy with N planets in it, one of which is the main planet (we default to Planet 1th for convenience), and all the other planets have and only one dependent on the planet. The main planet is not dependent on the planet.

We define the dependencies as follows: If planet A's dependent planet is B, then planet A depends on planet B. Furthermore, the dependency is transitive, even if planet a relies on Planet B, Planet B relies on Planet C, and planet A relies on planet C.

For this mysterious galaxy, small C preliminarily explored its nature and found that the dependence between the planets is non-ring. And starting from planet A can only reach its dependent planet B directly.

Each planet I has an energy coefficient of wi. Small c would like to conduct several experiments, the first I experiment, he will launch from the spacecraft to the planet di an energy collector with an initial energy of 0, the energy collector will start from the Planet di to the main planet, and collect some of the energy of each planet along the way, the amount of energy collected is equal to the

But the structure of galaxies is not immutable, and at some point the galaxies may change for some complex reason.

At some point, the energy of a planet will increase by a certain value all the planets that depend on it and his own energy coefficients. It is also possible that, at some point, the dependent planet of a planet will change, but the change will still satisfy the dependency relationship is non-ring.

Now little C has measured the energy coefficients of each planet at the moment 0 o'clock, and the dependent planets of each planet (except the main planet). For the next M-moment, there will be events at each moment. Where small c may perform several experiments, for each of his experiments, please tell him the ultimate energy of this experimental energy collector.

Input

The first line is an integer n, which represents the number of planets in the galaxy.

Next n-1 line an integer for each line, representing the planet 2-n's dependent planet number.

The next row of n integers represents the initial energy coefficient of each planet at the time of 0 o'clock WI.

The next line is an integer m, which represents the total number of events.

The event is divided into the following three types.

(1) "Q di" means small c to start an experiment, the initial position of the collector in the Planet Di.

(2) "C Xi Yi" indicates that Planet Xi's dependent planet has become a planet Yi.

(3) "F Pi Qi" indicates the planet Pi energy excitation, the constant is QI.

Output

For each event of the event type Q, the output is an integer representing the final energy of the Collector of the experiment.

Sample INPUT3
1
1
4 5 7
5
Q 2
F 1 3
Q 2
C 2 3
Q 2
Sample Output9
15
25HINT

n<=100000,m<=300000,1<di,xi<=n,wi,qi<=100000. Ensure the operation is lawful.

Source

by anonymous upload

Solution

Subtree about modification, obviously can't use link-cut-tree ...

The DFS sequence is considered, but the root change operation is obviously not maintained with the segment tree, so the splay! is supported by splitting and merging
We separate the stack in the DFS sequence, at a point when the value is +val[x], and the value of the stack is-val[x], so that the effect on his sub-tree will obviously offset

In this case, the query point x to follow the $\sum_{x}^{root}val[x]$, is equivalent to the prefix and query $\sum_{pl[1]}^{pl[x]}val[x]$

Sub-tree modification, is to hit the mark.

Change the root operation, the x to the Y, first the sub-tree of the X Division Open, and then the y sub-tree interval extraction, and then the X sub-tree interval to the y sub-tree interval of the leftmost

Code
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<stack>using namespaceStd;inlineintRead () {intx=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx;}#defineLL Long Long#defineMAXN 200010intN,M,POWER[MAXN],VAL[MAXN];structedgenode{intNext,to,d;} edge[maxn<<1];intHead[maxn],cnt=1; inlinevoidAddedge (intUintV) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;} InlinevoidInsertedge (intUintv) {Addedge (u,v); Addedge (v,u);}intPL[MAXN],DFN,PR[MAXN],PRE[MAXN],DEL[MAXN];voidDFS (intNowintLast ) {Pl[now]=++DFN; Pre[dfn]=now; del[dfn]=1;  for(intI=head[now]; I I=edge[i].next)if(edge[i].to!=Last )    DFS (Edge[i].to,now); Pr[now]=++DFN; del[dfn]=-1;}namespacesplaytree{intfa[maxn],son[maxn][2],SZ,ROOT,LN[MAXN],RN[MAXN],POS[MAXN];    LL SUM[MAXN],TAG[MAXN]; //LN records the number of left, RN record right number, tag tag    #defineLS (x) son[x][0]#defineRS (x) son[x][1]#defineINF 1000100inlinevoidUpdate (intNow ) {Sum[now]=sum[ls (now)]+sum[rs (now)]+(LL) Val[now]; Ln[now]=ln[ls (now)]+ln[rs (now)]+ (del[now]==1); Rn[now]=rn[ls (now)]+rn[rs (now)]+ (del[now]==-1); } InlinevoidPaintintNow,ll D) {if(now) val[now]+=d*del[now],sum[now]+=d* (Ln[now]-rn[now]), tag[now]+=D;} InlinevoidPushdown (intNow ) {        if(!tag[now])return; LL D=tag[now]; tag[now]=0; if(LS (now)) Paint (LS (now), D); if(RS (now)) Paint (RS (now), D); }         voidPrintintNow )        {pushdown (now); if(Val[now]!=inf && val[now]!=-INF) printf ("id=%d---%d [%d%d]%d%i64d%d %d%d%i64d\n", Now,fa[now],ls (now), RS (now), Val[now],sum[now],ln[now],rn[now],del[now],tag[now]); if(son[now][0]) Print (son[now][0]); if(son[now][1]) Print (son[now][1]); } InlineBOOLRight (intNow) {returnson[fa[now]][1]==Now ;} InlinevoidRotateintNow ) {pushdown (Fa[now]);        Pushdown (now); intF=fa[now],gf=fa[f],wh=Right (now); SON[F][WH]=son[now][wh^1]; fa[son[f][wh]]=F; FA[F]=now; son[now][wh^1]=f; fa[now]=GF; if(GF) son[gf][son[gf][1]==f]=Now ; Update (f);    Update (now); } InlinevoidSplay (intNowinttar) {         for(intF (F=fa[now])! =tar; rotate (now))if(Fa[f]!=tar) rotate (now) ==right (f)?F:now); if(!tar) root=Now ; } InlineintFmin (intx) { while(son[x][0]) x=son[x][0];returnx;} InlineintFmaxintx) { while(son[x][1]) x=son[x][1];returnx;} InlinevoidSplit (intLintr) {splay (L,0);intx=Fmax (LS (root)); Splay (R,0);inty=Fmin (RS (root)); Splay (x,0);    Splay (Y,X); } inline LL Query (intPOS) {Split (pl[1],pl[pos]);returnSum[ls (Root)];} InlinevoidChange (intXintD) {Split (pl[x],pr[x]); intRt=son[rs (Root) [0]; SUM[RT]+ = (LL) (Ln[rt]-rn[rt]) *d; VAL[RT]+=D*DEL[RT]; tag[rt]+=e; Update (root);    Update (root); } InlinevoidBuildtree (intLintRintLast ) {        if(r<l)return; intMid= (l+r) >>1, now=mid; Fa[now]=last; son[last][now>last]=Now ; if(l==R) {Sum[now]=val[now]; ln[now]=del[now]==1; rn[now]=1-ln[now];return;} Buildtree (L,mid-1, now); Buildtree (mid+1, R,now);    Update (now); } InlinevoidInit () {Buildtree (1, (n<<1)+2,0); Root= (1+ (n<<1)+2) >>1;} InlinevoidMakeroot (intNowintfather)        {Split (Pl[now],pr[now]); intrt=ls (rs (root)); Fa[ls (RS (root))]=0; Son[rs (root) [0]=0;        Update (root), update (root); Splay (Pl[father],0);        Splay (Fmin (root), root); Son[rs (root) [0]=rt; fa[rt]=rs (root);    Update (root), update (root); }}using namespaceSplaytree;intMain () {N=read ();  for(intI=2; i<=n; i++) Insertedge (I,read ()); DFN=1; DFS (1,0);  for(intI=1; i<=n; i++) power[i]=read ();//for (int i=1; i<=n; i++) printf ("id=%d [%d,%d]\n", Pre[i],pl[pre[i]],pr[pre[i]]);     for(intI=1; i<=n; i++) val[pl[i]]+=power[i],val[pr[i]]-=Power[i];//for (int i=1; i<=dfn; i++) printf ("%d", val[i]); Puts ("");m=read ();    Splaytree::init ();  while(m--)        {//print (root);            Charopt[5]; scanf"%s", opt);intx, y; if(opt[0]=='Q') X=read (), printf ("%i64d\n", Splaytree::query (x)); if(opt[0]=='C') X=read (), y=read (), Splaytree::makeroot (x, y); if(opt[0]=='F') X=read (), y=read (), Splaytree::change (x, y); }    return 0;}

A day ago. I: char, if DFS sequence without line tree, with splay words, can not support the subtree Exchange xxx ... char brother: ... woc! So scientific ....

So with Char Cape Sat, wrote a day splay, and then this problem yesterday didn't tune out ...

Finally wrote out ... 40s time limit 41s AC ....

Me: I must be within 40s!  ..... Me: When I did not say;

Me: Brother Char, do you have any tips on constant pressure? Char: Rewrite, your program is hopeless! ......

Native Plus O2 optimization:

"BZOJ-3786" Galaxy explore Splay + DFS sequence

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.