/* Bismillahir Rahmanir Rahim */
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int M = 2000005;
int mp[M], from[M];
struct TRIE{
map <char, int> trie[M];
int in[M], out[M], cnt[M];
int idx, t;
void init(){
memset(cnt, 0, sizeof(cnt));
idx = 1;
t = 0;
}
int insert(string str, int ot){
int cur = 1;
for(auto c : str){
if(trie[cur].count(c) == 0) trie[cur][c] = ++idx;
cur = trie[cur][c];
}
from[cur] = ot;
++cnt[cur];
return cur;
}
int query(string str){
int cur = 1;
for(auto c : str){
if(trie[cur].count(c) == 0) return -1;
cur = trie[cur][c];
}
return cur;
}
void dfs(int at){
in[at] = ++t;
mp[t] = at;
for(auto c : {'A', 'C', 'G', 'U'}){
if(trie[at].count(c)){
dfs(trie[at][c]);
}
}
out[at] = t;
}
};
int n, m, k;
int nn, mm;
string rna[N];
TRIE t1, t2;
int id1[N], id2[N];
int root[M];
int lf[19 * M], rf[19 * M];
int sum[19 * M];
int idx;
int update(int b, int e, int node, int pos, int val){
int cur = ++idx;
sum[cur] = sum[node] + val;
if(b == e) return cur;
int mid = (b + e) / 2;
lf[cur] = lf[node];
rf[cur] = rf[node];
if(pos <= mid) lf[cur] = update(b, mid, lf[node], pos, val);
else rf[cur] = update(mid + 1, e, rf[node], pos, val);
return cur;
}
int get(int b, int e, int node, int x, int y){
if(y < b || e < x || node == 0) return 0;
if(b >= x && e <= y) return sum[node];
int mid = (b + e) / 2;
return get(b, mid, lf[node], x, y) + get(mid + 1, e, rf[node], x, y);
}
void init(){
t1.init();
for(int i = 1; i <= n; ++i){
id1[i] = t1.insert(rna[i], 0);
}
t1.dfs(1);
nn = t1.idx;
for(int i = 1; i <= n; ++i) reverse(rna[i].begin(), rna[i].end());
for(int i = 1; i <= n; ++i){
id2[i] = t2.insert(rna[i], t1.in[ id1[i] ]);
}
t2.dfs(1);
mm = t2.idx;
for(int i = 1; i <= mm; ++i){
int at = mp[i];
if(from[at] == 0) root[i] = root[i-1];
else{
root[i] = update(1, nn, root[i-1], from[at], t2.cnt[at]);
}
}
}
int query(string p, string q){
int a = t1.query(p);
int b = t2.query(q);
if(a == -1 || b == -1) return 0;
int aa = get(1, nn, root[ t2.out[b] ], t1.in[a], t1.out[a]);
int bb = get(1, nn, root[ t2.in[b] - 1 ], t1.in[a], t1.out[a]);
return aa - bb;
}
int main(){
ios_base::sync_with_stdio(false);
t1.init();
t2.init();
cin >> n >> m;
for(int i = 1; i <= n; ++i){
cin >> rna[i];
}
init();
string p, q;
for(int i = 1; i <= m; ++i){
cin >> p >> q;
reverse(q.begin(), q.end());
cout << query(p, q) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
709220 KB |
Output is correct |
2 |
Correct |
56 ms |
709220 KB |
Output is correct |
3 |
Correct |
43 ms |
709220 KB |
Output is correct |
4 |
Correct |
53 ms |
709220 KB |
Output is correct |
5 |
Correct |
66 ms |
709220 KB |
Output is correct |
6 |
Correct |
59 ms |
709220 KB |
Output is correct |
7 |
Correct |
53 ms |
709220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
409 ms |
803796 KB |
Output is correct |
2 |
Correct |
403 ms |
798708 KB |
Output is correct |
3 |
Correct |
413 ms |
802404 KB |
Output is correct |
4 |
Correct |
396 ms |
797920 KB |
Output is correct |
5 |
Correct |
419 ms |
824588 KB |
Output is correct |
6 |
Correct |
456 ms |
826304 KB |
Output is correct |
7 |
Correct |
146 ms |
710936 KB |
Output is correct |
8 |
Correct |
379 ms |
778124 KB |
Output is correct |
9 |
Correct |
443 ms |
767300 KB |
Output is correct |
10 |
Correct |
283 ms |
765716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
709220 KB |
Output is correct |
2 |
Correct |
119 ms |
709484 KB |
Output is correct |
3 |
Correct |
136 ms |
709352 KB |
Output is correct |
4 |
Correct |
153 ms |
709220 KB |
Output is correct |
5 |
Correct |
163 ms |
709484 KB |
Output is correct |
6 |
Correct |
186 ms |
709352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
709220 KB |
Output is correct |
2 |
Correct |
56 ms |
709220 KB |
Output is correct |
3 |
Correct |
43 ms |
709220 KB |
Output is correct |
4 |
Correct |
53 ms |
709220 KB |
Output is correct |
5 |
Correct |
66 ms |
709220 KB |
Output is correct |
6 |
Correct |
59 ms |
709220 KB |
Output is correct |
7 |
Correct |
53 ms |
709220 KB |
Output is correct |
8 |
Correct |
409 ms |
803796 KB |
Output is correct |
9 |
Correct |
403 ms |
798708 KB |
Output is correct |
10 |
Correct |
413 ms |
802404 KB |
Output is correct |
11 |
Correct |
396 ms |
797920 KB |
Output is correct |
12 |
Correct |
419 ms |
824588 KB |
Output is correct |
13 |
Correct |
456 ms |
826304 KB |
Output is correct |
14 |
Correct |
146 ms |
710936 KB |
Output is correct |
15 |
Correct |
379 ms |
778124 KB |
Output is correct |
16 |
Correct |
443 ms |
767300 KB |
Output is correct |
17 |
Correct |
283 ms |
765716 KB |
Output is correct |
18 |
Correct |
163 ms |
709220 KB |
Output is correct |
19 |
Correct |
119 ms |
709484 KB |
Output is correct |
20 |
Correct |
136 ms |
709352 KB |
Output is correct |
21 |
Correct |
153 ms |
709220 KB |
Output is correct |
22 |
Correct |
163 ms |
709484 KB |
Output is correct |
23 |
Correct |
186 ms |
709352 KB |
Output is correct |
24 |
Correct |
539 ms |
786688 KB |
Output is correct |
25 |
Correct |
606 ms |
786684 KB |
Output is correct |
26 |
Correct |
386 ms |
785896 KB |
Output is correct |
27 |
Correct |
573 ms |
785760 KB |
Output is correct |
28 |
Runtime error |
576 ms |
721496 KB |
Execution timed out (wall clock limit exceeded) |
29 |
Halted |
0 ms |
0 KB |
- |