您的位置:首页 > 其它

nyoj----522 Interval (简单树状数组)

2014-04-18 22:12 274 查看

Interval

时间限制:2000 ms | 内存限制:65535 KB
难度:4

描述
There are n(1 <= n <= 100000) intervals [ai, bi] and m(1 <= m <= 100000) queries, -100000 <= ai <= bi <= 100000 are integers.
Each query contains an integer xi(-100000 <= x <= 100000). For each query, you should answer how many intervals convers xi.

输入The first line of input is the number of test case.
For each test case,
two integers n m on the first line,
then n lines, each line contains two integers ai, bi;
then m lines, each line contains an integer xi.输出m lines, each line an integer, the number of intervals that covers xi.样例输入
2
3 4
1 3
1 2
2 3
0
1
2
3
1 3
0 0
-1
0
1

样例输出
0
2
3
2
0
1
0

上传者ACM_赵铭浩 代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 200005   //整体平移100001个单位
#define lowbit(x) ((x)&(-x))
int aa[maxn+5];
void ope(int x,int val)
{
x+=100001;
while(x<=maxn)
{
aa[x]+=val;
x+=lowbit(x);
}
}
long long getsum(int x)
{
long long ans=0;
while(x>0)
{
ans+=aa[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int test,nn,m,i,a,b;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&nn,&m);
memset(aa,0,sizeof(aa));
for(i=0;i<nn;i++)
{
scanf("%d%d",&a,&b);
ope(a,1);
ope(b+1,-1);
}
for(i=0;i<m;i++)
{
scanf("%d",&a);
a+=100001;
printf("%I64d\n",getsum(a));
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: