#include <bits/stdc++.h>
using namespace std;
int n, q;
const int N = 1e5 + 5;
struct Edge {
int l, r, z, id;
};
vector<Edge> d;
pair<int, int> a[N], c[N];
int kq[N];
bool cmp(pair<int, int>& a, pair<int, int>& b) {
return a.first + a.second > b.first + b.second;
}
bool cmp1(Edge& a, Edge& b) {
return a.z > b.z;
}
map<int, int> bit[N];
void update(int x, int y, int v)
{
for (int i = x; i <= n; i += i & -i)
{
for (int j = y; j <= n; j += j & -j)
{
bit[i][j] += v;
}
}
}
int get(int x, int y)
{
int r = 0;
for (int i = x; i > 0; i -= i & -i) {
for (int j = y; j > 0; j -= j & -j) {
r += bit[i][j];
}
}
return r;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>q;
vector<int> b;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
b.push_back(a[i].first);
b.push_back(a[i].second);
}
sort(a + 1, a + 1 + n, cmp);
d.resize(q + 1);
for (int i = 1; i <= q; i++)
{
cin >> d[i].l >> d[i].r >> d[i].z;
d[i].id = i;
b.push_back(d[i].l);
b.push_back(d[i].r);
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
n = b.size();
for (int i = 1; i <= n; i++)
{
int x = lower_bound(b.begin(), b.end(), a[i].first) - b.begin() + 1;
int y = lower_bound(b.begin(), b.end(), a[i].second) - b.begin() + 1;
c[i].first = n - x + 1;
c[i].second = n - y + 1;
}
for (int i = 1; i <= q; i++)
{
int x = lower_bound(b.begin(), b.end(), d[i].l) - b.begin() + 1;
int y = lower_bound(b.begin(), b.end(), d[i].r) - b.begin() + 1;
d[i].l = n - x + 1;
d[i].r = n - y + 1;
}
sort(d.begin() + 1, d.end(), cmp1);
int t = 1;
for (int i = 1; i <= q; i++)
{
while (t <= n and a[t].first + a[t].second >= d[i].z)
{
update(c[t].first, c[t].second, 1);
t++;
}
kq[d[i].id] = get(d[i].l, d[i].r);
}
for (int i = 1; i <= q; i++)
cout<<kq[i]<<'\n';
}
# | 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... |