#include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.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 debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif constexpr int mod = int(1e9) + 7; int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } void add_to(int &a, int b) { a = add(a, b); } int sub(int a, int b) { return add(a, mod - b); } enum Color { Czer = 0, Ziel = 1, Nieb = 2 }; istream& operator>>(istream &i, Color &col) { char c; i >> c; if(c == 'C') col = Czer; else if(c == 'Z') col = Ziel; else if(c == 'N') col = Nieb; else assert(false); return i; } ostream& operator<<(ostream &o, Color c) { return o << int(c); } int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; vector<Color> in(n); REP(i, n) cin >> in[i]; int cnt_wrong0 = 0, cnt_wrong1 = 0; int cnt_blue = 0; int sum_delta = 0; auto add = [&](int i, Color c, int delta) { if(c == Czer or c == Ziel) { (i % 2 == int(c) ? cnt_wrong0 : cnt_wrong1) += delta; sum_delta += (c == Czer ? 1 : 2) * delta; } if(c == Nieb) cnt_blue += delta; }; REP(i, n) add(i, in[i], +1); debug(in, cnt_wrong0, cnt_wrong1, cnt_blue, sum_delta); vector<array<int, 3>> cnt_ways_blue(n + 1, array<int, 3>{{0, 0, 0}}); cnt_ways_blue[0][0] = 1; REP(i, n) REP(rest, 3) for(int blue : {1, 2}) add_to(cnt_ways_blue[i + 1][(rest + blue) % 3], cnt_ways_blue[i][rest]); debug(cnt_ways_blue); auto get_ans = [&] { if(n == 1) return in[0] == Nieb ? 2 : 1; int ret = 0; for(int cnt_wrong : {cnt_wrong0, cnt_wrong1}) if(n % 2 == 1 and cnt_wrong == n - cnt_blue) ret = sub(ret, 1); REP(rest_blue, 3) if((sum_delta + rest_blue) % 3 != 0) add_to(ret, cnt_ways_blue[cnt_blue][rest_blue]); return ret; }; cout << get_ans() << '\n'; while(q --> 0) { int i; Color col; cin >> i >> col; --i; add(i, in[i], -1); add(i, in[i] = col, +1); cout << get_ans() << '\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 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.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 debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif constexpr int mod = int(1e9) + 7; int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } void add_to(int &a, int b) { a = add(a, b); } int sub(int a, int b) { return add(a, mod - b); } enum Color { Czer = 0, Ziel = 1, Nieb = 2 }; istream& operator>>(istream &i, Color &col) { char c; i >> c; if(c == 'C') col = Czer; else if(c == 'Z') col = Ziel; else if(c == 'N') col = Nieb; else assert(false); return i; } ostream& operator<<(ostream &o, Color c) { return o << int(c); } int main() { cin.tie(0)->sync_with_stdio(0); int n, q; cin >> n >> q; vector<Color> in(n); REP(i, n) cin >> in[i]; int cnt_wrong0 = 0, cnt_wrong1 = 0; int cnt_blue = 0; int sum_delta = 0; auto add = [&](int i, Color c, int delta) { if(c == Czer or c == Ziel) { (i % 2 == int(c) ? cnt_wrong0 : cnt_wrong1) += delta; sum_delta += (c == Czer ? 1 : 2) * delta; } if(c == Nieb) cnt_blue += delta; }; REP(i, n) add(i, in[i], +1); debug(in, cnt_wrong0, cnt_wrong1, cnt_blue, sum_delta); vector<array<int, 3>> cnt_ways_blue(n + 1, array<int, 3>{{0, 0, 0}}); cnt_ways_blue[0][0] = 1; REP(i, n) REP(rest, 3) for(int blue : {1, 2}) add_to(cnt_ways_blue[i + 1][(rest + blue) % 3], cnt_ways_blue[i][rest]); debug(cnt_ways_blue); auto get_ans = [&] { if(n == 1) return in[0] == Nieb ? 2 : 1; int ret = 0; for(int cnt_wrong : {cnt_wrong0, cnt_wrong1}) if(n % 2 == 1 and cnt_wrong == n - cnt_blue) ret = sub(ret, 1); REP(rest_blue, 3) if((sum_delta + rest_blue) % 3 != 0) add_to(ret, cnt_ways_blue[cnt_blue][rest_blue]); return ret; }; cout << get_ans() << '\n'; while(q --> 0) { int i; Color col; cin >> i >> col; --i; add(i, in[i], -1); add(i, in[i] = col, +1); cout << get_ans() << '\n'; } } |