Submission #839455

# Submission time Handle Problem Language Result Execution time Memory
839455 2023-08-30T05:17:37 Z socpite Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
250 ms 420072 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e6 + 5;
const int mod = 1e9 + 2207;
const int base = 31;
const int sqr = 1300;

int n, q;

string inp[maxn], P[maxn], Q[maxn];
pair<string, int> A[maxn];
vector<int> qq[maxn];
pair<int, int> pfxr[maxn], sfxr[maxn];

int FW[maxn], ans[maxn];

void add(int idx)
{
    for (idx; idx <= n; idx += idx & (-idx))
    {
        FW[idx]++;
    }
}

int gt(int idx)
{
    int re = 0;
    for (idx; idx > 0; idx -= idx & (-idx))
    {
        re += FW[idx];
    }
    return re;
}

int gt(char x)
{
    switch (x)
    {
    case 'A':
        return 0;
    case 'G':
        return 1;
    case 'C':
        return 2;
    case 'U':
        return 3;
    }
    return -1;
}

struct node
{
    int c[4] = {0, 0, 0, 0};
    int l = 0, r = 0;
    vector<int> rt = {};
} pfx[maxn];

int nodecnt = 0, dfscnt = 1;

void add(node &nd, string &str, int id, int pos)
{
    if (pos == str.size())
    {
        nd.rt.push_back(id);
        return;
    }
    if (!nd.c[gt(str[pos])])
    {
        nd.c[gt(str[pos])] = ++nodecnt;
        pfx[nodecnt] = node();
    }
    add(pfx[nd.c[gt(str[pos])]], str, id, pos + 1);
}

void dfs(node &nd, int ty)
{
    nd.l = dfscnt;
    for (int i = 0; i < nd.rt.size(); i++)
    {
        if (!ty)
            A[dfscnt].first = inp[nd.rt[i]];
        else
            A[nd.rt[i]].second = dfscnt;
        dfscnt++;
    }
    for (int i = 0; i < 4; i++)
    {
        if (nd.c[i])
            dfs(pfx[nd.c[i]], ty);
    }
    nd.r = dfscnt - 1;
}

pair<int, int> gtrng(node &nd, string &str, int pos)
{
    if (pos == str.size())
    {
        return {nd.l, nd.r};
    }
    if (!nd.c[gt(str[pos])])
        return {0, 0};
    return gtrng(pfx[nd.c[gt(str[pos])]], str, pos + 1);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        cin >> inp[i];
        add(pfx[0], inp[i], i, 0);
    }
    dfs(pfx[0], 0);
    for (int i = 1; i <= q; i++)
    {
        cin >> P[i] >> Q[i];
        pfxr[i] = gtrng(pfx[0], P[i], 0);
    }
    dfscnt = 1;
    nodecnt = 0;
    pfx[0] = node();
    for (int i = 1; i <= n; i++)
    {
        reverse(A[i].first.begin(), A[i].first.end());
        add(pfx[0], A[i].first, i, 0);
    }
    dfs(pfx[0], 1);
    for (int i = 1; i <= n; i++)
    {
        reverse(A[i].first.begin(), A[i].first.end());
        // cout << A[i].first << " " << A[i].second << "\n";
    }
    for (int i = 1; i <= q; i++)
    {
        reverse(Q[i].begin(), Q[i].end());
        sfxr[i] = gtrng(pfx[0], Q[i], 0);
        if (pfxr[i].first && sfxr[i].first)
        {
            qq[pfxr[i].first - 1].push_back(-i);
            qq[pfxr[i].second].push_back(i);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        add(A[i].second);
        for (auto v : qq[i])
        {
            if (v < 0)
            {
                ans[-v] += gt(sfxr[-v].first - 1);
                ans[-v] -= gt(sfxr[-v].second);
            }
            else
            {
                ans[v] -= gt(sfxr[v].first - 1);
                ans[v] += gt(sfxr[v].second);
            }
        }
    }
    for (int i = 1; i <= q; i++)
        cout << ans[i] << "\n";
}

