Submission #1305432

#TimeUsernameProblemLanguageResultExecution timeMemory
1305432DaciejMaciejSelling RNA Strands (JOI16_selling_rna)C++20
100 / 100
354 ms450840 KiB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

template <class T>

using VV = vector<vector<T>>;

using VI = vector<int>;
using VVI = vector<vector<int>>;

using VL = vector<long long>;
using VVL = vector<vector<long long>>;

using VC = vector<char>;
using VVC = vector<vector<char>>;

using VB = vector<bool>;
using VVB = vector<vector<bool>>;

using VD = vector<double>;
using VVD = vector<vector<double>>;

using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;

using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;

#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sq(a) ((a) * (a))
#define sz(x) ((int)x.size())

#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr)
#define debug(x) cerr << #x << " = " << x << endl;

const ll MOD = 1e9 + 7;

template <class T>
inline void maxi(T &a, T b)
{
    a = max(a, b);
}

template <class T>
inline void mini(T &a, T b)
{
    a = min(a, b);
}

template <class T>
inline void addi(T &a, T b)
{
    a = (a + b) % MOD;
}

template <class T>
inline void subi(T &a, T b)
{
    a = (a - b + MOD) % MOD;
}

template <class T>
inline T add(T a, T b)
{
    return (a + b) % MOD;
}

template <class T>
inline T sub(T a, T b)
{
    return (a - b + MOD) % MOD;
}

template <class T>
inline T mul(T a, T b)
{
    return ((a % MOD) * (b % MOD)) % MOD;
}

constexpr ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }

    return res;
}

const int INF = 1e9 + 5;

struct Bit
{
    VI bit;
    int bitsz = 0;
    Bit(int _n)
    {
        bit.assign(_n + 1, 0);
        bitsz = _n;
    }

    void add(int idx, int v)
    {
        for (; idx <= bitsz; idx += idx & -idx)
        {
            bit[idx] += v;
        }
    }

    int get(int idx)
    {
        int ret = 0;
        for (; idx > 0; idx -= idx & -idx)
        {
            ret += bit[idx];
        }
        return ret;
    }
};
static const int MAX_W = 2e6 + 5;

struct Trie
{

    array<array<int, 26>, MAX_W> next;
    array<int, MAX_W> end;

    array<int, MAX_W> tin;
    array<int, MAX_W> tout;

    int triesz = 0;

    int time = 1;

    Trie()
    {
        for (auto &row : next)
            row.fill(-1);
        end.fill(-1);
    }

    int add(string_view s)
    {
        int node = 0;
        for (auto c : s)
        {
            int v = next[node][c - 'A'];
            if (v == -1)
            {
                v = ++triesz;
            }
            next[node][c - 'A'] = v;
            node = v;
        }

        end[node]++;

        return node;
    }

    void dfs(int c)
    {
        tin[c] = ++time;
        for (int i = 0; i < 26; i++)
        {
            int v = next[c][i];
            if (v != -1)
                dfs(v);
        }

        tout[c] = time;
    }
};

Trie pref, suf;
Bit bit(MAX_W);

void solve()
{
    int n, m;
    cin >> n >> m;

    VPII A(n), B(m);
    FOR(i, 0, n)
    {
        string str;
        cin >> str;

        int p = pref.add(str);
        int s = suf.add(string(RALL(str)));

        A[i] = {p, s};
    }

    FOR(i, 0, m)
    {
        string P, Q;
        cin >> P >> Q;

        int p = pref.add(P);
        int s = suf.add(string(RALL(Q)));

        B[i] = {p, s};
    }

    pref.dfs(0);
    suf.dfs(0);

    vector<array<int, 4>> events;

    FOR(i, 0, n)
    {
        auto [p, s] = A[i];
        int x = pref.tin[p];
        int y = suf.tin[s];

        events.PB({x, 1, y, -1}); // new point appears
    }

    FOR(i, 0, m)
    {
        auto [p, s] = B[i];
        int L1 = pref.tin[p];
        int R1 = pref.tout[p];

        events.PB({L1 - 1, 2, s, -(i + 1)});
        events.PB({R1, 2, s, i + 1});
    }

    sort(ALL(events));

    VI ans(m);
    for (auto [x, y, z, idx] : events)
    {
        if (y == 1)
        {
            bit.add(z, 1);
        }
        else
        {
            int md = idx > 0 ? 1 : -1;
            idx = abs(idx);

            int L2 = suf.tin[z];
            int R2 = suf.tout[z];
            ans[idx - 1] += md * (bit.get(R2) - bit.get(L2 - 1));
        }
    }

    FOR(i, 0, m)
    {
        cout << ans[i] << LINE;
    }
}

int main()
{
    fastio();

    int t = 1;
    // cin >> t;

    while (t--)
        solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...