#include<bits/stdc++.h>
using namespace std;
int n, q;
long long mod = 1e9 + 7;
struct ver{
vector<vector<unsigned long long> > s;
vector<vector<unsigned long long> > e;
vector<vector<unsigned long long> > b;
};
vector<ver> t;
ver merg(ver a, ver b){
ver ans;
ans.s.resize(2, vector<unsigned long long>(2, 0));
ans.e.resize(2, vector<unsigned long long>(2, 0));
ans.b.resize(2, vector<unsigned long long>(2, 0));
for(int i = 0; i <= 1; i++){
for(int j = 0; j <= 1; j++){
for(int u = 0; u <= 1; u++){
for(int uu = 0; uu <= 1; uu++){
if((u & uu) != 1){
// cout << u << " " << uu << endl;
ans.e[i][j] += a.e[i][u] * b.e[uu][j];
ans.e[i][j] %= mod;
ans.s[i][j] += (a.s[i][u] * ((b.s[uu][j] + b.e[uu][j] + b.b[uu][j]) % mod)) % mod;
ans.s[i][j] %= mod;
ans.s[i][j] += a.e[i][u] * (b.s[uu][j]);
ans.s[i][j] %= mod;
ans.b[i][j] += (a.b[i][u] * ((b.s[uu][j] + b.e[uu][j] + b.b[uu][j]) % mod)) % mod;
ans.b[i][j] %= mod;
ans.b[i][j] += a.e[i][u] * b.b[uu][j];
ans.b[i][j] %= mod;
}
}
}
}
}
return ans;
}
vector<int> a;
void build(int v, int l, int r){
if(l == r){
if(a[l] == 1){
t[v].e[0][1] = 1;
}
else if(a[l] == 3){
t[v].e[1][0] = 1;
}
else t[v].e[0][0] = 1;
for(int i = 0; i < a[l]; i++){
if(i == 1){
t[v].s[0][1]++;
}
else if(i == 3){
t[v].s[1][0]++;
}
else t[v].s[0][0]++;
}
for(int i = a[l] + 1; i <= 9; i++){
if(i == 1){
t[v].b[0][1]++;
}
else if(i == 3){
t[v].b[1][0]++;
}
else t[v].b[0][0]++;
}
// cout << v << ": " << endl;
// cout << "small " << t[v].s[0][0] << " " << t[v].s[0][1] << " " << t[v].s[1][0] << " " << t[v].s[1][1] << endl;
// cout << "eq " << t[v].e[0][0] << " " << t[v].e[0][1] << " " << t[v].e[1][0] << " " << t[v].e[1][1] << endl;
// cout << "b " << t[v].b[0][0] << " " << t[v].b[0][1] << " " << t[v].b[1][0] << " " << t[v].b[1][1] << endl;
return;
}
int mid = (l + r) / 2;
build(v * 2, l, mid);
build(v * 2 + 1, mid + 1, r);
t[v] = merg(t[v * 2], t[v * 2 + 1]);
// cout << v << ": " << endl;
// cout << "small " << t[v].s[0][0] << " " << t[v].s[0][1] << " " << t[v].s[1][0] << " " << t[v].s[1][1] << endl;
// cout << "eq " << t[v].e[0][0] << " " << t[v].e[0][1] << " " << t[v].e[1][0] << " " << t[v].e[1][1] << endl;
// cout << "b " << t[v].b[0][0] << " " << t[v].b[0][1] << " " << t[v].b[1][0] << " " << t[v].b[1][1] << endl;
}
void upd(int v, int l, int r, int x){
if(l == r){
t[v].s.clear();
t[v].e.clear();
t[v].b.clear();
t[v].s.resize(2, vector<unsigned long long>(2, 0));
t[v].e.resize(2, vector<unsigned long long>(2, 0));
t[v].b.resize(2, vector<unsigned long long>(2, 0));
if(a[l] == 1){
t[v].e[0][1] = 1;
}
else if(a[l] == 3){
t[v].e[1][0] = 1;
}
else t[v].e[0][0] = 1;
for(int i = 0; i < a[l]; i++){
if(i == 1){
t[v].s[0][1]++;
}
else if(i == 3){
t[v].s[1][0]++;
}
else t[v].s[0][0]++;
}
for(int i = a[l] + 1; i <= 9; i++){
if(i == 1){
t[v].b[0][1]++;
}
else if(i == 3){
t[v].b[1][0]++;
}
else t[v].b[0][0]++;
}
// cout << v << ": " << endl;
// cout << "small " << t[v].s[0][0] << " " << t[v].s[0][1] << " " << t[v].s[1][0] << " " << t[v].s[1][1] << endl;
// cout << "eq " << t[v].e[0][0] << " " << t[v].e[0][1] << " " << t[v].e[1][0] << " " << t[v].e[1][1] << endl;
// cout << "b " << t[v].b[0][0] << " " << t[v].b[0][1] << " " << t[v].b[1][0] << " " << t[v].b[1][1] << endl;
return;
}
int mid = (l + r) / 2;
if(x <= mid) upd(v * 2, l, mid, x);
else upd(v * 2 + 1, mid + 1, r, x);
t[v] = merg(t[v * 2], t[v * 2 + 1]);
}
ver find_ans(int v, int l, int r, int ql, int qr){
if(ql == l && r == qr) return t[v];
int mid = (l + r) / 2;
if(qr <= mid) return find_ans(v * 2, l, mid, ql, qr);
if(ql > mid) return find_ans(v * 2 + 1, mid + 1, r, ql, qr);
return merg(find_ans(v * 2, l, mid, ql, mid), find_ans(v * 2 + 1, mid + 1, r, mid + 1, qr));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
string s;
t.resize(4 * n + 1, {vector<vector<unsigned long long> > (2, vector<unsigned long long>(2, 0)), vector<vector<unsigned long long> > (2, vector<unsigned long long>(2, 0)), vector<vector<unsigned long long> > (2, vector<unsigned long long>(2, 0))});
a.resize(n + 1);
cin >> s;
for(int i = 0; i < s.size(); i++){
a[i + 1] = s[i] - '0';
}
build(1, 1, n);
ver an = find_ans(1, 1, n, 1, n);
cout << (((((((an.e[0][0] + an.e[0][1]) % mod + an.e[1][0]) % mod + an.e[1][1]) % mod + an.s[0][0]) % mod + an.s[0][1]) % mod + an.s[1][0]) % mod + an.s[1][1] ) % mod << endl;
int t, aa, b;
while(q--){
cin >> t >> aa >> b;
if(t == 1){
an = find_ans(1, 1, n, aa, b);
cout << (((((((an.e[0][0] + an.e[0][1]) % mod + an.e[1][0]) % mod + an.e[1][1]) % mod + an.s[0][0]) % mod + an.s[0][1]) % mod + an.s[1][0]) % mod + an.s[1][1] ) % mod << endl;
}
else{
a[aa] = b;
upd(1, 1, n, aa);
}
}
}
Compilation message
lucky.cpp: In function 'int main()':
lucky.cpp:142:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for(int i = 0; i < s.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
14660 KB |
Output is correct |
2 |
Correct |
181 ms |
18460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
14660 KB |
Output is correct |
2 |
Correct |
181 ms |
18460 KB |
Output is correct |
3 |
Execution timed out |
434 ms |
143820 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
137 ms |
14660 KB |
Output is correct |
8 |
Correct |
181 ms |
18460 KB |
Output is correct |
9 |
Correct |
173 ms |
14676 KB |
Output is correct |
10 |
Execution timed out |
223 ms |
18384 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
137 ms |
14660 KB |
Output is correct |
8 |
Correct |
181 ms |
18460 KB |
Output is correct |
9 |
Execution timed out |
434 ms |
143820 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |