#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5, MAX = 2e6 + 5, C = 26;
struct Trie {
int tot = 1;
int nxt[MAX][C], ord[MAX], tout[MAX];
int add(int p, char c) {
if (!nxt[p][c - 'A']) {
nxt[p][c - 'A'] = ++tot;
}
return nxt[p][c - 'A'];
}
void ins(const string &s) {
int p = 1;
for (char c : s) {
p = add(p, c);
}
}
int timer = 0;
void dfs(int u) {
ord[u] = ++timer;
for (int i = 0; i < C; ++i) {
if (nxt[u][i]) {
dfs(nxt[u][i]);
}
}
tout[u] = timer;
}
int search(const string &s) {
int p = 1;
for (char c : s) {
if (!nxt[p][c - 'A']) {
return 0;
}
p = nxt[p][c - 'A'];
}
return p;
}
} pf, sf;
int n, m;
int ft[MAX], res[N];
string s[N];
void upd(int i, int x) {
for (; i < MAX; i += i & -i) {
ft[i] += x;
}
}
int qry(int i) {
int res = 0;
for (; i; i -= i & -i) {
res += ft[i];
}
return res;
}
int qry(int l, int r) {
return qry(r) - qry(l - 1);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
pf.ins(s[i]);
reverse(s[i].begin(), s[i].end());
sf.ins(s[i]);
}
pf.dfs(1);
sf.dfs(1);
vector<array<int, 2>> pt;
for (int i = 1; i <= n; ++i) {
int y = sf.search(s[i]);
reverse(s[i].begin(), s[i].end());
int x = pf.search(s[i]);
pt.push_back({pf.ord[x], sf.ord[y]});
}
vector<array<int, 4>> Queries;
for (int i = 1; i <= m; ++i) {
string P, Q; cin >> P >> Q;
reverse(Q.begin(), Q.end());
int p = pf.search(P), q = sf.search(Q);
if (p && q) {
int x = pf.ord[p], y = pf.tout[p], a = sf.ord[q], b = sf.tout[q];
Queries.push_back({x - 1, a, b, -i});
Queries.push_back({y, a, b, i});
}
}
sort(Queries.begin(), Queries.end());
sort(pt.begin(), pt.end());
int j = 0;
for (auto [x, u, v, id] : Queries) {
while (j < pt.size() && pt[j][0] <= x) {
upd(pt[j][1], 1);
++j;
}
if (id > 0) {
res[id] += qry(u, v);
} else {
res[-id] -= qry(u, v);
}
}
for (int i = 1; i <= m; ++i) {
cout << res[i] << "\n";
}
return 0;
}
Compilation message
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:111:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | while (j < pt.size() && pt[j][0] <= x) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21340 KB |
Output is correct |
5 |
Correct |
2 ms |
21084 KB |
Output is correct |
6 |
Correct |
2 ms |
16988 KB |
Output is correct |
7 |
Correct |
2 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
239764 KB |
Output is correct |
2 |
Correct |
193 ms |
229400 KB |
Output is correct |
3 |
Correct |
198 ms |
227152 KB |
Output is correct |
4 |
Correct |
184 ms |
217424 KB |
Output is correct |
5 |
Correct |
226 ms |
282060 KB |
Output is correct |
6 |
Correct |
237 ms |
286928 KB |
Output is correct |
7 |
Correct |
47 ms |
24400 KB |
Output is correct |
8 |
Correct |
183 ms |
176900 KB |
Output is correct |
9 |
Correct |
180 ms |
152052 KB |
Output is correct |
10 |
Correct |
160 ms |
144400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
20056 KB |
Output is correct |
2 |
Correct |
21 ms |
20804 KB |
Output is correct |
3 |
Correct |
25 ms |
21324 KB |
Output is correct |
4 |
Correct |
21 ms |
19800 KB |
Output is correct |
5 |
Correct |
22 ms |
20816 KB |
Output is correct |
6 |
Correct |
27 ms |
21324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21084 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21340 KB |
Output is correct |
5 |
Correct |
2 ms |
21084 KB |
Output is correct |
6 |
Correct |
2 ms |
16988 KB |
Output is correct |
7 |
Correct |
2 ms |
16988 KB |
Output is correct |
8 |
Correct |
192 ms |
239764 KB |
Output is correct |
9 |
Correct |
193 ms |
229400 KB |
Output is correct |
10 |
Correct |
198 ms |
227152 KB |
Output is correct |
11 |
Correct |
184 ms |
217424 KB |
Output is correct |
12 |
Correct |
226 ms |
282060 KB |
Output is correct |
13 |
Correct |
237 ms |
286928 KB |
Output is correct |
14 |
Correct |
47 ms |
24400 KB |
Output is correct |
15 |
Correct |
183 ms |
176900 KB |
Output is correct |
16 |
Correct |
180 ms |
152052 KB |
Output is correct |
17 |
Correct |
160 ms |
144400 KB |
Output is correct |
18 |
Correct |
29 ms |
20056 KB |
Output is correct |
19 |
Correct |
21 ms |
20804 KB |
Output is correct |
20 |
Correct |
25 ms |
21324 KB |
Output is correct |
21 |
Correct |
21 ms |
19800 KB |
Output is correct |
22 |
Correct |
22 ms |
20816 KB |
Output is correct |
23 |
Correct |
27 ms |
21324 KB |
Output is correct |
24 |
Correct |
181 ms |
201736 KB |
Output is correct |
25 |
Correct |
212 ms |
201992 KB |
Output is correct |
26 |
Correct |
172 ms |
199116 KB |
Output is correct |
27 |
Correct |
184 ms |
191248 KB |
Output is correct |
28 |
Correct |
149 ms |
50248 KB |
Output is correct |
29 |
Correct |
73 ms |
26444 KB |
Output is correct |