Submission #1117978

# Submission time Handle Problem Language Result Execution time Memory
1117978 2024-11-24T14:20:08 Z Icelast Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
510 ms 497736 KB
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2e6+5, INF = 4e18+9;
struct Trie{
    struct Node{
        int child[4];
        int cnt = 0, exist = 0;
    } nodes[maxn*4];
    int cur = 0;
    Trie(){
        memset(nodes[cur].child, -1, sizeof(nodes[cur].child));
    }
    int new_node(){
        cur++;
        memset(nodes[cur].child, -1, sizeof(nodes[cur].child));
        return cur;
    }
    void add(vector<int> a){
        int pos = 0;
        for(int c : a){
            if(nodes[pos].child[c] == -1){
                nodes[pos].child[c] = new_node();
            }
            pos = nodes[pos].child[c];
            nodes[pos].cnt++;
        }
        nodes[pos].exist++;
    }
    int find(vector<int> a){
        int pos = 0;
        for(int c : a){
            if(nodes[pos].child[c] == -1){
                return -1;
            }
            pos = nodes[pos].child[c];
        }
        return pos;
    }
    vector<int> lt, rt;
    int timer;
    void dfs(){
        auto dfs = [&](auto dfs, int u) -> void{
            timer++;
            lt[u] = timer;
            for(int i = 0; i < 4; i++){
                int v = nodes[u].child[i];
                if(v == -1) continue;
                dfs(dfs, v);
            }
            rt[u] = timer;
        };
        lt.resize(cur+1);
        rt.resize(cur+1);
        timer = 0;
        dfs(dfs, 0);
    }

};
struct que{
    int l, r, id, t;
};
struct point{
    int x, y;
};
template <class T>
struct Fenwick {
    int n, log;
    vector<T> bit;

    Fenwick(int n) : n(n), log(32 - __builtin_clz(n + 1)), bit(n + 1, 0) {}

    void add(int i, T delta) {
        for (; i <= n; i += i & -i) {
            bit[i] += delta;
        }
    }

