Points in Segments (II)(线段树)

Points in Segments (II)
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

Given n segments (1 dimensional) and q points, for each point you have to find the number of segments which contain that point. A point pi will lie in a segment A B if A ≤ pi ≤ B.

For example, if the segments are (6 12), (8 8), (10 12), (8 11), (0 12) and the point is 11, then it is contained by 4 segments.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 50000) and q (1 ≤ q ≤ 50000).

Each of the next n lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.

Each of the next q lines contains an integer denoting a point. Each of them range in [0, 108].

Output

For each case, print the case number in a single line. Then for each point, print the number of segments that contain that point.

Sample Input

1

5 4

6 12

8 8

10 12

8 11

0 12

11

12

2

20

Sample Output

Case 1:

4

3

1

0


#include<iostream> 
#include<cstring> 
#include<cstdio> 
#include<algorithm> 
using namespace std; 
#define lson l,m,rt<<1 
#define rson m+1,r,rt<<1|1 
const int MAXN = 222222; 
int arr[MAXN]; 
struct node
{     
	int a,b; 
}s[MAXN]; 
int pt[MAXN],cov[MAXN<<2],N,T,S,Q; 
void build(int l,int r,int rt)
{     
	cov[rt] = 0;     
	if(l==r)
		return;     
	int m = (l+r)>>1;     
	build(lson);     
	build(rson); 
} 
void pushDOWN(int rt)
{     
	if(cov[rt])
	{         
		cov[rt<<1]+=cov[rt];         
		cov[rt<<1|1]+=cov[rt];         
		cov[rt] = 0;     
	} 
} 
void update(int L,int R,int l,int r,int rt)
{     
	if(L<=l&&R>=r)
	{         
		cov[rt]++;         
		return;     
	}     
	pushDOWN(rt);     
	int m = (l+r)>>1;     
	if(m>=L)update(L,R,lson);     
	if(m<R)update(L,R,rson); 
} 
int query(int pos,int l,int r,int rt)
{     
	if(l==r)
	{         
		return cov[rt];     
	}     
	pushDOWN(rt);     
	int m = (l+r)>>1;     
	if(pos<=m)return query(pos,lson);     
	else return query(pos,rson); 
} 
int bs(int x)
{     
	int l = 0,r = N-1,m;     
	while(l<=r)
	{         
		m = (l+r)>>1;         
		if(arr[m]==x)
			return m;         
		else if(arr[m]>x)
			r = m-1;         
		else l = m+1;     
	}     return -1; 
} 
int main()
{     
	scanf("%d",&T);     
	for(int cas=1;cas<=T;cas++)
	{         
		scanf("%d%d",&S,&Q);
		N = 0;         
		for(int i=0;i<S;i++)
		{             
			scanf("%d%d",&s[i].a,&s[i].b);             
			arr[N++] = s[i].a;             
			arr[N++] = s[i].b;         
		}         
		for(int i=0;i<Q;i++)
		{             
			scanf("%d",&pt[i]);             
			arr[N++] = pt[i];         
		}         
		sort(arr,arr+N);         
		int temp = 1;         
		for(int i=1;i<N;i++)
		if(arr[i]!=arr[i-1])
			arr[temp++] = arr[i];         
		N = temp;         
		build(0,N-1,1);         
		for(int i=0;i<S;i++)
		{             
			update(bs(s[i].a),bs(s[i].b),0,N-1,1);         
		}         
		printf("Case %d:\n",cas);         
		for(int i=0;i<Q;i++)
		{            
	 		printf("%d\n",query(bs(pt[i]),0,N-1,1));         
	 	}     
	}     
  	return 0; 
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值