#include <iostream> #include <string> #include <algorithm> #include <vector> #define LL long long using namespace std; constexpr LL MOD = 1e9 + 7; class SegmentTree { int n; vector<vector<LL>> data; public: SegmentTree(int n, int l, string &s) : n(n), data(vector<vector<LL>>(3, vector<LL>(n << 1))) { for (int i = 0; i < l; ++i) { if (s[i] == 'N') { data[0][n + i] = 1; data[1][n + i] = 1; } else if (s[i] == 'C') data[1][n + i] = 1; else data[0][n + i] = 1; } for (int v = n + l; v < (n << 1); ++v) { data[0][v] = 1; } for (int v = n - 1; v; --v) { data[0][v] = (data[0][v << 1] * data[0][v << 1 | 1] + data[1][v << 1] * data[2][v << 1 | 1] + data[2][v << 1] * data[1][v << 1 | 1]) % MOD; data[1][v] = (data[0][v << 1] * data[1][v << 1 | 1] + data[1][v << 1] * data[0][v << 1 | 1] + data[2][v << 1] * data[2][v << 1 | 1]) % MOD; data[2][v] = (data[2][v << 1] * data[0][v << 1 | 1] + data[0][v << 1] * data[2][v << 1 | 1] + data[1][v << 1] * data[1][v << 1 | 1]) % MOD; } } void update(int v, char c) { v += n; if (c == 'N') { data[0][v] = 1; data[1][v] = 1; } else if (c == 'C') { data[0][v] = 0; data[1][v] = 1; } else { data[0][v] = 1; data[1][v] = 0; } v >>= 1; while (v) { data[0][v] = (data[0][v << 1] * data[0][v << 1 | 1] + data[1][v << 1] * data[2][v << 1 | 1] + data[2][v << 1] * data[1][v << 1 | 1]) % MOD; data[1][v] = (data[0][v << 1] * data[1][v << 1 | 1] + data[1][v << 1] * data[0][v << 1 | 1] + data[2][v << 1] * data[2][v << 1 | 1]) % MOD; data[2][v] = (data[2][v << 1] * data[0][v << 1 | 1] + data[0][v << 1] * data[2][v << 1 | 1] + data[1][v << 1] * data[1][v << 1 | 1]) % MOD; v >>= 1; } } LL query(int r) { LL res = 0; for (int i = 0; i < 3; ++i) { if ((i + r) % 3) res += data[i][1]; res %= MOD; } return res; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; string s; cin >> s; SegmentTree T(1 << 18, n, s); vector<int> cnt(4); for (int i = 0; i < n; ++i) { if (s[i] != 'N') ++cnt[2 * (s[i] == 'C') + (i % 2)]; } LL res = T.query(n); if (!cnt[0] && !cnt[3]) --res; if (!cnt[1] && !cnt[2]) --res; if (res < 0) res += MOD; cout << res << "\n"; int pos; char c; while (q--) { cin >> pos >> c; --pos; if (s[pos] != 'N') --cnt[2 * (s[pos] == 'C') + pos % 2]; if (c != 'N') ++cnt[2 * (c == 'C') + pos % 2]; s[pos] = c; T.update(pos, c); res = T.query(n); if (!cnt[0] && !cnt[3]) --res; if (!cnt[1] && !cnt[2]) --res; if (res < 0) res += MOD; cout << res << "\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 | #include <iostream> #include <string> #include <algorithm> #include <vector> #define LL long long using namespace std; constexpr LL MOD = 1e9 + 7; class SegmentTree { int n; vector<vector<LL>> data; public: SegmentTree(int n, int l, string &s) : n(n), data(vector<vector<LL>>(3, vector<LL>(n << 1))) { for (int i = 0; i < l; ++i) { if (s[i] == 'N') { data[0][n + i] = 1; data[1][n + i] = 1; } else if (s[i] == 'C') data[1][n + i] = 1; else data[0][n + i] = 1; } for (int v = n + l; v < (n << 1); ++v) { data[0][v] = 1; } for (int v = n - 1; v; --v) { data[0][v] = (data[0][v << 1] * data[0][v << 1 | 1] + data[1][v << 1] * data[2][v << 1 | 1] + data[2][v << 1] * data[1][v << 1 | 1]) % MOD; data[1][v] = (data[0][v << 1] * data[1][v << 1 | 1] + data[1][v << 1] * data[0][v << 1 | 1] + data[2][v << 1] * data[2][v << 1 | 1]) % MOD; data[2][v] = (data[2][v << 1] * data[0][v << 1 | 1] + data[0][v << 1] * data[2][v << 1 | 1] + data[1][v << 1] * data[1][v << 1 | 1]) % MOD; } } void update(int v, char c) { v += n; if (c == 'N') { data[0][v] = 1; data[1][v] = 1; } else if (c == 'C') { data[0][v] = 0; data[1][v] = 1; } else { data[0][v] = 1; data[1][v] = 0; } v >>= 1; while (v) { data[0][v] = (data[0][v << 1] * data[0][v << 1 | 1] + data[1][v << 1] * data[2][v << 1 | 1] + data[2][v << 1] * data[1][v << 1 | 1]) % MOD; data[1][v] = (data[0][v << 1] * data[1][v << 1 | 1] + data[1][v << 1] * data[0][v << 1 | 1] + data[2][v << 1] * data[2][v << 1 | 1]) % MOD; data[2][v] = (data[2][v << 1] * data[0][v << 1 | 1] + data[0][v << 1] * data[2][v << 1 | 1] + data[1][v << 1] * data[1][v << 1 | 1]) % MOD; v >>= 1; } } LL query(int r) { LL res = 0; for (int i = 0; i < 3; ++i) { if ((i + r) % 3) res += data[i][1]; res %= MOD; } return res; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; string s; cin >> s; SegmentTree T(1 << 18, n, s); vector<int> cnt(4); for (int i = 0; i < n; ++i) { if (s[i] != 'N') ++cnt[2 * (s[i] == 'C') + (i % 2)]; } LL res = T.query(n); if (!cnt[0] && !cnt[3]) --res; if (!cnt[1] && !cnt[2]) --res; if (res < 0) res += MOD; cout << res << "\n"; int pos; char c; while (q--) { cin >> pos >> c; --pos; if (s[pos] != 'N') --cnt[2 * (s[pos] == 'C') + pos % 2]; if (c != 'N') ++cnt[2 * (c == 'C') + pos % 2]; s[pos] = c; T.update(pos, c); res = T.query(n); if (!cnt[0] && !cnt[3]) --res; if (!cnt[1] && !cnt[2]) --res; if (res < 0) res += MOD; cout << res << "\n"; } } |