Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int maxn=200005;
- typedef long long ll;
- int n, m;
- int niza[maxn];
- struct node {
- ll value;
- int level;
- };
- node segment_tree[3 * maxn];
- void build_tree(int L, int R, int pos) {
- if(L == R) {
- segment_tree[pos].value = niza[L];
- segment_tree[pos].level = 1;
- return;
- }
- int mid = (L + R) / 2;
- build_tree(L, mid, 2 * pos);
- build_tree(mid + 1, R, 2 * pos + 1);
- segment_tree[pos].level = segment_tree[2 * pos].level + 1;
- if(segment_tree[pos].level % 2 == 0) {
- segment_tree[pos].value = (segment_tree[2 * pos].value | segment_tree[2 * pos + 1].value);
- }
- else {
- segment_tree[pos].value = (segment_tree[2 * pos].value ^ segment_tree[2 * pos + 1].value);
- }
- }
- void update(int L, int R, int pos, int idx, ll new_value) {
- if(L == R) {
- segment_tree[pos].value = new_value;
- return;
- }
- int mid = (L + R) / 2;
- if(idx <= mid) {
- update(L, mid, 2 * pos, idx, new_value);
- }
- else {
- update(mid + 1, R,2 * pos + 1, idx, new_value);
- }
- if(segment_tree[pos].level % 2 == 0) {
- segment_tree[pos].value = (segment_tree[2 * pos].value | segment_tree[2 * pos + 1].value);
- }
- else {
- segment_tree[pos].value = (segment_tree[2 * pos].value ^ segment_tree[2 * pos + 1].value);
- }
- }
- int main()
- {
- int n, m;
- cin >> n >> m;
- n = (1 << n);
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- }
- build_tree(0, n - 1, 1);
- for(int i = 0; i < m; i++) {
- int id;
- ll v;
- cin >> id >> v;
- id--;
- update(0, n - 1, 1, id, v);
- cout << segment_tree[1].value << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement