/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 52ms 46.324 MiB
#2 Accepted 51ms 46.273 MiB
#3 Accepted 52ms 46.363 MiB
#4 Accepted 51ms 46.227 MiB
#5 Accepted 52ms 46.25 MiB
#6 Accepted 73ms 46.312 MiB
#7 Accepted 417ms 47.27 MiB
#8 Accepted 409ms 47.227 MiB
#9 Accepted 412ms 47.223 MiB
#10 Accepted 417ms 47.137 MiB
#11 Accepted 171ms 46.715 MiB
#12 Accepted 659ms 47.512 MiB
#13 Accepted 260ms 47.203 MiB

Code

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;

const int N = 1e5 + 3, mod = 1e9 + 7;
vector<vector<int>> t(4 * N, vector<int> (20, 0));

struct Segment_Tree {
    void merge(auto &l, auto &r, auto& Cur) { // <== Change this function
        for(int b = 0; b < 20; b++) Cur[b] = l[b] + r[b];
        return;
    }
    void build(int node, int st, int en, vector<int> &arr) { //=> O(N)
        if (st == en) {
            // t[node] = arr[st];
            auto& x = t[node];
            bitset<20> b1 = arr[st];
            for(int b = 0; b < 20; b++) x[b] = b1[b];
            return;
        }
        int mid = (st + en) >> 1;
        build(node << 1, st, mid, arr); // divide left side
        build(node << 1 | 1, mid + 1, en, arr); // divide right side
        // Merging left and right portion
        auto &Cur = t[node];
        auto &Left = t[node << 1];
        auto &Right = t[node << 1 | 1];
        merge(Left, Right, Cur);
        return;
    }
    void update(int node, int st, int en, int idx, int val) { //=> O(log n)
        if (st == en) {
            auto& x = t[node];
            bitset<20> b1 = val;
            for(int b = 0; b < 20; b++) x[b] ^= b1[b];
            return;
        }
        int mid = (st + en) >> 1;
        if (idx <= mid) update(node << 1, st, mid, idx, val);
        else update(node << 1 | 1, mid + 1, en, idx, val);
        // Merging left and right portion
        auto &Cur = t[node];
        auto &Left = t[node << 1];
        auto &Right = t[node << 1 | 1];
        merge(Left, Right, Cur);
        return;
    }
    
    auto query(int node, int st, int en, int l, int r) { //=> O(log n)
        if (st > r || en < l) { // No overlapping and out of range
            return vector<int> (20, 0); // <== careful 
        }
        if (l <= st && en <= r) { // Complete overlapped (l-r in range)
            return t[node];
        }
        // Partial overlapping
        int mid = (st + en) >> 1;
        auto Left = query(node << 1, st, mid, l, r);
        auto Right = query(node << 1 | 1, mid + 1, en, l, r);
        auto Cur = vector<int> (20);
        merge(Left, Right, Cur);
        return Cur;
    }
} st1;

ll Pow(ll a, ll b) { // O(log b)
    ll ans = 1 % mod;
    a %= mod;
    if(a < 0) a += mod;
    while (b) {
        if (b & 1) ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<int> arr(n + 1);
    for(int i = 1; i <= n; i++) cin >> arr[i];
    st1.build(1, 1, n, arr);
    short type;
    while(q--) {
        cin >> type;
        if(type == 1) {
            int pos, x;
            cin >> pos >> x;
            st1.update(1, 1, n, pos, x);
        }
        else {
            int l, r;
            cin >> l >> r;
            auto x = st1.query(1, 1, n, l, r);
            ll ans = 0;
            for(int b = 0; b < 20; b++) {
                int oddCnt = x[b];
                if(oddCnt == 0) continue;
                int evenCnt = (r - l + 1) - oddCnt;
                ll com = Pow(2, oddCnt - 1) * Pow(2, evenCnt) % mod;
                ans += com * Pow(2, b) % mod;
                ans %= mod;
                if(ans < 0) ans += mod;
            }
            cout << ans << endl;
        }
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1183 The Sorcerer’s Subsequence Sum
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 09:33:50
Judged At
2025-04-06 09:33:50
Judged By
Score
100
Total Time
659ms
Peak Memory
47.512 MiB