Submission #430944

# Submission time Handle Problem Language Result Execution time Memory
430944 2021-06-17T08:14:19 Z MarcoMeijer Selling RNA Strands (JOI16_selling_rna) C++14
35 / 100
394 ms 313300 KB
#include <bits/stdc++.h>
using namespace std;
 
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
 
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
 
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
 
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
 
void program();
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    program();
}
 

// mod library
ll MOD=1e9+7;

inline ll mod(ll x_) {
    return (x_)%MOD;
}
ll modpow(ll x_, ll N_) {
    if(N_ == 0) return 1;
    ll a = modpow(x_,N_/2);
    a = (a*a)%MOD;
    if(N_%2) a = (a*x_)%MOD;
    return a;
}
ll inv(ll x_) {
    return modpow(x_, MOD-2);
}
class mi {
public:
    mi(ll v=0) {value = v;}
    mi  operator+ (ll rs) {return mod(value+rs);}
    mi  operator- (ll rs) {return mod(value-rs+MOD);}
    mi  operator* (ll rs) {return mod(value*rs);}
    mi  operator/ (ll rs) {return mod(value*inv(rs));}
    mi& operator+=(ll rs) {*this = (*this)+rs; return *this;}
    mi& operator-=(ll rs) {*this = (*this)-rs; return *this;}
    mi& operator*=(ll rs) {*this = (*this)*rs; return *this;}
    mi& operator/=(ll rs) {*this = (*this)/rs; return *this;}
    operator ll&() {return value;}

    ll value;
};
typedef vector<mi> vmi;

//===================//
//   begin program   //
//===================//
 
const int MX = 2e5;
const int N = (1<<22);

// debug
void printString(string s) {
    FOR(c,s) c+='a';
    OUTL(s);
}

int n, m;
string s[MX], p[MX], q[MX];

// hashing
mi hsh[4];
mi hashString(string s) {
    mi result = 0;
    FOR(c,s)
        result = result*mi(2) + hsh[c];
    return result;
}

// tree
int nodes=1;
vi adj[N];
int w[N];
void createNode(int u) {
    RE(i,4) adj[u].pb(nodes++);
}

// trie
string t;
int bg[MX];
int addTrie(int u, int i) {
    if(t.size() == i)
        return u;
    if(adj[u].empty())
        createNode(u);
    return addTrie(adj[u][t[i]],i+1);
}
int getTrie(int u, int i) {
    if(t.size() == i)
        return u;
    if(adj[u].empty())
        return -1;
    return getTrie(adj[u][t[i]],i+1);
}

// dfs pre order
int sgi[N], sge[N], nextSeg = 0;
void dfs(int u) {
    sgi[u] = nextSeg++;
    FOR(v,adj[u])
        dfs(v);
    sge[u] = nextSeg - 1;
}

// segment tree
int seg[N*2];
void addSeg(int i, int v, int p=1, int l=0, int r=N-1) {
    if(i < l || i > r) return;
    seg[p] += v;
    if(l == r)
        return;
    int m=(l+r)/2;
    addSeg(i,v,p*2  ,l  ,m);
    addSeg(i,v,p*2+1,m+1,r);
}
int getSeg(int i, int j, int p=1, int l=0, int r=N-1) {
    if(j < l || i > r) return 0;
    if(i <= l && j >= r) return seg[p];
    int m=(l+r)/2;
    int a=getSeg(i,j,p*2  ,l  ,m);
    int b=getSeg(i,j,p*2+1,m+1,r);
    return a+b;
}

// answer
map<int,vi> mp;
int ans[N];

// dfs ans
mi curHash = 0;
vi inter;
void dfsAns(int i) {
    if(inter.empty())
        return;
    RE(c,4) {
        vi rem, newInter;
        FOR(x,inter) {
            if(s[x].size() == i || s[x][i] != c)
                rem.pb(x);
            else
                newInter.pb(x);
        }
        inter = newInter;
        newInter.clear(); newInter.shrink_to_fit();
        FOR(j,rem)
            addSeg(sgi[bg[j]],-1);
        mi preHash = curHash;
        curHash = curHash*mi(2) + hsh[c];

        // answer queries
        vi queries = mp[(int)curHash];
        FOR(x,queries) {
            t = q[x];
            reverse(all(t));
            int u = getTrie(0,0);
            if(u == -1)
                continue;
            ans[x] = getSeg(sgi[u], sge[u]);
        }

        dfsAns(i+1);

        // return back to normal
        curHash = preHash;
        FOR(j,rem)
            addSeg(sgi[bg[j]], 1);
        FOR(j,rem)
            inter.pb(j);
    }
}

void program() {
    // pre computating
    RE(i,4) hsh[i] = rng()%MOD;

    // input
    IN(n,m);
    RE(i,n) IN(s[i]);
    RE(i,m) IN(p[i],q[i]);

    // compress strings
    map<char,char> charToComp;
    charToComp['A']=0, charToComp['U']=1, charToComp['G']=2, charToComp['C']=3;
    RE(i,n) FOR(c,s[i]) c = charToComp[c];
    RE(i,m) {
        FOR(c,p[i]) c = charToComp[c];
        FOR(c,q[i]) c = charToComp[c];
    }

    // create trie
    createNode(0);
    RE(i,n) {
        t = s[i];
        reverse(all(t));
        bg[i] = addTrie(0,0);
    }
    dfs(0);
    RE(i,n) addSeg(sgi[bg[i]],1);

    // fill map
    RE(i,m) {
        mp[hashString(p[i])].pb(i);
    }

    RE(i,n) inter.pb(i);
    dfsAns(0);

    RE(i,m) OUTL(ans[i]);
}

Compilation message

selling_rna.cpp: In function 'mi hashString(std::string)':
selling_rna.cpp:112:37: warning: array subscript has type 'char' [-Wchar-subscripts]
  112 |         result = result*mi(2) + hsh[c];
      |                                     ^
selling_rna.cpp: In function 'int addTrie(int, int)':
selling_rna.cpp:128:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  128 |     if(t.size() == i)
      |        ~~~~~~~~~^~~~
selling_rna.cpp: In function 'int getTrie(int, int)':
selling_rna.cpp:135:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  135 |     if(t.size() == i)
      |        ~~~~~~~~~^~~~
selling_rna.cpp: In function 'void dfsAns(int)':
selling_rna.cpp:184:28: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  184 |             if(s[x].size() == i || s[x][i] != c)
      |                ~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 67 ms 117628 KB Output is correct
2 Correct 70 ms 117748 KB Output is correct
3 Correct 67 ms 117680 KB Output is correct
4 Correct 69 ms 117712 KB Output is correct
5 Correct 68 ms 117700 KB Output is correct
6 Correct 71 ms 117648 KB Output is correct
7 Correct 71 ms 117704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 394 ms 313300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 270 ms 119316 KB Output is correct
2 Correct 229 ms 119088 KB Output is correct
3 Correct 237 ms 119340 KB Output is correct
4 Correct 239 ms 118636 KB Output is correct
5 Correct 236 ms 119788 KB Output is correct
6 Correct 257 ms 119420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 117628 KB Output is correct
2 Correct 70 ms 117748 KB Output is correct
3 Correct 67 ms 117680 KB Output is correct
4 Correct 69 ms 117712 KB Output is correct
5 Correct 68 ms 117700 KB Output is correct
6 Correct 71 ms 117648 KB Output is correct
7 Correct 71 ms 117704 KB Output is correct
8 Runtime error 394 ms 313300 KB Execution killed with signal 11
9 Halted 0 ms 0 KB -