Submission #685629

#TimeUsernameProblemLanguageResultExecution timeMemory
685629treap_enjoyerLucky Numbers (RMI19_lucky)C++17
100 / 100
134 ms38860 KiB
#include<bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2") using namespace std; int n, q; long long mod = 1e9 + 7; struct ver{ unsigned long long s[2][2]; unsigned long long e[2][2]; unsigned long long b[2][2]; }; vector<ver> t; ver merg(ver a, ver b){ ver ans; for(int i = 0; i <= 1; i++){ for(int j = 0; j <= 1; j++){ ans.s[i][j] = 0; ans.e[i][j] = 0; ans.b[i][j] = 0; 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){ for(int i = 0; i <= 1; i++){ for(int j = 0; j <= 1; j++){ t[v].b[i][j] = 0; t[v].s[i][j] = 0; t[v].e[i][j] = 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(NULL); cout.tie(NULL); cin >> n >> q; string s; t.resize(4 * n + 1); 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]+ an.e[1][0] + an.e[1][1] + an.s[0][0] + an.s[0][1] + an.s[1][0] + an.s[1][1] ) % mod << '\n'; 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]+ an.e[1][0] + an.e[1][1] + an.s[0][0] + an.s[0][1] + an.s[1][0] + an.s[1][1] ) % mod << '\n'; } else{ a[aa] = b; upd(1, 1, n, aa); } } }

Compilation message (stderr)

lucky.cpp: In function 'int main()':
lucky.cpp:145:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |     for(int i = 0; i < s.size(); i++){
      |                    ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...