跳到正文

题解

题解:P7707 「Wdsr-2.7」百花齐放的太阳花田

c_a_s_t_o_r_i_c_e约 6 分钟
原文

这是 2026年10月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月5日
收录
2026年10月6日
更新
2026年11月15日
篇幅
约 6 分钟
获赞
1
题目
P7707

版本

  1. v2当前
  2. v1

模拟赛题,我说根号老哥未必不如双老哥(连续两天模拟赛根号老哥过了标程双老哥的题)。

题意

有 nn 个数,每个数有一个颜色,查询一个区间内小于 xx 的数形成几个颜色段。修改为在最后加入一个数。

思路

首先有个显然的分块做法,考虑到一个块内只有 O(n)O(\sqrt n) 个数,块内离散化一下预处理每个块的答案就行,查询就是暴力在每个块里找出这个查询的数在块内离散化后的答案。

然后问题就是在线离散化是要带 log⁡\log 的,那有没有办法去掉呢,答案是我没想到,离线容易做到单根号,但是在线好像好像只能带 log⁡\log。

那怎么办呢,其实吧 O(nnlog⁡)O(n\sqrt{n\log}) 也不是不能过 5e55e5,乱调块长到 10241024 就过了。

代码

赛时数据较水,没咋卡就过了,洛谷上通过的代码经过 deepseekdeepseek 卡常,但其实也没咋卡,主要是调了调块长就过了。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
const int B=1024,T=N/B+10;
int n,m,k;
struct node{
	int x,l,r;
};
inline node operator + (node x,node y){
	if(x.x==0) return y;
	if(y.x==0) return x;
	return {x.x+y.x-(x.r==y.l),x.l,y.r};
}
int a[N],pos[N];
node c[N];
node f[T+10][B+10];
int sz[T+10];
int ls[T+10][B+10];

void build(int p){
	int l=p*B-B+1,r=p*B;
	int* L=ls[p];
	int cnt=0;
	for(int i=l;i<=r;i++) L[cnt++]=a[i];
	sort(L,L+cnt);
	cnt=unique(L,L+cnt)-L;
	sz[p]=cnt;
	node* F=f[p];
	for(int i=0;i<=cnt;i++) F[i]={0,-1,-1};
	for(int i=l;i<=r;i++){
		int x=lower_bound(L,L+cnt,a[i])-L+1;
		node ci=c[i];
		for(int j=x;j<=cnt;j++){
			node& fj=F[j];
			if(fj.x==0) fj=ci;
			else{
				fj.x+=ci.x-(fj.r==ci.l);
				fj.r=ci.r;
			}
		}
	}
}
inline int rd(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline void wr(int x){
	if(x<0){putchar('-');x=-x;}
	if(x>9) wr(x/10);
	putchar(x%10+'0');
}
signed main(){
	// freopen("sunflower.in","r",stdin);
	// freopen("sunflower.out","w",stdout);
	n=rd();m=rd();k=rd();
	for(int i=1;i<=n;i++){
		a[i]=rd();
		pos[i]=(i-1)/B+1;
	}
	for(int i=1;i<=n;i++){
		c[i].l=rd();
		c[i].r=c[i].l;
		c[i].x=1;
	}
	for(int i=1;i<=n;i++){
		if(i%B==0) build(pos[i]);
	}
	int las=0;
	for(int op,l,r,x,y;m--;){
		op=rd();
		if(op==1){
			l=rd();r=rd();x=rd();
			l^=(las*k);r^=(las*k);x^=(las*k);
			int xl=pos[l],xr=pos[r];
			node ans={0,-1,-1};
			if(xl==xr){
				for(int i=l;i<=r;i++) if(a[i]<=x){
					if(ans.x==0) ans=c[i];
					else{ans.x+=1-(ans.r==c[i].l);ans.r=c[i].r;}
				}
			}
			else{
				int end_l=xl*B;
				for(int i=l;i<=end_l;i++) if(a[i]<=x){
					if(ans.x==0) ans=c[i];
					else{ans.x+=1-(ans.r==c[i].l);ans.r=c[i].r;}
				}
				for(int i=xl+1;i<xr;i++){
					int k=upper_bound(ls[i],ls[i]+sz[i],x)-ls[i];
					if(k==0) continue;
					node fi=f[i][k];
					if(ans.x==0) ans=fi;
					else{
						ans.x+=fi.x-(ans.r==fi.l);
						ans.r=fi.r;
					}
				}
				int start_r=(xr-1)*B+1;
				for(int i=start_r;i<=r;i++) if(a[i]<=x){
					if(ans.x==0) ans=c[i];
					else{ans.x+=1-(ans.r==c[i].l);ans.r=c[i].r;}
				}
			}
			las=ans.x;
			wr(las);
			putchar('\n');
		}
		else{
			x=rd();y=rd();
			x^=(las*k);y^=(las*k);
			a[++n]=x;
			c[n]={1,y,y};
			pos[n]=(n-1)/B+1;
			if(n%B==0) build(pos[n]);
		}
	}
	return 0;
}