BZOJ3589 dynamic tree [Tree sectional / DV / inclusion and exclusion]

0 operation, apparently to resolve directly segment tree.

Operation 1, wherein the overlapping chain bottleneck counted only once. The segment tree view, if a range is covered, then only count this interval, sub-tree which will go on.

Consider marking to indicate whether the node coverage. However, if after reunification kick, is not easy to calculate the marked point and mark. Clear objectives, is now hoping to cover a large range between many communities to update answers in his later be stigmatized. `` `` `

It can be expressed this idea in the covered section of the tree and for each point maintenance $ acc_i $. So, when there is a greater section covering up, directly into the $ acc_i $ $ sum_i $, you can pass up, but marked mark has covered at this point. Otherwise normal pushup. The answer is $ acc_ {root} $

This ensures the accuracy of the answers covering counted only once.

Another problem is that after the completion of the operation, it should be clear that the mark be deleted. Marker interval coverage contemplated herein Tag $ $, 1 is the time when he is completely covered before, -1 indicates no coverage, ACC $ $ clear that for the next recording operation, the cover should set 0 denotes the subtree interval cleared.

When decentralization mark, you can ensure that the left and right sub-tree and roots are $ acc $ 0, can be re-used. When it is time to $ tag $ 1, you can normally delegated, may not be delegated, but will be unified at 0 and 1 code easier. Complexity $ (nklog ^ 2n) O $

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define dbg(x) cerr << #x << " = " << x <<endl
 7 using namespace std;
 8 typedef long long ll;
 9 typedef double db;
10 typedef pair<int,int> pii;
11 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
12 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
13 template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,1):0;}
14 template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,1):0;}
15 template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
16 template<typename T>inline T read(T&x){
17     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
18     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
19 }
20 const int N=2e5+7;
21 struct thxorz{int to,nxt;}G[N<<1];
22 int hd[N],tot;
23 int n,q;
24 inline void Addedge(int x,int y){
25     G[++tot].to=y,G[tot].nxt=hd[x],hd[x]=tot;
26     G[++tot].to=x,G[tot].nxt=hd[y],hd[y]=tot;
27 }
28 #define y G[j].to
29 int fa[N],d[N],son[N],cnt[N],topfa[N],st[N],tim;
30 void dfs1(int x,int f){
31     fa[x]=f,d[x]=d[f]+1,cnt[x]=1;int tmp=-1;
32     for(register int j=hd[x];j;j=G[j].nxt)if(y^f)dfs1(y,x),cnt[x]+=cnt[y],MAX(tmp,cnt[y])&&(son[x]=y);
33 }
34 void dfs2(int x,int topf){
35     topfa[x]=topf,st[x]=++tim;if(!son[x])return;dfs2(son[x],topf);
36     for(register int j=hd[x];j;j=G[j].nxt)if(y^fa[x]&&y^son[x])dfs2(y,y);
37 }
38 #undef y
39 #define lc i<<1
40 #define rc i<<1|1
41 int sumv[N<<2],acc[N<<2],stag[N<<2],atag[N<<2];
42 inline void Pushup(int i){sumv[i]=sumv[lc]+sumv[rc],acc[i]=acc[lc]+acc[rc];}
43 inline void pd_sum(int i,int L,int R){
44     if(stag[i]){
45         int mid=L+R>>1;
46         sumv[lc]+=(mid-L+1)*stag[i],sumv[rc]+=(R-mid)*stag[i],stag[lc]+=stag[i],stag[rc]+=stag[i],stag[i]=0;
47     }
48 }
49 inline void pd_acc(int i){
50     if(~atag[i])
51         acc[lc]=atag[i]?sumv[lc]:0,acc[rc]=atag[i]?sumv[rc]:0,atag[rc]=atag[lc]=atag[i],atag[i]=-1;
52 }
53 void Update_add(int i,int L,int R,int ql,int qr,int k){
54     if(ql<=L&&qr>=R){sumv[i]+=(R-L+1)*k,stag[i]+=k;return;}
55     int mid=L+R>>1;pd_sum(i,L,R);
56     if(ql<=mid)Update_add(lc,L,mid,ql,qr,k);
57     if(qr>mid)Update_add(rc,mid+1,R,ql,qr,k);
58     Pushup(i);
59 }
60 void Update_account(int i,int L,int R,int ql,int qr){
61     if(ql<=L&&qr>=R){acc[i]=sumv[i],atag[i]=1;return;}
62     int mid=L+R>>1;pd_sum(i,L,R),pd_acc(i);
63     if(ql<=mid)Update_account(lc,L,mid,ql,qr);
64     if(qr>mid)Update_account(rc,mid+1,R,ql,qr);
65     Pushup(i);
66 }
67 inline void clear_the_tree(){atag[1]=0,acc[1]=0;}
68 inline void Tree_query(int x,int y){
69     if(d[x]<d[y])x^=y^=x^=y;
70     while(topfa[x]^topfa[y])Update_account(1,1,n,st[topfa[x]],st[x]),x=fa[topfa[x]];
71     Update_account(1,1,n,st[y],st[x]);
72 }
73 
74 int main(){//freopen("test.in","r",stdin);freopen("test.ans","w",stdout);
75     read(n);for(register int i=1,x,y;i<n;++i)read(x),read(y),Addedge(x,y);
76     dfs1(1,0),dfs2(1,1);
77     read(q);memset(atag,-1,sizeof atag);
78     for(register int i=1,opt,k,x,y;i<=q;++i){
79         read(opt);
80         if(opt){
81             read(k);while(k--)read(x),read(y),Tree_query(x,y);
82             printf("%d\n",acc[1]&0x7fffffff);clear_the_tree();
83         }
84         else read(x),read(k),Update_add(1,1,n,st[x],st[x]+cnt[x]-1,k);
85     }
86     return 0;
87 }
View Code

There is also a point of $ 2 ^ {31} $ modulo natural overflow because an int before ringing off the hook 31, to carry the sign bit, the sign bit will change, but does not affect the results of the first 31 bits of the mod. Finally, as long as the sign bit to change it, so to $ \ text {and} 2 ^ {31} -1 $.

Reflection: mark represents the design thinking is not very good. When the segment tree design constraints of inquiry, can maintain some additional information, such as covering the range of this question and.

PS: This question there are two other approaches.

A: violence

Always ask to skip the whole of each of the heavy chain to get out of these intervals by dfs sequence from small to large, and then merge the interval, violence inquiry. Complexity $ O (n (klognlog (klogn) + klog ^ 2n)) $. More cards.

Two: the inclusion-exclusion

The query request corresponding to a plurality of chain and may repellent capacity.


$sum(\bigcup\limits_{i=1}^{n}A_i)=\sum\limits_{k=1}^{n}(-1)^{k-1}\sum\limits_{1<=j_1<j_2<...<j_k<=n}sum(A_{j_1}\cap A_{j_2}\cap ...\cap A_{j_k})$

Reference this .

Guess you like

Origin www.cnblogs.com/saigyouji-yuyuko/p/11586276.html