제출 #1336726

#제출 시각아이디문제언어결과실행 시간메모리
1336726iamhereforfunSelling RNA Strands (JOI16_selling_rna)C++20
10 / 100
1173 ms114744 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 1e5 + 5;
const int M = 2e6 + 5;
const int K = 1e3 + 5;
const int LG = 20;
const int INF = 1e9 + 5;
const int B = 1000;
const int MOD = 1e9 + 7;

struct Fenwick
{
    vector<int> ft;
    int n;
    Fenwick(int len)
    {
        n = len;
        ft.assign(n + 1, 0);
    }
    void update(int pos, int val)
    {
        while (pos <= n)
        {
            ft[pos] += val;
            pos += LSOne(pos);
        }
    }
    int get(int pos)
    {
        int sum = 0;
        while (pos > 0)
        {
            sum += ft[pos];
            pos -= LSOne(pos);
        }
        return sum;
    }
    int get(int l, int r)
    {
        return get(r) - get(l - 1);
    }
};

struct Node
{
    Node *child[4];
    int st, en;
    Node()
    {
        child[0] = child[1] = child[2] = child[3] = NULL;
        st = en = 0;
    }
} *root;

int n, m, cid, i, ans[N];
string s[N], p[N], q[N];
array<int, 4> query[N];
pair<int, int> points[N];
vector<array<int, 4>> thing;

inline void add(string s, Node *root)
{
    Node *p = root;
    for (char &h : s)
    {
        int c = h - '0';
        if (!p->child[c])
        {
            p->child[c] = new Node();
        }
        p = p->child[c];
    }
}

inline void setQuery(string s, int id, Node *root)
{
    Node *p = root;
    for (char &h : s)
    {
        int c = h - '0';
        if (!p->child[c])
        {
            // cout << "BRUH\n";
            ans[id + 1] = -INF;
            return;
        }
        p = p->child[c];
    }
    // cout << p->st << " " << p->en << " " << s << "v\n";
    if (i == 0)
    {
        query[id][0] = p->st;
        query[id][2] = p->en;
    }
    else
    {
        query[id][1] = p->st;
        query[id][3] = p->en;
    }
}

inline void setPoint(string s, int id, Node *root)
{
    Node *p = root;
    for (char &h : s)
    {
        int c = h - '0';
        if (!p->child[c])
        {
            return;
        }
        p = p->child[c];
    }
    if (i == 0)
    {
        points[id].first = p->st;
    }
    else
    {
        points[id].second = p->en;
    }
}

inline void dfs(Node *p)
{
    p->st = cid++;
    for (int x = 0; x < 4; x++)
    {
        if (!p->child[x])
            continue;
        dfs(p->child[x]);
    }
    p->en = cid++;
}

bool cmp(array<int, 4> a, array<int, 4> b)
{
    if (a[0] != b[0])
    {
        return a[0] < b[0];
    }
    if (a[3] == 0)
        return true;
    if (b[3] == 0)
        return false;
    return a < b;
}

inline void solve()
{
    cin >> n >> m;
    for (int x = 0; x < n; x++)
    {
        cin >> s[x];
        for (char &c : s[x])
        {
            if (c == 'A')
                c = '0';
            if (c == 'G')
                c = '1';
            if (c == 'C')
                c = '2';
            if (c == 'U')
                c = '3';
        }
    }
    for (int x = 0; x < m; x++)
    {
        cin >> p[x] >> q[x];
        for (char &c : p[x])
        {
            if (c == 'A')
                c = '0';
            if (c == 'G')
                c = '1';
            if (c == 'C')
                c = '2';
            if (c == 'U')
                c = '3';
        }
        for (char &c : q[x])
        {
            if (c == 'A')
                c = '0';
            if (c == 'G')
                c = '1';
            if (c == 'C')
                c = '2';
            if (c == 'U')
                c = '3';
        }
    }
    root = new Node();
    i = 0;
    cid = 1;
    for (int x = 0; x < n; x++)
    {
        add(s[x], root);
    }
    dfs(root);
    for (int x = 0; x < m; x++)
    {
        setQuery(p[x], x, root);
    }
    for (int x = 0; x < n; x++)
    {
        setPoint(s[x], x, root);
    }
    root = new Node();
    i = 1;
    cid = 1;
    for (int x = 0; x < n; x++)
    {
        reverse(s[x].begin(), s[x].end());
        add(s[x], root);
    }
    dfs(root);
    for (int x = 0; x < m; x++)
    {
        reverse(q[x].begin(), q[x].end());
        setQuery(q[x], x, root);
    }
    for (int x = 0; x < n; x++)
    {
        setPoint(s[x], x, root);
    }
    for (int x = 0; x < n; x++)
    {
        thing.push_back({points[x].second, points[x].first, 0, 0});
    }
    for (int x = 0; x < m; x++)
    {
        thing.push_back({query[x][1] - 1, query[x][0], query[x][2], -(x + 1)});
        thing.push_back({query[x][3], query[x][0], query[x][2], (x + 1)});
    }
    sort(thing.begin(), thing.end(), cmp);
    Fenwick ft(M);
    for (auto &[a, b, c, t] : thing)
    {
        if (t == 0)
        {
            // cout << b << "\n";
            ft.update(b, 1);
        }
        else
        {
            // cout << b << " " << c << "\n";
            if (t < 0)
            {
                if (ans[-t] == -INF)
                    continue;
                ans[-t] -= ft.get(b, c);
            }
            else
            {
                if (ans[t] == -INF)
                    continue;
                ans[t] += ft.get(b, c);
            }
        }
    }
    for (int x = 1; x <= m; x++)
    {
        if (ans[x] == -INF)
            ans[x] = 0;
        cout << ans[x] << "\n";
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...