Submission #1099501

# Submission time Handle Problem Language Result Execution time Memory
1099501 2024-10-11T13:26:19 Z ShadowShark Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
101 ms 68956 KB
#include <bits/stdc++.h>
using namespace std;

const int maxN = 1e5 + 5, maxC = 2e6 + 5;
int convert[26], ans[maxN]; string S[maxN];

namespace pref {
    int to[maxC][4], mn[maxC], mx[maxC], currNode = 0;

    void add_string(string s, int id) {
        int u = 0;
        for (auto ch: s) {
            int x = ch - 'A'; x = convert[x];
            if (!to[u][x]) to[u][x] = ++currNode;
            u = to[u][x];
            mx[u] = id; if (!mn[u]) mn[u] = id;
        }
    }

    pair <int, int> get(string s) {
        int u = 0;
        for (auto ch: s) {
            int x = ch - 'A'; x = convert[x];
            if (!to[u][x]) return {-1, -1};
            u = to[u][x];
        }
        return {mn[u], mx[u]};
    }
}

namespace suff {
    int to[maxC][4], cnt[maxC], currNode = 0;

    void add_string(string S) {
        int u = 0;
        for (int i = S.size() - 1; i >= 0; i--) {
            int x = S[i] - 'A'; x = convert[x];
            if (!to[u][x]) to[u][x] = ++currNode;
            u = to[u][x]; cnt[u]++;
        }
    }

    void delete_string(string S) {
        int u = 0;
        for (int i = S.size() - 1; i >= 0; i--) {
            int x = S[i] - 'A'; x = convert[x];
            u = to[u][x]; cnt[u]--;
        }
    }

    int get(string S) {
        int u = 0;
        for (int i = S.size() - 1; i >= 0; i--) {
            int x = S[i] - 'A'; x = convert[x];
            if (!to[u][x]) return 0;
            u = to[u][x];
        }
        return cnt[u];
    }
}

struct event {
    string P, Q;
    int id;

    bool operator < (const event& rhs) const {
        return P < rhs.P;
    }

} query[maxN];

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    convert[0] = 0; convert[2] = 1; convert[6] = 2; convert[20] = 3;

    int n, TiaChem;
    cin >> n >> TiaChem;

    for (int i = 1; i <= n; i++)
        cin >> S[i];

    sort(S + 1, S + n + 1);
    for (int i = 1; i <= n; i++) {
        pref::add_string(S[i], i);
        //cout << i << ' ' << S[i] << '\n';
    }

    for (int i = 1; i <= TiaChem; i++) {
        cin >> query[i].P >> query[i].Q;
        query[i].id = i;
    }

    sort(query + 1, query + TiaChem + 1);

    //cout << '\n';

    int L = 1, R = 0;
    for (int i = 1; i <= TiaChem; i++) {
        pair<int, int> range = pref::get(query[i].P);

        //cout << query[i].P << ' ' << query[i].Q << ' ' << range.first << ' ' << range.second << '\n';

        if (range.first == -1) {
            ans[query[i].id] = 0;
            continue;
        }

        while (R < range.second) suff::add_string(S[++R]);
        while (R > range.second) suff::delete_string(S[R--]);
        while (L < range.first) suff::delete_string(S[L++]);

        //cout << L << ' ' << R << '\n';

        ans[query[i].id] = suff::get(query[i].Q);
    }

    for (int i = 1; i <= TiaChem; i++)
        cout << ans[i] << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10584 KB Output is correct
2 Correct 5 ms 10636 KB Output is correct
3 Correct 4 ms 10628 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 4 ms 10688 KB Output is correct
6 Correct 5 ms 10644 KB Output is correct
7 Correct 5 ms 10584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 57396 KB Output is correct
2 Correct 50 ms 55120 KB Output is correct
3 Correct 63 ms 64340 KB Output is correct
4 Correct 67 ms 62140 KB Output is correct
5 Correct 65 ms 68180 KB Output is correct
6 Correct 69 ms 68956 KB Output is correct
7 Correct 40 ms 21848 KB Output is correct
8 Correct 67 ms 53424 KB Output is correct
9 Correct 56 ms 48304 KB Output is correct
10 Correct 36 ms 42560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 11604 KB Output is correct
2 Correct 26 ms 11344 KB Output is correct
3 Correct 25 ms 11348 KB Output is correct
4 Correct 21 ms 11352 KB Output is correct
5 Correct 23 ms 11352 KB Output is correct
6 Correct 28 ms 11356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10584 KB Output is correct
2 Correct 5 ms 10636 KB Output is correct
3 Correct 4 ms 10628 KB Output is correct
4 Correct 5 ms 10588 KB Output is correct
5 Correct 4 ms 10688 KB Output is correct
6 Correct 5 ms 10644 KB Output is correct
7 Correct 5 ms 10584 KB Output is correct
8 Correct 52 ms 57396 KB Output is correct
9 Correct 50 ms 55120 KB Output is correct
10 Correct 63 ms 64340 KB Output is correct
11 Correct 67 ms 62140 KB Output is correct
12 Correct 65 ms 68180 KB Output is correct
13 Correct 69 ms 68956 KB Output is correct
14 Correct 40 ms 21848 KB Output is correct
15 Correct 67 ms 53424 KB Output is correct
16 Correct 56 ms 48304 KB Output is correct
17 Correct 36 ms 42560 KB Output is correct
18 Correct 28 ms 11604 KB Output is correct
19 Correct 26 ms 11344 KB Output is correct
20 Correct 25 ms 11348 KB Output is correct
21 Correct 21 ms 11352 KB Output is correct
22 Correct 23 ms 11352 KB Output is correct
23 Correct 28 ms 11356 KB Output is correct
24 Correct 52 ms 50512 KB Output is correct
25 Correct 61 ms 51328 KB Output is correct
26 Correct 50 ms 50000 KB Output is correct
27 Correct 67 ms 56208 KB Output is correct
28 Correct 101 ms 25348 KB Output is correct
29 Correct 67 ms 14676 KB Output is correct