#include<bits/stdc++.h>
using namespace std;
const int SZ = (1 << 17);
static char buf[450 << 20];
void* operator new(size_t s) {
static size_t i = sizeof buf;
assert(s < i);
return (void*) & buf[i -= s];
}
void operator delete(void*) {}
// [0, SZ - 1) queries for each axis
template<class T> struct Node {
T val = 0;
Node<T>* c[2];
Node() { c[0] = c[1] = NULL; }
void upd(int ind, T v, int L = 0, int R = SZ - 1) { // add v
if (L == ind && R == ind) { val += v; return; }
int M = (L + R) / 2;
if (ind <= M) {
if (!c[0]) c[0] = new Node();
c[0]->upd(ind, v, L, M);
} else {
if (!c[1]) c[1] = new Node();
c[1]->upd(ind, v, M + 1, R);
}
val = 0;
for (int i = 0; i < 2; i++)
if (c[i]) val += c[i]->val;
}
T query(int lo, int hi, int L = 0, int R = SZ - 1) { // query sum of segment
if (hi < L || R < lo) return 0;
if (lo <= L && R <= hi) return val;
int M = (L + R) / 2;
T res = 0;
if (c[0]) res += c[0]->query(lo, hi, L, M);
if (c[1]) res += c[1]->query(lo, hi, M + 1, R);
return res;
}
void update_2d(int ind, Node* c0, Node* c1, int L = 0, int R = SZ - 1) { // for 2D segtree
if (L != R) {
int M = (L + R) / 2;
if (ind <= M) {
if (!c[0]) c[0] = new Node();
c[0]->update_2d(ind, (c0 ? c0->c[0] : NULL), (c1 ? c1->c[0] : NULL), L, M);
} else {
if (!c[1]) c[1] = new Node();
c[1]->update_2d(ind, (c0 ? c0->c[1] : NULL), (c1 ? c1->c[1] : NULL), M + 1, R);
}
}
val = (c0 ? c0->val : 0) + (c1 ? c1->val : 0);
}
};
template<class T> struct BITSeg { // 0-indexed implicitly
std::vector<Node<T>> seg;
BITSeg() {
seg.resize(SZ + 1);
for (int i = 0; i < SZ; i++)
seg[i] = Node<T>();
}
void upd(int x, int y, int v) { // add v
x++;
for (; x < SZ + 1; x += x & -x)
seg[x].upd(y, v);
}
T query(int x, int y1, int y2) {
if (x <= 0) return 0;
T res = 0;
for (; x; x -= x & -x)
res += seg[x].query(y1, y2);
return res;
}
T query(int x1, int x2, int y1, int y2) { // query sum of rectangle
x1++, x2++;
return query(x2, y1, y2) - query(x1 - 1, y1, y2);
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q; cin >> n >> q;
vector<array<int, 2>> v(n);
set<int> xs, ys;
for (int i = 0; i < n; i++) {
cin >> v[i][0] >> v[i][1];
xs.insert(v[i][0]);
ys.insert(v[i][1]);
}
vector<int> ans(q);
vector<array<int, 4>> queries(q);
for (int i = 0; i < q; i++) {
queries[i][0] = i;
cin >> queries[i][1] >> queries[i][2] >> queries[i][3];
xs.insert(queries[i][1]);
ys.insert(queries[i][2]);
}
map<int, int> mx, my;
int cnt = 0;
for (int x : xs)
mx[x] = cnt, cnt++;
cnt = 0;
for (int y : ys)
my[y] = cnt, cnt++;
sort(queries.begin(), queries.end(), [](array<int, 4> a, array<int, 4> b) {
return a[3] < b[3];
});
sort(v.begin(), v.end(), [](array<int, 2> a, array<int, 2> b) {
return a[0] + a[1] < b[0] + b[1];
});
BITSeg<int> seg;
int qv = n;
for (int i = q - 1; i >= 0; i--) {
int sum = queries[i][3];
while (qv > 0 && v[qv - 1][0] + v[qv - 1][1] >= sum) {
qv--;
seg.upd(mx[v[qv][0]], my[v[qv][1]], 1);
}
ans[queries[i][0]] = seg.query(mx[queries[i][1]], SZ - 1, my[queries[i][2]], SZ - 1);
}
for (int i = 0; i < q; i++)
cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
Output is correct |
2 |
Correct |
3 ms |
3436 KB |
Output is correct |
3 |
Correct |
3 ms |
3436 KB |
Output is correct |
4 |
Correct |
3 ms |
3436 KB |
Output is correct |
5 |
Correct |
3 ms |
3436 KB |
Output is correct |
6 |
Correct |
3 ms |
3436 KB |
Output is correct |
7 |
Correct |
23 ms |
9836 KB |
Output is correct |
8 |
Correct |
24 ms |
9964 KB |
Output is correct |
9 |
Correct |
23 ms |
9964 KB |
Output is correct |
10 |
Correct |
14 ms |
5996 KB |
Output is correct |
11 |
Correct |
16 ms |
7532 KB |
Output is correct |
12 |
Correct |
10 ms |
3564 KB |
Output is correct |
13 |
Correct |
24 ms |
9964 KB |
Output is correct |
14 |
Correct |
24 ms |
9964 KB |
Output is correct |
15 |
Correct |
24 ms |
9836 KB |
Output is correct |
16 |
Correct |
16 ms |
7532 KB |
Output is correct |
17 |
Correct |
13 ms |
5740 KB |
Output is correct |
18 |
Correct |
8 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1667 ms |
213420 KB |
Output is correct |
2 |
Correct |
1618 ms |
212844 KB |
Output is correct |
3 |
Correct |
1644 ms |
212480 KB |
Output is correct |
4 |
Correct |
579 ms |
50996 KB |
Output is correct |
5 |
Correct |
552 ms |
79000 KB |
Output is correct |
6 |
Correct |
229 ms |
6764 KB |
Output is correct |
7 |
Correct |
1312 ms |
203256 KB |
Output is correct |
8 |
Correct |
1474 ms |
203372 KB |
Output is correct |
9 |
Correct |
1083 ms |
194796 KB |
Output is correct |
10 |
Correct |
490 ms |
75244 KB |
Output is correct |
11 |
Correct |
400 ms |
42604 KB |
Output is correct |
12 |
Correct |
197 ms |
6344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1667 ms |
213420 KB |
Output is correct |
2 |
Correct |
1618 ms |
212844 KB |
Output is correct |
3 |
Correct |
1644 ms |
212480 KB |
Output is correct |
4 |
Correct |
579 ms |
50996 KB |
Output is correct |
5 |
Correct |
552 ms |
79000 KB |
Output is correct |
6 |
Correct |
229 ms |
6764 KB |
Output is correct |
7 |
Correct |
1312 ms |
203256 KB |
Output is correct |
8 |
Correct |
1474 ms |
203372 KB |
Output is correct |
9 |
Correct |
1083 ms |
194796 KB |
Output is correct |
10 |
Correct |
490 ms |
75244 KB |
Output is correct |
11 |
Correct |
400 ms |
42604 KB |
Output is correct |
12 |
Correct |
197 ms |
6344 KB |
Output is correct |
13 |
Correct |
1420 ms |
212492 KB |
Output is correct |
14 |
Correct |
1659 ms |
213100 KB |
Output is correct |
15 |
Correct |
1641 ms |
212480 KB |
Output is correct |
16 |
Correct |
482 ms |
51052 KB |
Output is correct |
17 |
Correct |
514 ms |
79164 KB |
Output is correct |
18 |
Correct |
231 ms |
6740 KB |
Output is correct |
19 |
Correct |
1474 ms |
212328 KB |
Output is correct |
20 |
Correct |
1500 ms |
212704 KB |
Output is correct |
21 |
Correct |
1236 ms |
206372 KB |
Output is correct |
22 |
Correct |
483 ms |
75372 KB |
Output is correct |
23 |
Correct |
407 ms |
42732 KB |
Output is correct |
24 |
Correct |
201 ms |
6380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3436 KB |
Output is correct |
2 |
Correct |
3 ms |
3436 KB |
Output is correct |
3 |
Correct |
3 ms |
3436 KB |
Output is correct |
4 |
Correct |
3 ms |
3436 KB |
Output is correct |
5 |
Correct |
3 ms |
3436 KB |
Output is correct |
6 |
Correct |
3 ms |
3436 KB |
Output is correct |
7 |
Correct |
23 ms |
9836 KB |
Output is correct |
8 |
Correct |
24 ms |
9964 KB |
Output is correct |
9 |
Correct |
23 ms |
9964 KB |
Output is correct |
10 |
Correct |
14 ms |
5996 KB |
Output is correct |
11 |
Correct |
16 ms |
7532 KB |
Output is correct |
12 |
Correct |
10 ms |
3564 KB |
Output is correct |
13 |
Correct |
24 ms |
9964 KB |
Output is correct |
14 |
Correct |
24 ms |
9964 KB |
Output is correct |
15 |
Correct |
24 ms |
9836 KB |
Output is correct |
16 |
Correct |
16 ms |
7532 KB |
Output is correct |
17 |
Correct |
13 ms |
5740 KB |
Output is correct |
18 |
Correct |
8 ms |
3564 KB |
Output is correct |
19 |
Correct |
1667 ms |
213420 KB |
Output is correct |
20 |
Correct |
1618 ms |
212844 KB |
Output is correct |
21 |
Correct |
1644 ms |
212480 KB |
Output is correct |
22 |
Correct |
579 ms |
50996 KB |
Output is correct |
23 |
Correct |
552 ms |
79000 KB |
Output is correct |
24 |
Correct |
229 ms |
6764 KB |
Output is correct |
25 |
Correct |
1312 ms |
203256 KB |
Output is correct |
26 |
Correct |
1474 ms |
203372 KB |
Output is correct |
27 |
Correct |
1083 ms |
194796 KB |
Output is correct |
28 |
Correct |
490 ms |
75244 KB |
Output is correct |
29 |
Correct |
400 ms |
42604 KB |
Output is correct |
30 |
Correct |
197 ms |
6344 KB |
Output is correct |
31 |
Correct |
1420 ms |
212492 KB |
Output is correct |
32 |
Correct |
1659 ms |
213100 KB |
Output is correct |
33 |
Correct |
1641 ms |
212480 KB |
Output is correct |
34 |
Correct |
482 ms |
51052 KB |
Output is correct |
35 |
Correct |
514 ms |
79164 KB |
Output is correct |
36 |
Correct |
231 ms |
6740 KB |
Output is correct |
37 |
Correct |
1474 ms |
212328 KB |
Output is correct |
38 |
Correct |
1500 ms |
212704 KB |
Output is correct |
39 |
Correct |
1236 ms |
206372 KB |
Output is correct |
40 |
Correct |
483 ms |
75372 KB |
Output is correct |
41 |
Correct |
407 ms |
42732 KB |
Output is correct |
42 |
Correct |
201 ms |
6380 KB |
Output is correct |
43 |
Runtime error |
512 ms |
75628 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
44 |
Halted |
0 ms |
0 KB |
- |