답안 #732061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
732061 2023-04-28T10:22:19 Z bgnbvnbv Selling RNA Strands (JOI16_selling_rna) C++14
100 / 100
535 ms 251172 KB
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=1e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll val[300];
struct TrieNode
{
    int child[4];
    int sz;
    int bigchild;
    vector<int>id,query;
    TrieNode()
    {
        for(int j=0;j<4;j++) child[j]=0;
        sz=0;
        bigchild=0;
    }
};
string s[maxN],a[maxN],b[maxN];
struct Trieu
{
    vector<TrieNode>trie;
    void kt()
    {
        trie.pb(TrieNode());
    }
    void add(int j)
    {
        ll p=0;
        for(int i=0;i<s[j].size();i++)
        {
            int nxt=val[s[j][i]];
            if(trie[p].child[nxt]==0)
            {
                trie[p].child[nxt]=trie.size();
                trie.pb(TrieNode());
            }
            p=trie[p].child[nxt];
            trie[p].sz++;
        }
    }
    void ers(int j)
    {
        ll p=0;
        for(int i=0;i<=s[j].size()-1;i++)
        {
            int nxt=val[s[j][i]];
            if(trie[p].child[nxt]==0)
            {
                trie[p].child[nxt]=trie.size();
                trie.pb(TrieNode());
            }
            p=trie[p].child[nxt];
            trie[p].sz--;
        }
    }
    int query(int j)
    {
        ll p=0;
        for(int i=0;i<=a[j].size()-1;i++)
        {
            int nxt=val[a[j][i]];
            if(trie[p].child[nxt]==0)
            {
                trie[p].child[nxt]=trie.size();
                trie.pb(TrieNode());
            }
            p=trie[p].child[nxt];
        }
        return trie[p].sz;
    }
}trieu;ll ans[maxN];
struct Triev
{
    vector<TrieNode>trie;
    void kt()
    {
        trie.pb(TrieNode());
    }
    void add(int j)
    {
        ll p=0;
        for(int i=s[j].size()-1;i>=0;i--)
        {
            int nxt=val[s[j][i]];
            if(trie[p].child[nxt]==0)
            {
                trie[p].child[nxt]=trie.size();
                trie.pb(TrieNode());
            }
            p=trie[p].child[nxt];
        }
        trie[p].id.push_back(j);
        trie[p].sz+=s[j].size();
    }
    void addq(int j)
    {
        ll p=0;
        for(int i=b[j].size()-1;i>=0;i--)
        {
            int nxt=val[b[j][i]];
            if(trie[p].child[nxt]==0)
            {
                trie[p].child[nxt]=trie.size();
                trie.pb(TrieNode());
            }
            p=trie[p].child[nxt];
        }
        trie[p].query.push_back(j);
    }
    void dfs_sz(int u=0)
    {
        trie[u].bigchild=-1;
        for(int i=0;i<4;i++)
        {
            int v=trie[u].child[i];
            if(v!=0)
            {
                dfs_sz(v);
                trie[u].sz+=trie[v].sz;
                int &x=trie[u].bigchild;
                if(x==-1||trie[v].sz>trie[x].sz)
                {
                    x=v;
                }
            }
        }
    }
    void dfs(int u=0,bool keep=1)
    {
        for(int i=0;i<4;i++)
        {
            int v=trie[u].child[i];
            if(v!=0&&v!=trie[u].bigchild)
            {
                dfs(v,0);
            }
        }

        if(trie[u].bigchild!=-1)
        {
            dfs(trie[u].bigchild,1);
            int x=trie[u].bigchild;
            swap(trie[u].id,trie[x].id);
            for(auto zz:trie[x].id) {trie[u].id.pb(zz),trieu.add(zz);}
        }
        else
        {
            for(auto zz:trie[u].id)
            {
                trieu.add(zz);
            }
        }
        for(int i=0;i<4;i++)
        {
            int v=trie[u].child[i];
            if(v!=0&&v!=trie[u].bigchild)
            {
                for(auto zz:trie[v].id)
                {
                    trie[u].id.pb(zz);
                    trieu.add(zz);
                }
            }
        }

        for(auto zz:trie[u].query)
        {
            ans[zz]=trieu.query(zz);
            //if(u==2) cout << zz<<' ';
        }
        if(keep==0)
        {
            for(auto zz:trie[u].id) trieu.ers(zz);
        }
    }
}trie2;
ll n,m;
void solve()
{
    cin >> n >> m;
    for(int i=1;i<=n;i++) cin >> s[i];
    val['A']=0;
    val['U']=1;
    val['G']=2;
    val['C']=3;
    trieu.kt();
    trie2.kt();
    for(int i=1;i<=m;i++)
    {
        cin >> a[i] >> b[i];
    }
    for(int i=1;i<=n;i++)
    {
        trie2.add(i);
    }
    for(int i=1;i<=m;i++)
    {
        trie2.addq(i);
    }
    trie2.dfs_sz();
    trie2.dfs();
    for(int i=1;i<=m;i++) cout << ans[i]<<'\n';
}
int main()
{
    //fastio
    //freopen(TASKNAME".INP","r",stdin);
    //freopen(TASKNAME".OUT","w",stdout);
    solve();
}

