首页 > 代码库 > Interval(南阳oj522)(树状数组)
Interval(南阳oj522)(树状数组)
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
/*树状数组应用,统计数字在所给区间出现的次数。 TLE哭了,MAX大小:200002是AC,200005是TLE!!! 注意将负数转换到正值区间上,否则还是会TLE!!! */ #include<stdio.h> #include<string.h> #define MAX 200002 int a[MAX]; int lowbit(int N) { return N&(-N); } void asd(int i,int M) { while(i<=MAX) { a[i]+=M; i+=lowbit(i); } } int sum(int j) { int sum=0; while(j>0) { sum+=a[j]; j-=lowbit(j); } return sum; } int main() { int i,n,m,T,t,k,p; scanf("%d",&T); while(T--) { memset(a,0,sizeof(a)); scanf("%d%d",&m,&n); while(m--) { scanf("%d%d",&k,&t); asd(k+100001,1); asd(t+100002,-1); } while(n--) { scanf("%d",&p); printf("%d\n",sum(p+100001)); } } return 0; }
Interval(南阳oj522)(树状数组)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。