【BZOJ】【P1146】【CTSC2008】【网络管理Network】【题解】【整体二分+dfs序+树状数组】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1146

当年写的log^4n链剖树套树二分被log^2完虐http://blog.csdn.net/iamzky/article/details/24550247


这就是差距

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=80005;
vector<int>G[maxn];
int n,m,siz[maxn],fa[maxn],top[maxn],dep[maxn],son[maxn],w[maxn],mp[maxn],z,L[maxn<<1],R[maxn<<1],tot;
struct BIT{
	int d[maxn<<1];
	inline int lowbit(int x){return x&-x;}
	void updata(int x,int f){
		while(x<=tot)d[x]+=f,x+=lowbit(x);
	}
	int get(int x){
		int ans=0;
		while(x)ans+=d[x],x-=lowbit(x);
		return ans;
	}
}T;
int getint(){
	int res=0;char c=getchar();
	while(!isdigit(c))c=getchar();
	while(isdigit(c))res=res*10+c-'0',c=getchar();
	return res;
}
void dfs(int u){
	L[u]=++tot;siz[u]=1;
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(v!=fa[u]){
			fa[v]=u;dep[v]=dep[u]+1;
			dfs(v);
			siz[u]+=siz[v];
			if(siz[son[u]]<siz[v])son[u]=v;
		}
	}R[u]=++tot;
}
void build(int u,int tp){
	mp[u]=++z;top[u]=tp;
	if(son[u])build(son[u],tp);
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(v!=fa[u]&&v!=son[u])build(v,v);
	}
}
int lca(int u,int v){
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		u=fa[top[u]];
	}return dep[u]<dep[v]?u:v;
}
int count(int u,int v){
	int ans=0;
	while(top[u]!=top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		ans+=abs(dep[u]-dep[fa[top[u]]]);
		u=fa[top[u]];
	}return ans+abs(dep[u]-dep[v])+1;
}
struct qes{
	int x,y,z,ty,delta,cur,id;
	qes(int _x=0,int _y=0,int _z=0,int _ty=0,int _delta=0,int _cur=0,int _id=0):
		x(_x),y(_y),z(_z),ty(_ty),delta(_delta),cur(_cur),id(_id){};
}q[maxn*3];
//ty 1 insert 2 erase 3 query
int qsize,anss[maxn],mx,md,tmp[maxn];
bool part(qes &Q){
	if(Q.ty==3){
		if(Q.delta+Q.cur>Q.z-1)return 1;
		Q.cur+=Q.delta;Q.delta=0;return 0;		
	}return Q.y<=md;
}
void solve(int lef,int rig,int l,int r){
	if(l==r){
		for(int i=lef;i<=rig;i++)if(q[i].ty==3)
			anss[q[i].id]=q[i].z>0?l:-1;
		return;
	}int mid=(l+r)>>1;md=mid;
	for(int i=lef;i<=rig;i++){
		if(q[i].ty==1&&q[i].y<=mid)
			T.updata(L[q[i].x],1),T.updata(R[q[i].x],-1),tmp[q[i].x]++;			
		if(q[i].ty==2&&q[i].y<=mid)
			T.updata(L[q[i].x],-1),T.updata(R[q[i].x],1),tmp[q[i].x]--;
		if(q[i].ty==3)
			q[i].delta=T.get(L[q[i].x])+T.get(L[q[i].y])-2*T.get(L[lca(q[i].x,q[i].y)])+tmp[lca(q[i].x,q[i].y)];
	}for(int i=lef;i<=rig;i++){
		if(q[i].ty==1&&q[i].y<=mid)
			T.updata(L[q[i].x],-1),T.updata(R[q[i].x],1),tmp[q[i].x]--;			
		if(q[i].ty==2&&q[i].y<=mid)
			T.updata(L[q[i].x],1),T.updata(R[q[i].x],-1),tmp[q[i].x]++;
	}int dv=stable_partition(q+lef,q+rig+1,part)-q-1; 
    if(lef<=dv)
	solve(lef,dv,l,mid);  
	if(dv+1<=rig)
    solve(dv+1,rig,mid+1,r);  	
}
int main(){
	n=getint();m=getint();
	for(int i=1;i<=n;i++){
		w[i]=getint();mx=max(mx,w[i]);
		q[++qsize]=qes(i,w[i],0,1);
	}
	for(int i=1;i<n;i++){
		int u=getint(),v=getint();
		G[u].push_back(v);G[v].push_back(u);
	}int root=1;//(n+1)>>1;
	dfs(root);
	build(root,root);
	for(int i=1;i<=m;i++){
		int k=getint(),a=getint(),b=getint();
		if(k==0){
			q[++qsize]=qes(a,w[a],0,2);
			q[++qsize]=qes(a,w[a]=b,0,1);
			mx=max(mx,w[a]);
		}else{
			int x=count(a,b);k=x-k+1;
			q[++qsize]=qes(a,b,k,3,0,0,++anss[0]);
		}
	}solve(1,qsize,0,mx);
	for(int i=1;i<=anss[0];i++)if(~anss[i])printf("%d\n",anss[i]);else puts("invalid request!");
	return 0;
}


你可能感兴趣的:(bzoj)