Compilation message

selling_rna.cpp: In member function 'void Trieu::add(int)':
selling_rna.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |         for(int i=0;i<s[j].size();i++)
      |                     ~^~~~~~~~~~~~
selling_rna.cpp:40:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   40 |             int nxt=val[s[j][i]];
      |                                ^
selling_rna.cpp: In member function 'void Trieu::ers(int)':
selling_rna.cpp:53:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for(int i=0;i<=s[j].size()-1;i++)
      |                     ~^~~~~~~~~~~~~~~
selling_rna.cpp:55:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   55 |             int nxt=val[s[j][i]];
      |                                ^
selling_rna.cpp: In member function 'int Trieu::query(int)':
selling_rna.cpp:68:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for(int i=0;i<=a[j].size()-1;i++)
      |                     ~^~~~~~~~~~~~~~~
selling_rna.cpp:70:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   70 |             int nxt=val[a[j][i]];
      |                                ^
selling_rna.cpp: In member function 'void Triev::add(int)':
selling_rna.cpp:93:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   93 |             int nxt=val[s[j][i]];
      |                                ^
selling_rna.cpp: In member function 'void Triev::addq(int)':
selling_rna.cpp:109:32: warning: array subscript has type 'char' [-Wchar-subscripts]
  109 |             int nxt=val[b[j][i]];
      |                                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9712 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9664 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 515 ms 167248 KB Output is correct
2 Correct 527 ms 167196 KB Output is correct
3 Correct 301 ms 168336 KB Output is correct
4 Correct 292 ms 168168 KB Output is correct
5 Correct 512 ms 249840 KB Output is correct
6 Correct 509 ms 251172 KB Output is correct
7 Correct 159 ms 23760 KB Output is correct
8 Correct 405 ms 147312 KB Output is correct
9 Correct 378 ms 139860 KB Output is correct
10 Correct 399 ms 132500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 11336 KB Output is correct
2 Correct 33 ms 11528 KB Output is correct
3 Correct 38 ms 11760 KB Output is correct
4 Correct 32 ms 11084 KB Output is correct
5 Correct 30 ms 11448 KB Output is correct
6 Correct 34 ms 11588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9712 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9664 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 515 ms 167248 KB Output is correct
9 Correct 527 ms 167196 KB Output is correct
10 Correct 301 ms 168336 KB Output is correct
11 Correct 292 ms 168168 KB Output is correct
12 Correct 512 ms 249840 KB Output is correct
13 Correct 509 ms 251172 KB Output is correct
14 Correct 159 ms 23760 KB Output is correct
15 Correct 405 ms 147312 KB Output is correct
16 Correct 378 ms 139860 KB Output is correct
17 Correct 399 ms 132500 KB Output is correct
18 Correct 33 ms 11336 KB Output is correct
19 Correct 33 ms 11528 KB Output is correct
20 Correct 38 ms 11760 KB Output is correct
21 Correct 32 ms 11084 KB Output is correct
22 Correct 30 ms 11448 KB Output is correct
23 Correct 34 ms 11588 KB Output is correct
24 Correct 519 ms 167524 KB Output is correct
25 Correct 528 ms 168020 KB Output is correct
26 Correct 535 ms 167476 KB Output is correct
27 Correct 305 ms 168728 KB Output is correct
28 Correct 401 ms 40204 KB Output is correct
29 Correct 169 ms 16068 KB Output is correct