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?
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.
1≤n,m,K,Q≤100,000 .
1≤x≤n,1≤y≤m .
1≤x1≤x2≤n,1≤y1≤y2≤m .
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.
1≤n,m,K,Q≤100,000 .
1≤x≤n,1≤y≤m .
1≤x1≤x2≤n,1≤y1≤y2≤m .
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 YesHintHuge 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;
}