AcWing 802. 区间和
原题链接
简单
作者:
_empty
,
2020-05-04 19:41:36
,
所有人可见
,
阅读 466
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
struct item
{
int idx, val;
bool operator < (const item &W) const
{
return idx < W.idx;
}
}item[N];
int a[N];
int n, m;
int main()
{
scanf("%d%d", &n, &m);
int x, c;
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &item[i].idx, &item[i].val);
}
sort(item + 1, item + 1 + n);
for (int i = 1; i <= n; i++)
{
item[i].val += item[i - 1].val;
a[i] = item[i].idx;
}
int l, r;
while (m --)
{
scanf("%d%d", &l, &r);
l = lower_bound(a + 1, a + 1 + n, l) - (a + 1) + 1;
r = upper_bound(a + 1, a + 1 + n, r) - (a + 1) + 1;
r--;
int res = item[r].val - item[l - 1].val;
printf("%d\n", res);
}
return 0;
}