답안 #685629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
685629 2023-01-24T17:19:31 Z treap_enjoyer Lucky Numbers (RMI19_lucky) C++17
100 / 100
134 ms 38860 KB
#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

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++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 3384 KB Output is correct
2 Correct 55 ms 4176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 3384 KB Output is correct
2 Correct 55 ms 4176 KB Output is correct
3 Correct 97 ms 30952 KB Output is correct
4 Correct 107 ms 31072 KB Output is correct
5 Correct 126 ms 35028 KB Output is correct
6 Correct 122 ms 38736 KB Output is correct
7 Correct 122 ms 38756 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 43 ms 3384 KB Output is correct
8 Correct 55 ms 4176 KB Output is correct
9 Correct 48 ms 3412 KB Output is correct
10 Correct 63 ms 4176 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 43 ms 3384 KB Output is correct
8 Correct 55 ms 4176 KB Output is correct
9 Correct 97 ms 30952 KB Output is correct
10 Correct 107 ms 31072 KB Output is correct
11 Correct 126 ms 35028 KB Output is correct
12 Correct 122 ms 38736 KB Output is correct
13 Correct 122 ms 38756 KB Output is correct
14 Correct 48 ms 3412 KB Output is correct
15 Correct 63 ms 4176 KB Output is correct
16 Correct 113 ms 31076 KB Output is correct
17 Correct 111 ms 31004 KB Output is correct
18 Correct 117 ms 34884 KB Output is correct
19 Correct 129 ms 38744 KB Output is correct
20 Correct 134 ms 38860 KB Output is correct