#include <bits/stdc++.h> using namespace std;template<class A,class B>auto&operator<<(ostream&o,pair<A,B> x){return o<<'('<<x.first<<' '<<x.second<<')';}template<class T>auto operator<<( ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++ <<e;return o<<'}';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(x...) #endif #define MOD ((int)1e9 + 7) #define INV 333333336 struct Query { int index; char value; }; int z_odd = 0; int z_even = 0; int z_total = 0; int c_odd = 0; int c_even = 0; int c_total = 0; int n_total = 0; void process(string &input, int index, int change) { if (input[index] == 'Z') { z_total += change; if (index & 1) z_odd += change; else z_even += change; } else if (input[index] == 'C') { c_total += change; if (index & 1) c_odd += change; else c_even += change; } else { n_total += change; } } vector<int> powers_of_two; int factor(int value) { value = ((value % 6) + 6) % 6; switch (value) { case 0: return 2; case 1: return 1; case 2: return -1; case 3: return -2; case 4: return -1; case 5: return 1; } return 0x1337; } int get_result(int length, int start) { long long result = powers_of_two[length] + factor(length - (2 * start)); result *= INV; result %= MOD; result += MOD; result %= MOD; return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int size, number_of_queries; string input; cin >> size >> number_of_queries >> input; powers_of_two.resize(size + 1); powers_of_two[0] = 1; for (int index = 1; index <= size; index++) powers_of_two[index] = (powers_of_two[index - 1] << 1) % MOD; for (int index = 0; index < size; index++) process(input, index, 1); vector<Query> queries(number_of_queries + 1); queries[0].index = 0; queries[0].value = input[0]; for (int index = 1; index <= number_of_queries; index++) { cin >> queries[index].index >> queries[index].value; queries[index].index--; } for (auto &query : queries) { process(input, query.index, -1); input[query.index] = query.value; process(input, query.index, 1); int start = (((-size - c_total) % 3) + 3) % 3; int result = powers_of_two[n_total] - get_result(n_total, start); if (size > 1 && size & 1) { if (z_odd == 0 && c_even == 0) result--; if (z_even == 0 && c_odd == 0) result--; } result %= MOD; result += MOD; result %= MOD; cout << result << '\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 | #include <bits/stdc++.h> using namespace std;template<class A,class B>auto&operator<<(ostream&o,pair<A,B> x){return o<<'('<<x.first<<' '<<x.second<<')';}template<class T>auto operator<<( ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++ <<e;return o<<'}';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(x...) #endif #define MOD ((int)1e9 + 7) #define INV 333333336 struct Query { int index; char value; }; int z_odd = 0; int z_even = 0; int z_total = 0; int c_odd = 0; int c_even = 0; int c_total = 0; int n_total = 0; void process(string &input, int index, int change) { if (input[index] == 'Z') { z_total += change; if (index & 1) z_odd += change; else z_even += change; } else if (input[index] == 'C') { c_total += change; if (index & 1) c_odd += change; else c_even += change; } else { n_total += change; } } vector<int> powers_of_two; int factor(int value) { value = ((value % 6) + 6) % 6; switch (value) { case 0: return 2; case 1: return 1; case 2: return -1; case 3: return -2; case 4: return -1; case 5: return 1; } return 0x1337; } int get_result(int length, int start) { long long result = powers_of_two[length] + factor(length - (2 * start)); result *= INV; result %= MOD; result += MOD; result %= MOD; return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int size, number_of_queries; string input; cin >> size >> number_of_queries >> input; powers_of_two.resize(size + 1); powers_of_two[0] = 1; for (int index = 1; index <= size; index++) powers_of_two[index] = (powers_of_two[index - 1] << 1) % MOD; for (int index = 0; index < size; index++) process(input, index, 1); vector<Query> queries(number_of_queries + 1); queries[0].index = 0; queries[0].value = input[0]; for (int index = 1; index <= number_of_queries; index++) { cin >> queries[index].index >> queries[index].value; queries[index].index--; } for (auto &query : queries) { process(input, query.index, -1); input[query.index] = query.value; process(input, query.index, 1); int start = (((-size - c_total) % 3) + 3) % 3; int result = powers_of_two[n_total] - get_result(n_total, start); if (size > 1 && size & 1) { if (z_odd == 0 && c_even == 0) result--; if (z_even == 0 && c_odd == 0) result--; } result %= MOD; result += MOD; result %= MOD; cout << result << '\n'; } } |