答案正确的TLE代码求卡常QAQ(50pts)
查看原帖
答案正确的TLE代码求卡常QAQ(50pts)
185223
huangziqinFJ人炖蒟蒻楼主2024/3/5 09:34
#include<bits/stdc++.h>
using namespace std;

#define mid ((l+r)>>1)
#define lson l,mid,now<<1
#define rson mid+1,r,now<<1|1
const int N=3e5+7;
const int M=7e5+7;

int bf[N],af[N],a[N],n,m,cnt=0,tot,opt,x,y,z;
map<int,int> mp;
set<int> st[M];
struct Node{int mn,mx,st,en,gcd,bf,af;}tr[N<<2];

inline int in()
{
	int x=0; char ch=getchar();
	while(ch>'9'||ch<'0') ch=getchar();
	while(ch<='9'&&ch>='0')
	{
		x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
	return x;
}

int GCD(int x,int y)
{
	if(!y) return x;
	else return GCD(y,x%y);
}

Node pushup(Node t1,Node t2)
{
	Node t; int k=(t2.st-t1.en>=0?t2.st-t1.en:t1.en-t2.st);
	t.mn=min(t1.mn,t2.mn); t.mx=max(t1.mx,t2.mx);
	t.st=t1.st; t.en=t2.en;
	if(t1.gcd>=0&&t2.gcd>=0) t.gcd=GCD(k,GCD(t1.gcd,t2.gcd));
	else if(t1.gcd>=0) t.gcd=GCD(t1.gcd,k);
		else if(t2.gcd>=0) t.gcd=GCD(t2.gcd,k);
			else t.gcd=k;
	t.bf=max(t1.bf,t2.bf); t.af=min(t1.af,t2.af);
	return t;
}

void bdtr(int l,int r,int now)
{
	if(l==r)
	{
		tr[now].mn=tr[now].mx=tr[now].st=tr[now].en=a[l];
		tr[now].gcd=-1; tr[now].bf=bf[l]; tr[now].af=af[l];
		return;
	}
	bdtr(lson); bdtr(rson);
	tr[now]=pushup(tr[now<<1],tr[now<<1|1]);
}

void xgtr(int l,int r,int now,int x)
{
	if(l==r)
	{
		tr[now].mn=tr[now].mx=tr[now].st=tr[now].en=a[x];
		tr[now].gcd=-1; tr[now].bf=bf[x]; tr[now].af=af[x];
		return;
	}
	if(x<=mid) xgtr(lson,x);
	else xgtr(rson,x);
	tr[now]=pushup(tr[now<<1],tr[now<<1|1]);
}

void xgtra(int l,int r,int now,int x)
{
	if(l==r)
	{
		tr[now].af=af[x]; return;
	}
	if(x<=mid) xgtra(lson,x);
	else xgtra(rson,x);
	tr[now].af=min(tr[now<<1].af,tr[now<<1|1].af);
}

void xgtrb(int l,int r,int now,int x)
{
	if(l==r)
	{
		tr[now].bf=bf[x]; return;
	}
	if(x<=mid) xgtrb(lson,x);
	else xgtrb(rson,x);
	tr[now].bf=max(tr[now<<1].bf,tr[now<<1|1].bf);
}

Node fdtr(int l,int r,int now,int x,int y)
{
	if(x<=l&&r<=y) return tr[now];
	if(y<=mid) return fdtr(lson,x,y);
	if(x>mid) return fdtr(rson,x,y);
	return pushup(fdtr(lson,x,y),fdtr(rson,x,y));
}

int main()
{
//	freopen("P5278_1.in","r",stdin);
//	freopen("P5278.out","w",stdout);
	n=in(); m=in();
	for(int i=1;i<=n;i++)
	{
		a[i]=in();
		if(mp.find(a[i])==mp.end()) mp[a[i]]=++cnt;
		st[mp[a[i]]].insert(i);
	}
	for(int i=1;i<=cnt;i++)
	{
		st[i].insert(n+1); st[i].insert(0);
		for(set<int>::iterator it=st[i].begin();it!=st[i].end();it++)
		{
			if((*it)==0||(*it)==n+1) continue;
            int nw=*it;
			bf[nw]=*(--it); it++; af[nw]=*(++it); it--;
		}
	}
	bdtr(1,n,1);
	tot=0;
	for(int i=1;i<=m;i++)
	{
		opt=in();
		if(opt==1)
		{
			x=in(); y=in();
			x^=tot; y^=tot;
			int xg1=bf[x],xg2=af[x],xg3,xg4;
			af[bf[x]]=af[x]; bf[af[x]]=bf[x];
			if(xg1>=1&&xg1<=n) xgtra(1,n,1,xg1); 
			if(xg2>=1&&xg2<=n) xgtrb(1,n,1,xg2);
			st[mp[a[x]]].erase(x);
			if(mp.find(y)==mp.end()) mp[y]=++cnt,st[cnt].insert(n+1),st[cnt].insert(0);
			set<int>::iterator k=upper_bound(st[mp[y]].begin(),st[mp[y]].end(),x);
			af[x]=*(k); bf[x]=*(--k); 
			if(bf[x]>=1&&bf[x]<=n) af[bf[x]]=x,xgtra(1,n,1,bf[x]);
			if(af[x]>=1&&af[x]<=n) bf[af[x]]=x,xgtrb(1,n,1,af[x]);
			a[x]=y;
			st[mp[y]].insert(x);
			xgtr(1,n,1,x);
		}
		if(opt==2)
		{
			x=in(); y=in(); z=in();
			x^=tot; y^=tot; z^=tot;
			if(x==y)
			{
				printf("Yes\n"); tot++; continue;
			}
			Node t=fdtr(1,n,1,x,y);
			if((long long)t.mx-t.mn==(long long)(y-x)*z&&(t.gcd==z||t.gcd==-1))
			{
				if((z&&t.bf<x&&t.af>y)||(!z))
				{
					printf("Yes\n"); tot++;
				}
				else printf("No\n");
			}
			else printf("No\n");
		}
	}
	return 0;
}
2024/3/5 09:34
加载中...