#include <bits/stdc++.h>
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
struct Node {
int in, out;
map<char, Node*> mp;
Node() {}
};
struct Trie {
Node *root;
int timer = 0;
Trie() { root = new Node(); }
~Trie() { delete root; }
void insert(string s) {
Node *u = root;
for(char &ch : s) {
if(!u->mp.count(ch)) u->mp[ch] = new Node();
u = u->mp[ch];
}
}
pii search(string s) {
Node *u = root;
for(char &ch : s) {
if(!u->mp.count(ch)) return { -1, -1 };
u = u->mp[ch];
}
return { u->in, u->out };
}
void dfs(Node *u) {
u->in = timer++;
for(auto &x : u->mp) dfs(x.second);
u->out = timer;
}
void callDFS() {
dfs(root);
}
};
struct BIT {
int n;
vector<int> tree;
BIT(int _n) : n(_n+50), tree(_n+100) {}
void update(int p, int v) { for(p++; p<n; p+=p&-p) tree[p] += v; }
int query(int p) { int ans = 0; for(p++; p; p-=p&-p) ans += tree[p]; return ans; }
int query(int l, int r) { return query(r) - query(l-1); }
};
const int M = 2e6 + 5;
vector<int> by_x[M];
vector<array<int, 3> > on[M], off[M];
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, q;
cin >> n >> q;
Trie trie, rev_trie;
vector<string> vec(n);
for(int i=0; i<n; i++) {
cin >> vec[i];
trie.insert(vec[i]);
reverse(vec[i].begin(), vec[i].end());
rev_trie.insert(vec[i]);
reverse(vec[i].begin(), vec[i].end());
}
trie.callDFS();
rev_trie.callDFS();
vector<array<int, 5> > qus;
vector<int> ans(q);
for(int i=0; i<q; i++) {
string a, b;
cin >> a >> b;
reverse(b.begin(), b.end());
auto [in1, out1] = trie.search(a);
auto [in2, out2] = rev_trie.search(b);
if(in1 == -1 || in2 == -1) continue;
qus.push_back({ i, in1, in2, out1, out2 });
}
for(int i=0; i<n; i++) {
int a = trie.search(vec[i]).first;
reverse(vec[i].begin(), vec[i].end());
int b = rev_trie.search(vec[i]).first;
by_x[a].push_back(b);
}
for(auto &[id, x1, y1, x2, y2] : qus) {
on[x2-1].push_back({ id, y1, y2-1 });
off[x1-1].push_back({ id, y1, y2-1 });
}
BIT bit(M);
for(int i=0; i<M; i++) {
for(int &p : by_x[i]) bit.update(p, 1);
for(auto &[id, l, r] : off[i]) ans[id] -= bit.query(l, r);
for(auto &[id, l, r] : on[i]) ans[id] += bit.query(l, r);
}
for(int &x : ans) cout << x << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
149076 KB |
Output is correct |
2 |
Correct |
55 ms |
149072 KB |
Output is correct |
3 |
Correct |
57 ms |
149072 KB |
Output is correct |
4 |
Correct |
55 ms |
149072 KB |
Output is correct |
5 |
Correct |
56 ms |
149076 KB |
Output is correct |
6 |
Correct |
53 ms |
149076 KB |
Output is correct |
7 |
Correct |
56 ms |
149076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
285 ms |
398568 KB |
Output is correct |
2 |
Correct |
312 ms |
385336 KB |
Output is correct |
3 |
Correct |
302 ms |
395072 KB |
Output is correct |
4 |
Correct |
314 ms |
383548 KB |
Output is correct |
5 |
Correct |
321 ms |
455748 KB |
Output is correct |
6 |
Correct |
334 ms |
460304 KB |
Output is correct |
7 |
Correct |
176 ms |
151616 KB |
Output is correct |
8 |
Correct |
275 ms |
330568 KB |
Output is correct |
9 |
Correct |
329 ms |
301456 KB |
Output is correct |
10 |
Correct |
232 ms |
297020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
154040 KB |
Output is correct |
2 |
Correct |
86 ms |
153632 KB |
Output is correct |
3 |
Correct |
72 ms |
154476 KB |
Output is correct |
4 |
Correct |
70 ms |
152772 KB |
Output is correct |
5 |
Correct |
77 ms |
153160 KB |
Output is correct |
6 |
Correct |
72 ms |
153800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
149076 KB |
Output is correct |
2 |
Correct |
55 ms |
149072 KB |
Output is correct |
3 |
Correct |
57 ms |
149072 KB |
Output is correct |
4 |
Correct |
55 ms |
149072 KB |
Output is correct |
5 |
Correct |
56 ms |
149076 KB |
Output is correct |
6 |
Correct |
53 ms |
149076 KB |
Output is correct |
7 |
Correct |
56 ms |
149076 KB |
Output is correct |
8 |
Correct |
285 ms |
398568 KB |
Output is correct |
9 |
Correct |
312 ms |
385336 KB |
Output is correct |
10 |
Correct |
302 ms |
395072 KB |
Output is correct |
11 |
Correct |
314 ms |
383548 KB |
Output is correct |
12 |
Correct |
321 ms |
455748 KB |
Output is correct |
13 |
Correct |
334 ms |
460304 KB |
Output is correct |
14 |
Correct |
176 ms |
151616 KB |
Output is correct |
15 |
Correct |
275 ms |
330568 KB |
Output is correct |
16 |
Correct |
329 ms |
301456 KB |
Output is correct |
17 |
Correct |
232 ms |
297020 KB |
Output is correct |
18 |
Correct |
76 ms |
154040 KB |
Output is correct |
19 |
Correct |
86 ms |
153632 KB |
Output is correct |
20 |
Correct |
72 ms |
154476 KB |
Output is correct |
21 |
Correct |
70 ms |
152772 KB |
Output is correct |
22 |
Correct |
77 ms |
153160 KB |
Output is correct |
23 |
Correct |
72 ms |
153800 KB |
Output is correct |
24 |
Correct |
312 ms |
358480 KB |
Output is correct |
25 |
Correct |
340 ms |
360376 KB |
Output is correct |
26 |
Correct |
299 ms |
355476 KB |
Output is correct |
27 |
Correct |
292 ms |
355664 KB |
Output is correct |
28 |
Correct |
244 ms |
192704 KB |
Output is correct |
29 |
Correct |
134 ms |
161784 KB |
Output is correct |