HDOJ 5408 Conturbatio (双线段树+单点更新+区间和)

Conturbatio

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 680    Accepted Submission(s): 314


Problem Description
There are many rook on a chessboard, a rook can attack the row and column it belongs, including its own place.

There are also many queries, each query gives a rectangle on the chess board, and asks whether every grid in the rectangle will be attacked by any rook?
 

Input
The first line of the input is a integer T , meaning that there are T test cases.

Every test cases begin with four integers n,m,K,Q .
K is the number of Rook, Q is the number of queries.

Then K lines follow, each contain two integers x,y describing the coordinate of Rook.

Then Q lines follow, each contain four integers x1,y1,x2,y2 describing the left-down and right-up coordinates of query.

1n,m,K,Q100,000 .

1xn,1ym .

1x1x2n,1y1y2m .
 

Output
For every query output "Yes" or "No" as mentioned above.
 

Sample Input
  
  
2 2 2 1 2 1 1 1 1 1 2 2 1 2 2 2 2 2 1 1 1 1 2 2 1 2 2
 

Sample Output
  
  
Yes No Yes
Hint
Huge input, scanf recommended.
 


题意:给你一个n*m的棋盘,上面会有车,车走直线这个都知道吧,这个车能攻击和它在一条上的点,包括它自己,然后给你几个查询,问是否这个区域内的所有点都能被攻击到。

思路:建两个线段树表示横纵,然后查询时查看区间和是否符合即可。




ac代码:
#include<stdio.h>
#define MAXN 440000
struct s
{
	int left;
	int right;
	int sum;
}tree[MAXN],tree1[MAXN];
int MAX(int a,int b) 
{
    return a>b?a:b;
}
int MIN(int a,int b) 
{
    return a>b?b:a;
}
int num[MAXN];
void build(int i,int l,int r)
{
	tree[i].left=l;
	tree[i].right=r;
	if(l==r)
	{
		tree[i].sum=0;
	}
	else
	{
		int mid=(l+r)/2;
		build(i*2,l,mid);
		build(i*2+1,mid+1,r);
		tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;
	}
}
void build1(int i,int l,int r)
{
	tree1[i].left=l;
	tree1[i].right=r;
	if(l==r)
	{
		tree1[i].sum=0;
	}
	else
	{
		int mid=(l+r)/2;
		build1(i*2,l,mid);
		build1(i*2+1,mid+1,r);
		tree1[i].sum=tree1[i*2].sum+tree1[i*2+1].sum;
	}
}
void update(int a,int b,int i)
{
    if(tree[i].left==tree[i].right)
    {
    	tree[i].sum=b;
    	return;
	}
	if(tree[i*2].right>=a)
	{
		update(a,b,i*2);
	}
	else
	{
		update(a,b,i*2+1);
	}
	tree[i].sum=tree[i*2].sum+tree[i*2+1].sum;
}
void update1(int a,int b,int i)
{
    if(tree1[i].left==tree1[i].right)
    {
    	tree1[i].sum=b;
    	return;
	}
	if(tree1[i*2].right>=a)
	{
		update1(a,b,i*2);
	}
	else
	{
		update1(a,b,i*2+1);
	}
	tree1[i].sum=tree1[i*2].sum+tree1[i*2+1].sum;
}
int query(int i,int l,int r)
{
	if(tree[i].left==l&&tree[i].right==r)
	{
		return tree[i].sum;
	}
	if(r<=tree[i*2].right)
	{
		return query(i*2,l,r);
	}
	if(l>=tree[i*2+1].left)
	{
		return query(i*2+1,l,r);
	}
	int mid=(tree[i].left+tree[i].right)/2;
	return query(i*2,l,mid)+query(i*2+1,mid+1,r);
}
int query1(int i,int l,int r)
{
	if(tree1[i].left==l&&tree1[i].right==r)
	{
		return tree1[i].sum;
	}
	if(r<=tree1[i*2].right)
	{
		return query1(i*2,l,r);
	}
	if(l>=tree1[i*2+1].left)
	{
		return query1(i*2+1,l,r);
	}
	int mid=(tree1[i].left+tree1[i].right)/2;
	return query1(i*2,l,mid)+query1(i*2+1,mid+1,r);
}
int main()
{
	int n,m,i,k,q;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d",&n,&m,&k,&q);
		build(1,1,n);
		build1(1,1,m);
		while(k--)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			update(a,1,1);
			update1(b,1,1);
		}
		//for(i=1;i<=3;i++)
		//printf("%d %d %d\n",tree1[i].left,tree1[i].right,tree1[i].sum);
		while(q--)
		{
			int x1,x2,y1,y2;
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			int mx=MAX(x1,x2),mix=MIN(x1,x2);
			int cnt=query(1,mix,mx);
			//printf("cnt=%d\n",cnt);
			if(cnt==mx-mix+1)
			{
				printf("Yes\n");
				continue;
			}
			int my=MAX(y1,y2),miy=MIN(y1,y2);
			cnt=query1(1,miy,my);
			//printf("cnt=%d\n",cnt);
			if(cnt==my-miy+1)
			printf("Yes\n");
			else
			printf("No\n");
		}
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值