[题目链接]
https://codeforces.com/problemset/problem/339/D
[算法]
线段树模拟即可
时间复杂度 :O(MN)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 18 const int MAXS = 1 << MAXN; int n,m; int a[MAXS]; struct SegmentTree { struct Node { int l,r; int value,d; } Tree[MAXS << 2]; inline void update(int index) { if (Tree[index].d == 1) Tree[index].value = Tree[index << 1].value | Tree[index << 1 | 1].value; else Tree[index].value = Tree[index << 1].value ^ Tree[index << 1 | 1].value; } inline void build(int index,int l,int r) { Tree[index].l = l; Tree[index].r = r; if (l == r) { Tree[index].value = a[l]; Tree[index].d = 0; return; } int mid = (l + r) >> 1; build(index << 1,l,mid); build(index << 1 | 1,mid + 1,r); Tree[index].d = (Tree[index << 1].d + 1) % 2; update(index); } inline void modify(int index,int pos,int val) { if (Tree[index].l == Tree[index].r) { Tree[index].value = val; return; } int mid = (Tree[index].l + Tree[index].r) >> 1; if (mid >= pos) modify(index << 1,pos,val); else modify(index << 1 | 1,val); update(index); } inline int query() { return Tree[1].value; } } T; template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == ‘-‘) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - ‘0‘; x *= f; } int main() { read(n); read(m); for (int i = 1; i <= (1 << n); i++) read(a[i]); T.build(1,1,(1 << n)); while (m--) { int x,y; read(x); read(y); T.modify(1,x,y); printf("%d\n",T.query()); } return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。