跳到正文

题解

P15094 [UOI 2025 II Stage] Three Queries

Dea_He约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年2月11日
收录
2026年2月13日
更新
2026年10月24日
篇幅
约 12 分钟
获赞
4
题目
P15094

版本

  1. v2当前
  2. v1

题目大意

有三种类型的操作:

  1. 1 x,将 xx 颜色的状态反转。

  2. 2 l r,询问 a[l,r]a[l,r] 中有多少种本质不同的,状态为 11 的颜色。

  3. 3 x y,赋值 a[x]a[x] 为 yy。

解题思路

以下均假设 nn 和 qq 同阶。

容易想到根号分治。

重新对大小块定义:对操作序列分块,将每个操作块中对颜色有改变的颜色提出来作为大块,未改变的称为小块。

  • 对于小块,可以对扫描线,每次只让每种颜色最后一次出现的位置有贡献,可以采用 O(1)O(1) 修改 O(n)O(\sqrt n) 查询。

  • 对于大块,需要处理每个位置向后第一个颜色等于 xx 的位置,若 p[x][l]≤rp[x][l] \le r 则有贡献。但此时发现不能处理散块位置,这样会导致复杂度退化为 O(n2)O(n^2)。那么我们可以对每个询问暴力找其 ll 端点对应的散块的贡献,整块采用 O(n)O(\sqrt n) 修改 O(1)O(1) 查询。

