跳到正文

题解

做题心得 - 2025 NOIP #52 - T3

ExFish约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月1日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

发现这题学长有很牛的做法,遂学习之。做法是直接对操作分块,然后修改转为前缀和,块内再暴力枚举,每一块运行就直接前缀和。具体的,我们可以先把三个操作分开,分为三个分块,最后计算答案的时候只需要统计三个块内的值即可。我们先把块长改为 2112^{11},因为区间长度都是 2202^{20}。

然后我们在块内修改,并注意对编号进行取模。现在我们需要注意的是如何修改。对于操作 11,因为只涉及到单点加,只需要使用一个 O(2n)\mathcal O(\sqrt{2^n}) 做法来枚举超集来累加答案即可。对于操作 22 也就是超集加,我们可以先直接枚举超集 ii,然后发现一个点对前缀和的贡献是二的其中 i⊕Si\oplus S 的 11 数 kk 次方,也就是 2k2^k。这是为什么?这个就是超集多出的那部分的 11 个数的 2k2^k。多出来的 11 都有也可以不有,自然都有 2k2^k 倍贡献。同理,子集的贡献就是 iand⁡Si\operatorname{and} S。

最后一步,如何统计询问的答案。我们发现答案其实就是把种修改对答案的贡献全部统计起来,然后就结束了。代码清晰好懂,但是要想到这个比较有难度,加练分块。

C++
#include<bits/stdc++.h>
#define ll long long
#define getchar getchar_unlocked
#define putchar putchar_unlocked
using namespace std;
const int N=11e5+10,P=998244353;
int n,q,op,S,c;
const int BV=11,BS=1<<11,BM=1<<9;
int f[3][BM+5][BS+5];
ll pc[(1<<20)+5];
template<typename type>
inline void read(type &x){
    x=0;bool flag(0);char ch=getchar();
    while(!isdigit(ch))flag=ch=='-',ch=getchar();
    while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
    flag?x=-x:0;return;
}
template<typename type>
inline void write(type x){
    x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
    do Stack[++top]=x%10,x/=10; while(x);
    while(top) putchar(Stack[top--]|48);
}
inline void mod(int &x,int y){
	x+=y;
	if(x>=P) x-=P;
}
int main(){
	freopen("gene.in","r",stdin);
	freopen("gene.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	read(n);
	read(q);
	for(int i=0;i<1048576;++i)
		pc[i]=1<<__builtin_popcount(i);
	while(q--){
		read(op);
		read(S);
		int SBV=S>>BV,SBS=S%BS;
		if(op) read(c);
		if(!op){
			int ans=0;
			for(int i=0;i<BM;++i)if((i|SBV)==SBV)
				mod(ans,f[0][i][SBS]),
				mod(ans,f[1][i][SBS]*pc[i^SBV]%P);
			for(int i=0;i<BM;++i)
				mod(ans,f[2][i][SBS]*pc[i&SBV]%P);
			cout<<ans<<'\n';
		}else if(op==1){
			for(int i=0;i<BS;++i)if((SBS|i)==i)
				mod(f[0][SBV][i],c);
		}else if(op==2){
			for(int i=0;i<BS;++i)if((SBS|i)==i)
				mod(f[1][SBV][i],c*pc[i^SBS]%P);
		}else if(op==3){
			for(int i=0;i<BS;++i)
				mod(f[2][SBV][i],c*pc[i&SBS]%P);
		}
	}
	return 0;
}
原文