Submission #839481

# Submission time Handle Problem Language Result Execution time Memory
839481 2023-08-30T06:37:54 Z CDuong Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
171 ms 194368 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;

struct node {
    int l, r;
    node *nxt[4];
    vi idx;
};

struct Op {
    int x, y, idx;
    Op(int x, int y, int idx) : x(x), y(y), idx(idx) {}

    bool operator < (const Op &o) const {
        return x < o.x;
    }
};

struct FenwickTree {
    int n;
    vi data;

    FenwickTree(int n) : n(n), data(n + 1) {}

    void update(int idx, int val) {
        while (idx <= n) {
            data[idx] += val;
            idx += idx & -idx;
        }
    }

    int get(int idx) {
        int res = 0;
        while (idx) {
            res += data[idx];
            idx -= idx & -idx;
        }
        return res;
    }
};

array<int, 2> points[mxN];
int n, q, ans[mxN], enc[256], cc;
node *root1 = new node();
node *root2 = new node();

void add(string &s, int idx) {
    node *cur = root1;
    for (char c : s) {
        int ch = enc[(int)c];
        if (!cur->nxt[ch]) {
            cur->nxt[ch] = new node();
        }
        cur = cur->nxt[ch];
    }
    cur->idx.emplace_back(idx);

    cur = root2;
    reverse(all(s));
    for (char c : s) {
        int ch = enc[(int)c];
        if (!cur->nxt[ch]) {
            cur->nxt[ch] = new node();
        }
        cur = cur->nxt[ch];
    }
    cur->idx.emplace_back(idx);
}

void dfs(node *cur, int idx) {
    cur->l = cc;
    for (int val : cur->idx)
        points[val][idx] = cc++;
    for (int i = 0; i < 4; ++i) {
        if (cur->nxt[i]) {
            dfs(cur->nxt[i], idx);
        }
    }
    cur->r = cc - 1;
}

pii get(string &s, node *cur) {
    for (char c : s) {
        int ch = enc[(int)c];
        if (!cur->nxt[ch]) {
            return pair{-1, -1};
        }
        cur = cur->nxt[ch];
    }
    return pair{cur->l, cur->r};
}

void solve() {
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
        string s; cin >> s;
        add(s, i);
    }

    cc = 1; dfs(root1, 0);
    cc = 1; dfs(root2, 1);
    sort(points + 1, points + n + 1);

    vector<Op> op;
    auto addRange = [&](int x1, int x2, int y1, int y2, int idx) -> void {
        op.emplace_back(x2, y2, idx);
        op.emplace_back(x1 - 1, y2, -idx);
        op.emplace_back(x2, y1 - 1, -idx);
        op.emplace_back(x1 - 1, y1 - 1, idx);
    };

    for (int i = 1; i <= q; ++i) {
        string pfx, sfx;
        cin >> pfx >> sfx;
        reverse(all(sfx));
        pii x = get(pfx, root1);
        pii y = get(sfx, root2);
        if (x.ff == -1 || y.ff == -1) {
            ans[i] = 0;
        }
        else {
            addRange(x.ff, x.ss, y.ff, y.ss, i);
        }
        // cout << x.ff << " " << x.ss << " " << y.ff << " " << y.ss << endl;
    }
    sort(all(op));

    FenwickTree fenw(n);
    int ptr = 1;
    for (auto &[x, y, idx] : op) {
        while (ptr <= n && points[ptr][0] <= x) {
            fenw.update(points[ptr][1], 1);
            ++ptr;
        }
        ans[abs(idx)] += fenw.get(y) * (idx > 0 ? 1 : -1);
    }

    for (int i = 1; i <= q; ++i) {
        cout << ans[i] << "\n";
    }
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    enc['A'] = 0;
    enc['C'] = 1;
    enc['G'] = 2;
    enc['U'] = 3;
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
   auto end = chrono::high_resolution_clock::now();
   cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 155088 KB Output is correct
2 Correct 117 ms 147148 KB Output is correct
3 Correct 122 ms 153208 KB Output is correct
4 Correct 115 ms 145804 KB Output is correct
5 Correct 171 ms 191500 KB Output is correct
6 Correct 166 ms 194368 KB Output is correct
7 Correct 28 ms 1264 KB Output is correct
8 Correct 127 ms 112884 KB Output is correct
9 Correct 140 ms 94780 KB Output is correct
10 Correct 85 ms 91912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 4488 KB Output is correct
2 Correct 16 ms 3032 KB Output is correct
3 Correct 23 ms 4576 KB Output is correct
4 Correct 16 ms 4044 KB Output is correct
5 Correct 17 ms 3064 KB Output is correct
6 Correct 22 ms 4600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 126 ms 155088 KB Output is correct
9 Correct 117 ms 147148 KB Output is correct
10 Correct 122 ms 153208 KB Output is correct
11 Correct 115 ms 145804 KB Output is correct
12 Correct 171 ms 191500 KB Output is correct
13 Correct 166 ms 194368 KB Output is correct
14 Correct 28 ms 1264 KB Output is correct
15 Correct 127 ms 112884 KB Output is correct
16 Correct 140 ms 94780 KB Output is correct
17 Correct 85 ms 91912 KB Output is correct
18 Correct 22 ms 4488 KB Output is correct
19 Correct 16 ms 3032 KB Output is correct
20 Correct 23 ms 4576 KB Output is correct
21 Correct 16 ms 4044 KB Output is correct
22 Correct 17 ms 3064 KB Output is correct
23 Correct 22 ms 4600 KB Output is correct
24 Correct 113 ms 128344 KB Output is correct
25 Correct 127 ms 129812 KB Output is correct
26 Correct 111 ms 126092 KB Output is correct
27 Correct 129 ms 126656 KB Output is correct
28 Correct 92 ms 25196 KB Output is correct
29 Correct 57 ms 8412 KB Output is correct