QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379371#8576. Symphony in c++ majorucup-team1198#ML 0ms3624kbC++203.7kb2024-04-06 17:19:132024-04-06 17:19:13

Judging History

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

  • [2024-04-06 17:19:13]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:3624kb
  • [2024-04-06 17:19:13]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

const int K = 7;
const int INF = 1e9;

string lines[K] = {"do", "re", "mi", "fa", "so", "la", "ti"};
int dp[(1 << 20) + 228][K + 1][K + 1];

void recalc(int v, int left, int right) {
    for (int i = 0; i <= K; ++i)
        fill(dp[v][i], dp[v][i] + K + 1, INF);
    for (int i = 0; i <= K; ++i) {
        for (int j = 0; j <= K; ++j) {
            for (int l = 0; l <= K; ++l) {
                dp[v][i][l] = min(dp[v][i][l], dp[2 * v + 1][i][j] + dp[2 * v + 2][j][l]);
            }
        }
    }
    int mid = (left + right) / 2;
    for (int i = 0; i <= K; ++i) {
        for (int j = 0; j <= K; ++j) {
            dp[v][i][j] = min(dp[v][i][j], dp[2 * v + 1][i][j] + right - mid);
            dp[v][i][j] = min(dp[v][i][j], mid - left + dp[2 * v + 2][i][j]);
        }
    }
}

void upd_symbol(int v, char c) {
    // left dim is right
    // right dim is left
    for (int i = 0; i <= K; ++i)
        fill(dp[v][i], dp[v][i] + K + 1, INF);
    for (int i = 0; i < K; ++i) {
        if (c == lines[i][1])
            dp[v][i][K] = 0;
    }
    for (int i = 0; i < K; ++i) {
        if (c == lines[i][0])
            dp[v][K][i] = 0;
    }
    dp[v][K][K] = 1;
}

void build(int v, int left, int right, string& s) {
    if (left + 1 == right) {
        upd_symbol(v, s[left]);
        return;
    }
    int mid = (left + right) / 2;
    build(2 * v + 1, left, mid, s);
    build(2 * v + 2, mid, right, s);
    recalc(v, left, right);
}

void upd(int v, int left, int right, int x, int y, string& s) {
    if (y <= left || right <= x)
        return;
    if (left + 1 == right) {
        upd_symbol(v, s[left - x]);
        return;
    }
    int mid = (left + right) / 2;
    upd(2 * v + 1, left, mid, x, y, s);
    upd(2 * v + 2, mid, right, x, y, s);
    recalc(v, left, right);
}

int cur_dp[K + 1];
int tmp_cur_dp[K + 1];

void get(int v, int left, int right, int x, int y) {
    if (y <= left || right <= x)
        return;
    if (x <= left && right <= y) {
        fill(tmp_cur_dp, tmp_cur_dp + K + 1, INF);
        for (int i = 0; i <= K; ++i) {
            for (int j = 0; j <= K; ++j)
                tmp_cur_dp[j] = min(tmp_cur_dp[j], cur_dp[i] + dp[v][i][j]);
        }
        for (int i = 0; i <= K; ++i)
            cur_dp[i] = tmp_cur_dp[i];
        return;
    }
    int mid = (left + right) / 2;
    get(2 * v + 1, left, mid, x, y);
    get(2 * v + 2, mid, right, x, y);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n, q;
    cin >> n >> q;
    string s;

    /*for (int i = 0; i < n; ++i)
        s += char('a' + rand() % 26);*/

    cin >> s;
    build(0, 0, n, s);
    string t;
    while (q--) {
        char c;
        
        //c = '#';

        cin >> c;

        
        if (c == '?') {
            int i, j;
            cin >> i >> j;

            /*i = rand() % n + 1;
            j = i + rand() % (n - i + 1);*/

            fill(cur_dp, cur_dp + K + 1, INF);
            cur_dp[K] = 0;
            get(0, 0, n, i - 1, j);
            cout << cur_dp[K] << '\n';
        } else {
            int l, r;
            cin >> l >> r;
            
            /*l = rand() % n + 1;
            r = l + 1;
            t = char('a' + rand() % 26);*/

            cin >> t;
            upd(0, 0, n, l - 1, r, t);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3624kb

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:

ok 6 numbers

Test #2:

score: -100
Memory Limit Exceeded

input:

500000 300000
rfsraltzititrofomloldlaefikdemaddaafmimiioiuaueiaedifacmxamttiiitaaltiiireexeafsaaraedosotlaioootaiosizlifiioadhnxiofsasaflleaiisstaotdlliulilxatrpdaraaraaoiaeoooiumwuumarlifesroloistedoaaieolisaoatamsredorrfiifaaidfeassfioaiasmstomasallsftsrfaiiirteomaeiirefldmlaoaofrxlaeuilioirafotoa...

output:

122151
133262
96922
55212
91547
148150
73505
4097
300800
54039
56741
265921
127608
170707
79236
209443
84732
83219
184042
77169
94062
172946
202750
97798
92449
67243
171524
145772
53856
165837
104913
179165
35068
55895
17287
74510
319357
244761
118810
162817
175172
136079
43107
237581
112894
48610
1...

result: