Submission #678149

# Submission time Handle Problem Language Result Execution time Memory
678149 2023-01-05T08:54:45 Z Tien_Noob Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
325 ms 300344 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5, block = 330, M = 2e6;
vector<int> pre[M + 1], suf[M + 1], adj[M + 1], ans[M + 1];
bool built[M + 1];
bitset<N + 1> visited;
struct Trie
{
    struct node
    {
        int child[26];
        node()
        {
            memset(child, -1, sizeof(child));
        }
    };
    vector<node> tree;
    void new_node()
    {
        node tmp;
        tree.push_back(tmp);
    }
    void reset()
    {
        tree.clear();
        new_node();
    }
    int add_query(string &s)
    {
        int id = 0;
        for (char c : s)
        {
            int w = c - 'A';
            if (tree[id].child[w] == -1)
            {
                tree[id].child[w] = tree.size();
                new_node();
            }
            id = tree[id].child[w];
        }
        return id;
    }
    void add_pattern(int i, int t, string &s)
    {
        int id = 0;
        for (char c : s)
        {
            int w = c - 'A';
            if (tree[id].child[w] == -1)
            {
                return ;
            }
            id = tree[id].child[w];
            if (t)
            {
                suf[id].push_back(i);
            }
            else
            {
                pre[id].push_back(i);
            }
        }
    }
} tree;
void build(int u)
{
    built[u] = true;
    visited.reset();
    sort(adj[u].begin(), adj[u].end());
    adj[u].erase(unique(adj[u].begin(), adj[u].end()), adj[u].end());
    for (int i : pre[u])
    {
        visited[i] = true;
    }
    for (int v : adj[u])
    {
        int res = 0;
        for (int i : suf[v])
        {
            res += visited[i];
        }
        ans[u].push_back(res);
    }
}
string s[N + 1], p[N + 1], q[N + 1];
int n, m, a[N + 1], b[N + 1];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> s[i];
    }
    for (int i = 1; i <= m; ++ i)
    {
        cin >> p[i] >> q[i];
    }
}
void solve()
{
    tree.reset();
    for (int i = 1; i <= m; ++ i)
    {
        a[i] = tree.add_query(p[i]);
        //cerr << a[i] << '\n';
    }
    for (int i = 1; i <= n; ++ i)
    {
        tree.add_pattern(i, 0, s[i]);
        reverse(s[i].begin(), s[i].end());
    }
    tree.reset();
    for (int i = 1; i <= m; ++ i)
    {
        reverse(q[i].begin(), q[i].end());
        b[i] = tree.add_query(q[i]);
    }
    for (int i = 1; i <= n; ++ i)
    {
        tree.add_pattern(i, 1, s[i]);
    }
    for (int i = 1; i <= m; ++ i)
    {
        adj[a[i]].push_back(b[i]);
        //cerr << a[i] << ' ' << b[i] << '\n';
    }
    for (int i = 1; i <= m; ++ i)
    {
        if (pre[a[i]].size() <= block)
        {
            int res = 0;
            for (int u : pre[a[i]])
            {
                res += binary_search(suf[b[i]].begin(), suf[b[i]].end(), u);
            }
            cout << res << '\n';
        }
        else
        {
            if (!built[a[i]])
            {
                build(a[i]);
            }
            int j = lower_bound(adj[a[i]].begin(), adj[a[i]].end(), b[i]) - adj[a[i]].begin();
            cout << ans[a[i]][j] << '\n';
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case #" << __ << ":" << '\n';
        read();
        solve();
    }
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 95 ms 197596 KB Output is correct
2 Correct 105 ms 197512 KB Output is correct
3 Correct 93 ms 197492 KB Output is correct
4 Correct 140 ms 197596 KB Output is correct
5 Correct 109 ms 197592 KB Output is correct
6 Correct 101 ms 197572 KB Output is correct
7 Correct 103 ms 197548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 165 ms 217296 KB Output is correct
2 Correct 149 ms 216732 KB Output is correct
3 Correct 157 ms 215384 KB Output is correct
4 Correct 171 ms 216548 KB Output is correct
5 Correct 234 ms 278100 KB Output is correct
6 Correct 206 ms 276892 KB Output is correct
7 Correct 173 ms 225024 KB Output is correct
8 Correct 317 ms 300344 KB Output is correct
9 Correct 325 ms 292344 KB Output is correct
10 Correct 156 ms 215932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 199948 KB Output is correct
2 Correct 118 ms 199148 KB Output is correct
3 Correct 110 ms 199404 KB Output is correct
4 Correct 119 ms 199600 KB Output is correct
5 Correct 303 ms 199148 KB Output is correct
6 Correct 225 ms 199476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 197596 KB Output is correct
2 Correct 105 ms 197512 KB Output is correct
3 Correct 93 ms 197492 KB Output is correct
4 Correct 140 ms 197596 KB Output is correct
5 Correct 109 ms 197592 KB Output is correct
6 Correct 101 ms 197572 KB Output is correct
7 Correct 103 ms 197548 KB Output is correct
8 Correct 165 ms 217296 KB Output is correct
9 Correct 149 ms 216732 KB Output is correct
10 Correct 157 ms 215384 KB Output is correct
11 Correct 171 ms 216548 KB Output is correct
12 Correct 234 ms 278100 KB Output is correct
13 Correct 206 ms 276892 KB Output is correct
14 Correct 173 ms 225024 KB Output is correct
15 Correct 317 ms 300344 KB Output is correct
16 Correct 325 ms 292344 KB Output is correct
17 Correct 156 ms 215932 KB Output is correct
18 Correct 108 ms 199948 KB Output is correct
19 Correct 118 ms 199148 KB Output is correct
20 Correct 110 ms 199404 KB Output is correct
21 Correct 119 ms 199600 KB Output is correct
22 Correct 303 ms 199148 KB Output is correct
23 Correct 225 ms 199476 KB Output is correct
24 Correct 150 ms 213824 KB Output is correct
25 Correct 146 ms 211564 KB Output is correct
26 Correct 156 ms 216884 KB Output is correct
27 Correct 230 ms 214000 KB Output is correct
28 Correct 200 ms 218512 KB Output is correct
29 Correct 152 ms 212192 KB Output is correct