#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll
struct query {
int a, b, sum, id;
query() {};
query(int a, int b, int sum, int id) : a(a), b(b), sum(sum), id(id) {};
};
const int N = 2e5 + 10;
vector<int> vls[4 * N], fen[4 * N], xx[N];
int on[4 * N], ans[N];
void merge(int v) {
int ptr1 = 0, ptr2 = 0;
while (ptr1 < SZ(vls[2 * v]) && ptr2 < SZ(vls[2 * v + 1])) {
if (vls[2 * v][ptr1] < vls[2 * v + 1][ptr2]) {
if (vls[v].empty() || vls[v].back() != vls[2 * v][ptr1]) {
vls[v].pb(vls[2 * v][ptr1]);
}
ptr1++;
}
else {
if (vls[v].empty() || vls[v].back() != vls[2 * v + 1][ptr2]) {
vls[v].pb(vls[2 * v + 1][ptr2]);
}
ptr2++;
}
}
while (ptr1 < SZ(vls[2 * v])) {
if (vls[v].empty() || vls[v].back() != vls[2 * v][ptr1]) {
vls[v].pb(vls[2 * v][ptr1]);
}
ptr1++;
}
while (ptr2 < SZ(vls[2 * v + 1])) {
if (vls[v].empty() || vls[v].back() != vls[2 * v + 1][ptr2]) {
vls[v].pb(vls[2 * v + 1][ptr2]);
}
ptr2++;
}
}
void build(int v, int l, int r) {
if (l == r) {
vls[v] = xx[l];
on[v] = 0;
fen[v].resize(SZ(vls[v]) + 1);
return;
}
int mid = (l + r) / 2;
build(2 * v, l, mid);
build(2 * v + 1, mid + 1, r);
merge(v);
on[v] = 0;
fen[v].resize(SZ(vls[v]) + 1);
}
void update(int v, int l, int r, int x, int y) {
on[v]++;
int pos = upper_bound(all(vls[v]), y) - vls[v].begin();
for (; pos <= SZ(vls[v]); pos += pos & (-pos)) {
fen[v][pos]++;
}
if (l == r) {
return;
}
int mid = (l + r) / 2;
if (x <= mid) {
update(2 * v, l, mid, x, y);
}
else {
update(2 * v + 1, mid + 1, r, x, y);
}
}
int get(int v, int l, int r, int a, int b, int y) {
if (l > b || r < a) {
return 0;
}
if (l >= a && r <= b) {
int pos = lower_bound(all(vls[v]), y) - vls[v].begin(), res = on[v];
for (; pos > 0; pos -= pos & (-pos)) {
res -= fen[v][pos];
}
return res;
}
int mid = (l + r) / 2;
return get(2 * v, l, mid, a, b, y) + get(2 * v + 1, mid + 1, r, a, b, y);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n), b(n), sa, sb, ss;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
sa.pb(a[i]);
sb.pb(b[i]);
ss.pb(a[i] + b[i]);
}
vector<query> que(m);
for (int i = 0; i < m; i++) {
cin >> que[i].a >> que[i].b >> que[i].sum;
que[i].id = i;
sa.pb(que[i].a);
sb.pb(que[i].b);
ss.pb(que[i].sum);
}
sort(all(sa));
sort(all(sb));
sort(all(ss));
sa.resize(unique(all(sa)) - sa.begin());
sb.resize(unique(all(sb)) - sb.begin());
ss.resize(unique(all(ss)) - ss.begin());
vector<vector<pair<int, int>>> add(SZ(ss));
for (int i = 0; i < n; i++) {
int cs = lower_bound(all(ss), a[i] + b[i]) - ss.begin();
a[i] = upper_bound(all(sa), a[i]) - sa.begin();
b[i] = upper_bound(all(sb), b[i]) - sb.begin();
xx[a[i]].pb(b[i]);
add[cs].pb({a[i], b[i]});
}
for (int i = 1; i <= SZ(sa); i++) {
sort(all(xx[i]));
xx[i].resize(unique(all(xx[i])) - xx[i].begin());
}
build(1, 1, SZ(sa));
vector<vector<pair<pair<int, int>, int>>> check(SZ(ss));
for (int i = 0; i < m; i++) {
que[i].sum = lower_bound(all(ss), que[i].sum) - ss.begin();
que[i].a = upper_bound(all(sa), que[i].a) - sa.begin();
que[i].b = upper_bound(all(sb), que[i].b) - sb.begin();
check[que[i].sum].pb(make_pair(make_pair(que[i].a, que[i].b), que[i].id));
}
for (int i = SZ(ss) - 1; i >= 0; i--) {
for (auto it : add[i]) {
update(1, 1, SZ(sa), it.F, it.S);
}
for (auto it : check[i]) {
ans[it.S] = get(1, 1, SZ(sa), it.F.F, SZ(sa), it.F.S);
}
}
for (int i = 0; i < m; i++) {
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
42616 KB |
Output is correct |
2 |
Correct |
32 ms |
42616 KB |
Output is correct |
3 |
Correct |
32 ms |
42616 KB |
Output is correct |
4 |
Correct |
44 ms |
42616 KB |
Output is correct |
5 |
Correct |
33 ms |
42616 KB |
Output is correct |
6 |
Correct |
34 ms |
42616 KB |
Output is correct |
7 |
Correct |
49 ms |
44280 KB |
Output is correct |
8 |
Correct |
48 ms |
44280 KB |
Output is correct |
9 |
Correct |
45 ms |
44280 KB |
Output is correct |
10 |
Correct |
42 ms |
44152 KB |
Output is correct |
11 |
Correct |
39 ms |
43488 KB |
Output is correct |
12 |
Correct |
37 ms |
43000 KB |
Output is correct |
13 |
Correct |
47 ms |
44152 KB |
Output is correct |
14 |
Correct |
49 ms |
44152 KB |
Output is correct |
15 |
Correct |
47 ms |
44152 KB |
Output is correct |
16 |
Correct |
41 ms |
43128 KB |
Output is correct |
17 |
Correct |
40 ms |
44152 KB |
Output is correct |
18 |
Correct |
33 ms |
42932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
857 ms |
81000 KB |
Output is correct |
2 |
Correct |
817 ms |
80940 KB |
Output is correct |
3 |
Correct |
828 ms |
80936 KB |
Output is correct |
4 |
Correct |
389 ms |
69588 KB |
Output is correct |
5 |
Correct |
332 ms |
59068 KB |
Output is correct |
6 |
Correct |
372 ms |
51796 KB |
Output is correct |
7 |
Correct |
771 ms |
78868 KB |
Output is correct |
8 |
Correct |
787 ms |
80920 KB |
Output is correct |
9 |
Correct |
700 ms |
79060 KB |
Output is correct |
10 |
Correct |
214 ms |
56660 KB |
Output is correct |
11 |
Correct |
319 ms |
68180 KB |
Output is correct |
12 |
Correct |
103 ms |
51284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
857 ms |
81000 KB |
Output is correct |
2 |
Correct |
817 ms |
80940 KB |
Output is correct |
3 |
Correct |
828 ms |
80936 KB |
Output is correct |
4 |
Correct |
389 ms |
69588 KB |
Output is correct |
5 |
Correct |
332 ms |
59068 KB |
Output is correct |
6 |
Correct |
372 ms |
51796 KB |
Output is correct |
7 |
Correct |
771 ms |
78868 KB |
Output is correct |
8 |
Correct |
787 ms |
80920 KB |
Output is correct |
9 |
Correct |
700 ms |
79060 KB |
Output is correct |
10 |
Correct |
214 ms |
56660 KB |
Output is correct |
11 |
Correct |
319 ms |
68180 KB |
Output is correct |
12 |
Correct |
103 ms |
51284 KB |
Output is correct |
13 |
Correct |
922 ms |
83928 KB |
Output is correct |
14 |
Correct |
937 ms |
83160 KB |
Output is correct |
15 |
Correct |
804 ms |
81128 KB |
Output is correct |
16 |
Correct |
455 ms |
71000 KB |
Output is correct |
17 |
Correct |
351 ms |
60248 KB |
Output is correct |
18 |
Correct |
127 ms |
51928 KB |
Output is correct |
19 |
Correct |
938 ms |
83800 KB |
Output is correct |
20 |
Correct |
937 ms |
83408 KB |
Output is correct |
21 |
Correct |
886 ms |
83288 KB |
Output is correct |
22 |
Correct |
223 ms |
56660 KB |
Output is correct |
23 |
Correct |
323 ms |
68180 KB |
Output is correct |
24 |
Correct |
97 ms |
51284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
42616 KB |
Output is correct |
2 |
Correct |
32 ms |
42616 KB |
Output is correct |
3 |
Correct |
32 ms |
42616 KB |
Output is correct |
4 |
Correct |
44 ms |
42616 KB |
Output is correct |
5 |
Correct |
33 ms |
42616 KB |
Output is correct |
6 |
Correct |
34 ms |
42616 KB |
Output is correct |
7 |
Correct |
49 ms |
44280 KB |
Output is correct |
8 |
Correct |
48 ms |
44280 KB |
Output is correct |
9 |
Correct |
45 ms |
44280 KB |
Output is correct |
10 |
Correct |
42 ms |
44152 KB |
Output is correct |
11 |
Correct |
39 ms |
43488 KB |
Output is correct |
12 |
Correct |
37 ms |
43000 KB |
Output is correct |
13 |
Correct |
47 ms |
44152 KB |
Output is correct |
14 |
Correct |
49 ms |
44152 KB |
Output is correct |
15 |
Correct |
47 ms |
44152 KB |
Output is correct |
16 |
Correct |
41 ms |
43128 KB |
Output is correct |
17 |
Correct |
40 ms |
44152 KB |
Output is correct |
18 |
Correct |
33 ms |
42932 KB |
Output is correct |
19 |
Correct |
857 ms |
81000 KB |
Output is correct |
20 |
Correct |
817 ms |
80940 KB |
Output is correct |
21 |
Correct |
828 ms |
80936 KB |
Output is correct |
22 |
Correct |
389 ms |
69588 KB |
Output is correct |
23 |
Correct |
332 ms |
59068 KB |
Output is correct |
24 |
Correct |
372 ms |
51796 KB |
Output is correct |
25 |
Correct |
771 ms |
78868 KB |
Output is correct |
26 |
Correct |
787 ms |
80920 KB |
Output is correct |
27 |
Correct |
700 ms |
79060 KB |
Output is correct |
28 |
Correct |
214 ms |
56660 KB |
Output is correct |
29 |
Correct |
319 ms |
68180 KB |
Output is correct |
30 |
Correct |
103 ms |
51284 KB |
Output is correct |
31 |
Correct |
922 ms |
83928 KB |
Output is correct |
32 |
Correct |
937 ms |
83160 KB |
Output is correct |
33 |
Correct |
804 ms |
81128 KB |
Output is correct |
34 |
Correct |
455 ms |
71000 KB |
Output is correct |
35 |
Correct |
351 ms |
60248 KB |
Output is correct |
36 |
Correct |
127 ms |
51928 KB |
Output is correct |
37 |
Correct |
938 ms |
83800 KB |
Output is correct |
38 |
Correct |
937 ms |
83408 KB |
Output is correct |
39 |
Correct |
886 ms |
83288 KB |
Output is correct |
40 |
Correct |
223 ms |
56660 KB |
Output is correct |
41 |
Correct |
323 ms |
68180 KB |
Output is correct |
42 |
Correct |
97 ms |
51284 KB |
Output is correct |
43 |
Correct |
1158 ms |
102616 KB |
Output is correct |
44 |
Correct |
1121 ms |
107480 KB |
Output is correct |
45 |
Correct |
1114 ms |
107352 KB |
Output is correct |
46 |
Correct |
568 ms |
94680 KB |
Output is correct |
47 |
Correct |
392 ms |
71512 KB |
Output is correct |
48 |
Correct |
149 ms |
52056 KB |
Output is correct |
49 |
Correct |
1054 ms |
106584 KB |
Output is correct |
50 |
Correct |
1087 ms |
107408 KB |
Output is correct |
51 |
Correct |
1000 ms |
106496 KB |
Output is correct |
52 |
Correct |
316 ms |
69208 KB |
Output is correct |
53 |
Correct |
411 ms |
86840 KB |
Output is correct |