Submission #1287356

#TimeUsernameProblemLanguageResultExecution timeMemory
1287356quan606303Selling RNA Strands (JOI16_selling_rna)C++20
100 / 100
428 ms623624 KiB
/*
 * @Author: RMQuan 
 * @Date: 2025-11-04 15:02:12 
 * @Last Modified by: RMQuan
 * @Last Modified time: 2025-11-04 15:30:54
 */
/*idea : 



*/
#include <bits/stdc++.h>
bool M1;
#define int long long
#define ll long long
#define INTMAX INT_MAX
#define INTMIN INT_MIN
#define LONGMAX LLONG_MAX
#define LONGMIN LLONG_MIN
#define fi first
#define se second
#define memfull(a,b) memset(a,b,sizeof(a));
#define endl '\n'
#define TASK "RNA"
#define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
using namespace std;
const int MOD=1e9+7;
const int maxn=1e5+7;
const int inf=1e18;
struct node1
{
    node1 *child[26];
    int L,R;
    node1()
    {
        for (int i=0;i<26;i++)child[i]=NULL;
        L=inf;
        R=-inf;
    }
};
struct trie1
{
    node1 *root;
    trie1(int cnt)
    {
        root=new node1();
    }
    void add(string s,int idx)
    {
        node1 *p=root;
        for (auto i:s)
        {
            int index=i-'A';
            if (p->child[index]==NULL)p->child[index]=new node1();
            p=p->child[index];
            p->L=min(p->L,idx);
            p->R=max(p->R,idx);
        }
    }
    pair<int,int>  get(string s)
    {
        node1 *p=root;
        for (auto i:s)
        {
            int index=i-'A';
            if (p->child[index]==NULL)return {inf,inf};
            p=p->child[index];
        }
        return {p->L,p->R};
    }
};
struct node2
{
    node2 *child[26];
    vector<int> vt;
    node2()
    {
        for (int i=0;i<26;i++)child[i]=NULL;
        vt.clear();
    }
};
struct trie2
{
    node2 *root;
    trie2(int cnt)
    {
        root=new node2();
    }
    void add(string s,int idx)
    {
        node2 *p=root;
        for (auto i:s)
        {
            int index=i-'A';
            if (p->child[index]==NULL)p->child[index]=new node2();
            p=p->child[index];
            p->vt.push_back(idx);
        }
    }
    int get(string s,int L,int R)
    {
        node2 *p=root;
        for (auto i:s)
        {
            int index=i-'A';
            if (p->child[index]==NULL)return 0;
            p=p->child[index];
        }
      //  for (auto j:p->vt)cerr<<j<<" ";
       // cerr<<endl;
        return (upper_bound(p->vt.begin(),p->vt.end(),R))-(lower_bound(p->vt.begin(),p->vt.end(),L));
    }
};
string a[maxn];
int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    file();
    int n,m;
    cin>>n>>m;
    for (int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+1+n);
    trie1 st1(0);
    trie2 st2(0);
    for (int i=1;i<=n;i++)
    {
        st1.add(a[i],i);
      //  cerr<<a[i]<<endl;
        reverse(a[i].begin(),a[i].end());
        st2.add(a[i],i);
    }
    for (int i=1;i<=m;i++)
    {
        int L,R;
        string pre,suf;
        cin>>pre>>suf;
        reverse(suf.begin(),suf.end());
        tie(L,R)=st1.get(pre);
        if (L==inf)cout<<0<<endl;
        else
        {
            //cerr<<"get "<<L<<" "<<R<<endl;
            cout<<st2.get(suf,L,R)<<endl;
        }
    }
   
    return 0;
}

Compilation message (stderr)

selling_rna.cpp: In function 'int32_t main()':
selling_rna.cpp:25:50: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:120:5: note: in expansion of macro 'file'
  120 |     file();
      |     ^~~~
selling_rna.cpp:25:81: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 | #define file() if (fopen(TASK".inp","r")){freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout);}
      |                                                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:120:5: note: in expansion of macro 'file'
  120 |     file();
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...