CSP-S2024 T4 arena

这个做法来看,对数做法到线性做法的跨度还比较大(?)

将打擂的过程看成一颗线段树。

定义“不定人”为一个 ai 不确定的的人,“固定人”反之。

性质 1:一个子树内的可能胜者要么唯一且是固定人,要么存在一个不定人。

若可能胜者有两个且无不定人,那么这两个人在合并的过程中必然会进行一次打擂。

性质 2:一个人不可能成为胜者当且仅当:

  • 某次打擂兄弟子树的胜者唯一且是固定人,且兄弟子树守擂,且守擂成功。
  • 某次打擂自己是擂主,但守擂失败。

显然,若一个人不满足以上两个条件,那么这个人就可能成为胜者。

考虑每一个 id[1,2logn] 的编号能对哪些前缀造成贡献。

性质 3:一个 id 能成为可能胜者的位置构成一段区间。

当某次与兄弟子树打擂失败后,这个 id 从兄弟子树内胜者确定的时刻开始便不会成为可能胜者。

首先遍历线段树预处理出来每个子树内的确定胜者和确定胜者的时刻,然后我们考虑线段树从上往下递归求答案。假设当前节点的所有点的可能贡献区间为 [l,r],那么对于擂主子树,其子树内的可能贡献区间显然不会发生变化;对于攻擂子树,若擂主子树内的胜者能守擂成功,那么攻擂子树内的可能区间将变为 [l,tp),其中 tp 是擂主子树胜者确定的时刻。对每段
(2k1,2k] 分别求答案即可。为了实现方便,将 [l,r] 写成左闭右开的形式。区间加差分即可。

由于线段树总点数 O(n),故时间复杂度 O(T(n+m))

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define IOS ios::sync_with_stdio(false)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define pc putchar
#define pb emplace_back
#define un using namespace
#define popc __builtin_popcountll
#define all(x) x.begin(),x.end()
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
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<<1)+(x<<3)+ch-48;ch=getchar();}return x*f;
}
template<typename T>
inline void write(T x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=131080,maxm=4e5+5,maxk=18,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q,K;
int a[maxn],b[maxn];
char s[maxk][maxn];
i64 c[maxn];
int X_qwq[maxn];
//能确定胜者的最小时刻 
int t[maxn<<2],g[maxn<<2];
inline void build(int p,int R,int G){
	if(R==0)return t[p]=g[p]=G+1,void();
	build(lson(p),R-1,lson(G)),build(rson(p),R-1,rson(G));
	if(s[R][G]=='0'){
		if(a[g[lson(p)]]>=R){
			t[p]=t[lson(p)],g[p]=g[lson(p)];
		}else{
			t[p]=t[rson(p)],g[p]=g[rson(p)];
		}
	}else{
		if(a[g[rson(p)]]>=R){
			t[p]=t[rson(p)],g[p]=g[rson(p)];
		}else{
			t[p]=t[rson(p)],g[p]=g[lson(p)];
		}
	}
}
inline void qry(int p,int R,int G,int lim,int l,int r){
	if(l>=r)return;
	if(R==0){
		const int id=G+1;
		if(a[id]>=lim)c[l]+=id,c[r]-=id;
		else if(l<id)c[l]+=id,c[min(r,id)]-=id;
		return;
	}
	if(s[R][G]=='0'){
		qry(lson(p),R-1,lson(G),max(lim,R),l,r);
		qry(rson(p),R-1,rson(G),lim,l,min(r,a[g[lson(p)]]>=R?t[lson(p)]:inf));
	}else{
		qry(rson(p),R-1,rson(G),max(lim,R),l,r);
		qry(lson(p),R-1,lson(G),lim,l,min(r,a[g[rson(p)]]>=R?t[rson(p)]:inf));
	}
}
inline void solve_the_problem(){
	rep(i,0,3)X_qwq[i]=rd();
	rep(i,1,n)a[i]^=X_qwq[i%4];
	build(1,K,0);
	rep(i,1,n)c[i]=0;
	rep(i,0,K)qry((1<<i),K-i,0,0,i==K?1:((1<<(K-i-1))+1),(1<<(K-i))+1);
	rep(i,1,n)c[i]+=c[i-1];
	i64 ans=0;
//	rep(i,1,n)write(c[i],32);
	rep(i,1,Q)ans^=c[b[i]]*i;
	write(ans,10);
	rep(i,1,n)a[i]^=X_qwq[i%4];
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	n=rd(),Q=rd();
	rep(i,1,n)a[i]=rd();
	rep(i,1,Q)b[i]=rd();
	K=ceil(log2(n));
	rep(i,1,K)scanf("%s",s[i]);
	
	int _=rd();
	while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18516707

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示