#pragma GCC optimize("O2,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define all(x) x.begin() , x.end()
const int N = 131072;
const int mod = 1e9 + 7;
inline void ckadd(int &a , int b){
a += b;
if(a >= mod)a -= mod;
}
struct Matrix{
int mat[4][4];
Matrix(){
memset(mat , 0 , sizeof(mat));
}
friend Matrix mult(Matrix &a , Matrix &b){
Matrix c;
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
for(int k = 0;k<4;k++){
ckadd(c.mat[i][j] , 1LL * a.mat[i][k] * b.mat[k][j] % mod);
}
}
}
return c;
}
void print(){
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
} iden;
vector < Matrix > t;
Matrix merge(Matrix &x , Matrix &y){
return mult(x,y);
}
void init(){
t.assign(2*N , iden);
}
void update(int p, Matrix value) { // set value at position p
for (t[p += N] = value; p >>= 1; ) t[p] = mult(t[p<<1], t[p<<1|1]);
}
Matrix query(int l, int r) { // sum on interval [l, r]
Matrix resl = iden , resr = iden;
for (r+=1 , l += N, r += N; l < r; l >>= 1, r >>= 1) {
if (l&1) resl = mult(resl , t[l++]);
if (r&1) resr = mult(t[--r] , resr);
}
return mult(resl , resr);
}
void solve(){
for(int i = 0;i<4;i++){
iden.mat[i][i] = 1;
}
init();
int n,q;
cin >> n >> q;
string str;
cin >> str;
auto upd = [&](int p , int x){// p yi x yap
Matrix tran;
tran.mat[0][0] = 8;
tran.mat[0][1] = 1;
tran.mat[0][2] = 1;
tran.mat[0][3] = 0;
tran.mat[1][0] = 8;
tran.mat[1][1] = 1;
tran.mat[1][2] = 0;
tran.mat[1][3] = 0;
tran.mat[2][0] = 8;
tran.mat[2][1] = 1;
tran.mat[2][2] = 1;
tran.mat[2][3] = 0;
tran.mat[3][0] = x;
if(x > 3){
tran.mat[3][0] -= 2;
}
else if(x > 1){
tran.mat[3][0] -= 1;
}
if(x > 1){
tran.mat[3][1] = 1;
}
if((p == 0 or str[p-1] != '1') and x > 3){
tran.mat[3][2] = 1;
}
else{
tran.mat[3][2] = 0;
}
if(p > 0 and str[p-1] == '1' and x == 3){
tran.mat[3][3] = 0;
}
else{
tran.mat[3][3] = 1;
}
// cout << p << " tran : " << endl;
// tran.print();
update(p , tran);
};
auto que = [&](int l , int r){
Matrix st;
int x = str[l] - '0';
st.mat[0][0] = x;
if(x > 1){
st.mat[0][0]--;
st.mat[0][1]++;
}
if(x > 3){
st.mat[0][0]--;
st.mat[0][2]++;
}
st.mat[0][3] = 1;
// cout << "st : " << endl;
// st.print();
Matrix res = l == r ? iden : query(l+1 , r);
st = mult(st , res);
int ans = 0;
for(int i = 0;i<4;i++){
ckadd(ans , st.mat[0][i]);
}
// cout << "res : " << endl;
// st.print();
return ans;
};
for(int i = 0;i<n;i++){
upd(i , str[i] - '0');
}
cout << que(0,n-1) << endl;
while(q--){
int typ;
cin >> typ;
if(typ == 1){
int l,r;
cin >> l >> r;
l-- , r--;
cout << que(l,r) << endl;
}
else{
int p,x;
cin >> p >> x;
p--;
upd(p , x);
str[p] = '0' + x;
}
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase=1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |