luogu P4585 [FJOI2015]火星商店问题

luogu

异或最大值显然可以01trie贪心选取

然后涉及到时间区间内元素贡献,可以把trie可持久化

还涉及区间内集合贡献,那么我们搞个线段树,把操作放到对应节点到根的链上,把询问放到对应区间的log个节点上,然后对着每个线段树节点计算贡献,算完后清空trie,空间\(O(nlogn)\),时间两个\(log\)

还有些和时间无关的物品,单独处理即可

#include<bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db double

using namespace std;
const int N=1e5+10;
int rd()
{
	int x=0,w=1;char ch=0;
	while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*w;
}
int n,m,q,an[N];
int s[N*70],ch[N*70][2],rt[N],tt;
void inst(int o1,int o2,int x)
{
	s[o1]=s[o2]+1;
	for(int i=16;~i;--i)
	{
		int xx=x>>i&1;
		ch[o1][xx]=++tt,ch[o1][xx^1]=ch[o2][xx^1];
		o1=ch[o1][xx],o2=ch[o2][xx];
		s[o1]=s[o2]+1;
	}
}
int quer(int o1,int o2,int x)
{
	int an=0;
	for(int i=16;~i;--i)
	{
		int xx=(x>>i&1)^1;
		if(s[ch[o1][xx]]-s[ch[o2][xx]]) an|=1<<i,o1=ch[o1][xx],o2=ch[o2][xx];
		else o1=ch[o1][xx^1],o2=ch[o2][xx^1];
	}
	return an;
}
struct node
{
	int o,d,x;
};
vector<node> op[N<<2],op2[N<<2];
vector<int> px;
void setop(bool oo,int o,int l,int r,int lx,node x)
{
	oo?op[o].push_back(x):op2[o].push_back(x);
	if(l==r) return;
	int mid=(l+r)>>1;
	if(lx<=mid) setop(oo,o<<1,l,mid,lx,x);
	else setop(oo,o<<1|1,mid+1,r,lx,x);
}
void setqr(bool oo,int o,int l,int r,int ll,int rr,node x)
{
	if(ll<=l&&r<=rr){oo?op[o].push_back(x):op2[o].push_back(x);return;}
	int mid=(l+r)>>1;
	if(ll<=mid) setqr(oo,o<<1,l,mid,ll,rr,x);
	if(rr>mid) setqr(oo,o<<1|1,mid+1,r,ll,rr,x);
}
int fdd(int x)
{
	int an=0,l=0,r=px.size()-1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(px[mid]<x) an=px[mid],l=mid+1;
		else r=mid-1;
	}
	return an;
}
void wk(int o,int l,int r)
{
	int latt=tt,nn=0;
	vector<node>::iterator it;
	for(it=op[o].begin();it!=op[o].end();++it)
	{
		node nw=*it;
		if(!nw.o)
		{
			px.push_back(nw.d);
			++nn;
			inst(rt[nw.d]=++tt,nn>=2?rt[px[nn-2]]:0,nw.x);
		}
		else if(nn)
		{
			int o1=px[nn-1],o2=o?fdd(nw.d):0;
			if(o1>o2) an[nw.o]=max(an[nw.o],quer(rt[o1],rt[o2],nw.x));
		}
	}
	vector<int>::iterator i1;
	for(i1=px.begin();i1!=px.end();++i1)
	{
		int xx=*i1;
		rt[xx]=0;
	}
	px.clear();
	while(tt>latt)
	{
		s[tt]=ch[tt][0]=ch[tt][1]=0;
		--tt;
	}
	nn=0;
	for(it=op2[o].begin();it!=op2[o].end();++it)
	{
		node nw=*it;
		if(!nw.o)
		{
			int las=rt[1];
			inst(rt[1]=++tt,las,nw.x);
		}
		else
			an[nw.o]=max(an[nw.o],quer(rt[1],rt[0],nw.x));
	}
	while(tt>latt)
	{
		s[tt]=ch[tt][0]=ch[tt][1]=0;
		--tt;
	}
	if(l<r) wk(o<<1,l,(l+r)>>1),wk(o<<1|1,((l+r)>>1)+1,r);
}

int main()
{
	n=rd(),q=rd();
	for(int i=1;i<=n;++i) setop(0,1,1,n,i,(node){0,i,rd()});
	for(int i=1,j=0;i<=q;++i)
	{
		int ty=rd();
		if(ty==0)
		{
			++j;
			int x=rd(),y=rd();
			setop(1,1,1,n,x,(node){0,j,y});
		}
		else
		{
			int l=rd(),r=rd(),x=rd(),d=rd();
			setqr(1,1,1,n,l,r,(node){++m,j-d+1,x});
			setqr(0,1,1,n,l,r,(node){m,j-d+1,x});
		}
	}
	wk(1,1,n);
	for(int i=1;i<=m;++i) printf("%d\n",an[i]);
	return 0; 
}
posted @ 2019-09-20 20:25  ✡smy✡  阅读(159)  评论(0编辑  收藏  举报