#include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; // Zlozonosc: // preprocessing O(n) // zapytania O(qlogn) long long potegi[200009], newton[200009][3], n, q; long long r, g, b, diff1, diff2; const long long M = 1e9 + 7; string s; long long fastpow(long long a, long long p) { if(p == 0) return 1; if(p == 1) return a; if(p % 2 == 0) { long long b = fastpow(a, p/2); return ((b * b) % M); } long long b = fastpow(a, p-1); return ((a * b) % M); } void prep() { potegi[0] = 1; for(int i=1; i<=n; i++) potegi[i] = (2 * potegi[i-1]) % M; newton[0][0] = 1; for(int i=1; i<=n; i++) { newton[i][0] = (newton[i-1][0] + newton[i-1][2]) % M; newton[i][1] = (newton[i-1][0] + newton[i-1][1]) % M; newton[i][2] = (newton[i-1][1] + newton[i-1][2]) % M; } for(int i=0; i<s.size(); i++) { if(s[i] == 'C') r++; else if(s[i] == 'Z') g++; else b++; } for(int i=0; i<s.size(); i++) if((i % 2 == 0 && s[i] == 'C') || (i % 2 == 1 && s[i] == 'Z')) diff1++; for(int i=0; i<s.size(); i++) if((i % 2 == 0 && s[i] == 'Z') || (i % 2 == 1 && s[i] == 'C')) diff2++; return; } void solve() { long long d, wyn; d = (n - 2 * r) % 3; if(d < 0) d += 3; if(d != 0) d = 3 - d; // 2 * x = n - 2 * r (mod 3) wyn = newton[b][d]; if(n % 2 == 1) { if(diff1 == 0) wyn++; if(diff2 == 0) wyn++; wyn %= M; } wyn = (potegi[b] - wyn) % M; if(wyn < 0) wyn += M; cout<<wyn<<"\n"; return; } void upd() { int a; char c; cin>>a>>c; if(s[a-1] == 'C') { r--; if((a-1) % 2 == 0) diff1--; else diff2--; } else if(s[a-1] == 'Z') { g--; if((a-1) % 2 == 0) diff2--; else diff1--; } else b--; if(c == 'C') { r++; if((a-1) % 2 == 0) diff1++; else diff2++; } else if(c == 'Z') { g++; if((a-1) % 2 == 0) diff2++; else diff1++; } else b++; s[a-1] = c; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>q>>s; r = g = b = 0; diff1 = diff2 = 0; prep(); solve(); for(int i=1; i<=q; i++) { upd(); solve(); } return 0; }
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; // Zlozonosc: // preprocessing O(n) // zapytania O(qlogn) long long potegi[200009], newton[200009][3], n, q; long long r, g, b, diff1, diff2; const long long M = 1e9 + 7; string s; long long fastpow(long long a, long long p) { if(p == 0) return 1; if(p == 1) return a; if(p % 2 == 0) { long long b = fastpow(a, p/2); return ((b * b) % M); } long long b = fastpow(a, p-1); return ((a * b) % M); } void prep() { potegi[0] = 1; for(int i=1; i<=n; i++) potegi[i] = (2 * potegi[i-1]) % M; newton[0][0] = 1; for(int i=1; i<=n; i++) { newton[i][0] = (newton[i-1][0] + newton[i-1][2]) % M; newton[i][1] = (newton[i-1][0] + newton[i-1][1]) % M; newton[i][2] = (newton[i-1][1] + newton[i-1][2]) % M; } for(int i=0; i<s.size(); i++) { if(s[i] == 'C') r++; else if(s[i] == 'Z') g++; else b++; } for(int i=0; i<s.size(); i++) if((i % 2 == 0 && s[i] == 'C') || (i % 2 == 1 && s[i] == 'Z')) diff1++; for(int i=0; i<s.size(); i++) if((i % 2 == 0 && s[i] == 'Z') || (i % 2 == 1 && s[i] == 'C')) diff2++; return; } void solve() { long long d, wyn; d = (n - 2 * r) % 3; if(d < 0) d += 3; if(d != 0) d = 3 - d; // 2 * x = n - 2 * r (mod 3) wyn = newton[b][d]; if(n % 2 == 1) { if(diff1 == 0) wyn++; if(diff2 == 0) wyn++; wyn %= M; } wyn = (potegi[b] - wyn) % M; if(wyn < 0) wyn += M; cout<<wyn<<"\n"; return; } void upd() { int a; char c; cin>>a>>c; if(s[a-1] == 'C') { r--; if((a-1) % 2 == 0) diff1--; else diff2--; } else if(s[a-1] == 'Z') { g--; if((a-1) % 2 == 0) diff2--; else diff1--; } else b--; if(c == 'C') { r++; if((a-1) % 2 == 0) diff1++; else diff2++; } else if(c == 'Z') { g++; if((a-1) % 2 == 0) diff2++; else diff1++; } else b++; s[a-1] = c; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>q>>s; r = g = b = 0; diff1 = diff2 = 0; prep(); solve(); for(int i=1; i<=q; i++) { upd(); solve(); } return 0; } |