/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
struct node {
int l, r;
node *nxt[4];
vi idx;
};
struct Op {
int x, y, idx;
Op(int x, int y, int idx) : x(x), y(y), idx(idx) {}
bool operator < (const Op &o) const {
return x < o.x;
}
};
struct FenwickTree {
int n;
vi data;
FenwickTree(int n) : n(n), data(n + 1) {}
void update(int idx, int val) {
while (idx <= n) {
data[idx] += val;
idx += idx & -idx;
}
}
int get(int idx) {
int res = 0;
while (idx) {
res += data[idx];
idx -= idx & -idx;
}
return res;
}
};
array<int, 2> points[mxN];
int n, q, ans[mxN], enc[256], cc;
node *root1 = new node();
node *root2 = new node();
void add(string &s, int idx) {
node *cur = root1;
for (char c : s) {
int ch = enc[(int)c];
if (!cur->nxt[ch]) {
cur->nxt[ch] = new node();
}
cur = cur->nxt[ch];
}
cur->idx.emplace_back(idx);
cur = root2;
reverse(all(s));
for (char c : s) {
int ch = enc[(int)c];
if (!cur->nxt[ch]) {
cur->nxt[ch] = new node();
}
cur = cur->nxt[ch];
}
cur->idx.emplace_back(idx);
}
void dfs(node *cur, int idx) {
cur->l = cc;
for (int val : cur->idx)
points[val][idx] = cc++;
for (int i = 0; i < 4; ++i) {
if (cur->nxt[i]) {
dfs(cur->nxt[i], idx);
}
}
cur->r = cc - 1;
}
pii get(string &s, node *cur) {
for (char c : s) {
int ch = enc[(int)c];
if (!cur->nxt[ch]) {
return pair{-1, -1};
}
cur = cur->nxt[ch];
}
return pair{cur->l, cur->r};
}
void solve() {
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
string s; cin >> s;
add(s, i);
}
cc = 1; dfs(root1, 0);
cc = 1; dfs(root2, 1);
sort(points + 1, points + n + 1);
vector<Op> op;
auto addRange = [&](int x1, int x2, int y1, int y2, int idx) -> void {
op.emplace_back(x2, y2, idx);
op.emplace_back(x1 - 1, y2, -idx);
op.emplace_back(x2, y1 - 1, -idx);
op.emplace_back(x1 - 1, y1 - 1, idx);
};
for (int i = 1; i <= q; ++i) {
string pfx, sfx;
cin >> pfx >> sfx;
reverse(all(sfx));
pii x = get(pfx, root1);
pii y = get(sfx, root2);
if (x.ff == -1 || y.ff == -1) {
ans[i] = 0;
}
else {
addRange(x.ff, x.ss, y.ff, y.ss, i);
}
// cout << x.ff << " " << x.ss << " " << y.ff << " " << y.ss << endl;
}
sort(all(op));
FenwickTree fenw(n);
int ptr = 1;
for (auto &[x, y, idx] : op) {
while (ptr <= n && points[ptr][0] <= x) {
fenw.update(points[ptr][1], 1);
++ptr;
}
ans[abs(idx)] += fenw.get(y) * (idx > 0 ? 1 : -1);
}
for (int i = 1; i <= q; ++i) {
cout << ans[i] << "\n";
}
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
enc['A'] = 0;
enc['C'] = 1;
enc['G'] = 2;
enc['U'] = 3;
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
155144 KB |
Output is correct |
2 |
Correct |
119 ms |
147224 KB |
Output is correct |
3 |
Correct |
143 ms |
153228 KB |
Output is correct |
4 |
Correct |
119 ms |
145804 KB |
Output is correct |
5 |
Correct |
184 ms |
191516 KB |
Output is correct |
6 |
Correct |
168 ms |
194408 KB |
Output is correct |
7 |
Correct |
28 ms |
1312 KB |
Output is correct |
8 |
Correct |
130 ms |
112832 KB |
Output is correct |
9 |
Correct |
122 ms |
94732 KB |
Output is correct |
10 |
Correct |
94 ms |
91952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
4488 KB |
Output is correct |
2 |
Correct |
16 ms |
3104 KB |
Output is correct |
3 |
Correct |
20 ms |
4576 KB |
Output is correct |
4 |
Correct |
16 ms |
4044 KB |
Output is correct |
5 |
Correct |
17 ms |
3024 KB |
Output is correct |
6 |
Correct |
22 ms |
4600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
132 ms |
155144 KB |
Output is correct |
9 |
Correct |
119 ms |
147224 KB |
Output is correct |
10 |
Correct |
143 ms |
153228 KB |
Output is correct |
11 |
Correct |
119 ms |
145804 KB |
Output is correct |
12 |
Correct |
184 ms |
191516 KB |
Output is correct |
13 |
Correct |
168 ms |
194408 KB |
Output is correct |
14 |
Correct |
28 ms |
1312 KB |
Output is correct |
15 |
Correct |
130 ms |
112832 KB |
Output is correct |
16 |
Correct |
122 ms |
94732 KB |
Output is correct |
17 |
Correct |
94 ms |
91952 KB |
Output is correct |
18 |
Correct |
24 ms |
4488 KB |
Output is correct |
19 |
Correct |
16 ms |
3104 KB |
Output is correct |
20 |
Correct |
20 ms |
4576 KB |
Output is correct |
21 |
Correct |
16 ms |
4044 KB |
Output is correct |
22 |
Correct |
17 ms |
3024 KB |
Output is correct |
23 |
Correct |
22 ms |
4600 KB |
Output is correct |
24 |
Correct |
116 ms |
128344 KB |
Output is correct |
25 |
Correct |
126 ms |
129832 KB |
Output is correct |
26 |
Correct |
111 ms |
126096 KB |
Output is correct |
27 |
Correct |
116 ms |
126616 KB |
Output is correct |
28 |
Correct |
93 ms |
25180 KB |
Output is correct |
29 |
Correct |
58 ms |
8460 KB |
Output is correct |