点击打开链接http://acm.nyist.net/JudgeOnline/problem.php?pid=522
#include <stdio.h>
#include <memory.h>
const long M=100001;
long c[2*M+10]; // c[i]=x 以端点 i为尾(i-lowbit(i)+1~~i)长度为 Lowbit(i)的线段出现过 x次
long m,n;
long Lowbit(long x)
{
return x&(-x);
}
void Add(long x,long d)
{
while(x>0)
{
c[x]+=d;
x-=Lowbit(x);
}
}
long sum(long x) //
{
long ans=0;
while(x<=2*M+10)
{
ans+=c[x];
x+=Lowbit(x);
}
return ans;
}
int main()
{
int t;
long i,j,k;
scanf("%d",&t);
while(t--)
{
memset(c,0,sizeof(c));
scanf("%ld%ld",&m,&n);
for(i=1;i<=m;i++)
{
long a,b;
scanf("%ld%ld",&a,&b);
Add(b+M,1);
Add(a-1+M,-1); // +M 负区间转为正的
}
for(i=1;i<=n;i++)
{
long x;
scanf("%ld",&x);
printf("%ld\n",sum(x+M));
}
}
return 0;
}