// Karol Kosinski 2022 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);} #endif #define DEBL DEB(1,1,void,0) #define DEBF(f,x...) DEB(1,1,f,x) #define DEBC(p,x...) DEB(0,p,printf,x) #define DEBUG(x...) DEB(0,1,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 200'005; constexpr LL MOD = 1e9 + 7; struct Monoid { enum Elem { A, B, o, a, b, c, z }; static const Elem M[7][7]; LL S[7]; void set(char ch) { FOR(i,0,7) S[i] = 0; switch (ch) { case 'C': S[a] = 1; break; case 'Z': S[b] = 1; break; case 'N': S[a] = S[b] = 1; break; } } bool is_zeroed() const { FOR(i,0,7) if ( S[i] != 0 ) return false; return true; } }; const Monoid::Elem Monoid::M[7][7] = { { B, o, A, B, o, A, A }, { o, A, B, o, A, B, B }, { A, B, o, A, B, o, o }, { B, o, A, B, c, A, a }, { o, A, B, z, A, b, B }, { A, B, o, a, B, c, o }, { A, B, o, A, b, o, z } }; void deb_write(const Monoid& x) { DEBUG("{ "); FOR(i,0,7) DEBUG("%3lld", x.S[i]); DEBUG(" }\n"); } void add(const Monoid& x, const Monoid& y, Monoid& output) { if ( x.is_zeroed() ) { output = y; return; } if ( y.is_zeroed() ) { output = x; return; } FOR(i,0,7) output.S[i] = 0; FOR(i,0,7) { const auto& xi = x.S[i]; if ( xi == 0 ) continue; FOR(j,0,7) { auto& ot = output.S[ Monoid::M[i][j] ]; ot = ( ot + ( xi * y.S[j] ) % MOD ) % MOD; } } } template <int N> struct SumTree { static constexpr int R2K(int x) { int r = 1; while ( r < x ) r <<= 1; return r; } int n; Monoid Sum[ R2K(N) << 1 ]; void init(int nt, char* t = nullptr) { int n0 = t ? nt : 0; n = R2K(nt); FOR(i,0,n0) Sum[ n + i ].set( t[i] ); FOR(i,n0,n) Sum[ n + i ].set(0); FD_( i, n - 1 , 1 ) { add( Sum[ i << 1 ], Sum[ i << 1 | 1 ], Sum[i] ); DEBF(deb_write, Sum[i]); } } void set(int x, char v) { DEBF(write_row); for ( Sum[ x += n ].set(v); x > 1; x >>= 1 ) { DEBF(deb_write, Sum[x]); DEBF(deb_write, Sum[ x ^ 1 ]); DEBUG("** %d\n", x); if ( x & 1 == 1 ) add( Sum[ x ^ 1 ], Sum[x], Sum[ x >> 1 ] ); else add( Sum[x], Sum[ x ^ 1 ], Sum[ x >> 1 ] ); } DEBF(deb_write, Sum[x]); DEBUG("** %d\n", x); } Monoid top() const { return Sum[1]; } void write_row() { FOR(i,0,n) deb_write(Sum[ n + i ]); } }; char Str[NX]; SumTree<NX> T {}; void write(int n) { auto res = T.top(); DEBF(deb_write, res); if ( n == 1 ) printf("%lld\n", ( res.S[Monoid::a] + res.S[Monoid::b] ) % MOD ); else printf("%lld\n", ( res.S[Monoid::A] + res.S[Monoid::B] ) % MOD ); DEBUG("\n\n"); } int main() { int n, q; scanf("%d%d%s", &n, &q, Str); T.init(n, Str); write(n); FOR(i,0,q) { int k; char ch[2]; scanf("%d%s", &k, ch); T.set( k - 1, ch[0] ); write(n); } 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 169 170 171 172 173 | // Karol Kosinski 2022 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define TIE(x...) int x;tie(x) #define X first #define Y second #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);} #endif #define DEBL DEB(1,1,void,0) #define DEBF(f,x...) DEB(1,1,f,x) #define DEBC(p,x...) DEB(0,p,printf,x) #define DEBUG(x...) DEB(0,1,printf,x) using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; constexpr int NX = 200'005; constexpr LL MOD = 1e9 + 7; struct Monoid { enum Elem { A, B, o, a, b, c, z }; static const Elem M[7][7]; LL S[7]; void set(char ch) { FOR(i,0,7) S[i] = 0; switch (ch) { case 'C': S[a] = 1; break; case 'Z': S[b] = 1; break; case 'N': S[a] = S[b] = 1; break; } } bool is_zeroed() const { FOR(i,0,7) if ( S[i] != 0 ) return false; return true; } }; const Monoid::Elem Monoid::M[7][7] = { { B, o, A, B, o, A, A }, { o, A, B, o, A, B, B }, { A, B, o, A, B, o, o }, { B, o, A, B, c, A, a }, { o, A, B, z, A, b, B }, { A, B, o, a, B, c, o }, { A, B, o, A, b, o, z } }; void deb_write(const Monoid& x) { DEBUG("{ "); FOR(i,0,7) DEBUG("%3lld", x.S[i]); DEBUG(" }\n"); } void add(const Monoid& x, const Monoid& y, Monoid& output) { if ( x.is_zeroed() ) { output = y; return; } if ( y.is_zeroed() ) { output = x; return; } FOR(i,0,7) output.S[i] = 0; FOR(i,0,7) { const auto& xi = x.S[i]; if ( xi == 0 ) continue; FOR(j,0,7) { auto& ot = output.S[ Monoid::M[i][j] ]; ot = ( ot + ( xi * y.S[j] ) % MOD ) % MOD; } } } template <int N> struct SumTree { static constexpr int R2K(int x) { int r = 1; while ( r < x ) r <<= 1; return r; } int n; Monoid Sum[ R2K(N) << 1 ]; void init(int nt, char* t = nullptr) { int n0 = t ? nt : 0; n = R2K(nt); FOR(i,0,n0) Sum[ n + i ].set( t[i] ); FOR(i,n0,n) Sum[ n + i ].set(0); FD_( i, n - 1 , 1 ) { add( Sum[ i << 1 ], Sum[ i << 1 | 1 ], Sum[i] ); DEBF(deb_write, Sum[i]); } } void set(int x, char v) { DEBF(write_row); for ( Sum[ x += n ].set(v); x > 1; x >>= 1 ) { DEBF(deb_write, Sum[x]); DEBF(deb_write, Sum[ x ^ 1 ]); DEBUG("** %d\n", x); if ( x & 1 == 1 ) add( Sum[ x ^ 1 ], Sum[x], Sum[ x >> 1 ] ); else add( Sum[x], Sum[ x ^ 1 ], Sum[ x >> 1 ] ); } DEBF(deb_write, Sum[x]); DEBUG("** %d\n", x); } Monoid top() const { return Sum[1]; } void write_row() { FOR(i,0,n) deb_write(Sum[ n + i ]); } }; char Str[NX]; SumTree<NX> T {}; void write(int n) { auto res = T.top(); DEBF(deb_write, res); if ( n == 1 ) printf("%lld\n", ( res.S[Monoid::a] + res.S[Monoid::b] ) % MOD ); else printf("%lld\n", ( res.S[Monoid::A] + res.S[Monoid::B] ) % MOD ); DEBUG("\n\n"); } int main() { int n, q; scanf("%d%d%s", &n, &q, Str); T.init(n, Str); write(n); FOR(i,0,q) { int k; char ch[2]; scanf("%d%s", &k, ch); T.set( k - 1, ch[0] ); write(n); } return 0; } |