C++
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define pb push_back
#define ll long long
#define Clean(x) !x.empty()&&(x.shrink_to_fit(),x.clear(),1)
#define low lower_bound
#define Low(x) (int)(low(t+1,t+cnt_t+1,x)-t)
using namespace std;
using namespace __gnu_pbds;
char BEGIN;
namespace FastIO
{
    const int BUF_SIZE=1<<20;
    char in_buf[BUF_SIZE],out_buf[BUF_SIZE];
    char* in_ptr=in_buf+BUF_SIZE;
    char* out_ptr=out_buf;
    char get_char()
    {
        if(in_ptr==in_buf+BUF_SIZE)
        {
            in_ptr=in_buf;
            fread(in_buf,1,BUF_SIZE,stdin);
        }
        return *in_ptr++;
    }
    void put_char(char c)
    {
        if(out_ptr==out_buf+BUF_SIZE)
        {
            fwrite(out_buf,1,BUF_SIZE,stdout);
            out_ptr=out_buf;
        }
        *out_ptr++=c;
    }
    struct Flusher
    {
        ~Flusher()
        {
            if(out_ptr!=out_buf)
            {
                fwrite(out_buf,1,out_ptr-out_buf,stdout);
            }
        }
    } flusher;
}
#define getchar FastIO::get_char
#define putchar FastIO::put_char
inline int read()
{
    int x=0;
    bool zf=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
        {
            zf=0;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return zf?x:-x;
}
void print(const int x)
{
    if(x==0)
    {
        putchar('0');
        return;
    }
    char buf[12];
    int len=0,y=x;
    if(y<0)
    {
        putchar('-');
        y=-y;
    }
    while(y)
    {
        buf[len++]=(y%10)+'0';
        y/=10;
    }
    while(len--)
    {
        putchar(buf[len]);
    }
}
inline void princh(const int x,const char ch)
{
    print(x);
    putchar(ch);
}
namespace hwq{
	const int MAXN=6e5+5;
    int n,q,B,num,tot;
	int bl[MAXN],br[MAXN],belong[MAXN];
    int a[MAXN],s[MAXN],id[MAXN];
    int t[MAXN],cnt_t,Ans[MAXN];
    bool col[MAXN],vis[MAXN];
    struct ope{
    	int op,l,r,x,y;
	}Ope[MAXN];
	struct qry{
		int l,r,id;
		bool operator<(const qry &a)const{return r<a.r;}
	}Q[MAXN];
	struct big{
		int mn[550],val[550];
		inline void init(){for(int i=1;i<=num;i++) val[i]=n+1;} 
		inline void add(int x){int t=belong[x];val[t]=min(val[t],x);}
		inline void build(){
			mn[num+1]=n+1;
			for(int i=num;i>=1;i--) mn[i]=min(mn[i+1],val[i]);
		}
		inline void chg(int x,int c){
			int t=belong[x];
			val[t]=n+1;
			for(int i=br[t];i>=bl[t];i--) if(a[i]==c) val[t]=i;
			for(int i=t;i>=1;i--) mn[i]=min(mn[i+1],val[i]);
		}
	}Big[2405];
	inline void Init(){
		tot=0;
		for(int i=1;i<=cnt_t;i++) if(vis[i]) s[id[i]=++tot]=i,Big[tot].init(); 
		for(int i=1;i<=n;i++) if(vis[a[i]]) Big[id[a[i]]].add(i);
		for(int i=1;i<=tot;i++) Big[i].build();
	}
	int sum1[MAXN],sum2[MAXN],pre[MAXN];
	int main(){
		n=read(),q=read();
		for(int i=1;i<=n;i++) t[++cnt_t]=a[i]=read();
		for(int i=1,op,l=0,r=0,x=0,y=0;i<=q;i++){
			op=read();
			if(op==1) x=read();
			else if(op==2) l=read(),r=read();	
			else x=read(),t[++cnt_t]=y=read();
			Ope[i]={op,l,r,x,y};
		}
		sort(t+1,t+cnt_t+1),cnt_t=unique(t+1,t+cnt_t+1)-(t+1);
		for(int i=1;i<=n;i++) a[i]=Low(a[i]);
		B=1200;
		for(int i=1,j;i<=n;i=j+1){
			j=min(n,i+B-1),bl[++num]=i,br[num]=j;
			for(int k=i;k<=j;k++) belong[k]=num;
		}
		for(int i=1;i<=cnt_t;i++) col[i]=1; 
		for(int i=1,j,op,l,r,x,y;i<=q;i=j+1){
			for(int k=1;k<=n;k++) pre[a[k]]=0,sum2[k]=0;
			for(int k=1;k<=num;k++) sum1[k]=0;
//			j=min(q,i+550-1);
			j=i;
			int cnt=0;
			while(j<q&&cnt<=1200) cnt+=Ope[j++].op!=2;
			int top=0;
			for(int k=i;k<=j;k++){
				op=Ope[k].op,l=Ope[k].l,r=Ope[k].r,x=Ope[k].x,y=Ope[k].y;
				if(op==1){
					int tx=Low(x);
					if(t[tx]!=x) continue;
					vis[tx]=1;
				}else if(op==2) Q[++top]={l,r,k}; 
				else vis[a[x]]=vis[Low(y)]=1;
			}
			Init();
//			printf("%d\n",tot);
			sort(Q+1,Q+top+1);
			for(int k=1,tq=1;k<=n;k++){
				if(!vis[a[k]]&&col[a[k]]){
					int t=pre[a[k]];
					sum1[belong[t]]--,sum2[t]--;
					sum1[belong[k]]++,sum2[k]++;
					pre[a[k]]=k;
				}
				while(tq<=top&&Q[tq].r==k){
					int l=Q[tq].l,r=Q[tq].r,id=Q[tq].id;tq++;
					if(r-l+1<=1500) continue;
					int tl=belong[l],tr=belong[r];
					if(tl==tr){
						for(int i=l;i<=r;i++) Ans[id]+=sum2[i];
						continue;
					}
					for(int i=l;i<=br[tl];i++) Ans[id]+=sum2[i];
					for(int i=bl[tr];i<=r;i++) Ans[id]+=sum2[i];
					for(int i=tl+1;i<tr;i++) Ans[id]+=sum1[i];
				}
			}
			for(int k=1;k<=cnt_t;k++) vis[k]=0; 
			for(int k=i;k<=j;k++){
				op=Ope[k].op,l=Ope[k].l,r=Ope[k].r,x=Ope[k].x,y=Ope[k].y;
				if(op==1){
					int tx=Low(x);
					if(t[tx]!=x) continue;
					col[tx]^=1;
				}else if(op==2){
					int ans=Ans[k],t=belong[l];
					if(r-l+1<=1500){
						ans=0;
						for(int i=l;i<=r;i++) if(!vis[a[i]]&&col[a[i]]) ans++,vis[a[i]]=1;
						for(int i=l;i<=r;i++) vis[a[i]]=0;
					}else{
						for(int i=l;i<=min(r,br[t]);i++) if(col[a[i]]&&id[a[i]]&&!vis[a[i]]) ans++,vis[a[i]]=1;
						for(int i=1;i<=tot;i++){
							if(col[s[i]]&&!vis[s[i]]&&Big[i].mn[t+1]<=r) ans++;
							vis[s[i]]=0;
						}
					}
					princh(ans,'\n');
				}else{
					y=Low(y);
					int t=a[x];
					if(t==y) continue;
					a[x]=y;
					Big[id[t]].chg(x,t);
					Big[id[y]].chg(x,y);
				}
			}
			for(int i=1;i<=tot;i++) id[s[i]]=vis[s[i]]=0;
		}
		return 0;
	}
}
char END;
signed main(){
#ifdef HQYG
	printf("Memory : %lld MB\n",(&BEGIN-&END)>>20);
	freopen("P15094_162.in","r",stdin);
	freopen("1.out","w",stdout);
#endif
	hwq::main();
	return 0;
}
/*
*/
原文