#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int MAX_N = 1e5 + 10;
const int MAX_Q = 1e5 + 10;
const int B = 330;
int S[MAX_N], T[MAX_N], ST[MAX_N];
int X[MAX_Q], Y[MAX_Q], Z[MAX_Q];
int ans[MAX_Q];
vector <int> pA[2 * MAX_N], pB[2 * MAX_N];
class FenwickTree {
private :
int N;
vector <int> fenwick;
public :
FenwickTree() {}
FenwickTree(const int &n) : N(n), fenwick(N + 1, 0) {}
void update(const int &idx, const int &val) {
for(int i = idx; i <= N; i += (i & -i)) {
fenwick[i] += val;
}
}
int get(const int &idx) {
int res = 0;
for(int i = idx; i >= 1; i -= (i & -i)) {
res += fenwick[i];
}
return res;
}
int query(const int &idx) {
return get(N) - get(idx - 1);
}
};
class Query {
public :
int a, b, c, i;
Query() {}
Query(const int &a_, const int &b_, const int &c_, const int &i_) : a(a_), b(b_), c(c_), i(i_) {}
bool operator<(const Query &o) const {
return make_pair((a + B - 1) / B, b) < make_pair((o.a + B - 1) / B, o.b);
}
};
void compress(vector <int> &vec) {
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, Q;
cin >> N >> Q;
vector <int> sa({-1}), sb({-1}), sc({-1});
for(int i = 1; i <= N; i++) {
cin >> S[i] >> T[i];
ST[i] = S[i] + T[i];
sa.push_back(S[i]);
sb.push_back(T[i]);
sc.push_back(ST[i]);
}
for(int i = 1; i <= Q; i++) {
cin >> X[i] >> Y[i] >> Z[i];
sa.push_back(X[i]);
sb.push_back(Y[i]);
sc.push_back(Z[i]);
}
// Compress
compress(sa);
compress(sb);
compress(sc);
for(int i = 1; i <= N; i++) {
S[i] = lower_bound(sa.begin(), sa.end(), S[i]) - sa.begin();
T[i] = lower_bound(sb.begin(), sb.end(), T[i]) - sb.begin();
ST[i] = lower_bound(sc.begin(), sc.end(), ST[i]) - sc.begin();
pA[S[i]].push_back(i);
pB[T[i]].push_back(i);
}
for(int i = 1; i <= Q; i++) {
X[i] = lower_bound(sa.begin(), sa.end(), X[i]) - sa.begin();
Y[i] = lower_bound(sb.begin(), sb.end(), Y[i]) - sb.begin();
Z[i] = lower_bound(sc.begin(), sc.end(), Z[i]) - sc.begin();
}
// MO's algorithm
vector <Query> query;
for(int i = 1; i <= Q; i++) {
query.push_back(Query(X[i], Y[i], Z[i], i));
}
sort(query.begin(), query.end());
FenwickTree fw(sc.size());
int cur_l, cur_r;
for(int i = 0; i < Q; i++) {
auto [l, r, c, id] = query[i];
if(i == 0) {
cur_l = l, cur_r = r;
for(int i = 1; i <= N; i++) {
if(S[i] >= cur_l and T[i] >= cur_r) {
fw.update(ST[i], 1);
}
}
}
else {
while(cur_l > l) {
cur_l--;
for(auto idx : pA[cur_l]) {
if(T[idx] >= cur_r) {
fw.update(ST[idx], 1);
}
}
}
while(cur_r < r) {
for(auto idx : pB[cur_r]) {
if(S[idx] >= cur_l) {
fw.update(ST[idx], -1);
}
}
cur_r++;
}
while(cur_l < l) {
for(auto idx : pA[cur_l]) {
if(T[idx] >= cur_r) {
fw.update(ST[idx], -1);
}
}
cur_l++;
}
while(cur_r > r) {
cur_r--;
for(auto idx : pB[cur_r]) {
if(S[idx] >= cur_l) {
fw.update(ST[idx], 1);
}
}
}
}
ans[id] = fw.query(c);
}
for(int i = 1; i <= Q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
7 ms |
9748 KB |
Output is correct |
5 |
Correct |
5 ms |
9664 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
17 ms |
10196 KB |
Output is correct |
8 |
Correct |
16 ms |
10196 KB |
Output is correct |
9 |
Correct |
17 ms |
10196 KB |
Output is correct |
10 |
Correct |
13 ms |
10068 KB |
Output is correct |
11 |
Correct |
40 ms |
10248 KB |
Output is correct |
12 |
Correct |
27 ms |
10124 KB |
Output is correct |
13 |
Correct |
17 ms |
10348 KB |
Output is correct |
14 |
Correct |
20 ms |
10256 KB |
Output is correct |
15 |
Correct |
15 ms |
10324 KB |
Output is correct |
16 |
Correct |
52 ms |
10204 KB |
Output is correct |
17 |
Correct |
13 ms |
10196 KB |
Output is correct |
18 |
Correct |
15 ms |
10068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2829 ms |
21188 KB |
Output is correct |
2 |
Correct |
2804 ms |
23652 KB |
Output is correct |
3 |
Correct |
2668 ms |
23664 KB |
Output is correct |
4 |
Correct |
1037 ms |
21476 KB |
Output is correct |
5 |
Execution timed out |
3069 ms |
21024 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2829 ms |
21188 KB |
Output is correct |
2 |
Correct |
2804 ms |
23652 KB |
Output is correct |
3 |
Correct |
2668 ms |
23664 KB |
Output is correct |
4 |
Correct |
1037 ms |
21476 KB |
Output is correct |
5 |
Execution timed out |
3069 ms |
21024 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
7 ms |
9748 KB |
Output is correct |
5 |
Correct |
5 ms |
9664 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
17 ms |
10196 KB |
Output is correct |
8 |
Correct |
16 ms |
10196 KB |
Output is correct |
9 |
Correct |
17 ms |
10196 KB |
Output is correct |
10 |
Correct |
13 ms |
10068 KB |
Output is correct |
11 |
Correct |
40 ms |
10248 KB |
Output is correct |
12 |
Correct |
27 ms |
10124 KB |
Output is correct |
13 |
Correct |
17 ms |
10348 KB |
Output is correct |
14 |
Correct |
20 ms |
10256 KB |
Output is correct |
15 |
Correct |
15 ms |
10324 KB |
Output is correct |
16 |
Correct |
52 ms |
10204 KB |
Output is correct |
17 |
Correct |
13 ms |
10196 KB |
Output is correct |
18 |
Correct |
15 ms |
10068 KB |
Output is correct |
19 |
Correct |
2829 ms |
21188 KB |
Output is correct |
20 |
Correct |
2804 ms |
23652 KB |
Output is correct |
21 |
Correct |
2668 ms |
23664 KB |
Output is correct |
22 |
Correct |
1037 ms |
21476 KB |
Output is correct |
23 |
Execution timed out |
3069 ms |
21024 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |