【2016北京集训】Mushroom

Portal --> broken qwq

Description

  一开始有个蘑菇,蘑菇里面有(n)个房间,是一棵有根树,(1)号是根,每个房间里面都有杂草,现在要支持以下操作:将某个指定蘑菇复制一份作为一个新的蘑菇;将蘑菇(v)合并到蘑菇(u)中,有杂草的房间取并(合并后(v)不会消失);某个蘑菇子树除草/除子树外除草;某个蘑菇路径除草/除路径外除草;某个蘑菇标号为(lsim r)房间除草/除了这些房间外除草;查询清除某个蘑菇上面所有杂草的时间:一单位时间内可以除最多连续(w)个下标连续的房间里面的草

  数据范围:(n<=50000,q<=100000,win[0,200])
  

Solution

  首先吐槽一句这什么鬼题啊==突然想丢出题人蘑菇qwq

  首先看那个查询操作,当然是选择贪心啊,每次找到下标最小的有杂草的房间然后往后面跳(w)位,再继续找就好了

​  然后。。前面的呢==维护一个二位的数据结构吗qwq

​  实际上正解是bitset,注意到每个房间只有两种状态:有杂草或者没有杂草,所以我们可以用(0)(1)来表示这两个状态,然后。。这个时候我们就可以快乐压位并且使用强大无比的位运算了

​  考虑每一个蘑菇开一个bitset,然后复制直接复制,合并就是两个bitset(&)一下,那么其他的各种花式除草怎么搞呢。。注意到花式除草什么的其实我们只要把那一部分的房间状态全部变成(0)就好了,然后每个蘑菇内部的树结构是一样的,所以我们可以先预处理出树上每个节点子树内的房间全(1)的bitset和树上每个节点到根路径上房间全(1)的bitset,然后我们实现一个(del)过程:将两个bitset中都为(1)的位变为(0),那么子树除草直接(del(subtree)),子树外除草直接(&subtree),路径除草的两种操作类似,只要先处理出路径上房间全(1)的bitset即可(可以用到根路径的bitset亦或起来再强制将(lca)那位变为(1)),然后至于下标区间修改和查询。。难道暴力跳吗==

  这里就出现了问题,所以我们需要手写bitset,因为自带的bitset并不支持大段大段位往后跳的操作

​  手写bitset的话,实现起来我们可以将(64)位压成一个unsigned long long,这样在最后两个与下标相关的操作中,我们就可以像。。分块那样大段大段跳了,写起来。。也和分块有点像(需要注意的是,压位的时候用二进制写出来是二进制的低位是对应排在前面的

​  最后这里mark几个用位运算的操作:

[egin{aligned} a\% 2^n&=a&(n-1)\ a*2^n&=a<<n\ a/2^n&=a>>n end{aligned} ]

  但是。。其实还没有完。。

​  算一下发现空间其实。。有点== 所以这里有一个空间优化的地方就是:因为我们可以离线,所以可以先将那些询问中不需要用到的子树bitset和到根bitset全部delete掉

  

​  代码大概长这个样子

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ull unsigned long long
#define Start(x) (x<<6)
#define End(x) (63+(x<<6))
using namespace std;
const int N=50010,TOP=16,B=64,Div=(1<<16)-1;
struct xxx{
	int y,nxt;
}a[N*2];
ull tmprec[N];
int Cnt0[1<<16];
ull lowbit(ull x){return x&-x;}
int CntZero(ull x){//计算写成二进制之后的末尾的0的个数
	if (x<=Div) return Cnt0[x];
	if (x<=(1LL<<32)-1) return 16+Cnt0[x>>16&Div];
	if (x<=(1LL<<48)-1) return 32+Cnt0[x>>32&Div];
	return 48+Cnt0[x>>48&Div];
}
struct bset{/*{{{*/
	ull *a;
	int cntb,cnt1;
	void set(int x){a[x>>6]|=1ULL<<(x&63);}
	void flip(int x){a[x>>6]^=1ULL<<(x&63);}
	void clear(int x){set(x); flip(x);}
	void alloc(int len){
		if (a!=NULL) Clear();
		cntb=(int)ceil(1.0*len/B);
		a=new ull[cntb];
		memset(a,0,sizeof(ull)*cntb);
		int debug=1;
	}
	void Clear(){
		if (a!=NULL){delete a; a=NULL;}
	}
	void operator |= (const bset &x)const{
		for (int i=0;i<cntb;++i) a[i]|=x.a[i];
	}
	void operator ^= (bset x){
		if (x.cntb>cntb) cntb=x.cntb;
		for (int i=0;i<cntb;++i) 
			a[i]^=x.a[i];
	}
	void operator &= (const bset &x)const{
		for (int i=0;i<cntb;++i) a[i]&=x.a[i];
	}
	void operator = (bset x){
		if (a==NULL) 
			alloc(x.cntb*64);
		for (int i=0;i<cntb;++i) a[i]=x.a[i];
	}
	void del_seg(int l,int r){
		if (l>r) return;
		int L=l>>6,R=r>>6;
		if (L==R)
			for (int i=l;i<=r;++i) 
				clear(i);
		else{
			for (int i=L+1;i<R;++i) a[i]=0;
			a[L]&=(1ULL<<(l-Start(L)))-1;
			if (r==End(R))
				a[R]=0;
			else 
				a[R]&=~((1ULL<<(r-Start(R))+1)-1);
		}
	}
	void del(bset &x){
		for (int i=0;i<cntb;++i) a[i]^=(a[i]&x.a[i]);
	}
	int query(int w){
		int now=0,ret=0,tmp,st,ed;
		for (int i=0;i<cntb;++i) tmprec[i]=a[i];
		while (now<cntb&&!tmprec[now]) ++now;
		while (1){
			while (now<cntb&&!tmprec[now]) ++now;
			if (now>=cntb) break;
			++ret;
			st=Start(now)+CntZero(lowbit(tmprec[now]));//有零要跳
			ed=st+w;
			tmp=ed>>6;
			if (ed==End(tmp))
				tmprec[tmp]=0,now=tmp+1;
			else
				tmprec[tmp]&=~((1ULL<<(ed-Start(tmp)+1))-1),now=tmp;
		}
		return ret;
	} 
}to_rt[N],subtree[N],rec[N*2];/*}}}*/
struct Q{/*{{{*/
	int op,u,x,y;
	void read(){
		scanf("%d",&op);
		if (op==1) scanf("%d",&u);
		else if (op<=4||op==9) scanf("%d%d",&u,&x);
		else scanf("%d%d%d",&u,&x,&y);
	}
}q[N*2];/*}}}*/
int h[N],f[N][TOP+1],dep[N];
bool use_tort[N],use_subtree[N];
int n,m,tot,id;
//tree part{{{
void add(int x,int y){a[++tot].y=y; a[tot].nxt=h[x]; h[x]=tot;}
void dfs(int fa,int x,int d){
	int u;
	to_rt[x]=to_rt[fa]; to_rt[x].set(x-1); 
	subtree[x].alloc(n); subtree[x].set(x-1);
	f[x][0]=fa; dep[x]=d;
	for (int i=1;i<=TOP;++i) f[x][i]=f[f[x][i-1]][i-1];
	for (int i=h[x];i!=-1;i=a[i].nxt){
		u=a[i].y;
		if (u==fa) continue;
		dfs(x,u,d+1);
		subtree[x]|=subtree[u];
	}
}
int get_lca(int x,int y){
	if (dep[x]<dep[y]) swap(x,y);
	for (int i=TOP;i>=0;--i)
		if (dep[f[x][i]]>=dep[y]) x=f[x][i];
	if (x==y) return x;
	for (int i=TOP;i>=0;--i)
		if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
	return f[x][0];
}/*}}}*/

void prework(){
	Cnt0[0]=1;
	for (int i=1;i<16;++i) Cnt0[1<<i]=i;
}
void init(){
	memset(h,-1,sizeof(h));
	tot=0;
}
void presolve(){
	int x,y;
	scanf("%d",&n);
	for (int i=1;i<n;++i){
		scanf("%d%d",&x,&y);
		add(x,y); add(y,x);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;++i){
		q[i].read();
		if (q[i].op<=2) continue;
		if (q[i].op==3||q[i].op==4)
			use_subtree[q[i].x]=true;
		else if (q[i].op<=6)
			use_tort[q[i].x]=true,use_tort[q[i].y]=true;
	}
}
void get_info(){
	to_rt[0].alloc(n);
	dfs(0,1,1);
	for (int i=1;i<=n;++i){
		if (!use_tort[i]) to_rt[i].Clear();
		if (!use_subtree[i]) subtree[i].Clear();
	}
}
void print(ull x){
	for (int i=0;i<64;++i)
		printf("%d",x>>i&1);
}
void debug(bset &x){
	for (int i=0;i<x.cntb;++i)
		print(x.a[i]);
	printf("
");
}
void solve(){
	id=1;
	rec[id].alloc(n);
	for (int i=1;i<=n;++i) rec[id].set(i-1);
	static bset tmp,tmp1; 
	tmp.alloc(n);
	for (int i=1;i<=m;++i){
		if (q[i].op==1) rec[++id]=rec[q[i].u];
		else if (q[i].op==9)
			printf("%d
",rec[q[i].u].query(q[i].x));
		else{
			tmp=rec[q[i].u];
			if (q[i].op==2)
				tmp|=rec[q[i].x];
			else if (q[i].op==3)
				tmp.del(subtree[q[i].x]);
			else if (q[i].op==4)
				tmp&=subtree[q[i].x];
			else if (q[i].op==7)
				tmp.del_seg(q[i].x-1,q[i].y-1);
			else if (q[i].op==8){
				tmp.del_seg(0,q[i].x-2);
				tmp.del_seg(q[i].y,n-1);
			}
			else{
				//debug(tmp);
				tmp1=to_rt[q[i].x];
				//debug(to_rt[q[i].x]);
				tmp1^=to_rt[q[i].y];
				//debug(to_rt[q[i].y]);
				tmp1.set(get_lca(q[i].x,q[i].y)-1);
				if (q[i].op==5)
					tmp.del(tmp1);
				else
					tmp&=tmp1;
				//debug(tmp);
			}
			rec[q[i].u]=tmp;
		}
		//debug(rec[q[i].u]);
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
#endif
	prework();
	init();
	presolve();
	get_info();
	solve();
}
原文地址:https://www.cnblogs.com/yoyoball/p/9690420.html