QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379143#8576. Symphony in c++ majorucup-team228#ML 0ms0kbC++203.5kb2024-04-06 16:22:182024-04-06 16:22:19

Judging History

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

  • [2024-04-06 16:22:19]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-04-06 16:22:18]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

template <typename T>
struct segment_tree {
    static const int N = 5e5 + 10;
    const T good_value = {}; // 0 for sum, -inf for max, and inf for min
    T a[N], t[4 * N];
    T merge(T x, T y) {
        return x + y;
    }
    void build(int v = 1, int tl = 0, int tr = N - 1) {
        if (tl > tr) return;
        if (tl == tr) {
            t[v] = a[tl];
            return;
        }
        int tm = (tl + tr) / 2;
        build(v + v, tl, tm);
        build(v + v + 1, tm + 1, tr);
        t[v] = merge(t[v + v], t[v + v + 1]);
    }
    void update(int pos, T val, int v = 1, int tl = 0, int tr = N - 1) {
        if (tl > tr || tl > pos || pos > tr) return;
        if (tl == tr) {
            t[v] = val;
            return;
        }
        int tm = (tl + tr) / 2;
        update(pos, val, v + v, tl, tm);
        update(pos, val, v + v + 1, tm + 1, tr);
        t[v] = merge(t[v + v], t[v + v + 1]);
    }
    T get(int l, int r, int v = 1, int tl = 0, int tr = N - 1) {
        if (tl > tr || l > r || tl > r || l > tr) return good_value;
        if (l <= tl && tr <= r) {
            return t[v];
        }
        int tm = (tl + tr) / 2;
        return merge(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
    }
};

const int inf = 1e9;

const int C = 7;

const vector<string> all = {
        "do",
        "re",
        "mi",
        "fa",
        "so",
        "la",
        "ti"
};

struct Node {
    int dp[C + 1][C + 1];
    Node() {
        for (int i = 0; i <= C; i++) {
            for (int j = 0; j <= C; j++) {
                dp[i][j] = -inf;
            }
        }
        dp[C][C] = 0;
    }
    Node(char ch) {
        for (int i = 0; i <= C; i++) {
            for (int j = 0; j <= C; j++) {
                if ((i == C || all[i][1] == ch) && (j == C || all[j][0] == ch)) {
                    dp[i][j] = (i != C) + (j != C);
                } else {
                    dp[i][j] = -inf;
                }
            }
        }
    }
    friend Node operator+(const Node& a, const Node& b) {
        Node res;
        for (int i = 0; i <= C; i++) {
            for (int j = 0; j <= C; j++) {
                res.dp[i][j] = max(a.dp[i][j], b.dp[i][j]);
                for (int k = 0; k <= C; k++) {
                    res.dp[i][j] = max(res.dp[i][j], a.dp[i][k] + b.dp[k][j]);
                }
            }
        }
        return res;
    }
};

segment_tree<Node> tree;

void foo() {
    const string s = "eldorado";
    for (int i = 0; i < s.size(); i++) {
        tree.update(i + 1, s[i]);
    }
    auto res = tree.get(1, 3);
    cout << res.dp[C][C] << "\n";
    exit(0);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    // foo();

    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    for (int i = 1; i <= n; i++) {
        tree.update(i, s[i - 1]);
    }
    while (q--) {
        char type;
        cin >> type;
        int l, r;
        cin >> l >> r;
        if (type == '#') {
            string cur;
            cin >> cur;
            for (int i = 0; i < cur.size(); i++) {
                tree.update(l + i, cur[i]);
            }
        } else {
            cout << (r - l + 1) - tree.get(l, r).dp[C][C] << "\n";
        }
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

Details

Tip: Click on the bar to expand more detailed information

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: