**区间和的C语言版本**
#include <stdio.h>
#include <stdlib.h>
#define N 300010
typedef struct{
int first;
int second;
}PII;
int a[N],s[N];
int alls[N];
PII add[N];
PII query[N];
void quick_sort(int alls[],int l,int r){
if(l>=r) return;
int i=l-1,j=r+1,x=alls[(r+l)/2];
while(i<j){
do i++; while(alls[i]<x);
do j--; while(alls[j]>x);
if(i<j){
int temp=alls[i];
alls[i]=alls[j];
alls[j]=temp;
}
}
quick_sort(alls,l,j);
quick_sort(alls,j+1,r);
}
int unique(int alls[],int len){
int* end=alls+len;
int* result_end=alls+1;
for(int* p=alls+1;p<end;p++){
if(*p!=*(p-1)){
*result_end=*p;
result_end++;
}
}
return result_end-alls;
}
int find(int x,int size){
int l=0,r=size-1;
while(l<r){
int mid=(l+r)/2;
if(alls[mid]<x) l=mid+1;
else r=mid;
}
return r+1;
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
int add_count=0,query_count=0,alls_count=0;
for(int i=0;i<n;i++){
int x,c;
scanf("%d %d",&x,&c);
add[add_count++]=(PII){x,c};
alls[alls_count++]=x;
}
for(int i=0;i<m;i++){
int l,r;
scanf("%d %d",&l,&r);
query[query_count++]=(PII){l,r};
alls[alls_count++]=l;
alls[alls_count++]=r;
}
quick_sort(alls,0,alls_count-1);
alls_count=unique(alls,alls_count);
for(int i=0;i<alls_count;i++){
int x=find(add[i].first,alls_count);
a[x]+=add[i].second;
}
for(int i=1;i<=alls_count;i++){
s[i]=s[i-1]+a[i];
}
for(int i=0;i<query_count;i++){
int l=find(query[i].first,alls_count);
int r=find(query[i].second,alls_count);
printf("%d\n",s[r]-s[l-1]);
}
return 0;
}