Submission #1048159

# Submission time Handle Problem Language Result Execution time Memory
1048159 2024-08-08T03:22:35 Z vjudge1 Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
101 ms 182096 KB
#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
#define lb(s, a) lower_bound(all(s), (a)) - s.begin()
#define ii pair <int, int>
#define fi first
#define se second

using namespace std;

typedef long long ll;

const int ar = 2e6+5;
const ll mod = 1e9+7;
const int oo = 1e9;

int n, m, maxx = 0, maxy = 0;

int cs(char a) {
    if(a == 'C') return 0;
    if(a == 'U') return 1;
    if(a == 'G') return 2;
    return 3;
}

struct Trie {
    struct Node{
        int child[5];
    } nodes[ar];

    int cur;
    Trie() : cur(0) {
        memset(nodes[0].child, -1, sizeof(nodes[cur].child));
    };

    int new_node() {
        cur++;
        memset(nodes[cur].child, -1, sizeof(nodes[cur].child));
        return cur;
    }

    void add(string s) {
        int pos = 0;
        for (auto f : s) {
            int c = cs(f);
            if (nodes[pos].child[c] == -1) {
                nodes[pos].child[c] = new_node();
            }
            pos = nodes[pos].child[c];
        }
    }

    int trace(string s) {
        int pos = 0;
        for (auto f : s) {
            int c = cs(f);
            pos = nodes[pos].child[c];
        }
        return in[pos];
    }

    int in[ar], out[ar], timeDfs = 0;

    void dfs(int u) {
        in[u] = ++timeDfs;
        for(int i = 0; i < 4; ++i) if(nodes[u].child[i] != -1)
            dfs(nodes[u].child[i]);
        out[u] = timeDfs;
    }

    ii get(string s) {
        int pos = 0;
        for (auto f : s) {
            int c = cs(f);
            if (nodes[pos].child[c] == -1)
                return {-1, -1};
            pos = nodes[pos].child[c];
        }
        return {in[pos], out[pos]};
    }
} t1, t2;

int bit[ar], res[ar], L[ar];
vector <int> x[ar];
struct Query {
    int l, r, i;
    bool t;
};
vector <Query> query[ar];
string s[100005];

void update(int i, int val) {
    for(; i <= maxy; i += i & -i)
        bit[i] += val;
}

int get(int i) {
    if(i == 0) return 0;
    int res = 0;
    for(; i > 0; i -= i & -i)
        res += bit[i];
    return res;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define task "b"
    if(fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) {
        cin >> s[i];
        t1.add(s[i]);
        string tmp = s[i];
        reverse(all(tmp));
        t2.add(tmp);
    }

    t1.dfs(0);
    t2.dfs(0);

    for(int i = 1; i <= n; ++i) {
        string tmp = s[i];
        reverse(all(tmp));
        int tmp1 = t1.trace(s[i]), tmp2 = t2.trace(tmp);
        x[tmp1].push_back(tmp2);
//        cout << i << ' ' << tmp1 << ' ' << tmp2 << '\n';
    }

    string p, q;
    for(int i = 1; i <= m; ++i) {
        cin >> p >> q;
        ii p1 = t1.get(p);
        if(p1.fi == -1) continue;
        reverse(all(q));
        ii p2 = t2.get(q);
        if(p2.fi == -1) continue;
//        cout << i << ' ' << p1.fi << ' ' << p1.se << ' ' << p2.fi << ' ' << p2.se << '\n';
//        if(p1.fi > p2.fi) swap(p1.fi, p2.fi);
//        if(p1.se > p2.se) swap(p2.se, p1.se);

        query[p1.fi - 1].push_back({p2.fi, p2.se, i, 0});
        query[p1.se].push_back({p2.fi, p2.se, i, 1});
        maxx = max({maxx, p1.se});
        maxy = max({maxy, p2.se});
    }

    for(int i = 1; i <= maxx; ++i) {
        for(auto y : x[i])
            update(y, 1);
        for(auto [l, r, j, t] : query[i]) {
            int tmp = get(r) - get(l - 1);
            if(t == 0) L[j] = tmp;
            else res[j] = tmp - L[j];
        }
    }
    for(int i = 1; i <= m; ++i)
        cout << res[i] << '\n';
}

Compilation message

selling_rna.cpp: In function 'int main()':
selling_rna.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 113496 KB Output is correct
2 Correct 14 ms 113504 KB Output is correct
3 Correct 14 ms 113504 KB Output is correct
4 Correct 14 ms 113388 KB Output is correct
5 Correct 15 ms 113492 KB Output is correct
6 Correct 15 ms 113496 KB Output is correct
7 Correct 15 ms 113500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 170836 KB Output is correct
2 Correct 71 ms 171344 KB Output is correct
3 Correct 69 ms 165204 KB Output is correct
4 Correct 66 ms 163156 KB Output is correct
5 Correct 97 ms 180304 KB Output is correct
6 Correct 101 ms 182096 KB Output is correct
7 Correct 45 ms 115280 KB Output is correct
8 Correct 72 ms 157520 KB Output is correct
9 Correct 68 ms 150984 KB Output is correct
10 Correct 54 ms 148852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 115912 KB Output is correct
2 Correct 25 ms 115292 KB Output is correct
3 Correct 26 ms 117692 KB Output is correct
4 Correct 23 ms 115156 KB Output is correct
5 Correct 23 ms 115020 KB Output is correct
6 Correct 26 ms 115292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 113496 KB Output is correct
2 Correct 14 ms 113504 KB Output is correct
3 Correct 14 ms 113504 KB Output is correct
4 Correct 14 ms 113388 KB Output is correct
5 Correct 15 ms 113492 KB Output is correct
6 Correct 15 ms 113496 KB Output is correct
7 Correct 15 ms 113500 KB Output is correct
8 Correct 75 ms 170836 KB Output is correct
9 Correct 71 ms 171344 KB Output is correct
10 Correct 69 ms 165204 KB Output is correct
11 Correct 66 ms 163156 KB Output is correct
12 Correct 97 ms 180304 KB Output is correct
13 Correct 101 ms 182096 KB Output is correct
14 Correct 45 ms 115280 KB Output is correct
15 Correct 72 ms 157520 KB Output is correct
16 Correct 68 ms 150984 KB Output is correct
17 Correct 54 ms 148852 KB Output is correct
18 Correct 32 ms 115912 KB Output is correct
19 Correct 25 ms 115292 KB Output is correct
20 Correct 26 ms 117692 KB Output is correct
21 Correct 23 ms 115156 KB Output is correct
22 Correct 23 ms 115020 KB Output is correct
23 Correct 26 ms 115292 KB Output is correct
24 Correct 64 ms 166156 KB Output is correct
25 Correct 70 ms 167768 KB Output is correct
26 Correct 62 ms 165460 KB Output is correct
27 Correct 66 ms 159824 KB Output is correct
28 Correct 81 ms 128684 KB Output is correct
29 Correct 51 ms 118612 KB Output is correct