#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, q;
pair<int, int> a[N], c[N];
struct Edge
{
int l, r, z, id;
};
vector<Edge> d;
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];
int m;
void update(int x, int y, int v)
{
for (int i = x; i <= m; i += i & -i)
for (int j = y; j <= m; j += j & -j)
bit[i][j] += v;
}
int get(int x, int y)
{
int res = 0;
for (int i = x; i > 0; i -= i & -i)
for (int j = y; j > 0; j -= j & -j)
res += bit[i][j];
return res;
}
signed main()
{
ios::sync_with_stdio(0); cin.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);
}
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(a + 1, a + 1 + n, cmp);
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
m = 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 = m - x + 1;
c[i].second = m - 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 = m - x + 1;
d[i].r = m - 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... |