339D - Xenia and Bit Operations - CodeForces Solution


data structures trees *1700

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <vector>

using namespace std;

vector <long long> a;
vector <long long> t;

void build(int v, int l, int r) {
    if (r - l == 1) {
        t[v] = a[l];
        return;
    }
    int m = (l + r) / 2;
    build(2 * v, l, m);
    build(2 * v + 1, m, r);
    if (__lg(r - l) % 2 == 1) {
        t[v] = t[2 * v] | t[2 * v + 1];
    } else {
        t[v] = t[2 * v] ^ t[2 * v + 1];
    }
}


void upd(int v, int l, int r, int i, long long x) {
    if (r - l == 1) {
        t[v] = x;
        return;
    }
    int m = (l + r) / 2;
    if (i < m) {
        upd(2 * v, l, m, i, x);
    } else {
        upd(2 * v + 1, m, r, i, x);
    }
    if (__lg(r - l) % 2 == 1) {
        t[v] = t[2 * v] | t[2 * v + 1];
    } else {
        t[v] = t[2 * v] ^ t[2 * v + 1];
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    a.resize(1 << n);
    t.resize(1 << (n + 2));
    for (int i = 0; i < (1 << n); ++i) {
        cin >> a[i];
    }
    build(1, 0, (1 << n));
    for (int i = 0; i < m; ++i) {
        int p, b;
        cin >> p >> b;
        upd(1, 0, (1 << n), p - 1, b);
        cout << t[1] << "\n";
    }
}

/* Tue Jan 31 2023 11:46:58 GMT+0300 (Moscow Standard Time) */


Comments

Submit
0 Comments
More Questions

221A - Little Elephant and Function
492C - Vanya and Exams
1369B - AccurateLee
892B - Wrath
999A - Mishka and Contest
727C - Guess the Array
1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game