//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; int mod = M7; int pow2[200'001]; int tab[200'001][3]; int calcbadchainred(char c, int i) { if(i%2==0) { if(c == 'Z') return 1; } else { if(c == 'C') return 1; } return 0; } int calcbadchaingreen(char c, int i) { if(i%2==0) { if(c == 'C') return 1; } else { if(c == 'Z') return 1; } return 0; } void calcpow2() { pow2[0] = 1; REP(i, 1, 200'000){ pow2[i]=pow2[i-1]*2; if(pow2[i]>mod)pow2[i]-=mod; } } void calctab(int n) { tab[0][0] = 1; tab[0][1] = 0; tab[0][2] = 0; REP(i, 1, n) { tab[i][0] = tab[i-1][1] + tab[i-1][2]; if(tab[i][0]>mod)tab[i][0]-=mod; tab[i][1] = tab[i-1][0] + tab[i-1][2]; if(tab[i][1]>mod)tab[i][1]-=mod; tab[i][2] = tab[i-1][0] + tab[i-1][1]; if(tab[i][2]>mod)tab[i][2]-=mod; } } int main(){ _upgrade mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(15); int n,q;cin>>n>>q; string s;cin>>s; calcpow2(); calctab(n); int numb = 0; int numrv = 0; int badchainreddiff = 0; int badchaingreendiff = 0; REP(i,0,n-1) { if(s[i] == 'N')++numb; if(s[i] == 'C')++numrv; if(s[i] == 'Z')numrv+=2; badchainreddiff += calcbadchainred(s[i], i); badchaingreendiff += calcbadchaingreen(s[i], i); } // auto calcresult = [&] () { // int allpos = pow2[numb]; // int badrvpos = tab[numb][numrv%3]; // int result = allpos - badrvpos; // if (result<0)result+=mod; // if(n%2==1 && n > 1) { // if(badchaingreendiff == 0)--result; // if(badchainreddiff == 0)--result; // if(result < 0)result+=mod; // } // return result; // }; int allpos = pow2[numb]; int badrvpos = tab[numb][numrv%3]; int result = allpos - badrvpos; if (result<0)result+=mod; if(n%2==1 && n > 1) { if(badchaingreendiff == 0)--result; if(badchainreddiff == 0)--result; if(result < 0)result+=mod; } cout << result << '\n'; REP(i,0,q-1) { int k;char c; cin>>k>>c; --k; badchaingreendiff += calcbadchaingreen(c, k); badchaingreendiff -= calcbadchaingreen(s[k], k); badchainreddiff += calcbadchainred(c, k); badchainreddiff -= calcbadchainred(s[k], k); if(c == 'N')++numb; if(c == 'C')++numrv; if(c == 'Z')numrv+=2; if(s[k] == 'N')--numb; if(s[k] == 'C')--numrv; if(s[k] == 'Z')numrv-=2; s[k] = c; allpos = pow2[numb]; badrvpos = tab[numb][numrv%3]; result = allpos - badrvpos; if (result<0)result+=mod; if(n%2==1 && n > 1) { if(badchaingreendiff == 0)--result; if(badchainreddiff == 0)--result; if(result < 0)result+=mod; } cout << result << '\n'; } return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). */
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | //~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; int mod = M7; int pow2[200'001]; int tab[200'001][3]; int calcbadchainred(char c, int i) { if(i%2==0) { if(c == 'Z') return 1; } else { if(c == 'C') return 1; } return 0; } int calcbadchaingreen(char c, int i) { if(i%2==0) { if(c == 'C') return 1; } else { if(c == 'Z') return 1; } return 0; } void calcpow2() { pow2[0] = 1; REP(i, 1, 200'000){ pow2[i]=pow2[i-1]*2; if(pow2[i]>mod)pow2[i]-=mod; } } void calctab(int n) { tab[0][0] = 1; tab[0][1] = 0; tab[0][2] = 0; REP(i, 1, n) { tab[i][0] = tab[i-1][1] + tab[i-1][2]; if(tab[i][0]>mod)tab[i][0]-=mod; tab[i][1] = tab[i-1][0] + tab[i-1][2]; if(tab[i][1]>mod)tab[i][1]-=mod; tab[i][2] = tab[i-1][0] + tab[i-1][1]; if(tab[i][2]>mod)tab[i][2]-=mod; } } int main(){ _upgrade mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(15); int n,q;cin>>n>>q; string s;cin>>s; calcpow2(); calctab(n); int numb = 0; int numrv = 0; int badchainreddiff = 0; int badchaingreendiff = 0; REP(i,0,n-1) { if(s[i] == 'N')++numb; if(s[i] == 'C')++numrv; if(s[i] == 'Z')numrv+=2; badchainreddiff += calcbadchainred(s[i], i); badchaingreendiff += calcbadchaingreen(s[i], i); } // auto calcresult = [&] () { // int allpos = pow2[numb]; // int badrvpos = tab[numb][numrv%3]; // int result = allpos - badrvpos; // if (result<0)result+=mod; // if(n%2==1 && n > 1) { // if(badchaingreendiff == 0)--result; // if(badchainreddiff == 0)--result; // if(result < 0)result+=mod; // } // return result; // }; int allpos = pow2[numb]; int badrvpos = tab[numb][numrv%3]; int result = allpos - badrvpos; if (result<0)result+=mod; if(n%2==1 && n > 1) { if(badchaingreendiff == 0)--result; if(badchainreddiff == 0)--result; if(result < 0)result+=mod; } cout << result << '\n'; REP(i,0,q-1) { int k;char c; cin>>k>>c; --k; badchaingreendiff += calcbadchaingreen(c, k); badchaingreendiff -= calcbadchaingreen(s[k], k); badchainreddiff += calcbadchainred(c, k); badchainreddiff -= calcbadchainred(s[k], k); if(c == 'N')++numb; if(c == 'C')++numrv; if(c == 'Z')numrv+=2; if(s[k] == 'N')--numb; if(s[k] == 'C')--numrv; if(s[k] == 'Z')numrv-=2; s[k] = c; allpos = pow2[numb]; badrvpos = tab[numb][numrv%3]; result = allpos - badrvpos; if (result<0)result+=mod; if(n%2==1 && n > 1) { if(badchaingreendiff == 0)--result; if(badchainreddiff == 0)--result; if(result < 0)result+=mod; } cout << result << '\n'; } return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). */ |