Submission #241578

# Submission time Handle Problem Language Result Execution time Memory
241578 2020-06-24T14:24:11 Z AM1648 Selling RNA Strands (JOI16_selling_rna) C++17
10 / 100
149 ms 72440 KB
/* be name khoda */

// #define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <set>
#include <map>
using namespace std;

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl

const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;

#define XL (x << 1)
#define XR (XL | 1)
#define MD ((l + r) >> 1)
#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

// -----------------------------------------------------------------------

const ll maxn = 100010;
const ll maxm = 2000010;

ll n, Q, nn;
vi T[maxn], P[maxn], S[maxn];
ll mapp[128];
ll trie[maxm][4], node[maxn];
ll st[maxm], ft[maxm], cnt[maxm];
pii stnode[maxn];
ll res[maxn];
vpii qs[maxn];

void inp(vi &o) {
    string s; cin >> s;
    o.resize(s.size());
    fori (i, s.size()) {
        o[i] = mapp[s[i]];
    }
}

ll tim = 0;
void dfs(ll x) {
    st[x] = tim++;
    fori (i, 4) if (trie[x][i]) {
        dfs(trie[x][i]);
    }
    ft[x] = tim;
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    mapp['A'] = 0;
    mapp['U'] = 1;
    mapp['G'] = 2;
    mapp['C'] = 3;

    cin >> n >> Q;
    fori (i, n) inp(T[i]);
    fori (i, Q) {
        inp(P[i]);
        inp(S[i]);
    }
    nn = 1;
    fori (i, n) {
        ll x = 0;
        for (auto c : T[i]) {
            if (!trie[x][c]) trie[x][c] = nn++;
            x = trie[x][c];
        }
        node[i] = x;
    }
    dfs(0);
    fori (i, n) {
        stnode[i] = {st[node[i]], i};
    }
    sort(stnode, stnode + n);
    fori (i, Q) {
        ll x = 0;
        for (auto c : P[i]) {
            if (!trie[x][c]) {
                x = -1;
                break;
            }
            x = trie[x][c];
        }
        if (x != -1) {
            ll stid = lower_bound(stnode, stnode + n, make_pair(st[x], -1)) - stnode;
            ll ftid = lower_bound(stnode, stnode + n, make_pair(ft[x], -1)) - stnode;
            qs[stid].eb(i, -1);
            qs[ftid].eb(i, 1);
        }
    }
    nn = 1;
    memset(trie, 0, sizeof trie);
    fori (i, n) reverse(all(S[i]));
    fori (i, Q) reverse(all(T[i]));
    fori (i, n + 1) {
        for (auto q : qs[i]) {
            ll x = 0;
            for (auto c : S[q.F]) {
                if (!trie[x][c]) {
                    x = -1;
                    break;
                }
                x = trie[x][c];
            }
            if (x != -1) res[q.F] += cnt[x] * q.S;
        }
        if (i == n) break;
        ll x = 0;
        ll ti = stnode[i].S;
        for (auto c : T[ti]) {
            if (!trie[x][c]) trie[x][c] = nn++;
            x = trie[x][c];
            ++cnt[x];
        }
    }
    fori (i, Q) cout << res[i] << '\n';

}

Compilation message

selling_rna.cpp: In function 'void inp(vi&)':
selling_rna.cpp:26:44: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define forifrom(i, s, n) for (ll i = s; i < n; ++i)
                                            ^
selling_rna.cpp:28:20: note: in expansion of macro 'forifrom'
 #define fori(i, n) forifrom (i, 0, n)
                    ^~~~~~~~
selling_rna.cpp:73:5: note: in expansion of macro 'fori'
     fori (i, s.size()) {
     ^~~~
selling_rna.cpp:74:25: warning: array subscript has type 'char' [-Wchar-subscripts]
         o[i] = mapp[s[i]];
                         ^
# Verdict Execution time Memory Grader output
1 Correct 27 ms 41088 KB Output is correct
2 Correct 28 ms 41088 KB Output is correct
3 Correct 28 ms 41208 KB Output is correct
4 Correct 29 ms 41088 KB Output is correct
5 Correct 28 ms 41088 KB Output is correct
6 Correct 27 ms 41088 KB Output is correct
7 Correct 27 ms 41088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 64892 KB Output is correct
2 Correct 88 ms 64632 KB Output is correct
3 Incorrect 149 ms 72440 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 65 ms 47956 KB Output is correct
2 Correct 52 ms 45560 KB Output is correct
3 Correct 57 ms 46576 KB Output is correct
4 Incorrect 51 ms 45688 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 41088 KB Output is correct
2 Correct 28 ms 41088 KB Output is correct
3 Correct 28 ms 41208 KB Output is correct
4 Correct 29 ms 41088 KB Output is correct
5 Correct 28 ms 41088 KB Output is correct
6 Correct 27 ms 41088 KB Output is correct
7 Correct 27 ms 41088 KB Output is correct
8 Correct 78 ms 64892 KB Output is correct
9 Correct 88 ms 64632 KB Output is correct
10 Incorrect 149 ms 72440 KB Output isn't correct
11 Halted 0 ms 0 KB -