示例答案
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 300010;
typedef pair<int, int> PII;
vector<int> alls;//储存所有的坐标
vector<PII> qu, al;//qu储存要查询的范围,al储存要增加的坐标和数
int a[N], s[N];
int Find(int x)//离散化操作
{
int l = 0, r = alls.size() - 1;
while (l < r)
{
int mid = l + r >> 1;
if (alls[mid] >= x) r = mid;
else l = mid + 1;
}
return r + 1;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
while (n--)
{
int x, c;
scanf("%d%d", &x, &c);
al.push_back({x, c});
alls.push_back(x);
}
while (m--)
{
int l, r;
scanf("%d%d", &l, &r);
qu.push_back({l, r});
alls.push_back(l);
alls.push_back(r);
}
sort(alls.begin(), alls.end());//排序
alls.erase(unique(alls.begin(), alls.end()), alls.end());//去重
for (auto i : al)
{
int x = Find(i.first);
a[x] += i.second;
}
for (int i = 1; i <= alls.size(); i++) s[i] = s[i - 1] + a[i];
for (auto i : qu)
{
int l = Find(i.first), r = Find(i.second);
cout << s[r] - s[l - 1] << endl;
}
return 0;
}