답안 #685612

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
685612 2023-01-24T16:56:22 Z treap_enjoyer Lucky Numbers (RMI19_lucky) C++17
14 / 100
108 ms 14744 KB
#include<bits/stdc++.h>
using namespace std;
int n, q;
long long mod = 1e9 + 7;
struct ver{
    vector<vector<long long> > s;
    vector<vector<long long> > e;
    vector<vector<long long> > b;
};
vector<ver> t;
ver merg(ver a, ver b){
    ver ans;
    ans.s.resize(2, vector<long long>(2, 0));
    ans.e.resize(2, vector<long long>(2, 0));
    ans.b.resize(2, vector<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]);
                        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]);
                        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<long long>(2, 0));
        t[v].e.resize(2, vector<long long>(2, 0));
        t[v].b.resize(2, vector<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<long long> > (2, vector<long long>(2, 0)), vector<vector<long long> > (2, vector<long long>(2, 0)), vector<vector<long long> > (2, vector<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] + an.e[1][0] + an.e[1][1] + an.s[0][0] + an.s[0][1] + an.s[1][0] + an.s[1][1] << 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] + an.e[1][0] + an.e[1][1] + an.s[0][0] + an.s[0][1] + an.s[1][0] + an.s[1][1] << '\n';
        }
        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++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 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 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 316 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 108 ms 14744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 108 ms 14744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 316 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 316 KB Output isn't correct
5 Halted 0 ms 0 KB -