//Jakub Trela #include <bits/stdc++.h> using namespace std; typedef long long LL; int M = 1<<18; int T=1; int MOD = 1e9+7; vector<int> treeL(2*M+1,-1); vector<int> lazyL(2*M+1); vector<int> treeR(2*M+1,-1); vector<int> lazyR(2*M+1); void updateL(int a, int b, int val){ if(a==0)return; a+=M; b+=M; while(a<=b){ if(a%2==1){ treeL[a]=val; lazyL[a]=T; a++; } if(b%2==0){ treeL[b]=val; lazyL[b]=T; b--; } a/=2; b/=2; } T++; } int queryL(int a){ a+=M; int maxi=-1,res=0; while(a>=1){ if(lazyL[a]>maxi){ maxi=lazyL[a]; res=treeL[a]; } a/=2; } return res; } void updateR(int a, int b, int val){ if(a==0)return; a+=M; b+=M; while(a<=b){ if(a%2==1){ treeR[a]=val; lazyR[a]=T; a++; } if(b%2==0){ treeR[b]=val; lazyR[b]=T; b--; } a/=2; b/=2; } T++; } int queryR(int a){ a+=M; int maxi=-1,res=0; while(a>=1){ if(lazyR[a]>maxi){ maxi=lazyR[a]; res=treeR[a]; } a/=2; } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // ----------------------------------------------------------------------------------- vector<vector<int>> dp(2e5+1,vector<int>(3)); dp[1]={0,1,1}; for(int i=2;i<=2e5;i++){ dp[i][0]=(dp[i-1][1]+dp[i-1][2])%MOD; dp[i][1]=(dp[i-1][0]+dp[i-1][2])%MOD; dp[i][2]=(dp[i-1][0]+dp[i-1][1])%MOD; } int n,q; cin>>n>>q; vector<char> tab(n+2); for(int i=1;i<=n;i++)cin>>tab[i]; //brut // if(n*q<=1000000){ for(int j=0;j<=q;j++){ if(j>0){ int a; char c; cin>>a>>c; tab[a]=c; } int sum=0; int firstC=-1,firstZ=-1; bool czyPara=false; int countN=0; for(int i=1;i<n;i++){ if(tab[i]=='C' && tab[i+1]=='C')czyPara=true; if(tab[i]=='Z' && tab[i+1]=='Z')czyPara=true; } vector<pair<int,int>> lewo(n+2),prawo(n+2); bool czyParaN=false; for(int i=1;i<=n;i++){ if(tab[i]=='C'){lewo[i]={1,i};sum=(sum+1)%3;firstC=i;} else if(tab[i]=='Z'){lewo[i]={2,i};sum=(sum+2)%3;firstZ=i;} else{ lewo[i]={lewo[i-1].first,lewo[i-1].second+1};countN++;} } for(int i=n;i>=1;i--){ if(tab[i]=='C')prawo[i]={1,i}; else if(tab[i]=='Z')prawo[i]={2,i}; else prawo[i]={prawo[i+1].first,prawo[i+1].second+1}; } for(int i=1;i<=n;i++){ if(tab[i]=='N'){ if(lewo[i].first==0 || prawo[i].first==0)continue; if(lewo[i].first==prawo[i].first && prawo[i].second%2==0)czyParaN=true; if(lewo[i].first!=prawo[i].first && prawo[i].second%2==1)czyParaN=true; } } if(countN==0 && !czyPara){ cout<<0<<endl; continue; } int res=(dp[countN][0]+dp[countN][1]+dp[countN][2]-dp[countN][(3-sum)%3])%MOD; if(n%2==1) if(countN==n)res-=2; if(n%2==1) if(!czyPara && !czyParaN){ if(firstC!=-1){ if(firstC%2==1){ if((3-sum)%3!=1)res--; }else{ if((3-sum)%3!=2)res--; } }else if(firstZ!=-1){ if(firstZ%2==0){ if((3-sum)%3!=1)res--; }else{ if((3-sum)%3!=2)res--; } } } res+=MOD; res%=MOD; cout<<res<<endl; } // } // ----------------------------------------------------------------------------------- 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 | //Jakub Trela #include <bits/stdc++.h> using namespace std; typedef long long LL; int M = 1<<18; int T=1; int MOD = 1e9+7; vector<int> treeL(2*M+1,-1); vector<int> lazyL(2*M+1); vector<int> treeR(2*M+1,-1); vector<int> lazyR(2*M+1); void updateL(int a, int b, int val){ if(a==0)return; a+=M; b+=M; while(a<=b){ if(a%2==1){ treeL[a]=val; lazyL[a]=T; a++; } if(b%2==0){ treeL[b]=val; lazyL[b]=T; b--; } a/=2; b/=2; } T++; } int queryL(int a){ a+=M; int maxi=-1,res=0; while(a>=1){ if(lazyL[a]>maxi){ maxi=lazyL[a]; res=treeL[a]; } a/=2; } return res; } void updateR(int a, int b, int val){ if(a==0)return; a+=M; b+=M; while(a<=b){ if(a%2==1){ treeR[a]=val; lazyR[a]=T; a++; } if(b%2==0){ treeR[b]=val; lazyR[b]=T; b--; } a/=2; b/=2; } T++; } int queryR(int a){ a+=M; int maxi=-1,res=0; while(a>=1){ if(lazyR[a]>maxi){ maxi=lazyR[a]; res=treeR[a]; } a/=2; } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // ----------------------------------------------------------------------------------- vector<vector<int>> dp(2e5+1,vector<int>(3)); dp[1]={0,1,1}; for(int i=2;i<=2e5;i++){ dp[i][0]=(dp[i-1][1]+dp[i-1][2])%MOD; dp[i][1]=(dp[i-1][0]+dp[i-1][2])%MOD; dp[i][2]=(dp[i-1][0]+dp[i-1][1])%MOD; } int n,q; cin>>n>>q; vector<char> tab(n+2); for(int i=1;i<=n;i++)cin>>tab[i]; //brut // if(n*q<=1000000){ for(int j=0;j<=q;j++){ if(j>0){ int a; char c; cin>>a>>c; tab[a]=c; } int sum=0; int firstC=-1,firstZ=-1; bool czyPara=false; int countN=0; for(int i=1;i<n;i++){ if(tab[i]=='C' && tab[i+1]=='C')czyPara=true; if(tab[i]=='Z' && tab[i+1]=='Z')czyPara=true; } vector<pair<int,int>> lewo(n+2),prawo(n+2); bool czyParaN=false; for(int i=1;i<=n;i++){ if(tab[i]=='C'){lewo[i]={1,i};sum=(sum+1)%3;firstC=i;} else if(tab[i]=='Z'){lewo[i]={2,i};sum=(sum+2)%3;firstZ=i;} else{ lewo[i]={lewo[i-1].first,lewo[i-1].second+1};countN++;} } for(int i=n;i>=1;i--){ if(tab[i]=='C')prawo[i]={1,i}; else if(tab[i]=='Z')prawo[i]={2,i}; else prawo[i]={prawo[i+1].first,prawo[i+1].second+1}; } for(int i=1;i<=n;i++){ if(tab[i]=='N'){ if(lewo[i].first==0 || prawo[i].first==0)continue; if(lewo[i].first==prawo[i].first && prawo[i].second%2==0)czyParaN=true; if(lewo[i].first!=prawo[i].first && prawo[i].second%2==1)czyParaN=true; } } if(countN==0 && !czyPara){ cout<<0<<endl; continue; } int res=(dp[countN][0]+dp[countN][1]+dp[countN][2]-dp[countN][(3-sum)%3])%MOD; if(n%2==1) if(countN==n)res-=2; if(n%2==1) if(!czyPara && !czyParaN){ if(firstC!=-1){ if(firstC%2==1){ if((3-sum)%3!=1)res--; }else{ if((3-sum)%3!=2)res--; } }else if(firstZ!=-1){ if(firstZ%2==0){ if((3-sum)%3!=1)res--; }else{ if((3-sum)%3!=2)res--; } } } res+=MOD; res%=MOD; cout<<res<<endl; } // } // ----------------------------------------------------------------------------------- return 0; } |