#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define int long long
const int N = 2e5 + 69;
int n, q, a[N], b[N], f[N], ans[N], c[N];
void Modify(int v, int val) {
for (int i = v;i < N;i += (i & -i))
f[i] += val;
}
int Get(int v) {
int res = 0;
for (int i = v;i > 0;i -= (i & -i))
res += f[i];
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> q;
for (int i = 1;i <= n;i++) {
cin >> a[i] >> b[i];
c[i] = a[i] + b[i];
}
vector<tuple<int, int, int>> t1, t2, t3;
for (int i = 1;i <= q;i++) {
int x, y, z;
cin >> x >> y >> z;
if (x + y >= z)
t1.push_back({x, y, i});
else {
t2.push_back({x, z, i});
t3.push_back({z, y, i});
}
}
//x-y
//x-z
//z-y
vector<int> all;
for (int i = 1;i <= n;i++)
all.push_back(b[i]);
for (auto u : t1)
all.push_back(get<1>(u));
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
sort(t1.rbegin(), t1.rend());
vector<int> ord(n);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [&](int i, int j) {
return a[i] < a[j];
});
int ptr = n - 1;
for (auto u : t1) {
int x, y, ind;
tie(x, y, ind) = u;
while (ptr >= 0 && a[ord[ptr]] >= x) {
auto u = lower_bound(all.begin(), all.end(), b[ord[ptr]]) - all.begin() + 1;
Modify(u, 1);
ptr--;
}
int p = lower_bound(all.begin(), all.end(), y) - all.begin() + 1;
ans[ind] = Get(N - 1) - Get(p - 1);
}
for (int i = 0;i < N;i++)
f[i] = 0;
all.clear();
for (int i = 1;i <= n;i++)
all.push_back(c[i]);
for (auto u : t2)
all.push_back(get<1>(u));
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
sort(t2.rbegin(), t2.rend());
ptr = n - 1;
for (auto u : t2) {
int x, z, ind;
tie(x, z, ind) = u;
while (ptr >= 0 && a[ord[ptr]] >= x) {
auto u = lower_bound(all.begin(), all.end(), c[ord[ptr]]) - all.begin() + 1;
Modify(u, 1);
ptr--;
}
int p = lower_bound(all.begin(), all.end(), z) - all.begin() + 1;
ans[ind] += Get(N - 1) - Get(p - 1);
}
for (int i = 0;i < N;i++)
f[i] = 0;
all.clear();
for (int i = 1;i <= n;i++)
all.push_back(b[i]);
for (auto u : t3)
all.push_back(get<1>(u));
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
sort(t3.rbegin(), t3.rend());
sort(ord.begin(), ord.end(), [&](int i, int j) {
return c[i] < c[j];
});
ptr = n - 1;
for (auto u : t3) {
int z, y, ind;
tie(z, y, ind) = u;
while (ptr >= 0 && c[ord[ptr]] >= z) {
auto u = lower_bound(all.begin(), all.end(), b[ord[ptr]]) - all.begin() + 1;
Modify(u, 1);
ptr--;
}
int p = lower_bound(all.begin(), all.end(), y) - all.begin() + 1;
ans[ind] -= Get(p - 1);
}
for (int i = 1;i <= q;i++)
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
5720 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
5 ms |
5980 KB |
Output is correct |
8 |
Correct |
5 ms |
5976 KB |
Output is correct |
9 |
Correct |
5 ms |
5976 KB |
Output is correct |
10 |
Correct |
4 ms |
5980 KB |
Output is correct |
11 |
Correct |
4 ms |
6748 KB |
Output is correct |
12 |
Correct |
3 ms |
6748 KB |
Output is correct |
13 |
Correct |
4 ms |
5980 KB |
Output is correct |
14 |
Correct |
4 ms |
5944 KB |
Output is correct |
15 |
Correct |
4 ms |
5888 KB |
Output is correct |
16 |
Correct |
4 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
13840 KB |
Output is correct |
2 |
Correct |
111 ms |
13992 KB |
Output is correct |
3 |
Correct |
119 ms |
14124 KB |
Output is correct |
4 |
Correct |
71 ms |
14268 KB |
Output is correct |
5 |
Correct |
98 ms |
14224 KB |
Output is correct |
6 |
Correct |
62 ms |
14012 KB |
Output is correct |
7 |
Correct |
112 ms |
14012 KB |
Output is correct |
8 |
Correct |
111 ms |
13976 KB |
Output is correct |
9 |
Correct |
105 ms |
13976 KB |
Output is correct |
10 |
Correct |
94 ms |
13504 KB |
Output is correct |
11 |
Correct |
65 ms |
13760 KB |
Output is correct |
12 |
Correct |
49 ms |
12868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
13840 KB |
Output is correct |
2 |
Correct |
111 ms |
13992 KB |
Output is correct |
3 |
Correct |
119 ms |
14124 KB |
Output is correct |
4 |
Correct |
71 ms |
14268 KB |
Output is correct |
5 |
Correct |
98 ms |
14224 KB |
Output is correct |
6 |
Correct |
62 ms |
14012 KB |
Output is correct |
7 |
Correct |
112 ms |
14012 KB |
Output is correct |
8 |
Correct |
111 ms |
13976 KB |
Output is correct |
9 |
Correct |
105 ms |
13976 KB |
Output is correct |
10 |
Correct |
94 ms |
13504 KB |
Output is correct |
11 |
Correct |
65 ms |
13760 KB |
Output is correct |
12 |
Correct |
49 ms |
12868 KB |
Output is correct |
13 |
Correct |
156 ms |
13748 KB |
Output is correct |
14 |
Correct |
143 ms |
15864 KB |
Output is correct |
15 |
Correct |
128 ms |
13980 KB |
Output is correct |
16 |
Correct |
101 ms |
13924 KB |
Output is correct |
17 |
Correct |
133 ms |
13700 KB |
Output is correct |
18 |
Correct |
71 ms |
14584 KB |
Output is correct |
19 |
Correct |
154 ms |
13676 KB |
Output is correct |
20 |
Correct |
149 ms |
13504 KB |
Output is correct |
21 |
Correct |
165 ms |
17164 KB |
Output is correct |
22 |
Correct |
95 ms |
14776 KB |
Output is correct |
23 |
Correct |
65 ms |
15552 KB |
Output is correct |
24 |
Correct |
49 ms |
13688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5468 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
5720 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
5 ms |
5980 KB |
Output is correct |
8 |
Correct |
5 ms |
5976 KB |
Output is correct |
9 |
Correct |
5 ms |
5976 KB |
Output is correct |
10 |
Correct |
4 ms |
5980 KB |
Output is correct |
11 |
Correct |
4 ms |
6748 KB |
Output is correct |
12 |
Correct |
3 ms |
6748 KB |
Output is correct |
13 |
Correct |
4 ms |
5980 KB |
Output is correct |
14 |
Correct |
4 ms |
5944 KB |
Output is correct |
15 |
Correct |
4 ms |
5888 KB |
Output is correct |
16 |
Correct |
4 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
112 ms |
13840 KB |
Output is correct |
20 |
Correct |
111 ms |
13992 KB |
Output is correct |
21 |
Correct |
119 ms |
14124 KB |
Output is correct |
22 |
Correct |
71 ms |
14268 KB |
Output is correct |
23 |
Correct |
98 ms |
14224 KB |
Output is correct |
24 |
Correct |
62 ms |
14012 KB |
Output is correct |
25 |
Correct |
112 ms |
14012 KB |
Output is correct |
26 |
Correct |
111 ms |
13976 KB |
Output is correct |
27 |
Correct |
105 ms |
13976 KB |
Output is correct |
28 |
Correct |
94 ms |
13504 KB |
Output is correct |
29 |
Correct |
65 ms |
13760 KB |
Output is correct |
30 |
Correct |
49 ms |
12868 KB |
Output is correct |
31 |
Correct |
156 ms |
13748 KB |
Output is correct |
32 |
Correct |
143 ms |
15864 KB |
Output is correct |
33 |
Correct |
128 ms |
13980 KB |
Output is correct |
34 |
Correct |
101 ms |
13924 KB |
Output is correct |
35 |
Correct |
133 ms |
13700 KB |
Output is correct |
36 |
Correct |
71 ms |
14584 KB |
Output is correct |
37 |
Correct |
154 ms |
13676 KB |
Output is correct |
38 |
Correct |
149 ms |
13504 KB |
Output is correct |
39 |
Correct |
165 ms |
17164 KB |
Output is correct |
40 |
Correct |
95 ms |
14776 KB |
Output is correct |
41 |
Correct |
65 ms |
15552 KB |
Output is correct |
42 |
Correct |
49 ms |
13688 KB |
Output is correct |
43 |
Correct |
168 ms |
17720 KB |
Output is correct |
44 |
Correct |
174 ms |
17764 KB |
Output is correct |
45 |
Correct |
162 ms |
19588 KB |
Output is correct |
46 |
Correct |
108 ms |
16928 KB |
Output is correct |
47 |
Correct |
146 ms |
16932 KB |
Output is correct |
48 |
Correct |
80 ms |
17944 KB |
Output is correct |
49 |
Correct |
162 ms |
18408 KB |
Output is correct |
50 |
Correct |
175 ms |
22032 KB |
Output is correct |
51 |
Correct |
175 ms |
21484 KB |
Output is correct |
52 |
Correct |
137 ms |
15976 KB |
Output is correct |
53 |
Correct |
66 ms |
16312 KB |
Output is correct |