#include <bits/stdc++.h> using namespace std; using lint = long long; using llf = long double; using pi = array<int, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() const int MAXT = 530005; const int mod = 1e9 + 7; struct mint { int val; mint() { val = 0; } mint(const lint &v) { val = (-mod <= v && v < mod) ? v : v % mod; if (val < 0) val += mod; } friend ostream &operator<<(ostream &os, const mint &a) { return os << a.val; } friend bool operator==(const mint &a, const mint &b) { return a.val == b.val; } friend bool operator!=(const mint &a, const mint &b) { return !(a == b); } friend bool operator<(const mint &a, const mint &b) { return a.val < b.val; } mint operator-() const { return mint(-val); } mint &operator+=(const mint &m) { if ((val += m.val) >= mod) val -= mod; return *this; } mint &operator-=(const mint &m) { if ((val -= m.val) < 0) val += mod; return *this; } mint &operator*=(const mint &m) { val = (lint)val * m.val % mod; return *this; } friend mint ipow(mint a, lint p) { mint ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend mint inv(const mint &a) { assert(a.val); return ipow(a, mod - 2); } mint &operator/=(const mint &m) { return (*this) *= inv(m); } friend mint operator+(mint a, const mint &b) { return a += b; } friend mint operator-(mint a, const mint &b) { return a -= b; } friend mint operator*(mint a, const mint &b) { return a *= b; } friend mint operator/(mint a, const mint &b) { return a /= b; } operator int64_t() const { return val; } }; struct seg { struct node { mint A[3][3]; node() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { A[i][j] = (i == j); } } } node(char c) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { A[i][j] = 0; } } if (c != 'C') { for (int i = 0; i < 3; i++) { A[i][(i + 1) % 3] += mint(1); } } if (c != 'Z') { for (int i = 0; i < 3; i++) { A[i][(i + 2) % 3] += mint(1); } } } node operator*(const node &nd) const { node ret; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ret.A[i][j] = 0; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { ret.A[j][k] += A[j][i] * nd.A[i][k]; } } } return ret; } } tree[MAXT]; int lim; void init(string &s) { for (lim = 1; lim <= sz(s); lim <<= 1) ; for (int i = 0; i < sz(s); i++) { tree[i + lim] = node(s[i]); } for (int i = lim - 1; i; i--) { tree[i] = tree[2 * i] * tree[2 * i + 1]; } } void update(int x, char c) { x += lim; tree[x] = node(c); while (x > 1) { x >>= 1; tree[x] = tree[2 * x] * tree[2 * x + 1]; } } } seg; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(69); int n, q; cin >> n >> q; string s; cin >> s; seg.init(s); int oddGap = 0, evenGap = 0; for (int x = 0; x < n; x++) { if (s[x] == 'N') continue; if (x % 2 != s[x] % 2) oddGap++; else evenGap++; } auto query = [&]() { mint ans = seg.tree[1].A[0][1] + seg.tree[1].A[0][2]; if (n % 2 == 1) { if (oddGap == 0) ans -= mint(1); if (evenGap == 0) ans -= mint(1); } return ans; }; cout << query() << "\n"; while (q--) { int x; char c; cin >> x >> c; x--; if (s[x] != 'N') { if (x % 2 != s[x] % 2) oddGap--; else evenGap--; } s[x] = c; seg.update(x, c); if (s[x] != 'N') { if (x % 2 != s[x] % 2) oddGap++; else evenGap++; } cout << query() << "\n"; } }
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | #include <bits/stdc++.h> using namespace std; using lint = long long; using llf = long double; using pi = array<int, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() const int MAXT = 530005; const int mod = 1e9 + 7; struct mint { int val; mint() { val = 0; } mint(const lint &v) { val = (-mod <= v && v < mod) ? v : v % mod; if (val < 0) val += mod; } friend ostream &operator<<(ostream &os, const mint &a) { return os << a.val; } friend bool operator==(const mint &a, const mint &b) { return a.val == b.val; } friend bool operator!=(const mint &a, const mint &b) { return !(a == b); } friend bool operator<(const mint &a, const mint &b) { return a.val < b.val; } mint operator-() const { return mint(-val); } mint &operator+=(const mint &m) { if ((val += m.val) >= mod) val -= mod; return *this; } mint &operator-=(const mint &m) { if ((val -= m.val) < 0) val += mod; return *this; } mint &operator*=(const mint &m) { val = (lint)val * m.val % mod; return *this; } friend mint ipow(mint a, lint p) { mint ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend mint inv(const mint &a) { assert(a.val); return ipow(a, mod - 2); } mint &operator/=(const mint &m) { return (*this) *= inv(m); } friend mint operator+(mint a, const mint &b) { return a += b; } friend mint operator-(mint a, const mint &b) { return a -= b; } friend mint operator*(mint a, const mint &b) { return a *= b; } friend mint operator/(mint a, const mint &b) { return a /= b; } operator int64_t() const { return val; } }; struct seg { struct node { mint A[3][3]; node() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { A[i][j] = (i == j); } } } node(char c) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { A[i][j] = 0; } } if (c != 'C') { for (int i = 0; i < 3; i++) { A[i][(i + 1) % 3] += mint(1); } } if (c != 'Z') { for (int i = 0; i < 3; i++) { A[i][(i + 2) % 3] += mint(1); } } } node operator*(const node &nd) const { node ret; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ret.A[i][j] = 0; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { ret.A[j][k] += A[j][i] * nd.A[i][k]; } } } return ret; } } tree[MAXT]; int lim; void init(string &s) { for (lim = 1; lim <= sz(s); lim <<= 1) ; for (int i = 0; i < sz(s); i++) { tree[i + lim] = node(s[i]); } for (int i = lim - 1; i; i--) { tree[i] = tree[2 * i] * tree[2 * i + 1]; } } void update(int x, char c) { x += lim; tree[x] = node(c); while (x > 1) { x >>= 1; tree[x] = tree[2 * x] * tree[2 * x + 1]; } } } seg; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(69); int n, q; cin >> n >> q; string s; cin >> s; seg.init(s); int oddGap = 0, evenGap = 0; for (int x = 0; x < n; x++) { if (s[x] == 'N') continue; if (x % 2 != s[x] % 2) oddGap++; else evenGap++; } auto query = [&]() { mint ans = seg.tree[1].A[0][1] + seg.tree[1].A[0][2]; if (n % 2 == 1) { if (oddGap == 0) ans -= mint(1); if (evenGap == 0) ans -= mint(1); } return ans; }; cout << query() << "\n"; while (q--) { int x; char c; cin >> x >> c; x--; if (s[x] != 'N') { if (x % 2 != s[x] % 2) oddGap--; else evenGap--; } s[x] = c; seg.update(x, c); if (s[x] != 'N') { if (x % 2 != s[x] % 2) oddGap++; else evenGap++; } cout << query() << "\n"; } } |