#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 |
7 ms |
5504 KB |
Output is correct |
2 |
Correct |
7 ms |
5504 KB |
Output is correct |
3 |
Correct |
5 ms |
5504 KB |
Output is correct |
4 |
Correct |
8 ms |
5504 KB |
Output is correct |
5 |
Correct |
8 ms |
5504 KB |
Output is correct |
6 |
Correct |
7 ms |
5504 KB |
Output is correct |
7 |
Correct |
7 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
15968 KB |
Output is correct |
2 |
Correct |
37 ms |
16500 KB |
Output is correct |
3 |
Correct |
37 ms |
16376 KB |
Output is correct |
4 |
Correct |
39 ms |
16372 KB |
Output is correct |
5 |
Correct |
32 ms |
12916 KB |
Output is correct |
6 |
Correct |
33 ms |
12916 KB |
Output is correct |
7 |
Correct |
34 ms |
18680 KB |
Output is correct |
8 |
Correct |
47 ms |
20596 KB |
Output is correct |
9 |
Correct |
47 ms |
20468 KB |
Output is correct |
10 |
Correct |
32 ms |
15476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
14380 KB |
Output is correct |
2 |
Correct |
53 ms |
10988 KB |
Output is correct |
3 |
Correct |
66 ms |
12652 KB |
Output is correct |
4 |
Correct |
49 ms |
11520 KB |
Output is correct |
5 |
Correct |
55 ms |
10988 KB |
Output is correct |
6 |
Correct |
65 ms |
12648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5504 KB |
Output is correct |
2 |
Correct |
7 ms |
5504 KB |
Output is correct |
3 |
Correct |
5 ms |
5504 KB |
Output is correct |
4 |
Correct |
8 ms |
5504 KB |
Output is correct |
5 |
Correct |
8 ms |
5504 KB |
Output is correct |
6 |
Correct |
7 ms |
5504 KB |
Output is correct |
7 |
Correct |
7 ms |
5504 KB |
Output is correct |
8 |
Correct |
29 ms |
15968 KB |
Output is correct |
9 |
Correct |
37 ms |
16500 KB |
Output is correct |
10 |
Correct |
37 ms |
16376 KB |
Output is correct |
11 |
Correct |
39 ms |
16372 KB |
Output is correct |
12 |
Correct |
32 ms |
12916 KB |
Output is correct |
13 |
Correct |
33 ms |
12916 KB |
Output is correct |
14 |
Correct |
34 ms |
18680 KB |
Output is correct |
15 |
Correct |
47 ms |
20596 KB |
Output is correct |
16 |
Correct |
47 ms |
20468 KB |
Output is correct |
17 |
Correct |
32 ms |
15476 KB |
Output is correct |
18 |
Correct |
70 ms |
14380 KB |
Output is correct |
19 |
Correct |
53 ms |
10988 KB |
Output is correct |
20 |
Correct |
66 ms |
12652 KB |
Output is correct |
21 |
Correct |
49 ms |
11520 KB |
Output is correct |
22 |
Correct |
55 ms |
10988 KB |
Output is correct |
23 |
Correct |
65 ms |
12648 KB |
Output is correct |
24 |
Correct |
79 ms |
18420 KB |
Output is correct |
25 |
Correct |
96 ms |
21616 KB |
Output is correct |
26 |
Correct |
51 ms |
17392 KB |
Output is correct |
27 |
Correct |
68 ms |
18544 KB |
Output is correct |
28 |
Correct |
252 ms |
33888 KB |
Output is correct |
29 |
Correct |
193 ms |
25184 KB |
Output is correct |