[CF339D] Xenia and Bit Operation - 线段树
作者:互联网
[CF339D] Xenia and Bit Operation - 线段树
Description
给定一个序列,每次选择一个点修改权值,然后求出全序列的值(每相邻两个元素按位或得到长度减半的序列,在对每相邻两个元素按位异或得到长度再次减半的序列……更替的进行按位或/异或)
Solution
不用想太多,直接线段树维护即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct SegmentTree
{
struct Node
{
int val;
int flag;
Node operator+(const Node &rhs)
{
if (flag)
return {val ^ rhs.val, 0};
else
return {val | rhs.val, 1};
}
};
vector<Node> nodes;
int n;
SegmentTree(int n) : n(n)
{
nodes.resize(4 * n + 4);
}
void pushup(int p)
{
nodes[p] = nodes[p * 2] + nodes[p * 2 + 1];
}
void build(int p, int l, int r, const vector<int> &src)
{
if (l == r)
{
nodes[p] = {src[l], 0};
}
else
{
build(p * 2, l, (l + r) / 2, src);
build(p * 2 + 1, (l + r) / 2 + 1, r, src);
pushup(p);
}
}
void modify(int p, int l, int r, int pos, int val)
{
if (l == r)
{
nodes[p] = {val, 0};
}
else
{
if (pos <= (l + r) / 2)
modify(p * 2, l, (l + r) / 2, pos, val);
else
modify(p * 2 + 1, (l + r) / 2 + 1, r, pos, val);
pushup(p);
}
}
void Build(const vector<int> &src)
{
build(1, 1, n, src);
}
void Modify(int pos, int key)
{
modify(1, 1, n, pos, key);
}
int Query()
{
return nodes[1].val;
}
};
signed main()
{
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
n = 1 << n;
vector<int> a(n + 2);
for (int i = 1; i <= n; i++)
cin >> a[i];
SegmentTree seg(n);
seg.Build(a);
for (int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
seg.Modify(x, y);
cout << seg.Query() << endl;
}
}
标签:src,CF339D,val,int,pos,Xenia,build,nodes,Bit 来源: https://www.cnblogs.com/mollnn/p/14424113.html