P10408 「SMOI-R1」Apple
据说是个典,记录一下吧。
题意
给你
1 x
,表示查询 的值。2 x v
,表示
分析
令
考虑一个经典的暴力:查询的时候暴力枚举所有子集的
发现修改和查询复杂度极度不平衡,考虑另一个能快速查询的暴力:设
考虑一下怎么结合这两个暴力?
考虑将
修改的话,由于我们要对前
查询的话,由于我们要对后
时间复杂度
点击查看代码
#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();
}
/*
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现