Submission #1091654

# Submission time Handle Problem Language Result Execution time Memory
1091654 2024-09-21T17:23:04 Z DucNguyen2007 Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
502 ms 595536 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e5+5,BLOCK=320;
const ll inf=2e18;
int n,m,ans[maxN+1];
struct query
{
    int l,r,id;
}p[maxN+1];
string a[maxN+1],q[maxN+1],b[maxN+1];
struct Trie
{
    struct Node
    {
        Node *child[26];
        int cnt,l,r;
        Node()
        {
            for(int i=0;i<26;i++)
            {
                child[i]=NULL;
            }
            cnt=l=r=0;
        }
    };
    Node root;
    void add(string s,int id)
    {
        Node *cur=&root;
        for(auto c:s)
        {
            int d=(c-'A');
            if(!(cur->child[d]))
            {
                cur->child[d]=new Node();
            }
            cur=cur->child[d];
            if(!cur->l)
            {
                cur->l=id;
            }
            cur->r=id;
            cur->cnt++;
        }
    }
    void del(string s)
    {
        Node *cur=&root;
        for(auto c:s)
        {
            int d=(c-'A');
            cur=cur->child[d];
            cur->cnt--;
        }
    }
    pii walk(string s)
    {
        Node *cur=&root;
        for(auto c:s)
        {
            int d=(c-'A');
            if(!(cur->child[d]))
            {
                return {-1,-1};
            }
            cur=cur->child[d];
        }
        return {cur->l,cur->r};
    }
    int Walk(string s)
    {
        Node *cur=&root;
        for(auto c:s)
        {
            int d=(c-'A');
            if(!(cur->child[d]))
            {
                return 0;
            }
            cur=cur->child[d];
        }
        return cur->cnt;
    }
}f,f1;
int main()
{
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        f.add(a[i],i);
        //cout<<a[i]<<'\n';
        b[i]=a[i];
        reverse(b[i].begin(),b[i].end());
    }
    memset(ans,0,sizeof(ans));
    for(int i=1;i<=m;i++)
    {
        string d;
        cin>>d>>q[i];
        reverse(q[i].begin(),q[i].end());
        pii tmp=f.walk(d);
        p[i].l=tmp.fi;
        p[i].r=tmp.se;
        p[i].id=i;
    }
    sort(p+1,p+m+1,[&](query x,query y)
    {
        if(x.r/BLOCK==y.r/BLOCK)
        {
            return x.l<y.l;
        }
        return x.r/BLOCK<y.r/BLOCK;
    });
    int lo=1,hi=0;
    for(int i=1;i<=m;i++)
    {
        int l=p[i].l,r=p[i].r;
        if(l==-1&&r==-1)
        {
            continue;
        }
        //cout<<l<<" "<<r<<'\n';
        while(hi<r)
        {
            hi++;
            f1.add(b[hi],i);
            //cout<<b[hi]<<'\n';
        }
        while(lo>l)
        {
            lo--;
            f1.add(b[lo],i);
        }
        while(hi>r)
        {
            f1.del(b[hi]);
            hi--;
        }
        while(lo<l)
        {
            f1.del(b[lo]);
            lo++;
        }
        //cout<<q[p[i].id]<<'\n';
        ans[p[i].id]=f1.Walk(q[p[i].id]);
    }
    for(int i=1;i<=m;i++)
    {
        cout<<ans[i]<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10072 KB Output is correct
2 Correct 4 ms 10064 KB Output is correct
3 Correct 4 ms 10076 KB Output is correct
4 Correct 4 ms 10076 KB Output is correct
5 Correct 4 ms 10188 KB Output is correct
6 Correct 4 ms 10156 KB Output is correct
7 Correct 4 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 457 ms 481172 KB Output is correct
2 Correct 405 ms 456276 KB Output is correct
3 Correct 326 ms 476448 KB Output is correct
4 Correct 304 ms 454336 KB Output is correct
5 Correct 502 ms 587132 KB Output is correct
6 Correct 493 ms 595536 KB Output is correct
7 Correct 51 ms 21588 KB Output is correct
8 Correct 240 ms 357420 KB Output is correct
9 Correct 191 ms 302932 KB Output is correct
10 Correct 151 ms 291144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 11356 KB Output is correct
2 Correct 22 ms 12248 KB Output is correct
3 Correct 21 ms 11864 KB Output is correct
4 Correct 15 ms 11060 KB Output is correct
5 Correct 18 ms 12160 KB Output is correct
6 Correct 22 ms 11868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10072 KB Output is correct
2 Correct 4 ms 10064 KB Output is correct
3 Correct 4 ms 10076 KB Output is correct
4 Correct 4 ms 10076 KB Output is correct
5 Correct 4 ms 10188 KB Output is correct
6 Correct 4 ms 10156 KB Output is correct
7 Correct 4 ms 10076 KB Output is correct
8 Correct 457 ms 481172 KB Output is correct
9 Correct 405 ms 456276 KB Output is correct
10 Correct 326 ms 476448 KB Output is correct
11 Correct 304 ms 454336 KB Output is correct
12 Correct 502 ms 587132 KB Output is correct
13 Correct 493 ms 595536 KB Output is correct
14 Correct 51 ms 21588 KB Output is correct
15 Correct 240 ms 357420 KB Output is correct
16 Correct 191 ms 302932 KB Output is correct
17 Correct 151 ms 291144 KB Output is correct
18 Correct 21 ms 11356 KB Output is correct
19 Correct 22 ms 12248 KB Output is correct
20 Correct 21 ms 11864 KB Output is correct
21 Correct 15 ms 11060 KB Output is correct
22 Correct 18 ms 12160 KB Output is correct
23 Correct 22 ms 11868 KB Output is correct
24 Correct 278 ms 396116 KB Output is correct
25 Correct 227 ms 396480 KB Output is correct
26 Correct 349 ms 391324 KB Output is correct
27 Correct 232 ms 393376 KB Output is correct
28 Correct 112 ms 70160 KB Output is correct
29 Correct 56 ms 15188 KB Output is correct