P10408 「SMOI-R1」Apple

据说是个典,记录一下吧。

题意

给你 {a2n}q 次操作,下标从 0 开始。操作如下:

  • 1 x,表示查询 yorx=xay 的值。
  • 2 x v,表示 axv

n20,q3×105

分析

yx 的子集当且仅当 yorx=x

考虑一个经典的暴力:查询的时候暴力枚举所有子集的 ai 并求和,修改 O(1),查询 O(2n)

发现修改和查询复杂度极度不平衡,考虑另一个能快速查询的暴力:设 fSS 的所有子集的权值和。操作前把所有的 fS 用高维前缀和 O(2nn) 求出来(https://www.cnblogs.com/dcytrl/p/18406487 在线推销高维前缀和),修改时需要修改 S 的所有超集的 f 值,修改 O(2n),查询 O(1)

考虑一下怎么结合这两个暴力?

考虑将 2n 的前 B=n2 位(低 B 位)和后 B 位(高 B 位)分别考虑,对于后 B 位维护 fS,T 表示当后十位为 S 时前十位的高维前缀和。也就是说,对于后 B 位,采用第一种暴力;对于前 B 位,采用第二种暴力。

修改的话,由于我们要对前 10 位采用第二种暴力,所以直接枚举 S 的前 B 位的所有超集并更新它们的 f,单次复杂度 O(2B)

查询的话,由于我们要对后 10 位采用第一种暴力,所以直接枚举 S 的后 B 位的所有子集,然后直接 O(1) 地取出对应的 S 的前 10 位的高维前缀和,单次复杂度 O(2B)

时间复杂度 O(2nB+q2B)。实现时,为了方便直接令 B=10

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#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 gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#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;
typedef long long i64;
typedef unsigned long long u64;
using pii=pair<int,int>;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
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;
}
inline void write(i64 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=1048576,maxm=1024,inf=0x3f3f3f3f,B=10,Ub=(1<<B)-1;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q;
int a[maxn];
i64 f[maxm][maxm];
inline void solve_the_problem(){
	n=(1<<rd())-1,Q=rd();rep(i,0,n)a[i]=rd();
	rep(i,0,Ub)rep(j,0,Ub)f[i][j]=a[(i<<B)|j];
	rep(S,0,Ub){
		rep(i,0,B-1)rep(T,0,Ub){
			if(T&(1<<i))f[S][T]+=f[S][T^(1<<i)];
		}
	}
	while(Q--){
		int op=rd(),S=rd(),x,delta;
		if(op==2){
			x=rd(),delta=x-a[S],a[S]=x;
			const int X=S>>B,S_=S&Ub,_S_=Ub^S_;
			for(int T=_S_;;T=(T-1)&_S_){
				f[X][T|S_]+=delta;
				if(!T)break;
			}
		}else{
			i64 ans=0;
			const int S_=S>>B,Y=S&Ub;
			for(int T=S_;;T=(T-1)&S_){
				ans+=f[T][Y];
				if(!T)break;
			}
			write(ans,10);
		}
	}
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;
	while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

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

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

posted @   dcytrl  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示