跳到正文

题解

题解:P7172 [COCI 2020/2021 #3] Specijacija

linruiyu约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年7月15日
收录
2026年7月18日
更新
2026年11月7日
篇幅
约 10 分钟
获赞
3
题目
P7172

版本

  1. v1当前

首先看到题目以后,很暴力的想法就是计算每个点在第 ii 层的父亲是谁。首先如果把每个数转换为在第 ii 列排在的位置,即把 ai=ai−(i−1)×i2a_i=a_i-\frac{(i-1)\times i}{2},再打个表,算出样例中从叶子节点开始向上对应的父亲节点如下图:

[1234123311221111]\begin{bmatrix}1&2&3&4\\1&2&3&3\\1&1&2&2\\1&1&1&1\end{bmatrix}

发现每行的变化为一段连续后缀减一,而第 ii 行的开始点为给出的 ai−(i−1)×i2a_i-\frac{(i-1)\times i}{2} 在第 i−1i-1 行所在位置 pp 加一。

此时的父亲数组还是从叶子节点开始的,所以需要推广到每一行的节点。经过观察发现它位于第一个与 ai−(i−1)×i2a_i-\frac{(i-1)\times i}{2} 相等的数的位置。以样例举例,若我想知道 66 的位置,就只需要去第二行中查 6−2×32=36-\frac{2\times 3}{2}=3 的位置,查到 33 在第三个,则它的父亲为接下来几行的第三个位置。

完成了这些后,再考虑快速地求出上方的数组。需要复制上一行的元素并进行修改,很自然地想到了可持久化线段树,再加上区间修改就好了。

最后要讲的就是,如何去算 LCA,像普通的 LCA 可以用倍增数组,但是有点难写。所以可以直接二分,计算它们的父亲在哪一层就好了。

C++
#include<bits/stdc++.h>
#define int long long
#define up(i,x,y) for(register int i=x;i<=y;i=-~i)
#define dn(i,y,x) for(register int i=y;i>=x;i--)
#define mst(x,y) memset(x,y,sizeof x)
#define D(x) cout<<#x<<": "<<x<<endl;
#define DE(x) cout<<#x<<": "<<x<<" ";
#define MAXSIZE 1<<21
// #define faster 1
#define pl lson[p]
#define pr rson[p]
using namespace std;
namespace lry{
	char buf[MAXSIZE],*p1=buf,*p2=buf;
	char pbuf[MAXSIZE],*pp=pbuf;
	inline int gc(){
		#if faster
			return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
		#else
			return getchar();
		#endif
	}
	inline void pc(const char &c){
		#if faster
			if(pp-pbuf==MAXSIZE) fwrite(pbuf,1,MAXSIZE,stdout),pp=pbuf;*pp++=c;
		#else
			putchar(c);
		#endif
	}
	inline bool blank(const char x){return !(x^32)||!(x^10)||!(x^13)||!(x^9);}
	inline void read(double &rdx){double t=0;int x=0,s=0,f=1;char c;do c=gc();while(!isdigit(c)&&c!='-'&&c!='.');if (c=='-')f=-1,c=gc();while(isdigit(c)&&c!='.')x=(c^48)+(x<<1)+(x<<3),c=gc();if(c=='.')c=gc();else{rdx=x*f;return;}while(c>='0'&&c<='9')t=t*10+(c^48),s=-~s,c=gc();while(s--)t/=10;rdx=(x+t)*f;}
	inline void read(string &x){x="";char a=gc();for(;blank(a)&&(a^-1);a=gc());for(;!blank(a)&&(a^-1);a=gc())x+=a;}
	inline void read(char &x){char a=gc();for(;blank(a)&&(a^-1);a=gc());if(!blank(a)&&(a^-1))x=gc();}
	inline void read(int &x){int ret=0,f=0;char ch=gc();while(!isdigit(ch)){if(ch=='-')f=1;ch=gc();}while(isdigit(ch)){ret=(ret<<1)+(ret<<3)+(ch^48);ch=gc();}x=(f?-ret:ret);}
	template<typename t,typename ...T> inline void read(t &x,T&...y){read(x),read(y...);}
	inline void write(int x){if(x<0)pc('-'),x=-x;if(x>9)write(x/10);pc(x%10+48);}
	inline void write(const char *x){fputs(x,stdout);}
	inline void write(const string &x){fputs(x.c_str(),stdout);}
	inline void writeln(int x){write(x),pc('\n');}
	inline void writetr(int x){write(x),pc(' ');}
	inline void writeln(string x){write(x),pc('\n');}
	inline void writetr(string x){write(x),pc(' ');}
	template<typename t,typename ...T> inline void write(t x,T...y){write(x),write(y...);}
}
using namespace lry;
constexpr int N=1000000+10,inf=0x3f3f3f3f3f3f3f3f;
int n,q,t,a[N],b[N];
int rt[N];
int tr[N*24],tag[N*24],lson[N*24],rson[N*24],tot;
int pre[N][20];
inline void build(int &p,int L,int R){
	p=++tot;
	if(L==R){
		tr[p]=L;
		return;
	}
	int mid=(L+R)>>1;
	build(pl,L,mid); build(pr,mid+1,R);
	tr[p]=max(tr[pl],tr[pr]);
}
inline void update(bool is_new,int &p,int rt,int L,int R,int l,int r,int k){
	if(!is_new){
		p=++tot;
		tr[p]=tr[rt]; tag[p]=tag[rt]; pl=lson[rt]; pr=rson[rt];
	}
	if(l<=L&&R<=r){
		tr[p]+=k;
		tag[p]+=k;
		return;
	}
	bool son_is_new=0;
	if(tag[p]){
		son_is_new=1;
		pl=++tot; tag[pl]+=tag[lson[rt]]+tag[p];
		tr[pl]=tr[lson[rt]]+tag[p]; lson[pl]=lson[lson[rt]]; rson[pl]=rson[lson[rt]];
		pr=++tot; tag[pr]+=tag[rson[rt]]+tag[p];
		tr[pr]=tr[rson[rt]]+tag[p]; lson[pr]=lson[rson[rt]]; rson[pr]=rson[rson[rt]];
		tag[p]=0;
	}
	int mid=(L+R)>>1;
	if(l<=mid) update(son_is_new,pl,lson[rt],L,mid,l,r,k);
	if(r>mid) update(son_is_new,pr,rson[rt],mid+1,R,l,r,k);
	tr[p]=max(tr[pl],tr[pr]);
}
inline int query(int p,int L,int R,int x,int pusheddown){
	if(L==R) return tr[p]+pusheddown;
	int mid=(L+R)>>1,res=0;
	if(x<=mid) return query(pl,L,mid,x,pusheddown+tag[p]);
	else return query(pr,mid+1,R,x,pusheddown+tag[p]);
}
inline int cnt(int p,int L,int R,int q,int pusheddown){
	if(L==R) return L;
	int mid=(L+R)>>1;
	pusheddown+=tag[p];
	if(tr[pl]+pusheddown>=q) return cnt(pl,L,mid,q,pusheddown);
	else return cnt(pr,mid+1,R,q,pusheddown);
}
signed main(){
	read(n,q,t);
	for(int i=1;i<=n;i++) read(a[i]),a[i]-=(i-1)*i/2;
	for(int i=1;i<=n+1;i++) b[i]=b[i-1]+i;
	build(rt[0],1,n+1);
	for(int i=1;i<=n;i++){
		int p=cnt(rt[i-1],1,n+1,a[n-i+1]+1,0);
		update(0,rt[i],rt[i-1],1,n+1,p,n+1,-1);
	}
	int ans=0;
	while(q--){
		int x,y;
		read(x,y);
		x=(x-1+t*ans)%((n+1)*(n+2)/2)+1;
		y=(y-1+t*ans)%((n+1)*(n+2)/2)+1;
		int l=lower_bound(b+1,b+1+n+1,x)-b;
		int r=lower_bound(b+1,b+1+n+1,y)-b;
		int tx=x-l*(l-1)/2,ty=y-r*(r-1)/2;
		l=n-l+1; r=n-r+1;
		tx=cnt(rt[l],1,n+1,tx,0);
		ty=cnt(rt[r],1,n+1,ty,0);
		int L=max(l,r),R=n,res=n;
		while(L<=R){
			int mid=(L+R)>>1;
			if(query(rt[mid],1,n+1,tx,0)==query(rt[mid],1,n+1,ty,0)) R=mid-1,res=mid;
			else L=mid+1;
		}
		writeln(ans=query(rt[res],1,n+1,tx,0)+(n-res+1)*(n-res)/2);
	}
	#if faster
		fwrite(pbuf,1,pp-pbuf,stdout);
		pp=pbuf;
	#endif
	return 0;
}
原文