#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ii = pair<int, int>;
const int MAXN = 6e5 + 5;
#define ALL(a) (a).begin(), (a).end()
#define LB(a, x) (lower_bound(ALL(a), x) - (a).begin() + 1)
struct student {
int x, y, z;
student(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}
};
struct query {
int x, y, z, p;
query(int x = 0, int y = 0, int z = 0, int p = 0) : x(x), y(y), z(z), p(p) {}
};
struct node {
int N;
vector<int> value;
vector<int> BIT;
node(int N = 0, vector<int> value = vector<int>(), vector<int> BIT = vector<int>()) : N(N), value(value), BIT(BIT) {}
void init() {
sort(ALL(value));
value.resize(unique(ALL(value)) - value.begin());
N = value.size() + 5;
BIT.assign(N + 1, 0);
}
void update(int x) {
x = LB(value, x);
for(; x; x -= (x & -x)) BIT[x]++;
}
int get(int x) {
x = LB(value, x);
int res = 0;
for(; x <= N; x += (x & -x))
res += BIT[x];
return res;
}
};
int N, Q;
student a[MAXN];
query b[MAXN];
int ans[MAXN];
struct segment_tree {
int N;
node tree[MAXN * 4];
void init(int _N) {
N = _N;
}
void build() {
for(int i = 1; i <= 4 * N ; i++) {
tree[i].init();
}
}
void update(int id, int l, int r, int p, int x) {
if(r < p or l > p) return;
tree[id].update(x);
if(l == r) return;
int m = (l + r) / 2;
update(id * 2, l, m, p, x);
update(id * 2 + 1, m + 1, r, p, x);
}
void update(int p, int x) {
return update(1, 1, N, p, x);
}
int get(int id, int l, int r, int u, int v, int x) {
if(r < u or l > v) return 0;
if(u <= l and r <= v) {
return tree[id].get(x);
}
int m = (l + r) / 2;
return get(id * 2, l, m, u, v, x) + get(id * 2 + 1, m + 1, r, u, v, x);
}
int get(int l, int r, int x) {
return get(1, 1, N, l, r, x);
}
} ST;
signed main() {
#define TASK ""
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
for(int i = 1; i <= N; i++) {
auto &[x, y, z] = a[i];
cin >> x >> y;
z = x + y;
}
for(int i = 1; i <= Q; i++) {
auto &[x, y, z, p] = b[i];
cin >> x >> y >> z;
p = i;
}
vector<int> comp;
for(int i = 1; i <= N; i++) {
auto [x, y, z] = a[i];
comp.emplace_back(x);
comp.emplace_back(y);
comp.emplace_back(z);
}
for(int i = 1; i <= Q; i++) {
auto [x, y, z, p] = b[i];
comp.emplace_back(x);
comp.emplace_back(y);
comp.emplace_back(z);
}
sort(ALL(comp));
comp.resize(unique(ALL(comp)) - comp.begin());
const int M = comp.size();
for(int i = 1; i <= N; i++) {
auto &[x, y, z] = a[i];
x = LB(comp, x);
y = LB(comp, y);
z = LB(comp, z);
}
for(int i = 1; i <= Q; i++) {
auto &[x, y, z, p] = b[i];
x = LB(comp, x);
y = LB(comp, y);
z = LB(comp, z);
}
ST.init(M);
for(int i = 1; i <= N; i++) {
int id = 1, l = 1, r = M;
int p = a[i].x;
int v = a[i].y;
while(l < r) {
int m = (l + r) / 2;
if(p <= m) {
id = id * 2;
r = m;
} else {
id = id * 2 + 1;
l = m + 1;
}
}
while(id >= 1) {
ST.tree[id].value.emplace_back(v);
id >>= 1;
}
}
ST.build();
sort(a + 1, a + N + 1, [&](const student a, const student b) {
if(a.z != b.z) return a.z > b.z;
if(a.x != b.x) return a.x > b.x;
return a.y > b.y;
});
sort(b + 1, b + Q + 1, [&](const query a, const query b) {
if(a.z != b.z) return a.z > b.z;
if(a.x != b.x) return a.x > b.x;
if(a.y != b.y) return a.y > b.y;
return a.p > b.p;
});
int j = 1;
for(int i = 1; i <= Q; i++) {
while(j <= N and a[j].z >= b[i].z) {
ST.update(a[j].x, a[j].y);
j++;
}
ans[b[i].p] = ST.get(b[i].x, M, b[i].y);
}
for(int i = 1; i <= Q; i++) cout << ans[i] << "\n";
return (0 ^ 0);
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(TASK ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(TASK ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
150620 KB |
Output is correct |
2 |
Correct |
20 ms |
150616 KB |
Output is correct |
3 |
Correct |
19 ms |
150620 KB |
Output is correct |
4 |
Correct |
19 ms |
150620 KB |
Output is correct |
5 |
Correct |
19 ms |
150620 KB |
Output is correct |
6 |
Correct |
19 ms |
150616 KB |
Output is correct |
7 |
Correct |
31 ms |
153796 KB |
Output is correct |
8 |
Correct |
30 ms |
153680 KB |
Output is correct |
9 |
Correct |
33 ms |
153668 KB |
Output is correct |
10 |
Correct |
27 ms |
152920 KB |
Output is correct |
11 |
Correct |
26 ms |
152668 KB |
Output is correct |
12 |
Correct |
21 ms |
150876 KB |
Output is correct |
13 |
Correct |
28 ms |
153436 KB |
Output is correct |
14 |
Correct |
30 ms |
153424 KB |
Output is correct |
15 |
Correct |
29 ms |
153436 KB |
Output is correct |
16 |
Correct |
24 ms |
151900 KB |
Output is correct |
17 |
Correct |
26 ms |
152404 KB |
Output is correct |
18 |
Correct |
21 ms |
150876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
447 ms |
193216 KB |
Output is correct |
2 |
Correct |
447 ms |
192860 KB |
Output is correct |
3 |
Correct |
442 ms |
193372 KB |
Output is correct |
4 |
Correct |
233 ms |
180108 KB |
Output is correct |
5 |
Correct |
284 ms |
179904 KB |
Output is correct |
6 |
Correct |
92 ms |
156864 KB |
Output is correct |
7 |
Correct |
444 ms |
191708 KB |
Output is correct |
8 |
Correct |
430 ms |
191372 KB |
Output is correct |
9 |
Correct |
411 ms |
190652 KB |
Output is correct |
10 |
Correct |
275 ms |
177084 KB |
Output is correct |
11 |
Correct |
196 ms |
179900 KB |
Output is correct |
12 |
Correct |
65 ms |
155852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
447 ms |
193216 KB |
Output is correct |
2 |
Correct |
447 ms |
192860 KB |
Output is correct |
3 |
Correct |
442 ms |
193372 KB |
Output is correct |
4 |
Correct |
233 ms |
180108 KB |
Output is correct |
5 |
Correct |
284 ms |
179904 KB |
Output is correct |
6 |
Correct |
92 ms |
156864 KB |
Output is correct |
7 |
Correct |
444 ms |
191708 KB |
Output is correct |
8 |
Correct |
430 ms |
191372 KB |
Output is correct |
9 |
Correct |
411 ms |
190652 KB |
Output is correct |
10 |
Correct |
275 ms |
177084 KB |
Output is correct |
11 |
Correct |
196 ms |
179900 KB |
Output is correct |
12 |
Correct |
65 ms |
155852 KB |
Output is correct |
13 |
Correct |
522 ms |
195760 KB |
Output is correct |
14 |
Correct |
512 ms |
193144 KB |
Output is correct |
15 |
Correct |
433 ms |
191936 KB |
Output is correct |
16 |
Correct |
269 ms |
181440 KB |
Output is correct |
17 |
Correct |
332 ms |
179828 KB |
Output is correct |
18 |
Correct |
92 ms |
157752 KB |
Output is correct |
19 |
Correct |
522 ms |
195772 KB |
Output is correct |
20 |
Correct |
504 ms |
194496 KB |
Output is correct |
21 |
Correct |
514 ms |
196288 KB |
Output is correct |
22 |
Correct |
268 ms |
177084 KB |
Output is correct |
23 |
Correct |
199 ms |
179388 KB |
Output is correct |
24 |
Correct |
64 ms |
155848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
150620 KB |
Output is correct |
2 |
Correct |
20 ms |
150616 KB |
Output is correct |
3 |
Correct |
19 ms |
150620 KB |
Output is correct |
4 |
Correct |
19 ms |
150620 KB |
Output is correct |
5 |
Correct |
19 ms |
150620 KB |
Output is correct |
6 |
Correct |
19 ms |
150616 KB |
Output is correct |
7 |
Correct |
31 ms |
153796 KB |
Output is correct |
8 |
Correct |
30 ms |
153680 KB |
Output is correct |
9 |
Correct |
33 ms |
153668 KB |
Output is correct |
10 |
Correct |
27 ms |
152920 KB |
Output is correct |
11 |
Correct |
26 ms |
152668 KB |
Output is correct |
12 |
Correct |
21 ms |
150876 KB |
Output is correct |
13 |
Correct |
28 ms |
153436 KB |
Output is correct |
14 |
Correct |
30 ms |
153424 KB |
Output is correct |
15 |
Correct |
29 ms |
153436 KB |
Output is correct |
16 |
Correct |
24 ms |
151900 KB |
Output is correct |
17 |
Correct |
26 ms |
152404 KB |
Output is correct |
18 |
Correct |
21 ms |
150876 KB |
Output is correct |
19 |
Correct |
447 ms |
193216 KB |
Output is correct |
20 |
Correct |
447 ms |
192860 KB |
Output is correct |
21 |
Correct |
442 ms |
193372 KB |
Output is correct |
22 |
Correct |
233 ms |
180108 KB |
Output is correct |
23 |
Correct |
284 ms |
179904 KB |
Output is correct |
24 |
Correct |
92 ms |
156864 KB |
Output is correct |
25 |
Correct |
444 ms |
191708 KB |
Output is correct |
26 |
Correct |
430 ms |
191372 KB |
Output is correct |
27 |
Correct |
411 ms |
190652 KB |
Output is correct |
28 |
Correct |
275 ms |
177084 KB |
Output is correct |
29 |
Correct |
196 ms |
179900 KB |
Output is correct |
30 |
Correct |
65 ms |
155852 KB |
Output is correct |
31 |
Correct |
522 ms |
195760 KB |
Output is correct |
32 |
Correct |
512 ms |
193144 KB |
Output is correct |
33 |
Correct |
433 ms |
191936 KB |
Output is correct |
34 |
Correct |
269 ms |
181440 KB |
Output is correct |
35 |
Correct |
332 ms |
179828 KB |
Output is correct |
36 |
Correct |
92 ms |
157752 KB |
Output is correct |
37 |
Correct |
522 ms |
195772 KB |
Output is correct |
38 |
Correct |
504 ms |
194496 KB |
Output is correct |
39 |
Correct |
514 ms |
196288 KB |
Output is correct |
40 |
Correct |
268 ms |
177084 KB |
Output is correct |
41 |
Correct |
199 ms |
179388 KB |
Output is correct |
42 |
Correct |
64 ms |
155848 KB |
Output is correct |
43 |
Correct |
725 ms |
259860 KB |
Output is correct |
44 |
Correct |
724 ms |
259396 KB |
Output is correct |
45 |
Correct |
764 ms |
259464 KB |
Output is correct |
46 |
Correct |
450 ms |
228032 KB |
Output is correct |
47 |
Correct |
399 ms |
221124 KB |
Output is correct |
48 |
Correct |
100 ms |
157152 KB |
Output is correct |
49 |
Correct |
703 ms |
260508 KB |
Output is correct |
50 |
Correct |
704 ms |
259772 KB |
Output is correct |
51 |
Correct |
670 ms |
260292 KB |
Output is correct |
52 |
Correct |
388 ms |
207808 KB |
Output is correct |
53 |
Correct |
268 ms |
199356 KB |
Output is correct |