QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#388427#8576. Symphony in c++ majorBUET_POTATOES#ML 0ms0kbC++203.5kb2024-04-13 15:36:552024-04-13 15:36:55

Judging History

你现在查看的是最新测评结果

  • [2024-04-13 15:36:55]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-04-13 15:36:55]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9, K = 8, N = 5e5 + 5;
void conv(string &s){
    for(auto &x : s){
        if(x == 'd')x = 's';
        else if(x == 'm')x = 't';
        else if(x == 'f')x = 'l';
    }
}
char cng(char c){
    if(c == 's')return 1;
    if(c == 't')return 2;
    if(c == 'l')return 3;
    if(c == 'r')return 4;
    if(c == 'o')return 4+1;
    if(c == 'i')return 4+2;
    if(c == 'a')return 4+3;
    if(c == 'e')return 4+4;
    return 0;
}
namespace ST{
    struct node{
        int val[K + 1][K + 1];
        void init(){
            for(int i = 0; i <= K; ++i){
                for(int j = 0; j <= K; ++j)
                    val[i][j] = -INF;
            }
            val[0][0] = 0;
        }
        void init(char c){
            c = cng(c);
            init();
            val[c][0] = 0;
            val[0][c] = 0;
        }
        node(){
            init();
        }
        node(char c){
            init(c);
        }
    };

    node comb(const node &a, const node &b){
        node ret;
        for(int i = 0; i <= K; ++i){
            for(int j = 0; j <= K; ++j){
                ret.val[i][j] = max(ret.val[i][j], a.val[i][0] + b.val[0][j]);
                ret.val[i][j] = max(ret.val[i][j], a.val[i][j]);
                ret.val[i][j] = max(ret.val[i][j], b.val[i][j]);
                for(int k = 1; k <= K / 2; ++k){
                    ret.val[i][j] = max(ret.val[i][j], a.val[i][k] + b.val[k + K / 2][j] + 2);
                }
            }
        }
        return ret;
    }
    node st[N << 2];
    void init(int u, int l, int r, const string &s){
        if(l == r){
            st[u].init(s[l]);
            return;
        }
        int mid = l + r >> 1;
        init(u << 1, l, mid, s);
        init(u << 1 | 1, mid + 1, r, s);
        st[u] = comb(st[u << 1], st[u << 1 | 1]);
//        cout << "NODE: " << u << ' ' << l << ' ' << r << endl;
//        for(int i = 0; i <= K; ++i){
//            for(int j = 0; j <= K; ++j)
//                if(st[u].val[i][j] >= 0)cout << i << ' ' << j << ' ' << st[u].val[i][j] << endl;
//        }
    }
    void update(int u, int l, int r, int L, int R, int &k, string &s){
        if(L > r || R < l)return;
        if(l == r){
            st[u].init(s[k++]);
            return;
        }
        int mid = l + r >> 1;
        update(u << 1, l, mid, L, R, k, s);
        update(u << 1 | 1, mid + 1, r, L, R, k, s);
        st[u] = comb(st[u << 1], st[u << 1 | 1]);
    }
    node query(int u, int l, int r, int L, int R){
        if(L > r || R < l)return node();
        if(L <= l && R >= r){
            return st[u];
        }
        int mid = l + r >> 1;
        return comb(query(u << 1, l, mid, L, R), query(u << 1 | 1, mid + 1, r, L, R));
    }
}

void testcase(int cs){
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    conv(s);

    ST::init(1, 0, n - 1, s);
    while(q--){
        char c;
        cin >> c;
        int l, r;
        cin >> l >> r;
        --l, --r;
        if(c == '?'){
            auto ans = ST::query(1, 0, n - 1, l, r);

            cout << r - l + 1 - ans.val[0][0] << "\n";
        }else{
            cin >> s;
            int k = 0;
            ST::update(1, 0, n - 1, l, r, k, s);
        }
    }
}
/*
4 3
eldo
? 1 2
? 3 4
? 1 4
*/
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int T = 1;
//    cin >> T;
    for(int cs = 1; cs <= T; ++cs){
        testcase(cs);
    }
}


详细

Test #1:

score: 0
Memory Limit Exceeded

input:

8 10
eldorado
? 1 3
? 1 8
# 6 7 it
? 1 8
# 3 3 t
? 1 8
# 1 8 streamer
? 1 8
# 1 8 symphony
? 1 8

output:

3
4
6
6
6
6

result: