This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int cys=1000000007;
int mod(ll x){return x%cys;}
void chkadd(int& a,int b){a=mod(a+b);}
int mul(int a,int b){return mod(a*1ll*b);}
int n,q;
char s[100005];
struct info{
int val[3][2][2];
void init(){
for(int i=0;i<3;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++) val[i][j][k]=0;
}
info(){init();}
}dp[400005];
info operator+ (const info& a,const info& b){
info ret;
for(int ia=0;ia<3;ia++){
for(int ja=0;ja<2;ja++){
for(int ka=0;ka<2;ka++){
for(int ib=0;ib<3;ib++){
for(int jb=0;jb<2;jb++){
for(int kb=0;kb<2;kb++){
if(ja==1&&kb==1) continue;
int i;
if(ia==2||(ia==1&&ib==2)) i=2;
else if(ia==1&&ib==1) i=1;
else i=0;
chkadd(ret.val[i][jb][ka],mul(a.val[ia][ja][ka],b.val[ib][jb][kb]));
}
}
}
}
}
}
return ret;
}
void build(int id,int l,int r){
if(l==r){
int d=s[l]-'0';
dp[id].init();
for(int dig=0;dig<10;dig++){
dp[id].val[dig<d?0:(dig==d?1:2)][dig==1][dig==3]+=1;
}
return;
}
int mid=(l+r)/2;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
dp[id]=dp[id<<1]+dp[id<<1|1];
}
void change(int id,int l,int r,int p){
if(l==r){
int d=s[l]-'0';
dp[id].init();
for(int dig=0;dig<10;dig++){
dp[id].val[dig<d?0:(dig==d?1:2)][dig==1][dig==3]+=1;
}
return;
}
int mid=(l+r)/2;
if(p<=mid) change(id<<1,l,mid,p);
else change(id<<1|1,mid+1,r,p);
dp[id]=dp[id<<1]+dp[id<<1|1];
}
info query(int id,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr) return dp[id];
int mid=(l+r)/2;
if(qr<=mid) return query(id<<1,l,mid,ql,qr);
else if(ql>mid) return query(id<<1|1,mid+1,r,ql,qr);
else return query(id<<1,l,mid,ql,qr)+query(id<<1|1,mid+1,r,ql,qr);
}
int solve(int l,int r){
info nd=query(1,1,n,l,r);
int res=0;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
chkadd(res,nd.val[i][j][k]);
}
}
}
return res;
}
int main(){
n=readint(); q=readint();
scanf("%s",s+1);
build(1,1,n);
printf("%d\n",solve(1,n));
while(q--){
int t=readint();
if(t==1){
int l=readint(),r=readint();
printf("%d\n",solve(l,r));
}else{
int p=readint(),d=readint();
s[p]=(char)(d+'0');
change(1,1,n,p);
}
}
return 0;
}
Compilation message (stderr)
lucky.cpp: In function 'int main()':
lucky.cpp:119:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | scanf("%s",s+1);
| ~~~~~^~~~~~~~~~
# | 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... |