#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 10;
const int MAX_Q = 1e5 + 10;
int S[MAX_N], T[MAX_N];
int X[MAX_Q], Y[MAX_Q], Z[MAX_Q], ans[MAX_Q];
vector <int> A, B, C;
vector <int> update, query;
class FenwickTree {
private :
int N;
vector <int> tree;
public :
FenwickTree() {}
FenwickTree(int n) : N(n), tree(N + 1, 0) {}
void add(int x, int v) {
for(int i = x; i <= N; i += (i & -i)) {
tree[i] += v;
}
}
int get(int x) {
int res = 0;
for(int i = x; i >= 1; i -= (i & -i)) {
res += tree[i];
}
return res;
}
};
int getIndex(vector <int> &a, int x) {
return lower_bound(a.begin(), a.end(), x) - a.begin() + 1;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, Q;
cin >> N >> Q;
for(int i = 1; i <= N; i++) {
cin >> S[i] >> T[i];
A.push_back(S[i]);
B.push_back(T[i]);
C.push_back(S[i] + T[i]);
}
for(int i = 1; i <= Q; i++) {
cin >> X[i] >> Y[i] >> Z[i];
A.push_back(X[i]);
B.push_back(Y[i]);
C.push_back(S[i] + T[i]);
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
sort(C.begin(), C.end());
A.resize(unique(A.begin(), A.end()) - A.begin());
B.resize(unique(B.begin(), B.end()) - B.begin());
C.resize(unique(C.begin(), C.end()) - C.begin());
int j;
FenwickTree fwA, fwB;
update.resize(N);
iota(update.begin(), update.end(), 1);
// X + Y >= Z
fwB = FenwickTree(B.size());
query.clear();
for(int i = 1; i <= Q; i++) {
if(X[i] + Y[i] >= Z[i]) {
query.push_back(i);
}
}
sort(update.begin(), update.end(), [&](const int &a, const int &b) {
return S[a] > S[b];
});
sort(query.begin(), query.end(), [&](const int &a, const int &b) {
return X[a] > X[b];
});
j = 0;
for(auto i : query) {
while(j < N and S[update[j]] >= X[i]) {
fwB.add(getIndex(B, T[update[j]]), 1);
j++;
}
ans[i] = j - fwB.get(getIndex(B, Y[i]) - 1);
}
// X + Y < Z
fwA = FenwickTree(A.size());
fwB = FenwickTree(B.size());
query.clear();
for(int i = 1; i <= Q; i++) {
if(X[i] + Y[i] < Z[i]) {
query.push_back(i);
}
}
sort(update.begin(), update.end(), [&](const int &a, const int &b) {
return S[a] + T[a] > S[b] + T[b];
});
sort(query.begin(), query.end(), [&](const int &a, const int &b) {
return Z[a] > Z[b];
});
j = 0;
for(auto i : query) {
while(j < N and S[update[j]] + T[update[j]] >= Z[i]) {
fwA.add(getIndex(A, S[update[j]]), 1);
fwB.add(getIndex(B, T[update[j]]), 1);
j++;
}
ans[i] += j - fwA.get(getIndex(A, X[i]) - 1) - fwB.get(getIndex(B, Y[i]) - 1);
}
for(int i = 1; i <= Q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
728 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
4 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
724 KB |
Output is correct |
14 |
Correct |
4 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
684 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
10024 KB |
Output is correct |
2 |
Correct |
155 ms |
10128 KB |
Output is correct |
3 |
Correct |
153 ms |
10068 KB |
Output is correct |
4 |
Correct |
105 ms |
8624 KB |
Output is correct |
5 |
Correct |
121 ms |
8968 KB |
Output is correct |
6 |
Correct |
73 ms |
7868 KB |
Output is correct |
7 |
Correct |
150 ms |
9948 KB |
Output is correct |
8 |
Correct |
149 ms |
9744 KB |
Output is correct |
9 |
Correct |
142 ms |
9804 KB |
Output is correct |
10 |
Correct |
113 ms |
8800 KB |
Output is correct |
11 |
Correct |
96 ms |
8416 KB |
Output is correct |
12 |
Correct |
53 ms |
7460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
10024 KB |
Output is correct |
2 |
Correct |
155 ms |
10128 KB |
Output is correct |
3 |
Correct |
153 ms |
10068 KB |
Output is correct |
4 |
Correct |
105 ms |
8624 KB |
Output is correct |
5 |
Correct |
121 ms |
8968 KB |
Output is correct |
6 |
Correct |
73 ms |
7868 KB |
Output is correct |
7 |
Correct |
150 ms |
9948 KB |
Output is correct |
8 |
Correct |
149 ms |
9744 KB |
Output is correct |
9 |
Correct |
142 ms |
9804 KB |
Output is correct |
10 |
Correct |
113 ms |
8800 KB |
Output is correct |
11 |
Correct |
96 ms |
8416 KB |
Output is correct |
12 |
Correct |
53 ms |
7460 KB |
Output is correct |
13 |
Correct |
191 ms |
10288 KB |
Output is correct |
14 |
Correct |
190 ms |
10368 KB |
Output is correct |
15 |
Correct |
150 ms |
9964 KB |
Output is correct |
16 |
Correct |
127 ms |
8876 KB |
Output is correct |
17 |
Correct |
139 ms |
9252 KB |
Output is correct |
18 |
Correct |
74 ms |
7824 KB |
Output is correct |
19 |
Correct |
187 ms |
10364 KB |
Output is correct |
20 |
Correct |
193 ms |
10428 KB |
Output is correct |
21 |
Correct |
191 ms |
10456 KB |
Output is correct |
22 |
Correct |
114 ms |
8892 KB |
Output is correct |
23 |
Correct |
96 ms |
8428 KB |
Output is correct |
24 |
Correct |
51 ms |
7412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
728 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
4 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
724 KB |
Output is correct |
14 |
Correct |
4 ms |
724 KB |
Output is correct |
15 |
Correct |
5 ms |
716 KB |
Output is correct |
16 |
Correct |
3 ms |
684 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
155 ms |
10024 KB |
Output is correct |
20 |
Correct |
155 ms |
10128 KB |
Output is correct |
21 |
Correct |
153 ms |
10068 KB |
Output is correct |
22 |
Correct |
105 ms |
8624 KB |
Output is correct |
23 |
Correct |
121 ms |
8968 KB |
Output is correct |
24 |
Correct |
73 ms |
7868 KB |
Output is correct |
25 |
Correct |
150 ms |
9948 KB |
Output is correct |
26 |
Correct |
149 ms |
9744 KB |
Output is correct |
27 |
Correct |
142 ms |
9804 KB |
Output is correct |
28 |
Correct |
113 ms |
8800 KB |
Output is correct |
29 |
Correct |
96 ms |
8416 KB |
Output is correct |
30 |
Correct |
53 ms |
7460 KB |
Output is correct |
31 |
Correct |
191 ms |
10288 KB |
Output is correct |
32 |
Correct |
190 ms |
10368 KB |
Output is correct |
33 |
Correct |
150 ms |
9964 KB |
Output is correct |
34 |
Correct |
127 ms |
8876 KB |
Output is correct |
35 |
Correct |
139 ms |
9252 KB |
Output is correct |
36 |
Correct |
74 ms |
7824 KB |
Output is correct |
37 |
Correct |
187 ms |
10364 KB |
Output is correct |
38 |
Correct |
193 ms |
10428 KB |
Output is correct |
39 |
Correct |
191 ms |
10456 KB |
Output is correct |
40 |
Correct |
114 ms |
8892 KB |
Output is correct |
41 |
Correct |
96 ms |
8428 KB |
Output is correct |
42 |
Correct |
51 ms |
7412 KB |
Output is correct |
43 |
Correct |
216 ms |
13108 KB |
Output is correct |
44 |
Correct |
213 ms |
13140 KB |
Output is correct |
45 |
Correct |
203 ms |
13364 KB |
Output is correct |
46 |
Correct |
132 ms |
10512 KB |
Output is correct |
47 |
Correct |
152 ms |
10744 KB |
Output is correct |
48 |
Correct |
77 ms |
7672 KB |
Output is correct |
49 |
Correct |
206 ms |
13008 KB |
Output is correct |
50 |
Correct |
221 ms |
13184 KB |
Output is correct |
51 |
Correct |
216 ms |
13020 KB |
Output is correct |
52 |
Correct |
137 ms |
10704 KB |
Output is correct |
53 |
Correct |
102 ms |
10012 KB |
Output is correct |