    T sum(int i) {
        T res = 0;
        for (; i > 0; i -= i & -i) {
            res += bit[i];
        }
        return res;
    }
    T sum(int l, int r) {
        T res = 0;
        return sum(r)-sum(l-1);
    }
    int kth(T k) {
        T sum = 0;
        int pos = 0;
        for (int l = log - 1; l >= 0; l--) {
            if (pos + (1 << l) <= n && sum + bit[pos + (1 << l)] <= k) {
                pos += 1 << l;
                sum += bit[pos];
            }
        }
        return pos;
    }
};
void solve(){
    int n, m;
    cin >> n >> m;
    auto conv = [&](char c) -> int{
        if(c == 'A') return 0;
        if(c == 'G') return 1;
        if(c == 'C') return 2;
        if(c == 'U') return 3;
        return -1;
    };
    Trie T1, T2;
    vector<point> a(n+1);
    for(int i = 1; i <= n; i++){
        string S;
        cin >> S;
        vector<int> s;
        for(char c : S){
            s.push_back(conv(c));
        }
        T1.add(s);
        int x = T1.find(s);
        reverse(s.begin(), s.end());
        T2.add(s);
        int y = T2.find(s);
        a[i] = {x, y};
    }
    T1.dfs();
    T2.dfs();
    for(int i = 1; i <= n; i++){
        a[i] = {T1.lt[a[i].x], T2.lt[a[i].y]};
    }
    int N = maxn;
    vector<vector<que>> sweepQ(N+1);
    vector<vector<point>> sweepA(N+1);
    vector<int> ans(m+1, 0);
    for(int i = 1; i <= n; i++){
        sweepA[a[i].x].push_back(a[i]);
    }
    for(int i = 1; i <= m; i++){
        string P, Q;
        cin >> P >> Q;
        vector<int> ap, aq;
        for(char c : P){
            ap.push_back(conv(c));
        }
        for(char c : Q){
            aq.push_back(conv(c));
        }
        reverse(aq.begin(), aq.end());
        int u1 = T1.find(ap), u2 = T2.find(aq);
        if(u1 == -1 || u2 == -1){
            ans[i] = 0;
        }else{
            sweepQ[T1.lt[u1]-1].push_back({T2.lt[u2], T2.rt[u2], i, -1});
            sweepQ[T1.rt[u1]].push_back({T2.lt[u2], T2.rt[u2], i, 1});
        }
    }
    Fenwick<int> bit(N+1);
    for(int i = 1; i <= N; i++){
        for(auto it : sweepA[i]){
            bit.add(it.y, 1);
        }
        for(auto it : sweepQ[i]){
            ans[it.id] += (ll)it.t*bit.sum(it.l, it.r);
        }
    }
    for(int i = 1; i <= m; i++){
        cout << ans[i] << "\n";
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
}

Compilation message

selling_rna.cpp: In instantiation of 'T Fenwick<T>::sum(int, int) [with T = int]':
selling_rna.cpp:166:54:   required from here
selling_rna.cpp:88:11: warning: unused variable 'res' [-Wunused-variable]
   88 |         T res = 0;
      |           ^~~
# Verdict Execution time Memory Grader output
1 Correct 377 ms 477768 KB Output is correct
2 Correct 386 ms 477804 KB Output is correct
3 Correct 366 ms 477768 KB Output is correct
4 Correct 373 ms 477768 KB Output is correct
5 Correct 383 ms 477712 KB Output is correct
6 Correct 378 ms 477772 KB Output is correct
7 Correct 376 ms 477772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 456 ms 493716 KB Output is correct
2 Correct 450 ms 493024 KB Output is correct
3 Correct 464 ms 493648 KB Output is correct
4 Correct 449 ms 492720 KB Output is correct
5 Correct 472 ms 497352 KB Output is correct
6 Correct 496 ms 497736 KB Output is correct
7 Correct 460 ms 478520 KB Output is correct
8 Correct 490 ms 489656 KB Output is correct
9 Correct 470 ms 487840 KB Output is correct
10 Correct 449 ms 487472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 409 ms 481212 KB Output is correct
2 Correct 431 ms 480596 KB Output is correct
3 Correct 421 ms 480560 KB Output is correct
4 Correct 404 ms 480112 KB Output is correct
5 Correct 406 ms 480052 KB Output is correct
6 Correct 450 ms 480584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 377 ms 477768 KB Output is correct
2 Correct 386 ms 477804 KB Output is correct
3 Correct 366 ms 477768 KB Output is correct
4 Correct 373 ms 477768 KB Output is correct
5 Correct 383 ms 477712 KB Output is correct
6 Correct 378 ms 477772 KB Output is correct
7 Correct 376 ms 477772 KB Output is correct
8 Correct 456 ms 493716 KB Output is correct
9 Correct 450 ms 493024 KB Output is correct
10 Correct 464 ms 493648 KB Output is correct
11 Correct 449 ms 492720 KB Output is correct
12 Correct 472 ms 497352 KB Output is correct
13 Correct 496 ms 497736 KB Output is correct
14 Correct 460 ms 478520 KB Output is correct
15 Correct 490 ms 489656 KB Output is correct
16 Correct 470 ms 487840 KB Output is correct
17 Correct 449 ms 487472 KB Output is correct
18 Correct 409 ms 481212 KB Output is correct
19 Correct 431 ms 480596 KB Output is correct
20 Correct 421 ms 480560 KB Output is correct
21 Correct 404 ms 480112 KB Output is correct
22 Correct 406 ms 480052 KB Output is correct
23 Correct 450 ms 480584 KB Output is correct
24 Correct 480 ms 492104 KB Output is correct
25 Correct 473 ms 493824 KB Output is correct
26 Correct 469 ms 491336 KB Output is correct
27 Correct 497 ms 491548 KB Output is correct
28 Correct 510 ms 489852 KB Output is correct
29 Correct 479 ms 484424 KB Output is correct