Compilation message

selling_rna.cpp: In function 'void add(int)':
selling_rna.cpp:20:10: warning: statement has no effect [-Wunused-value]
   20 |     for (idx; idx <= n; idx += idx & (-idx))
      |          ^~~
selling_rna.cpp: In function 'int gt(int)':
selling_rna.cpp:29:10: warning: statement has no effect [-Wunused-value]
   29 |     for (idx; idx > 0; idx -= idx & (-idx))
      |          ^~~
selling_rna.cpp: In function 'void add(node&, std::string&, int, int)':
selling_rna.cpp:63:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     if (pos == str.size())
      |         ~~~~^~~~~~~~~~~~~
selling_rna.cpp: In function 'void dfs(node&, int)':
selling_rna.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for (int i = 0; i < nd.rt.size(); i++)
      |                     ~~^~~~~~~~~~~~~~
selling_rna.cpp: In function 'std::pair<int, int> gtrng(node&, std::string&, int)':
selling_rna.cpp:97:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     if (pos == str.size())
      |         ~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 178 ms 407388 KB Output is correct
2 Correct 159 ms 407380 KB Output is correct
3 Correct 160 ms 407356 KB Output is correct
4 Correct 162 ms 407376 KB Output is correct
5 Correct 160 ms 407332 KB Output is correct
6 Correct 155 ms 407388 KB Output is correct
7 Correct 165 ms 407368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 226 ms 413900 KB Output is correct
2 Correct 221 ms 413960 KB Output is correct
3 Correct 222 ms 414004 KB Output is correct
4 Correct 229 ms 414064 KB Output is correct
5 Correct 243 ms 411740 KB Output is correct
6 Correct 250 ms 411844 KB Output is correct
7 Correct 200 ms 414964 KB Output is correct
8 Correct 240 ms 415980 KB Output is correct
9 Correct 234 ms 416000 KB Output is correct
10 Correct 215 ms 413348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 174 ms 409596 KB Output is correct
2 Correct 170 ms 408772 KB Output is correct
3 Correct 182 ms 409324 KB Output is correct
4 Correct 173 ms 408908 KB Output is correct
5 Correct 169 ms 408760 KB Output is correct
6 Correct 171 ms 409156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 407388 KB Output is correct
2 Correct 159 ms 407380 KB Output is correct
3 Correct 160 ms 407356 KB Output is correct
4 Correct 162 ms 407376 KB Output is correct
5 Correct 160 ms 407332 KB Output is correct
6 Correct 155 ms 407388 KB Output is correct
7 Correct 165 ms 407368 KB Output is correct
8 Correct 226 ms 413900 KB Output is correct
9 Correct 221 ms 413960 KB Output is correct
10 Correct 222 ms 414004 KB Output is correct
11 Correct 229 ms 414064 KB Output is correct
12 Correct 243 ms 411740 KB Output is correct
13 Correct 250 ms 411844 KB Output is correct
14 Correct 200 ms 414964 KB Output is correct
15 Correct 240 ms 415980 KB Output is correct
16 Correct 234 ms 416000 KB Output is correct
17 Correct 215 ms 413348 KB Output is correct
18 Correct 174 ms 409596 KB Output is correct
19 Correct 170 ms 408772 KB Output is correct
20 Correct 182 ms 409324 KB Output is correct
21 Correct 173 ms 408908 KB Output is correct
22 Correct 169 ms 408760 KB Output is correct
23 Correct 171 ms 409156 KB Output is correct
24 Correct 232 ms 414876 KB Output is correct
25 Correct 232 ms 416324 KB Output is correct
26 Correct 224 ms 414412 KB Output is correct
27 Correct 230 ms 415024 KB Output is correct
28 Correct 240 ms 420072 KB Output is correct
29 Correct 205 ms 411740 KB Output is correct