This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 2e5 + 5;
int N, Q;
struct Point {
int a, b, c, i;
} p[MN];
int b[MN], ans[MN];
void upd(int i, int v) {
for (; i < MN; i += i & -i) b[i] += v;
}
int get(int i) {
int r = 0;
for (; i; i -= i & -i) r += b[i];
return r;
}
void cdq(int l, int r) {
if (l == r) return;
int m = (l + r) / 2;
cdq(l, m), cdq(m + 1, r);
sort(p + l, p + r + 1, [](Point x, Point y) {
return x.a == y.a ? x.i < y.i : x.a > y.a;
});
for (int i = l; i <= r; i++) {
if (p[i].c <= m && !p[i].i) upd(p[i].b, 1);
if (p[i].c > m && p[i].i) ans[p[i].i] += get(2e5) - get(p[i].b - 1);
}
for (int i = l; i <= r; i++)
if (p[i].c <= m && !p[i].i) upd(p[i].b, -1);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> Q;
vector<int> ca, cb;
for (int i = 1; i <= N; i++) {
cin >> p[i].a >> p[i].b;
p[i].c = p[i].a + p[i].b;
ca.push_back(p[i].a);
cb.push_back(p[i].b);
}
for (int i = N + 1; i <= N + Q; i++) {
cin >> p[i].a >> p[i].b >> p[i].c;
p[i].i = i - N;
ca.push_back(p[i].a);
cb.push_back(p[i].b);
}
sort(p + 1, p + 1 + N + Q + 1, [](Point x, Point y) {
return x.c == y.c ? x.i < y.i : x.c > y.c;
});
sort(all(ca)), sort(all(cb));
ca.erase(unique(all(ca)), ca.end());
cb.erase(unique(all(cb)), cb.end());
for (int i = 1; i <= N + Q; i++) {
p[i].a = lower_bound(all(ca), p[i].a) - ca.begin() + 1;
p[i].b = lower_bound(all(cb), p[i].b) - cb.begin() + 1;
p[i].c = i;
}
cdq(1, N + Q);
for (int i = 1; i <= Q; i++) cout << ans[i] << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |