codeforces 916E Jamie and Tree dfs序列化+线段树+LCA

codeforces 916E  Jamie and Tree   dfs序列化+线段树+LCA

E. Jamie and Tree
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

To your surprise, Jamie is the final boss! Ehehehe.

Jamie has given you a tree with n vertices, numbered from 1 to n. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it.

Jamie also gives you three types of queries on the tree:

v — Change the tree's root to vertex with number v.

u v x — For each vertex in the subtree of smallest size that contains u and v, add x to its value.

v — Find sum of values of vertices in the subtree of vertex with number v.

A subtree of vertex v is a set of vertices such that v lies on shortest path from this vertex to root of the tree. Pay attention that subtree of a vertex can change after changing the tree's root.

Show your strength in programming to Jamie by performing the queries accurately!

Input

The first line of input contains two space-separated integers n and q (1 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of vertices in the tree and the number of queries to process respectively.

The second line contains n space-separated integers a1, a2, ..., an ( - 108 ≤ ai ≤ 108) — initial values of the vertices.

Next n - 1 lines contains two space-separated integers ui, vi (1 ≤ ui, vi ≤ n) describing edge between vertices ui and vi in the tree.

The following q lines describe the queries.

Each query has one of following formats depending on its type:

v (1 ≤ v ≤ n) for queries of the first type.

u v x (1 ≤ u, v ≤ n,  - 108 ≤ x ≤ 108) for queries of the second type.

v (1 ≤ v ≤ n) for queries of the third type.

All numbers in queries' descriptions are integers.

The queries must be carried out in the given order. It is guaranteed that the tree is valid.

Output

For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie.

Examples
input
6 7
1 4 2 8 5 7
1 2
3 1
4 3
4 5
3 6
3 1
2 4 6 3
3 4
1 6
2 2 4 -5
1 4
3 3
output
27
19
5
input
4 6
4 3 5 6
1 2
2 3
3 4
3 1
1 3
2 2 4 3
1 1
2 2 4 -3
3 1
output
18
21
Note

The following picture shows how the tree varies after the queries in the first sample.

codeforces 916E  Jamie and Tree   dfs序列化+线段树+LCA

大意:给出一颗树,初始根节点为1,三种操作:

1.把一个节点变成根节点

2.给出两个节点u,v,把包含这两个点的最小子树中每个节点权值加上x

3.查询以 u 为根的子树的权值和。

题解:

如果没有操作一,这题就是把LCA和dfs序列化以及线段树的板子放在一起。

但是,对于操作一,如何解决?

其实并不需要真的把节点提到根上。

我们可以一直以 1 为根节点,利用一些小结论来进行2和3操作。

 

codeforces 916E  Jamie and Tree   dfs序列化+线段树+LCA

假设在该图中,当前根节点为6,但是我们的dfs序列仍然是以1为根节点的。

如果当前根节点不在要修改(查询)的子树中,就没有任何影响,直接修改(查询)即可,此处不做讨论。

下面只说根节点在要修改(查询)的子树中的情况。

如果现在给出操作2,把包含2,4的最小子树上面的每个节点加delta。

我们应该处理的节点是 1 2 3 4 5

lca(2,4)=1     lca(2,6)=1    lca(4,6)=3

有一个大胆的猜想:

直接修改lca(2,4)=1的子树,然后把不该修改的哪部分改回去。

那么多修改的哪部分是那些呢:

大胆猜想:

取这三组lca中深度最大的那个(节点3)

当前的根节点6所在的     节点3的子树     就是多修改的那部分。

对于询问操作,思想大体相似

若当前根节点为6,询问1的子树的权值和。

先算上整颗树,然后减去多加的部分。

多加的部分就是根节点6所在的     节点1的子树,也就是3,4,5,6。

大胆猜想,不用证明,恩,这结论就是对的。

  1 /*
  2 Welcome Hacking
  3 Wish You High Rating
  4 */
  5 #include<iostream>
  6 #include<cstdio>
  7 #include<cstring>
  8 #include<ctime>
  9 #include<cstdlib>
 10 #include<algorithm>
 11 #include<cmath>
 12 #include<string>
 13 using namespace std;
 14 int read(){
 15     int xx=0,ff=1;char ch=getchar();
 16     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
 17     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
 18     return xx*ff;
 19 }
 20 const int maxn=100010;
 21 int N,Q,opt,t1,t2,t3,root,v[maxn];
 22 int tid[maxn],rk[maxn],tim,Prev[maxn],depth[maxn],end[maxn];
 23 int lin[maxn],len;
 24 struct edge{
 25     int y,next;
 26 }e[maxn<<1];
 27 inline void insert(int xx,int yy){
 28     e[++len].next=lin[xx];
 29     lin[xx]=len;
 30     e[len].y=yy;
 31 }
 32 void dfs(int x,int prev){
 33     depth[x]=depth[prev]+1;
 34     tid[x]=++tim;
 35     rk[tim]=x;
 36     Prev[x]=prev;
 37     for(int i=lin[x];i;i=e[i].next)
 38         if(e[i].y!=prev)
 39             dfs(e[i].y,x);
 40     end[x]=tim;
 41 }
 42 int f[17][maxn];
 43 int LCA(int x,int y){
 44     if(depth[x]<depth[y])
 45         swap(x,y);
 46     int dif=depth[x]-depth[y];
 47     for(int i=16;i>=0;i--)
 48         if(dif&(1<<i))
 49             x=f[i][x];
 50     if(x==y)
 51         return x;
 52     for(int i=16;i>=0;i--)
 53         if(f[i][x]!=f[i][y])
 54             x=f[i][x],y=f[i][y];
 55     return f[0][x];
 56 }
 57 int LCALCA(int x,int y){
 58     int t1=LCA(x,y),t2=LCA(x,root),t3=LCA(y,root);
 59     if(depth[t1]<depth[t2])
 60         swap(t1,t2);
 61     if(depth[t1]<depth[t3])
 62         return t3;
 63     return t1;
 64 }
 65 int get_son(int x){
 66     int temp=root;
 67     int dif=depth[root]-depth[x]-1;
 68     for(int i=16;i>=0;i--)
 69         if(dif&(1<<i))
 70             temp=f[i][temp];
 71     return temp;
 72 }
 73 struct Segment_Tree{
 74     long long sum,tag;
 75 }T[maxn<<2];
 76 int X,Y,Z;
 77 void build(int L,int R,int root){
 78     if(L==R){
 79         T[root].sum=v[rk[L]];
 80         return;
 81     }
 82     int mid=(L+R)>>1;
 83     build(L,mid,root<<1);
 84     build(mid+1,R,root<<1|1);
 85     T[root].sum=T[root<<1].sum+T[root<<1|1].sum;
 86 }
 87 void upd(int L,int R,int root){
 88     if(X>R||Y<L)
 89         return;
 90     if(X<=L&&Y>=R){
 91         T[root].tag+=Z;
 92         T[root].sum+=1LL*(R-L+1)*Z;
 93         return;
 94     }
 95     int mid=(L+R)>>1;
 96     if(T[root].tag){
 97         T[root<<1].tag+=T[root].tag;
 98         T[root<<1|1].tag+=T[root].tag;
 99         T[root<<1].sum+=T[root].tag*(mid-L+1);
100         T[root<<1|1].sum+=T[root].tag*(R-mid);
101         T[root].tag=0;
102     }
103     upd(L,mid,root<<1);
104     upd(mid+1,R,root<<1|1);
105     T[root].sum=T[root<<1].sum+T[root<<1|1].sum;
106 }
107 long long query(int L,int R,int root){
108     if(X>R||Y<L)
109         return 0;
110     if(X<=L&&Y>=R)
111         return T[root].sum;
112     int mid=(L+R)>>1;
113     if(T[root].tag){
114         T[root<<1].tag+=T[root].tag;
115         T[root<<1|1].tag+=T[root].tag;
116         T[root<<1].sum+=T[root].tag*(mid-L+1);
117         T[root<<1|1].sum+=T[root].tag*(R-mid);
118         T[root].tag=0;
119     }
120     return query(L,mid,root<<1)+query(mid+1,R,root<<1|1);
121 }
122 int main(){
123     //freopen("in.txt","r",stdin);
124     N=read(),Q=read();root=1;
125     for(int i=1;i<=N;i++)
126         v[i]=read();
127     for(int i=1;i<N;i++){
128         t1=read(),t2=read();
129         insert(t1,t2);
130         insert(t2,t1);
131     }
132     dfs(1,0);
133     /*for(int i=1;i<=N;i++)
134         printf("%d ",tid[i]);
135     puts("");
136     for(int i=1;i<=N;i++)
137         printf("%d ",rk[i]);
138     puts("");
139     for(int i=1;i<=N;i++)
140         printf("%d ",depth[i]);
141     puts("");*/
142     build(1,N,1);
143     for(int i=1;i<=N;i++)
144         f[0][i]=Prev[i];
145     for(int i=1;i<=16;i++)
146         for(int j=1;j<=N;j++)
147             f[i][j]=f[i-1][f[i-1][j]];
148     while(Q--){
149         opt=read();
150         if(opt==1)
151             root=read();
152         else if(opt==2){
153             t1=read(),t2=read(),t3=read();
154             int lca=LCALCA(t1,t2);
155             if(tid[root]>=tid[lca]&&tid[root]<=end[lca]){
156                 X=1,Y=N,Z=t3;
157                 upd(1,N,1);
158                 if(lca==root)
159                     continue;
160                 int temp=get_son(lca);
161                 X=tid[temp],Y=end[temp],Z=-t3;
162                 upd(1,N,1);
163             }
164             else{
165                 X=tid[lca],Y=end[lca],Z=t3;
166                 upd(1,N,1);
167             }
168         }
169         else{
170             int lca=read();
171             if(lca==root){
172                 X=1,Y=N;
173                 printf("%I64d
",query(1,N,1));
174             }
175             else if(tid[root]>=tid[lca]&&tid[root]<=end[lca]){
176                 long long ans=0;
177                 X=1,Y=N;
178                 ans=query(1,N,1);
179                 int temp=get_son(lca);
180                 X=tid[temp],Y=end[temp];
181                 ans-=query(1,N,1);
182                 printf("%I64d
",ans);
183             }
184             else{
185                 X=tid[lca],Y=end[lca];
186                 printf("%I64d
",query(1,N,1));
187             }
188         }
189     }
190     return 0;
191 }
View Code