二分,map,前缀和
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 100010;
typedef pair<int, int> PII;
PII points[N];
map<int, int> mp;
int s[N];
bool cmp(PII x, PII y){
return x.first < y.first;
}
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1;i <= n;i ++ ){
int x, c;
cin >> x >> c;
mp[x] += c;
}
int cnt = 0;
for(auto x : mp) points[++ cnt] = {x.first, x.second};
for(int i = 1;i <= n;i ++ ) s[i] = s[i - 1] + points[i].second;
sort(points + 1, points + cnt + 1, cmp);
while(m --){
int l, r;
cin >> l >> r;
//找到第一个大于等于 l 的
int ansL = 0, ansR = 0;
int L = 1, R = cnt;
while(L < R){
int mid = L + R >> 1;
if(points[mid].first < l) L = mid + 1;
else R = mid;
}
if(points[L].first < l){
printf("%d\n",0);
continue;
}else{
ansL = L;
}
//找到最后一个小等于等 r 的
L = 1, R = cnt;
while(L < R){
int mid = L + R + 1 >> 1;
if(points[mid].first > r) R = mid - 1;
else L = mid;
}
if(points[L].first > r){
printf("%d\n",0);
continue;
}else{
ansR = R;
}
cout << s[ansR] - s[ansL - 1] << endl;
}
return 0;
}