[bzoj1095] [ZJOI2007]Hide 捉迷藏

  珍爱生命远离STL

  珍爱空间远离STL

T_T

  动态信息的点分治。

  如果没有修改的话记录一下最大值就好了。

  有修改的话就用堆= =

  详细题解见 http://blog.csdn.net/popoqqq/article/details/44461423

  脑子几度当机导致调了一晚上= =。。。最后还是swm大爷提醒下才调出来的= =。

  调优先队列的话空间很成问题。。无用节点能不加就不加。。最后刚好卡过去...

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<queue>
  5 #include<algorithm>
  6 using namespace std;
  7 const int maxn=100023,inf=1000233333;
  8 struct zs{
  9     int too,pre;
 10 }e[maxn<<1];int tot,last[maxn];
 11 struct zs1{
 12     int id,v;
 13 };
 14 
 15 int sz[maxn],mx[maxn],p[maxn],dis[maxn],mxd[maxn],num,rt,poi;
 16 int v[maxn];
 17 int f[21][maxn],dist[21][maxn];
 18 priority_queue<zs1>q[maxn<<1];
 19 int i,j,k,n,m;
 20 bool gg[maxn],del[maxn];
 21 
 22 int ra;char rx;
 23 inline int read(){
 24     rx=getchar(),ra=0;
 25     while(rx<'0'||rx>'9')rx=getchar();
 26     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra;
 27 }
 28 inline void insert(int a,int b){
 29     e[++tot].too=b,e[tot].pre=last[a],last[a]=tot;
 30     e[++tot].too=a,e[tot].pre=last[b],last[b]=tot;
 31 }
 32 
 33 bool operator <(zs1 a,zs1 b){return a.v<b.v;}
 34 inline void clr0(){
 35     while(!q[0].empty()&&v[q[0].top().id]!=q[0].top().v)q[0].pop();
 36 }
 37 inline void clr(int x){
 38     while(!q[x].empty()&&gg[q[x].top().id])q[x].pop();
 39     if(q[x].empty())q[x].push((zs1){0,-1});
 40 }
 41 inline void clr1(int x){
 42     while(!q[x].empty()&&q[x].top().v!=q[q[x].top().id].top().v)q[x].pop();
 43 }
 44 inline zs1 getmx(int x){
 45     clr(x);
 46     return q[x].top();
 47 }
 48 inline zs1 getmx1(int x){
 49     clr1(x);
 50     return !q[x].empty()?q[x].top():(zs1){0,-1};
 51 }
 52 inline int getans(){
 53     clr0();
 54     return q[0].top().v;
 55 }
 56 inline int getsm(int x){
 57     zs1 tmp=getmx1(x),tmp1;
 58     if(tmp.v==-1)return !gg[x]?0:-1;
 59     tmp=q[x].top(),q[x].pop();
 60     while(!q[x].empty()&&(q[x].top().v!=q[q[x].top().id].top().v||q[x].top().id==tmp.id))q[x].pop();
 61     tmp1=!q[x].empty()?q[x].top():(zs1){0,-1};
 62     q[x].push(tmp);
 63     if(tmp1.v==-1)return !gg[x]?tmp.v:0;
 64     else return tmp.v+tmp1.v;
 65 }
 66  
 67 void getrt(int x,int fa){
 68     sz[x]=mx[x]=1;
 69     for(int i=last[x];i;i=e[i].pre)if(e[i].too!=fa&&!del[e[i].too])
 70         getrt(e[i].too,x),sz[x]+=sz[e[i].too],mx[x]=max(mx[x],sz[e[i].too]);
 71     mx[x]=max(mx[x],poi-mx[x]);
 72     if(mx[x]<mx[rt])rt=x;
 73 }
 74 void getpoi(int x,int fa){
 75     p[++num]=x;
 76     for(int i=last[x];i;i=e[i].pre)if(e[i].too!=fa&&!del[e[i].too])
 77         dis[e[i].too]=dis[x]+1,getpoi(e[i].too,x);
 78 }
 79 void work(int dep,int x,int precg){
 80     int cg,i;int tmp=0;
 81     getrt(x,rt=0),cg=rt;//printf("  cg:%d   precg:%d
",cg,precg);
 82     if(dep>1){
 83         dis[x]=1,num=0,getpoi(x,precg);
 84         for(i=1;i<=num;i++)q[cg+n].push((zs1){p[i],dis[p[i]]}),tmp=max(tmp,dis[p[i]]);
 85         q[precg].push((zs1){cg+n,tmp});
 86     }
 87     mxd[cg]=dep,dis[cg]=0,del[cg]=1;
 88     
 89     num=0,getpoi(cg,0);
 90     for(i=1;i<=num;i++)f[dep][p[i]]=cg,dist[dep][p[i]]=dis[p[i]];
 91     //,printf("   %d",p[i]);puts("");
 92     
 93     for(i=last[cg];i;i=e[i].pre)if(!del[e[i].too])
 94         poi=sz[e[i].too],work(dep+1,e[i].too,cg);
 95     q[0].push((zs1){cg,v[cg]=getsm(cg)});
 96 }
 97 inline void modify(int x){
 98 //    printf("modify:%d
",x);
 99     gg[x]^=1;int fa,tmp;
100     if(!gg[x]){
101         v[x]=getsm(x),q[0].push((zs1){x,v[x]});
102         for(i=mxd[x]-1;i;i--){
103             q[f[i+1][x]+n].push((zs1){x,dist[i][x]}),clr(f[i+1][x]+n),
104             fa=f[i][x];
105             if((tmp=q[f[i+1][x]+n].top().v)==dist[i][x])
106                 q[fa].push((zs1){f[i+1][x]+n,tmp});
107             v[fa]=getsm(fa),q[0].push((zs1){fa,v[fa]});
108         }
109     }else{
110         v[x]=getsm(x),q[0].push((zs1){x,v[x]});
111         for(i=mxd[x]-1;i;i--)
112             fa=f[i][x],
113             q[fa].push((zs1){f[i+1][x]+n,getmx(f[i+1][x]+n).v}),
114             v[fa]=getsm(fa),q[0].push((zs1){fa,v[fa]});
115         
116     }
117     clr0();
118 }
119 int main(){
120     mx[0]=inf;
121     n=read();
122     for(i=1;i<n;i++)insert(read(),read());
123     poi=n,work(1,1,0);
124     char s[23];
125     for(m=read();m;m--){
126         scanf("%s",s);
127         if(s[0]=='G')printf("%d
",getans());else modify(read());
128     }
129     return 0;
130 }
View Code
原文地址:https://www.cnblogs.com/czllgzmzl/p/5414702.html