#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 100000;
int x[N+5];
int y[N+5];
const int MAXC = 4000000;
int gde[MAXC+5][4];
int tjm;
vector <int> koji[MAXC+5];
vector <int> queries[MAXC+5];
void trie_add(int t, int ind, const string &s){
int v = 0;
for(int i=0; i<s.size(); i++){
int x = s[i] - 'A';
if(!gde[v][x]){
gde[v][x] = ++tjm;
}
v = gde[v][x];
}
if(t == 1) koji[v].push_back(ind);
else queries[v].push_back(ind);
}
int xl[N+5], yl[N+5], xr[N+5], yr[N+5];
void dfs(int t, int v){
int in = ++tjm;
for(auto c : koji[v]){
if(t == 1) x[c] = in;
else y[c] = in;
}
for(int x=0; x<4; x++) if(gde[v][x]) dfs(t, gde[v][x]);
int out = tjm;
for(auto c : queries[v]){
if(t == 1){
xl[c] = in, xr[c] = out;
}
else{
yl[c] = in, yr[c] = out;
}
}
queries[v].clear();
koji[v].clear();
for(int x=0; x<4; x++) gde[v][x] = 0;
}
string s[N+5];
string qpref[N+5];
string qsuf[N+5];
int bit[MAXC+5];
void bit_add(int x){
while(x <= MAXC){
bit[x]++;
x += x & -x;
}
}
int bit_get(int x){
int res = 0;
while(x){
res += bit[x];
x -= x & -x;
}
return res;
}
const int INF = 1000000000;
vector <int> upds[MAXC+5];
vector <pair <pair <int, int>, int>> quers[MAXC+5];
int res[N+5];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n, m;
cin >> n >> m;
for(int i=1; i<=n; i++){
cin >> s[i];
for(auto &c : s[i]){
if(c == 'G') c = 'B';
if(c == 'U') c = 'D';
}
trie_add(1, i, s[i]);
}
for(int i=1; i<=m; i++){
cin >> qpref[i] >> qsuf[i];
for(auto &c : qpref[i]){
if(c == 'G') c = 'B';
if(c == 'U') c = 'D';
}
for(auto &c : qsuf[i]){
if(c == 'G') c = 'B';
if(c == 'U') c = 'D';
}
trie_add(2, i, qpref[i]);
}
dfs(1, 0);
for(int i=1; i<=n; i++){
reverse(s[i].begin(), s[i].end());
trie_add(1, i, s[i]);
}
for(int i=1; i<=m; i++){
reverse(qsuf[i].begin(), qsuf[i].end());
trie_add(2, i, qsuf[i]);
}
tjm = 0;
dfs(2, 0);
for(int i=1; i<=n; i++){
upds[y[i]].push_back(x[i]);
}
for(int i=1; i<=m; i++){
quers[yr[i]].push_back({{xl[i], xr[i]}, i});
quers[yl[i] - 1].push_back({{xl[i], xr[i]}, -i});
}
for(int i=1; i<=MAXC; i++){
for(auto c : upds[i]) bit_add(c);
for(auto c : quers[i]){
int ind = c.second;
int kf = 1;
if(ind < 0){
ind = -ind;
kf = -1;
}
res[ind] += kf*(bit_get(c.first.second) - bit_get(c.first.first - 1));
}
}
for(int i=1; i<=m; i++) cout << res[i] << "\n";
return 0;
}
Compilation message
selling_rna.cpp: In function 'void trie_add(int, int, const string&)':
selling_rna.cpp:22:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0; i<s.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
385452 KB |
Output is correct |
2 |
Correct |
221 ms |
385468 KB |
Output is correct |
3 |
Correct |
225 ms |
385476 KB |
Output is correct |
4 |
Correct |
229 ms |
385556 KB |
Output is correct |
5 |
Correct |
217 ms |
385476 KB |
Output is correct |
6 |
Correct |
226 ms |
385636 KB |
Output is correct |
7 |
Correct |
221 ms |
385444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
421344 KB |
Output is correct |
2 |
Correct |
331 ms |
419748 KB |
Output is correct |
3 |
Correct |
334 ms |
428380 KB |
Output is correct |
4 |
Correct |
328 ms |
426692 KB |
Output is correct |
5 |
Correct |
377 ms |
432276 KB |
Output is correct |
6 |
Correct |
377 ms |
432864 KB |
Output is correct |
7 |
Correct |
266 ms |
392032 KB |
Output is correct |
8 |
Correct |
327 ms |
417864 KB |
Output is correct |
9 |
Correct |
322 ms |
413796 KB |
Output is correct |
10 |
Correct |
290 ms |
410404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
389848 KB |
Output is correct |
2 |
Correct |
243 ms |
388360 KB |
Output is correct |
3 |
Correct |
246 ms |
389044 KB |
Output is correct |
4 |
Correct |
248 ms |
388676 KB |
Output is correct |
5 |
Correct |
243 ms |
388444 KB |
Output is correct |
6 |
Correct |
248 ms |
389348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
385452 KB |
Output is correct |
2 |
Correct |
221 ms |
385468 KB |
Output is correct |
3 |
Correct |
225 ms |
385476 KB |
Output is correct |
4 |
Correct |
229 ms |
385556 KB |
Output is correct |
5 |
Correct |
217 ms |
385476 KB |
Output is correct |
6 |
Correct |
226 ms |
385636 KB |
Output is correct |
7 |
Correct |
221 ms |
385444 KB |
Output is correct |
8 |
Correct |
331 ms |
421344 KB |
Output is correct |
9 |
Correct |
331 ms |
419748 KB |
Output is correct |
10 |
Correct |
334 ms |
428380 KB |
Output is correct |
11 |
Correct |
328 ms |
426692 KB |
Output is correct |
12 |
Correct |
377 ms |
432276 KB |
Output is correct |
13 |
Correct |
377 ms |
432864 KB |
Output is correct |
14 |
Correct |
266 ms |
392032 KB |
Output is correct |
15 |
Correct |
327 ms |
417864 KB |
Output is correct |
16 |
Correct |
322 ms |
413796 KB |
Output is correct |
17 |
Correct |
290 ms |
410404 KB |
Output is correct |
18 |
Correct |
254 ms |
389848 KB |
Output is correct |
19 |
Correct |
243 ms |
388360 KB |
Output is correct |
20 |
Correct |
246 ms |
389044 KB |
Output is correct |
21 |
Correct |
248 ms |
388676 KB |
Output is correct |
22 |
Correct |
243 ms |
388444 KB |
Output is correct |
23 |
Correct |
248 ms |
389348 KB |
Output is correct |
24 |
Correct |
333 ms |
417128 KB |
Output is correct |
25 |
Correct |
341 ms |
419816 KB |
Output is correct |
26 |
Correct |
323 ms |
416040 KB |
Output is correct |
27 |
Correct |
331 ms |
423052 KB |
Output is correct |
28 |
Correct |
351 ms |
403800 KB |
Output is correct |
29 |
Correct |
298 ms |
394080 KB |
Output is correct |