답안 #521573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
521573 2022-02-02T12:15:26 Z tengiz05 Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
1171 ms 106128 KB
#include <bits/stdc++.h>

using i64 = long long;
constexpr i64 P = 1E9 + 7, M = 31;

struct Node {
    Node *to[4];
    int mn, mx;
    Node() {
        mn = 1E9, mx = 0;
        to[0] = to[1] = to[2] = to[3] = nullptr;
    }
};
void add(Node *u, std::string &s, int i, int p) {
    if (i == int(s.size())) {
        u->mn = std::min(u->mn, p);
        u->mx = std::max(u->mx, p);
        return;
    }
    int x = std::string("AGCU").find(s[i]);
    if (u->to[x] == nullptr) {
        u->to[x] = new Node();
    }
    add(u->to[x], s, i + 1, p);
    u->mn = std::min(u->mn, p);
    u->mx = std::max(u->mx, p);
}
std::pair<int, int> find(Node *u, std::string &s, int i) {
    if (i == int(s.size())) {
        return {u->mn, u->mx};
    }
    int x = std::string("AGCU").find(s[i]);
    if (u->to[x] == nullptr) {
        return {-1, -1};
    }
    return find(u->to[x], s, i + 1);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m;
    std::cin >> n >> m;
    
    std::vector<std::string> s(n);
    Node *root = new Node();
    for (int i = 0; i < n; i++) {
        std::cin >> s[i];
    }
    
    std::sort(s.begin(), s.end());
    for (int i = 0; i < n; i++) {
        add(root, s[i], 0, i);
    }
    
    std::vector<std::tuple<int, int, int, int>> Q;
    std::vector<int> f;
    
    for (int i = 0; i < m; i++) {
        std::string p, q;
        std::cin >> p >> q;
        auto [l, r] = find(root, p, 0);
        if (l == -1) {
            continue;
        }
        r++;
        i64 H = 0;
        for (char c : q) {
            H *= M;
            H += std::string("AGCU").find(c) + 1;
            H %= P;
        }
        Q.emplace_back(l, H, i, -1);
        Q.emplace_back(r, H, i, 1);
        f.push_back(H);
    }
    for (int i = 0; i < n; i++) {
        i64 h = 0, pw = 1;
        for (int j = int(s[i].size()) - 1; j >= 0; j--) {
            h = (h + pw * (std::string("AGCU").find(s[i][j]) + 1)) % P;
            pw *= M;
            pw %= P;
            f.push_back(h);
        }
    }
    
    std::sort(f.begin(), f.end());
    f.erase(std::unique(f.begin(), f.end()), f.end());
    
    std::sort(Q.begin(), Q.end());
    
    std::vector<int> ans(m);
    
    std::vector<int> cnt(f.size());
    int j = 0;
    for (int i = 0; i < n; i++) {
        int p, H, id, t;
        while (j < int(Q.size())) {
            std::tie(p, H, id, t) = Q[j];
            if (p != i) {
                break;
            }
            assert(std::binary_search(f.begin(), f.end(), H));
            H = std::lower_bound(f.begin(), f.end(), H) - f.begin();
            ans[id] += t * cnt[H];
            j++;
        }
        i64 h = 0, pw = 1;
        for (int j = int(s[i].size()) - 1; j >= 0; j--) {
            h = (h + pw * (std::string("AGCU").find(s[i][j]) + 1)) % P;
            pw *= M;
            pw %= P;
            assert(std::binary_search(f.begin(), f.end(), h));
            int H = std::lower_bound(f.begin(), f.end(), h) - f.begin();
            cnt[H]++;
        }
    }
    while (j < int(Q.size())) {
        int p, H, id, t;
        std::tie(p, H, id, t) = Q[j];
        assert(std::binary_search(f.begin(), f.end(), H));
        H = std::lower_bound(f.begin(), f.end(), H) - f.begin();
        ans[id] += t * cnt[H];
        j++;
    }
    
    for (int i = 0; i < m; i++) {
        std::cout << ans[i] << "\n";
    }
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1171 ms 19180 KB Output is correct
2 Correct 1096 ms 18688 KB Output is correct
3 Correct 564 ms 106128 KB Output is correct
4 Correct 544 ms 101592 KB Output is correct
5 Correct 680 ms 71376 KB Output is correct
6 Correct 695 ms 72364 KB Output is correct
7 Correct 383 ms 16116 KB Output is correct
8 Correct 857 ms 52932 KB Output is correct
9 Correct 808 ms 46648 KB Output is correct
10 Correct 717 ms 43812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 5336 KB Output is correct
2 Correct 37 ms 3360 KB Output is correct
3 Correct 44 ms 4556 KB Output is correct
4 Correct 34 ms 4024 KB Output is correct
5 Correct 36 ms 3424 KB Output is correct
6 Correct 44 ms 4628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1171 ms 19180 KB Output is correct
9 Correct 1096 ms 18688 KB Output is correct
10 Correct 564 ms 106128 KB Output is correct
11 Correct 544 ms 101592 KB Output is correct
12 Correct 680 ms 71376 KB Output is correct
13 Correct 695 ms 72364 KB Output is correct
14 Correct 383 ms 16116 KB Output is correct
15 Correct 857 ms 52932 KB Output is correct
16 Correct 808 ms 46648 KB Output is correct
17 Correct 717 ms 43812 KB Output is correct
18 Correct 43 ms 5336 KB Output is correct
19 Correct 37 ms 3360 KB Output is correct
20 Correct 44 ms 4556 KB Output is correct
21 Correct 34 ms 4024 KB Output is correct
22 Correct 36 ms 3424 KB Output is correct
23 Correct 44 ms 4628 KB Output is correct
24 Correct 992 ms 22204 KB Output is correct
25 Correct 1002 ms 23556 KB Output is correct
26 Correct 970 ms 21752 KB Output is correct
27 Correct 503 ms 90076 KB Output is correct
28 Correct 586 ms 30272 KB Output is correct
29 Correct 214 ms 18164 KB Output is correct