Submission #1106552

# Submission time Handle Problem Language Result Execution time Memory
1106552 2024-10-30T16:11:34 Z duyhoanho Selling RNA Strands (JOI16_selling_rna) C++17
35 / 100
555 ms 1048576 KB
#include<bits/stdc++.h>
#define forinc(i,a,b) for(int i=a;i<=b;i++)
#define fordec(i,a,b) for(int i=a;i>=b;i--)
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 6e6+10;
int n,m;
string a[N];
struct trie
{
    int child[26];
    int exist,cnt;
    int l,r;
} node[N];
int cur;
void st()
{
    memset(node[0].child , -1 ,sizeof node[0].child);
    node[0].cnt = node[0].exist = node[0].l = node[0].r   = 0 ;
}
int new_(int i)
{
    cur++;
    memset(node[cur].child , -1 ,sizeof node[cur].child);
    node[cur].cnt = node[cur].exist  = 0 ;
    node[cur].l = node[cur].r = i;
    return cur;
}
void ad(string s,int i)
{
    int pos = 0;
    for(auto f : s)
    {
        int c = f - 'A';
        if(node[pos].child[c] == -1) node[pos].child[c] = new_(i);
        pos = node[pos].child[c];
        node[pos].r = i;
    }
}
struct tir
{
    int chil[26];
    vector<int> v;
}no[N];
int cur2;
void st2()
{
    memset(no[0].chil , -1 , sizeof no[0].chil);
    no[0].v.clear();
}
int new_2(int i)
{
    cur2++;
    memset(no[cur2].chil , -1 , sizeof no[cur2].chil);
    no[cur2].v.clear();
    return cur2;
}
void ad2(string s , int i)
{
    int pos = 0;
    for(auto f : s)
    {
        int c = f - 'A';
        if(no[pos].chil[c] == -1) no[pos].chil[c] = new_2(i);
        pos = no[pos].chil[c];
        no[pos].v.pb(i);
    }
}
void fin(string s , int &l , int &r)
{
    int pos = 0;
    for(auto f : s)
    {
        int c = f - 'A';
        if(node[pos].child[c] == -1)
        {
            l = -1;
            r = -1;
            return;
        }
        pos = node[pos].child[c];
    }
     l = node[pos].l ;
     r = node[pos].r ;
}
void fin2(string s , int l , int r)
{
    int pos = 0;
    for(auto f : s)
    {
        int c = f - 'A';
        if(no[pos].chil[c] == -1)
        {
            cout<<0<<"\n";
            return;
        }
        pos = no[pos].chil[c];
    }
    int ll = lower_bound(no[pos].v.begin() , no[pos].v.end() , l) - no[pos].v.begin();
    int rr = upper_bound(no[pos].v.begin() , no[pos].v.end() , r) - no[pos].v.begin();
    cout<<rr - ll<<"\n";
}
int32_t main()
{
    #define task "task"
    if(fopen(task".inp","r"))
    {
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    forinc(i,1,n) cin>>a[i];
    sort(a+1,a+n+1);
    st();
    forinc(i,1,n) ad(a[i],i);
    st2();
    forinc(i,1,n)
    {
        string s = a[i];
        reverse(s.begin() , s.end());
        ad2(s,i);
    }
    forinc(i,0,cur2 - 1)
    {
        sort(no[i].v.begin() , no[i].v.end());
    }
    forinc(i,1,m)
    {
        string s1,s2;
        cin>>s1>>s2;
        reverse(s2.begin(),s2.end());
        int l,r;
        fin(s1,l,r);
        if(l == -1)
        {
            cout<<0<<"\n";
            continue;
        }
        fin2(s2,l,r);
    }

}

Compilation message

selling_rna.cpp: In function 'int32_t main()':
selling_rna.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         freopen(task".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 377 ms 940616 KB Output is correct
2 Correct 380 ms 940572 KB Output is correct
3 Correct 392 ms 940616 KB Output is correct
4 Correct 415 ms 940628 KB Output is correct
5 Correct 414 ms 940616 KB Output is correct
6 Correct 383 ms 940516 KB Output is correct
7 Correct 394 ms 940616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 555 ms 1005128 KB Output is correct
2 Correct 536 ms 1002080 KB Output is correct
3 Runtime error 446 ms 1048576 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 421 ms 941584 KB Output is correct
2 Correct 430 ms 941384 KB Output is correct
3 Correct 451 ms 941564 KB Output is correct
4 Correct 418 ms 941284 KB Output is correct
5 Correct 412 ms 941860 KB Output is correct
6 Correct 431 ms 941560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 377 ms 940616 KB Output is correct
2 Correct 380 ms 940572 KB Output is correct
3 Correct 392 ms 940616 KB Output is correct
4 Correct 415 ms 940628 KB Output is correct
5 Correct 414 ms 940616 KB Output is correct
6 Correct 383 ms 940516 KB Output is correct
7 Correct 394 ms 940616 KB Output is correct
8 Correct 555 ms 1005128 KB Output is correct
9 Correct 536 ms 1002080 KB Output is correct
10 Runtime error 446 ms 1048576 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -