#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 1e5+5;
struct SegmentTreeSum {
int t[N << 1];
void update(int x, int k) {
for(t[x += N] += k; x != 1; x >>= 1)
t[x >> 1] = t[x] + t[x ^ 1];
}
int query(int l, int r) {
int ret = 0;
for(l += N, r += N + 1; l < r; l >>= 1, r >>= 1) {
if(l & 1) ret += t[l++];
if(r & 1) ret += t[--r];
}
return ret;
}
} t;
int n, q;
int pos[N], idx[N], a[N], b[N], ans[N];
vector<int> L[N], R[N];
vector<string> S, pre, suf;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
iota(pos, pos + N, 0);
cin >> n >> q;
S.resize(n), pre.resize(q), suf.resize(q);
for(int i = 0; i < n; i++) cin >> S[i];
for(int i = 0; i < q; i++) cin >> pre[i] >> suf[i];
sort(S.begin(), S.end());
for(int i = 0; i < q; i++) {
int l = lower_bound(S.begin(), S.end(), pre[i]) - S.begin();
++pre[i].back();
int r = lower_bound(S.begin(), S.end(), pre[i]) - S.begin();
L[l].emplace_back(i), R[r].emplace_back(i);
}
vector<string> rev;
for(int i = 0; i < n; i++) {
reverse(S[i].begin(), S[i].end());
rev.emplace_back(S[i]);
}
sort(rev.begin(), rev.end());
sort(pos, pos + n, [&](int &a, int &b) { return S[a] < S[b]; });
for(int i = 0; i < n; i++) idx[pos[i]] = i;
for(int i = 0; i < q; i++) {
reverse(suf[i].begin(), suf[i].end());
a[i] = lower_bound(rev.begin(), rev.end(), suf[i]) - rev.begin();
++suf[i].back();
b[i] = lower_bound(rev.begin(), rev.end(), suf[i]) - rev.begin() - 1;
}
for(int i = n - 1; ~i; i--) {
t.update(idx[i], 1);
for(int x : L[i]) ans[x] += t.query(a[x], b[x]);
for(int x : R[i]) ans[x] -= t.query(a[x], b[x]);
}
for(int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5504 KB |
Output is correct |
2 |
Correct |
8 ms |
5504 KB |
Output is correct |
3 |
Correct |
8 ms |
5504 KB |
Output is correct |
4 |
Correct |
9 ms |
5504 KB |
Output is correct |
5 |
Correct |
9 ms |
5504 KB |
Output is correct |
6 |
Correct |
8 ms |
5504 KB |
Output is correct |
7 |
Correct |
8 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
15872 KB |
Output is correct |
2 |
Correct |
37 ms |
16500 KB |
Output is correct |
3 |
Correct |
37 ms |
16364 KB |
Output is correct |
4 |
Correct |
40 ms |
16500 KB |
Output is correct |
5 |
Correct |
32 ms |
12916 KB |
Output is correct |
6 |
Correct |
32 ms |
12924 KB |
Output is correct |
7 |
Correct |
34 ms |
18680 KB |
Output is correct |
8 |
Correct |
48 ms |
20596 KB |
Output is correct |
9 |
Correct |
46 ms |
20468 KB |
Output is correct |
10 |
Correct |
31 ms |
15476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
14380 KB |
Output is correct |
2 |
Correct |
54 ms |
10988 KB |
Output is correct |
3 |
Correct |
66 ms |
12652 KB |
Output is correct |
4 |
Correct |
55 ms |
11528 KB |
Output is correct |
5 |
Correct |
52 ms |
10988 KB |
Output is correct |
6 |
Correct |
68 ms |
12648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5504 KB |
Output is correct |
2 |
Correct |
8 ms |
5504 KB |
Output is correct |
3 |
Correct |
8 ms |
5504 KB |
Output is correct |
4 |
Correct |
9 ms |
5504 KB |
Output is correct |
5 |
Correct |
9 ms |
5504 KB |
Output is correct |
6 |
Correct |
8 ms |
5504 KB |
Output is correct |
7 |
Correct |
8 ms |
5504 KB |
Output is correct |
8 |
Correct |
26 ms |
15872 KB |
Output is correct |
9 |
Correct |
37 ms |
16500 KB |
Output is correct |
10 |
Correct |
37 ms |
16364 KB |
Output is correct |
11 |
Correct |
40 ms |
16500 KB |
Output is correct |
12 |
Correct |
32 ms |
12916 KB |
Output is correct |
13 |
Correct |
32 ms |
12924 KB |
Output is correct |
14 |
Correct |
34 ms |
18680 KB |
Output is correct |
15 |
Correct |
48 ms |
20596 KB |
Output is correct |
16 |
Correct |
46 ms |
20468 KB |
Output is correct |
17 |
Correct |
31 ms |
15476 KB |
Output is correct |
18 |
Correct |
70 ms |
14380 KB |
Output is correct |
19 |
Correct |
54 ms |
10988 KB |
Output is correct |
20 |
Correct |
66 ms |
12652 KB |
Output is correct |
21 |
Correct |
55 ms |
11528 KB |
Output is correct |
22 |
Correct |
52 ms |
10988 KB |
Output is correct |
23 |
Correct |
68 ms |
12648 KB |
Output is correct |
24 |
Correct |
64 ms |
18548 KB |
Output is correct |
25 |
Correct |
101 ms |
21744 KB |
Output is correct |
26 |
Correct |
53 ms |
17392 KB |
Output is correct |
27 |
Correct |
70 ms |
18468 KB |
Output is correct |
28 |
Correct |
257 ms |
33888 KB |
Output is correct |
29 |
Correct |
197 ms |
25160 KB |
Output is correct |