跳到正文

个人记录

CF339D 解题报告

gcx12012约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年12月17日
收录
2025年12月17日
更新
2026年11月9日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

你发现这个题的合并过程是这样的: 它很像线段树,所以可以直接构建一棵线段树来处理。因为是单点修改,我们可以直接递归到线段树的叶节点,然后对这个点单点修改之后上传到父节点然后合并信息,合并信息的方式取决于当前节点的深度。

详情请见代码:

C++
#include<bits/stdc++.h>
#include<cmath>
#define ll long long
#define ld long double
#define ull unsigned long long
#define lll __int128
#define N 500010
#define For(i,a,b) for(int i=a;i<=b;++i)
#define Rof(i,a,b) for(int i=a;i>=b;--i)
#define ls x<<1
#define rs x<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
#define mk make_pair
#define pb push_back
#define pii pair<ll,ll>
#define pque priority_queue
#define fi first
#define se second

using namespace std;
int a[1<<20];
int n,q;

ll read(){
    ll 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*10+ch-'0';ch=getchar();}
    return x*f;
}
void upd(int x,int l,int r,int dep,int u,int v){
    if(l==r){
        a[x]=v;
        return;
    }
    int mid=(l+r)>>1;
    if(u<=mid) upd(lson,dep-1,u,v);
    else upd(rson,dep-1,u,v);
    if(dep&1) a[x]=(a[ls]|a[rs]);
    else a[x]=(a[ls]^a[rs]);
}

int main()
{
    //freopen("5.in","r",stdin);
    //freopen("5.out","w",stdout);
    n=read(),q=read();
    For(i,1,(1<<n)){
        int u=read();
        upd(1,1,(1<<n),n,i,u);
    }
    while(q--){
        int u=read(),v=read();
        upd(1,1,(1<<n),n,u,v);
        cout<<a[1]<<endl;
    }
    return 0;
}
/*

*/
原文