CodeForces - 339D Xenia and Bit Operations

CodeForces - 339D Xenia and Bit Operations

题意:

给定n和m,给你一个包含2^n个数的数组,有m次操作,每次操作先将p位置的数修改,先两两相或使数组减少一半,再两两相异或使数组再减少一半,一直重复这两种操作,直到数组剩余一个元素。

m行操作

p  v (假设原数组为a)

将a[p]的值改为v

第一次计算 b1 = a[1] | a[2] , b2 = a[ 3 ] | a[ 4 ] , b3 = a[ 5 ] | a[ 6 ] , b4 = a[ 7 ] | a[ 8 ] 

第二次计算 c1 = b1^b2 , c2 = b3^b4

第三次计算 w = c1 | c2

输出 w

思路:

维护一棵线段树,根节点即为最后一个元素的值。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
int a[maxn],sum[maxn];
void pushup(int rt,int f)  //树的深度为n+1 叶子结点不需要操作所以需要操作n次
{
    if(f==1)    
        sum[rt] = sum[ls]|sum[rs];
    else
        sum[rt] = sum[ls]^sum[rs];
}
void build(int l,int r,int rt,int f)
{
    if(l==r)
    {
        sum[rt] = a[l];
        return ;
    }
    int m = (l+r)>>1;
    build(l,m,ls,1-f);
    build(m+1,r,rs,1-f);
    pushup(rt,f);
}
void update(int p,int c,int l,int r,int rt,int f)
{
    if(l==r)
    {
        sum[rt] = c;
        return ;
    }
    int m = (l+r)>>1;
    if(p<=m)
        update(p,c,l,m,ls,1-f);
    if(p>m)
        update(p,c,m+1,r,rs,1-f);
    pushup(rt,f);
}
int main()
{
    int n,m;
    int k;
    scanf("%d%d",&n,&m);
    k = (1<<n);
    for(int i=1;i<=k;i++)
        scanf("%d",&a[i]);
    build(1,k,1,n%2);  //保证从叶子节点开始的第一次操作为 或 操作
    for(int i=0;i<m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        update(x,y,1,k,1,n%2);
        printf("%d\n",sum[1]);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值