#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 2e5 + 5, base = 131;
const ll llinf = 1e18;
string str[maxn], p[maxn], q[maxn];
int N, M;
int id[maxn], pos[maxn];
int BIT[maxn];
int res[maxn];
int nl[maxn], nr[maxn];
vector<int> add[maxn], rem[maxn];
void upd(int i, int v)
{
for(; i < maxn; i += i & -i)
BIT[i] += v;
}
int sum(int i)
{
int res = 0;
for(; i; i -= i & -i)
res += BIT[i];
return res;
}
int rsum(int l, int r)
{
return sum(r) - sum(l - 1);
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> N >> M;
for(int i = 1; i <= N; ++i){
cin >> str[i];
id[i] = i;
}
for(int i = 1; i <= M; ++i){
cin >> p[i] >> q[i];
}
sort(str + 1, str + 1 + N);
for(int i = 1; i <= M; ++i){
int l = lower_bound(str + 1, str + 1 + N, p[i]) - str;
add[l].eb(i);
++p[i].back();
int r = lower_bound(str + 1, str + 1 + N, p[i]) - str;
rem[r].eb(i);
}
for(int i = 1; i <= N; ++i){
reverse(str[i].begin(), str[i].end());
}
sort(id + 1, id + 1 + N,[&](int x, int y)
{
return str[x] < str[y];
}
);
sort(str + 1, str + 1 + N);
for(int i = 1; i <= M; ++i){
reverse(q[i].begin(), q[i].end());
nl[i] = lower_bound(str + 1, str + 1 + N, q[i]) - str;
++q[i].back();
nr[i] = upper_bound(str + 1, str + 1 + N, q[i]) - str - 1;
}
for(int i = 1; i <= N; ++i)
pos[id[i]] = i;
for(int i = N; i >= 1; --i){
upd(pos[i], 1);
for(int id : add[i]) res[id] += rsum(nl[id], nr[id]);
for(int id : rem[i]) res[id] -= rsum(nl[id], nr[id]);
}
for(int i = 1; i <= M; ++i)
cout << res[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
28544 KB |
Output is correct |
2 |
Correct |
19 ms |
28544 KB |
Output is correct |
3 |
Correct |
19 ms |
28672 KB |
Output is correct |
4 |
Correct |
21 ms |
28672 KB |
Output is correct |
5 |
Correct |
19 ms |
28544 KB |
Output is correct |
6 |
Correct |
19 ms |
28544 KB |
Output is correct |
7 |
Correct |
19 ms |
28544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
32888 KB |
Output is correct |
2 |
Correct |
45 ms |
36948 KB |
Output is correct |
3 |
Correct |
44 ms |
36856 KB |
Output is correct |
4 |
Correct |
53 ms |
36856 KB |
Output is correct |
5 |
Correct |
42 ms |
34048 KB |
Output is correct |
6 |
Correct |
43 ms |
34040 KB |
Output is correct |
7 |
Correct |
46 ms |
39800 KB |
Output is correct |
8 |
Correct |
58 ms |
40952 KB |
Output is correct |
9 |
Correct |
57 ms |
40824 KB |
Output is correct |
10 |
Correct |
40 ms |
35832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
30584 KB |
Output is correct |
2 |
Correct |
60 ms |
30200 KB |
Output is correct |
3 |
Correct |
72 ms |
30712 KB |
Output is correct |
4 |
Correct |
56 ms |
30200 KB |
Output is correct |
5 |
Correct |
62 ms |
30028 KB |
Output is correct |
6 |
Correct |
70 ms |
30456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
28544 KB |
Output is correct |
2 |
Correct |
19 ms |
28544 KB |
Output is correct |
3 |
Correct |
19 ms |
28672 KB |
Output is correct |
4 |
Correct |
21 ms |
28672 KB |
Output is correct |
5 |
Correct |
19 ms |
28544 KB |
Output is correct |
6 |
Correct |
19 ms |
28544 KB |
Output is correct |
7 |
Correct |
19 ms |
28544 KB |
Output is correct |
8 |
Correct |
35 ms |
32888 KB |
Output is correct |
9 |
Correct |
45 ms |
36948 KB |
Output is correct |
10 |
Correct |
44 ms |
36856 KB |
Output is correct |
11 |
Correct |
53 ms |
36856 KB |
Output is correct |
12 |
Correct |
42 ms |
34048 KB |
Output is correct |
13 |
Correct |
43 ms |
34040 KB |
Output is correct |
14 |
Correct |
46 ms |
39800 KB |
Output is correct |
15 |
Correct |
58 ms |
40952 KB |
Output is correct |
16 |
Correct |
57 ms |
40824 KB |
Output is correct |
17 |
Correct |
40 ms |
35832 KB |
Output is correct |
18 |
Correct |
72 ms |
30584 KB |
Output is correct |
19 |
Correct |
60 ms |
30200 KB |
Output is correct |
20 |
Correct |
72 ms |
30712 KB |
Output is correct |
21 |
Correct |
56 ms |
30200 KB |
Output is correct |
22 |
Correct |
62 ms |
30028 KB |
Output is correct |
23 |
Correct |
70 ms |
30456 KB |
Output is correct |
24 |
Correct |
70 ms |
37752 KB |
Output is correct |
25 |
Correct |
99 ms |
39032 KB |
Output is correct |
26 |
Correct |
63 ms |
37240 KB |
Output is correct |
27 |
Correct |
72 ms |
37624 KB |
Output is correct |
28 |
Correct |
234 ms |
42488 KB |
Output is correct |
29 |
Correct |
191 ms |
35576 KB |
Output is correct |