1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #include<bits/stdc++.h>
using namespace std; typedef long long ll; const int N = 2e5 + 10, inf = 0x3f3f3f3f;
string s;
int a_add, a_sub, cnt;
struct Seg{ int add[N << 2], sub[N << 2], sum[N << 2];
void pu(int u){ add[u] = add[u << 1 | 1]; sub[u] = sub[u << 1 | 1]; sum[u] = sum[u << 1 | 1] + sum[u << 1]; if(sum[u << 1 | 1] & 1){ add[u] += sub[u << 1]; sub[u] += add[u << 1]; }else{ add[u] += add[u << 1]; sub[u] += sub[u << 1]; } }
void build(int u, int l, int r){ if(l == r){ if(s[l - 1] == 'A') sum[u] = 1; else add[u] = 1; sub[u] = 0; return; } int mid = (l + r) >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); pu(u); }
void qry(int u, int l, int r, int ql, int qr){ if(r < ql || l > qr) return; if(ql <= l && r <= qr){ cnt += sum[u]; if(sum[u] & 1){ swap(a_add, a_sub); } a_add += add[u]; a_sub += sub[u]; return; } int mid = (l + r) >> 1; qry(u << 1, l, mid, ql, qr); qry(u << 1 | 1, mid + 1, r, ql, qr); } }seg;
void kaibai() { int n, q; cin >> n >> q; cin >> s; seg.build(1, 1, n); ll ans = 0; while(q--){ int l, r, len; string t; cin >> l >> r >> t; l = (ans ^ l) % n + 1; r = (ans ^ r) % n + 1; len = t.size(); if(l > r) swap(l, r); ll x = 0, mx = (1ll << ((int)t.size())); for(char i : t){ x = x * 2 + i - '0'; } a_add = a_sub = cnt = 0; seg.qry(1, 1, n, l, r); if(cnt & 1) x = (mx - 1) - x; x += a_add; x -= a_sub; x %= mx; if(x < 0) x += mx; ans = x; t = ""; while(x){ t += '0' + (x % 2); x >>= 1; } while(t.size() < len) t += '0'; reverse(t.begin(), t.end()); cout << t << '\n'; } }
int main(void) { int T = 1; ios::sync_with_stdio(0); while (T--) { kaibai(); } }
|