#include <bits/stdc++.h> using namespace std; typedef long long lld; constexpr lld MOD = 1000000007; constexpr int N = 1 << 18; lld add(lld a, lld b) { lld z = a + b; if (z >= MOD) z -= MOD; return z; } typedef struct T { // 0, 1, 2, 3, 4, 5, 6 // CC, ZZ, ZZZ, C-C, C-Z, Z-C, Z-Z lld p[8] = {0, 0, 0, 0, 0, 0, 0, 0}; constexpr T() = default; constexpr T(int a) { p[7] = a; } constexpr T(char a, char b) { p[7] = 1; if (a == 'C' && b == 'C') p[0] = 1; else if (a == 'C' && b == 'Z') p[4] = 1; else if (a == 'C' && b == 'N') p[0] = 1, p[4] = 1; else if (a == 'Z' && b == 'C') p[5] = 1; else if (a == 'Z' && b == 'Z') p[1] = 1; else if (a == 'Z' && b == 'N') p[1] = 1, p[5] = 1; else if (a == 'N' && b == 'C') p[0] = 1, p[5] = 1; else if (a == 'N' && b == 'Z') p[1] = 1, p[4] = 1; else if (a == 'N' && b == 'N') p[0] = 1, p[5] = 1, p[1] = 1, p[4] = 1; } } T; // 0, 1, 2, 3, 4, 5, 6, 7, 8 // CC, ZZ, ZZZ, C-C, C-Z, Z-C, Z-Z, C, Z constexpr int r[7][9] = { {1, 2, 0, 2, 0, 0, 1, 2, 1}, {2, 0, 1, 0, 1, 1, 2, 0, 2}, {0, 1, 2, 1, 2, 2, 0, 1, 0}, {2, 0, 1, 0, 1, 3, 4, 0, 4}, {0, 1, 2, 3, 4, 3, 0, 3, 0}, {0, 1, 2, 1, 6, 5, 6, 1, 6}, {1, 2, 0, 5, 6, 0, 1, 5, 1} }; T merge(const T &a, const T &b) { if (b.p[7] == 0) return a; T res(1); for (int i = 0; i < 7; ++i) { for (int j = 0; j < 7; ++j) { res.p[r[i][j]] = (res.p[r[i][j]] + a.p[i] * b.p[j]) % MOD; } } return res; } T merge(const T &a, char b) { T res(1); if (b == 'C' || b == 'N') { for (int i = 0; i < 7; ++i) { res.p[r[i][7]] = (res.p[r[i][7]] + a.p[i]) % MOD; } } if (b == 'Z' || b == 'N') { for (int i = 0; i < 7; ++i) { res.p[r[i][8]] = (res.p[r[i][8]] + a.p[i]) % MOD; } } return res; } T t[N]; char s[N]; int pp[N]; int n, q; void upd(int a, char b) { // printf("%d %c: ", a, b); --a; s[a] = b; if (((n & 1) == 1) && (a >= n - 3)) { a = n - 3; t[pp[a]] = merge(T(s[a], s[a + 1]), s[a + 2]); } else { a = a - (a & 1); t[pp[a]] = T(s[a], s[a + 1]); } a = pp[a] >> 1; while (a > 0) { t[a] = merge(t[a << 1], t[(a << 1) + 1]); a >>= 1; } } lld get() { return (t[1].p[0] + t[1].p[1]) % MOD; } int main() { scanf("%d%d", &n, &q); scanf("%s", s); for (int i = 0; i < n; ++i) { pp[i] = (i >> 1) + (N >> 1); if (i & 1) t[pp[i]] = T(s[i - 1], s[i]); } if (n & 1) { pp[n - 1] = ((n - 3) >> 1) + (N >> 1); t[pp[n - 1]] = merge(t[pp[n - 1]], s[n - 1]); } for (int i = (N >> 1) - 1; i > 0; --i) { t[i] = merge(t[i << 1], t[(i << 1) + 1]); } printf("%lld\n", get()); while (q-- > 0) { int a; char b; scanf("%d %c", &a, &b); upd(a, b); printf("%lld\n", get()); } /* for (int i = 0; i < n; ++i) { printf("%d ", pp[i]); } puts(""); while (true) { scanf("%d", &q); for (int i = 0; i < 8; ++i) { printf("%lld ", t[q].p[i]); } puts(""); } */ 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 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 | #include <bits/stdc++.h> using namespace std; typedef long long lld; constexpr lld MOD = 1000000007; constexpr int N = 1 << 18; lld add(lld a, lld b) { lld z = a + b; if (z >= MOD) z -= MOD; return z; } typedef struct T { // 0, 1, 2, 3, 4, 5, 6 // CC, ZZ, ZZZ, C-C, C-Z, Z-C, Z-Z lld p[8] = {0, 0, 0, 0, 0, 0, 0, 0}; constexpr T() = default; constexpr T(int a) { p[7] = a; } constexpr T(char a, char b) { p[7] = 1; if (a == 'C' && b == 'C') p[0] = 1; else if (a == 'C' && b == 'Z') p[4] = 1; else if (a == 'C' && b == 'N') p[0] = 1, p[4] = 1; else if (a == 'Z' && b == 'C') p[5] = 1; else if (a == 'Z' && b == 'Z') p[1] = 1; else if (a == 'Z' && b == 'N') p[1] = 1, p[5] = 1; else if (a == 'N' && b == 'C') p[0] = 1, p[5] = 1; else if (a == 'N' && b == 'Z') p[1] = 1, p[4] = 1; else if (a == 'N' && b == 'N') p[0] = 1, p[5] = 1, p[1] = 1, p[4] = 1; } } T; // 0, 1, 2, 3, 4, 5, 6, 7, 8 // CC, ZZ, ZZZ, C-C, C-Z, Z-C, Z-Z, C, Z constexpr int r[7][9] = { {1, 2, 0, 2, 0, 0, 1, 2, 1}, {2, 0, 1, 0, 1, 1, 2, 0, 2}, {0, 1, 2, 1, 2, 2, 0, 1, 0}, {2, 0, 1, 0, 1, 3, 4, 0, 4}, {0, 1, 2, 3, 4, 3, 0, 3, 0}, {0, 1, 2, 1, 6, 5, 6, 1, 6}, {1, 2, 0, 5, 6, 0, 1, 5, 1} }; T merge(const T &a, const T &b) { if (b.p[7] == 0) return a; T res(1); for (int i = 0; i < 7; ++i) { for (int j = 0; j < 7; ++j) { res.p[r[i][j]] = (res.p[r[i][j]] + a.p[i] * b.p[j]) % MOD; } } return res; } T merge(const T &a, char b) { T res(1); if (b == 'C' || b == 'N') { for (int i = 0; i < 7; ++i) { res.p[r[i][7]] = (res.p[r[i][7]] + a.p[i]) % MOD; } } if (b == 'Z' || b == 'N') { for (int i = 0; i < 7; ++i) { res.p[r[i][8]] = (res.p[r[i][8]] + a.p[i]) % MOD; } } return res; } T t[N]; char s[N]; int pp[N]; int n, q; void upd(int a, char b) { // printf("%d %c: ", a, b); --a; s[a] = b; if (((n & 1) == 1) && (a >= n - 3)) { a = n - 3; t[pp[a]] = merge(T(s[a], s[a + 1]), s[a + 2]); } else { a = a - (a & 1); t[pp[a]] = T(s[a], s[a + 1]); } a = pp[a] >> 1; while (a > 0) { t[a] = merge(t[a << 1], t[(a << 1) + 1]); a >>= 1; } } lld get() { return (t[1].p[0] + t[1].p[1]) % MOD; } int main() { scanf("%d%d", &n, &q); scanf("%s", s); for (int i = 0; i < n; ++i) { pp[i] = (i >> 1) + (N >> 1); if (i & 1) t[pp[i]] = T(s[i - 1], s[i]); } if (n & 1) { pp[n - 1] = ((n - 3) >> 1) + (N >> 1); t[pp[n - 1]] = merge(t[pp[n - 1]], s[n - 1]); } for (int i = (N >> 1) - 1; i > 0; --i) { t[i] = merge(t[i << 1], t[(i << 1) + 1]); } printf("%lld\n", get()); while (q-- > 0) { int a; char b; scanf("%d %c", &a, &b); upd(a, b); printf("%lld\n", get()); } /* for (int i = 0; i < n; ++i) { printf("%d ", pp[i]); } puts(""); while (true) { scanf("%d", &q); for (int i = 0; i < 8; ++i) { printf("%lld ", t[q].p[i]); } puts(""); } */ return 0; } |