NYOJ 522 裸的树状数组

       又是一道水题,,话说这次月赛水题真的很多很多,貌似比赛时写出来的题都是水题。。。看来,水平也就能水一下题而已。。。。不过这道题比赛时还是坑了不少人,很多人在处理边界问题0的时候没有注意,都TLE了,,当时我也TLE了一次,后来仔细想了想,改过后就ac了。相比那些一直TLE到最后的孩纸来说,我算是幸运了。不过,这道题难度有点高了,除了边界问题外,就是道裸的树状数组,没什么难度的。题目:

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
ac代码:

 
#include <iostream>
#include <cstdio>
#include <string.h>
#include <cmath>
const int N=200002;
const int M=100001;
int num[N],ans[N];
int lowbit(int x){
	return x&(-x);
}
void update(int m,int value){
	while(m>0){
	  num[m]+=value;
	  m-=lowbit(m);
	}
}
int sum(int x){
	int s=0;
	while(x<=N){
	  s+=num[x];
	  x+=lowbit(x);
	}
	return s;
}
int main(){
	//freopen("1.txt","r",stdin);
	int numcase;
	scanf("%d",&numcase);
	while(numcase--){
	  memset(num,0,sizeof(num));
	 // memset(ans,0,sizeof(ans));
	  int n,m;
	  scanf("%d%d",&n,&m);
	  int x,y;
	  while(n--){
	    scanf("%d%d",&x,&y);
		x=x+M;
		y=y+M;
		update(x-1,-1);
		update(y,1);
	  }
	  while(m--){
	    scanf("%d",&x);
		x=x+M;
		//if(!ans[x])
		  printf("%d\n",sum(x));
		/*else
			printf("%d\n",ans[x]);*/
	  }
	}
	return 0;
}        


你可能感兴趣的:(NYOJ 522 裸的树状数组)