AcWing 802. 区间和
原题链接
简单
作者:
dsyami
,
2021-05-11 10:32:10
,
所有人可见
,
阅读 224
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 3e5 + 10;
typedef pair<int, int> PII;
vector<PII> add, query;
vector<int> alls;
int n, m, a[N], s[N];
int find(int x)
{
int l = 0, r = alls.size();
while(l < r)
{
int mid = l + r >> 1;
if(alls[mid] < x) l = mid + 1;
else r = mid;
}
return l + 1;
}
int main()
{
cin >> n >> m;
while(n -- )
{
int x, c;
cin >> x >> c;
add.push_back({x, c});
alls.push_back(x);
}
while(m -- )
{
int l, r;
cin >> l >> r;
query.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(int i = 0; i < add.size(); i ++ )
{
int x = find(add[i].first);
a[x] += add[i].second;
}
for(int i = 1; i <= alls.size(); i ++ )
{
s[i] = s[i - 1] + a[i];
}
for(int i = 0; i < query.size(); i ++ )
{
int l = find(query[i].first), r = find(query[i].second);
cout << s[r] - s[l - 1] << endl;
}
return 0;
}