[bzoj3631]松鼠的新家

树剖裸题

  1 // luogu-judger-enable-o2
  2 #include<bits/stdc++.h>
  3 using namespace std;
  4 #define ll long long
  5 const int N=3e5+777;
  6 vector<int> v[N],w[N];
  7 int size[N],fa[N],dep[N],zs[N];
  8 void ad(int x,int y,int d){
  9     v[x].push_back(y);
 10     w[x].push_back(d);
 11 }
 12 void dfs1(int fath,int son){
 13     dep[son]=dep[fath]+1;
 14     fa[son]=fath;
 15     size[son]=1;
 16     int maxs=0;
 17     for(int i=0;i<v[son].size();i++){
 18     int x=v[son][i];
 19     if(x!=fath){dfs1(son,x);
 20     size[son]+=size[x];
 21     if(maxs<size[x]){maxs=size[x];zs[son]=x;}
 22     }
 23     }
 24 }
 25 int cnt;                    // nov 当前点的时间戳 
 26 int top[N],nov[N],dot[N];// dot shijianchuo的点 
 27 void dfs2(int x,int to){
 28     cnt++;
 29     nov[x]=cnt;
 30     dot[cnt]=x;
 31     top[x]=to;
 32     if(zs[x]==-1)return;
 33     dfs2(zs[x],to);
 34     for(int i=0;i<v[x].size();i++){
 35     int xx=v[x][i];
 36     if(xx!=fa[x]&&xx!=zs[x])
 37     dfs2(xx,xx);
 38     }
 39 }
 40 int n,lca,root,q,vv,ql,qr;
 41 const int seg=1.5e6+444;
 42 int sum[seg],addv[N];
 43 void pushdown(ll o){
 44     addv[o*2]+=addv[o];
 45     addv[o*2+1]+=addv[o];
 46     addv[o]=0;
 47 }
 48 void wf(int o,int l,int r){
 49     sum[o]=0;
 50     if(l<r)
 51     sum[o]=sum[o*2]+sum[o*2+1];
 52     sum[o]+=addv[o]*(r-l+1);    
 53 }
 54 void mk(int o,int l,int r){
 55     if(ql<=l&&r<=qr)addv[o]+=vv;
 56     else{
 57     int m=l+(r-l)/2;
 58     if(l<r)pushdown(o);
 59     if(ql<=m)mk(o*2,l,m);else wf(o*2,l,m);
 60     if(m<qr)mk(o*2+1,m+1,r);else wf(o*2+1,m+1,r);
 61     }
 62     wf(o,l,r);
 63 }
 64 int ans;
 65 void qz(int o,int l,int r,int az){
 66     if(ql<=l&&r<=qr){ans+=sum[o]+az*(r-l+1);}
 67     else{
 68     int m=l+(r-l)/2;
 69     if(ql<=m)qz(o*2,l,m,az+addv[o]);
 70     if(m<qr)qz(o*2+1,m+1,r,az+addv[o]);
 71     }
 72 }
 73 int temp;
 74 void adddot(int x,int y,int val){
 75     int tx=top[x],ty=top[y];
 76     while(tx!=ty){
 77     if(dep[tx]>dep[ty]){
 78     vv=val;
 79     ql=nov[tx];
 80     qr=nov[x];
 81     mk(1,1,n);
 82     x=fa[tx];
 83     ans=0;
 84     qz(1,1,n,0);
 85     temp+=ans;
 86     }
 87     else {
 88     vv=val;
 89     ql=nov[top[ty]];
 90     qr=nov[y];
 91     mk(1,1,n);
 92     y=fa[ty];}
 93     tx=top[x];
 94     ty=top[y];
 95     }
 96     if(dep[x]>dep[y]){
 97     vv=val;
 98     ql=nov[y];
 99     qr=nov[x];
100     mk(1,1,n);
101     }else{
102     vv=val;
103     ql=nov[x];
104     qr=nov[y];
105     mk(1,1,n);
106     }
107 }
108 int a[N];
109 int main(){
110     memset(zs,-1,sizeof(zs));
111     ios::sync_with_stdio(0);
112     cin>>n;
113     for(int i=1;i<=n;i++)cin>>a[i];
114     for(int i=1;i<n;i++){
115     int a,b;
116     cin>>a>>b;
117     ad(a,b,0);
118     ad(b,a,0);
119     }
120     dfs1(0,1);
121     dfs2(1,1);
122     for(int i=1;i<n;i++)
123     adddot(a[i],a[i+1],1);
124     for(int i=1;i<=n;i++){
125     ql=qr=nov[a[i]];
126     ans=0;
127     qz(1,1,n,0);
128     if(1!=i)dot[a[i]]=ans-1;
129     else dot[a[i]]=ans;
130     }
131     for(int i=1;i<=n;i++)cout<<dot[i]<<endl;
132     return 0;
133 }
View Code

MLE20pts

戒骄戒躁
原文地址:https://www.cnblogs.com/lxzl/p/9862856.html