#include <bits/stdc++.h>
using namespace std;
struct binary_indexed_tree{
int N;
vector<int> BIT;
binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
}
void add(int i){
i++;
while (i <= N){
BIT[i]++;
i += i & -i;
}
}
int sum(int i){
int ans = 0;
while (i > 0){
ans += BIT[i];
i -= i & -i;
}
return ans;
}
int sum(int L, int R){
return sum(R) - sum(L);
}
};
int main(){
int N, M;
cin >> N >> M;
vector<string> S(N);
for (int i = 0; i < N; i++){
cin >> S[i];
}
vector<string> P(M), Q(M);
for (int i = 0; i < M; i++){
cin >> P[i] >> Q[i];
reverse(Q[i].begin(), Q[i].end());
}
vector<pair<string, int>> P1(N);
for (int i = 0; i < N; i++){
P1[i] = make_pair(S[i], i);
}
for (int i = 0; i < N; i++){
reverse(S[i].begin(), S[i].end());
}
vector<pair<string, int>> P2(N);
for (int i = 0; i < N; i++){
P2[i] = make_pair(S[i], i);
}
sort(P1.begin(), P1.end());
sort(P2.begin(), P2.end());
vector<int> L1(M), R1(M), L2(M), R2(M);
for (int i = 0; i < M; i++){
L1[i] = lower_bound(P1.begin(), P1.end(), make_pair(P[i], 0)) - P1.begin();
P[i].back()++;
R1[i] = lower_bound(P1.begin(), P1.end(), make_pair(P[i], 0)) - P1.begin();
L2[i] = lower_bound(P2.begin(), P2.end(), make_pair(Q[i], 0)) - P2.begin();
Q[i].back()++;
R2[i] = lower_bound(P2.begin(), P2.end(), make_pair(Q[i], N)) - P2.begin();
}
vector<int> X(N), Y(N);
for (int i = 0; i < N; i++){
X[P1[i].second] = i;
Y[P2[i].second] = i;
}
vector<vector<int>> query_add(N + 1), query_sub(N + 1);
for (int i = 0; i < M; i++){
query_sub[L1[i]].push_back(i);
query_add[R1[i]].push_back(i);
}
vector<vector<int>> update(N + 1);
for (int i = 0; i < N; i++){
update[X[i]].push_back(Y[i]);
}
vector<int> ans(M, 0);
binary_indexed_tree BIT(N);
for (int i = 0; i <= N; i++){
for (int j : query_add[i]){
ans[j] += BIT.sum(L2[j], R2[j]);
}
for (int j : query_sub[i]){
ans[j] -= BIT.sum(L2[j], R2[j]);
}
for (int j : update[i]){
BIT.add(j);
}
}
for (int i = 0; i < M; i++){
cout << ans[i] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
14544 KB |
Output is correct |
2 |
Correct |
121 ms |
15576 KB |
Output is correct |
3 |
Correct |
126 ms |
15152 KB |
Output is correct |
4 |
Correct |
122 ms |
15680 KB |
Output is correct |
5 |
Correct |
77 ms |
10692 KB |
Output is correct |
6 |
Correct |
80 ms |
10872 KB |
Output is correct |
7 |
Correct |
138 ms |
17408 KB |
Output is correct |
8 |
Correct |
201 ms |
19308 KB |
Output is correct |
9 |
Correct |
156 ms |
19136 KB |
Output is correct |
10 |
Correct |
102 ms |
13956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
16688 KB |
Output is correct |
2 |
Correct |
109 ms |
10168 KB |
Output is correct |
3 |
Correct |
125 ms |
13516 KB |
Output is correct |
4 |
Correct |
105 ms |
11248 KB |
Output is correct |
5 |
Correct |
97 ms |
10292 KB |
Output is correct |
6 |
Correct |
137 ms |
13464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
101 ms |
14544 KB |
Output is correct |
9 |
Correct |
121 ms |
15576 KB |
Output is correct |
10 |
Correct |
126 ms |
15152 KB |
Output is correct |
11 |
Correct |
122 ms |
15680 KB |
Output is correct |
12 |
Correct |
77 ms |
10692 KB |
Output is correct |
13 |
Correct |
80 ms |
10872 KB |
Output is correct |
14 |
Correct |
138 ms |
17408 KB |
Output is correct |
15 |
Correct |
201 ms |
19308 KB |
Output is correct |
16 |
Correct |
156 ms |
19136 KB |
Output is correct |
17 |
Correct |
102 ms |
13956 KB |
Output is correct |
18 |
Correct |
182 ms |
16688 KB |
Output is correct |
19 |
Correct |
109 ms |
10168 KB |
Output is correct |
20 |
Correct |
125 ms |
13516 KB |
Output is correct |
21 |
Correct |
105 ms |
11248 KB |
Output is correct |
22 |
Correct |
97 ms |
10292 KB |
Output is correct |
23 |
Correct |
137 ms |
13464 KB |
Output is correct |
24 |
Correct |
173 ms |
18608 KB |
Output is correct |
25 |
Correct |
237 ms |
21996 KB |
Output is correct |
26 |
Correct |
147 ms |
17572 KB |
Output is correct |
27 |
Correct |
191 ms |
18592 KB |
Output is correct |
28 |
Correct |
532 ms |
48092 KB |
Output is correct |
29 |
Correct |
468 ms |
35496 KB |
Output is correct |