#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct Triple {
int A, B, C;
Triple() {}
Triple(int A, int B, int C) : A(A), B(B), C(C) {}
bool operator < (const Triple &o) const { return C < o.C; }
};
int N, Q;
vector<pair<Triple, int>> student;
vector<pair<Triple, int>> query;
vector<int> answer;
vector<ordered_set<pair<int, int>>> seg; // merge sort tree with insertions and lower bound operation
void InsertElement(int pos, pair<int, int> elem) {
for (pos += N; pos > 0; pos /= 2) {
seg[pos].insert(elem);
}
}
int OrderOfKey(int l, int r, int key) { // find nummber of elements in [l...r] that is < key
int res = 0;
for (l += N, r += N; l < r; l /= 2, r /= 2) {
if (l & 1) res += seg[l++].order_of_key(make_pair(key, -1));
if (r & 1) res += seg[--r].order_of_key(make_pair(key, -1));
}
return res;
}
int NumberOfElements(int l, int r) {
int res = 0;
for (l += N, r += N; l < r; l /= 2, r /= 2) {
if (l & 1) res += seg[l++].size();
if (r & 1) res += seg[--r].size();
}
return res;
}
void read() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> Q;
for (int i = 0; i < N; i++) {
int A, B;
cin >> A >> B;
student.emplace_back(Triple(A, B, A + B), i);
}
for (int i = 0; i < Q; i++) {
int A, B, C;
cin >> A >> B >> C;
query.emplace_back(Triple(A, B, C), i);
answer.emplace_back(-1);
}
}
void solve() {
vector<pair<int, int>> allA; // coordinate compression
for (int i = 0; i < N; i++) {
allA.emplace_back(student[i].first.A, student[i].second);
}
sort(begin(allA), end(allA));
// sort by C, inserting elements that fulfill the required C into the merge sort tree
sort(begin(student), end(student));
sort(begin(query), end(query));
reverse(begin(query), end(query));
seg.resize(2 * N);
for (auto &q_ : query) {
Triple q = q_.first;
int qi = q_.second;
while (!student.empty() && student.back().first.C >= q.C) {
int pos = lower_bound(begin(allA), end(allA), make_pair(student.back().first.A, student.back().second)) - begin(allA);
InsertElement(pos, make_pair(student.back().first.B, student.back().second));
student.pop_back();
}
int l = lower_bound(begin(allA), end(allA), make_pair(q.A, -1)) - begin(allA);
answer[qi] = NumberOfElements(l, N) - OrderOfKey(l, N, q.B);
}
}
void write() {
for (int i = 0; i < Q; i++) {
cout << answer[i] << "\n";
}
}
int main() {
read();
solve();
write();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
20 ms |
3584 KB |
Output is correct |
8 |
Correct |
20 ms |
3584 KB |
Output is correct |
9 |
Correct |
20 ms |
3456 KB |
Output is correct |
10 |
Correct |
19 ms |
3584 KB |
Output is correct |
11 |
Correct |
19 ms |
3584 KB |
Output is correct |
12 |
Correct |
18 ms |
3456 KB |
Output is correct |
13 |
Correct |
21 ms |
3584 KB |
Output is correct |
14 |
Correct |
20 ms |
3584 KB |
Output is correct |
15 |
Correct |
21 ms |
3584 KB |
Output is correct |
16 |
Correct |
17 ms |
3584 KB |
Output is correct |
17 |
Correct |
18 ms |
3584 KB |
Output is correct |
18 |
Correct |
15 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2595 ms |
137408 KB |
Output is correct |
2 |
Correct |
2596 ms |
137480 KB |
Output is correct |
3 |
Correct |
2586 ms |
137572 KB |
Output is correct |
4 |
Correct |
1625 ms |
136664 KB |
Output is correct |
5 |
Correct |
2296 ms |
136964 KB |
Output is correct |
6 |
Correct |
1595 ms |
136052 KB |
Output is correct |
7 |
Correct |
2428 ms |
137404 KB |
Output is correct |
8 |
Correct |
2424 ms |
137432 KB |
Output is correct |
9 |
Correct |
2186 ms |
137328 KB |
Output is correct |
10 |
Correct |
2033 ms |
136672 KB |
Output is correct |
11 |
Correct |
1411 ms |
136424 KB |
Output is correct |
12 |
Correct |
1036 ms |
135684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2595 ms |
137408 KB |
Output is correct |
2 |
Correct |
2596 ms |
137480 KB |
Output is correct |
3 |
Correct |
2586 ms |
137572 KB |
Output is correct |
4 |
Correct |
1625 ms |
136664 KB |
Output is correct |
5 |
Correct |
2296 ms |
136964 KB |
Output is correct |
6 |
Correct |
1595 ms |
136052 KB |
Output is correct |
7 |
Correct |
2428 ms |
137404 KB |
Output is correct |
8 |
Correct |
2424 ms |
137432 KB |
Output is correct |
9 |
Correct |
2186 ms |
137328 KB |
Output is correct |
10 |
Correct |
2033 ms |
136672 KB |
Output is correct |
11 |
Correct |
1411 ms |
136424 KB |
Output is correct |
12 |
Correct |
1036 ms |
135684 KB |
Output is correct |
13 |
Correct |
2419 ms |
137940 KB |
Output is correct |
14 |
Correct |
2631 ms |
137888 KB |
Output is correct |
15 |
Correct |
2634 ms |
137684 KB |
Output is correct |
16 |
Correct |
1643 ms |
137144 KB |
Output is correct |
17 |
Correct |
2354 ms |
137108 KB |
Output is correct |
18 |
Correct |
1602 ms |
136116 KB |
Output is correct |
19 |
Correct |
2423 ms |
138088 KB |
Output is correct |
20 |
Correct |
2550 ms |
138256 KB |
Output is correct |
21 |
Correct |
2252 ms |
137776 KB |
Output is correct |
22 |
Correct |
2051 ms |
136620 KB |
Output is correct |
23 |
Correct |
1393 ms |
136548 KB |
Output is correct |
24 |
Correct |
1037 ms |
135784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
20 ms |
3584 KB |
Output is correct |
8 |
Correct |
20 ms |
3584 KB |
Output is correct |
9 |
Correct |
20 ms |
3456 KB |
Output is correct |
10 |
Correct |
19 ms |
3584 KB |
Output is correct |
11 |
Correct |
19 ms |
3584 KB |
Output is correct |
12 |
Correct |
18 ms |
3456 KB |
Output is correct |
13 |
Correct |
21 ms |
3584 KB |
Output is correct |
14 |
Correct |
20 ms |
3584 KB |
Output is correct |
15 |
Correct |
21 ms |
3584 KB |
Output is correct |
16 |
Correct |
17 ms |
3584 KB |
Output is correct |
17 |
Correct |
18 ms |
3584 KB |
Output is correct |
18 |
Correct |
15 ms |
3456 KB |
Output is correct |
19 |
Correct |
2595 ms |
137408 KB |
Output is correct |
20 |
Correct |
2596 ms |
137480 KB |
Output is correct |
21 |
Correct |
2586 ms |
137572 KB |
Output is correct |
22 |
Correct |
1625 ms |
136664 KB |
Output is correct |
23 |
Correct |
2296 ms |
136964 KB |
Output is correct |
24 |
Correct |
1595 ms |
136052 KB |
Output is correct |
25 |
Correct |
2428 ms |
137404 KB |
Output is correct |
26 |
Correct |
2424 ms |
137432 KB |
Output is correct |
27 |
Correct |
2186 ms |
137328 KB |
Output is correct |
28 |
Correct |
2033 ms |
136672 KB |
Output is correct |
29 |
Correct |
1411 ms |
136424 KB |
Output is correct |
30 |
Correct |
1036 ms |
135684 KB |
Output is correct |
31 |
Correct |
2419 ms |
137940 KB |
Output is correct |
32 |
Correct |
2631 ms |
137888 KB |
Output is correct |
33 |
Correct |
2634 ms |
137684 KB |
Output is correct |
34 |
Correct |
1643 ms |
137144 KB |
Output is correct |
35 |
Correct |
2354 ms |
137108 KB |
Output is correct |
36 |
Correct |
1602 ms |
136116 KB |
Output is correct |
37 |
Correct |
2423 ms |
138088 KB |
Output is correct |
38 |
Correct |
2550 ms |
138256 KB |
Output is correct |
39 |
Correct |
2252 ms |
137776 KB |
Output is correct |
40 |
Correct |
2051 ms |
136620 KB |
Output is correct |
41 |
Correct |
1393 ms |
136548 KB |
Output is correct |
42 |
Correct |
1037 ms |
135784 KB |
Output is correct |
43 |
Correct |
2434 ms |
140032 KB |
Output is correct |
44 |
Correct |
2422 ms |
139932 KB |
Output is correct |
45 |
Correct |
2665 ms |
139884 KB |
Output is correct |
46 |
Correct |
1589 ms |
138272 KB |
Output is correct |
47 |
Correct |
2337 ms |
138280 KB |
Output is correct |
48 |
Correct |
1421 ms |
135812 KB |
Output is correct |
49 |
Correct |
2457 ms |
139688 KB |
Output is correct |
50 |
Correct |
2264 ms |
139672 KB |
Output is correct |
51 |
Correct |
2156 ms |
139624 KB |
Output is correct |
52 |
Correct |
2080 ms |
138088 KB |
Output is correct |
53 |
Correct |
1413 ms |
137300 KB |
Output is correct |