Xenia and Bit Operations 【CodeForces - 339D】【线段树+位运算】

题目链接


  这道题难理解也就在于题意了,题目要求的是对于原来的这个数(一共(1<<N)个数)进行两两之间先或运算再亦或运算,因为数的个数刚好是2^N次,所以直接就是一个完全二叉树,直接线段树的做法即可了。

  每个查询会改变某一点的值,然后输出整个的答案。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 3e5 + 7;
int N, tot, M, tree[maxN<<2];
void buildTree(int rt, int l, int r, int div)
{
    if(l == r)
    {
        scanf("%d", &tree[rt]);
        return;
    }
    int mid = HalF;
    buildTree(Lson, div - 1);
    buildTree(Rson, div - 1);
    if(div & 1) tree[rt] = tree[lsn] ^ tree[rsn];
    else tree[rt] = tree[lsn] | tree[rsn];
}
void update(int rt, int l, int r, int qx, int val, int div)
{
    if(l == r) { tree[rt] = val;    return; }
    int mid = HalF;
    if(qx <= mid) update(Lson, qx, val, div - 1);
    else update(Rson, qx, val, div - 1);
    if(div & 1) tree[rt] = tree[lsn] ^ tree[rsn];
    else tree[rt] = tree[lsn] | tree[rsn];
}
int main()
{
    scanf("%d%d", &N, &M);
    tot = 1<<N;
    buildTree(1, 1, tot, N + 1);
    int x, y;
    while(M--)
    {
        scanf("%d%d", &x, &y);
        update(1, 1, tot, x, y, N + 1);
        printf("%d\n", tree[1]);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值