zoj 3005 矩形覆盖 + 搜索

题目:



Bacteria Colony

Time Limit: 2 Seconds       Memory Limit: 65536 KB

The biologist Mr.R likes cultivating bacteria on the culture medium. What is interesting is that the new cultivated bacterial colonies always form a rectangle, whose sides are parallel with the x-axis or the y-axis of the plane.

One day, Mr.R cultivated a lot of bacteria on the culture medium. Some of the colonies were overlapping. To his surprise, he found all the new cultivated bacteria defeat the old ones, which means when the new bacteria had been cultivated, the old bacteria lived in the new bacteria's colony disappeared.

Mr.R has picked out some of the bacteria and he wants to know which bacteria have defeated them.

Input

The problem has multiple test cases.

For each test case, the first line contains a integer n (1 <= n <= 500), which represents the total number of bacteria.

The next n lines describe the colonies of the bacteria. Each line has four integers x1 y1 x2 y2 (x1 < x2, y1 < y2, -100,000 <= x1, x2, y1, y2 <= 100,000). They are the coordinates of the left-top and the right-bottom points of the rectangle. The bacteria are numbered from 1 to n. The first rectangle is the colony of the first cultivated bacteria; the second rectangle is the colony of the second cultivated one, etc.

The following line contains a number m ( m <= n ), which means the number of the bacteria that Mr.R picked out.

The last line of the test case contains m numbers (between 1 and n) indicating the picked out bacteria.

Output

For each test case, there are m lines. The first number of each line is the number of the bacteria that have defeated the selected one, and then list them out in increasing order. The first line contains the answer to the first selected bacteria; the second line contains the answer to the second, etc.

Output a blank line after each test case.

Example Input

3
1 1 5 6
2 2 5 5
3 3 4 4
3
3 2 1

3
1 1 5 5
2 2 4 4
3 3 5 5
3
1 2 3

3
3 3 5 5
2 2 5 5
3 3 5 5
3
1 2 3

Example Output

0
1 3
1 2

2 2 3
1 3
0

1 2
1 3
0
由下到上给出n个矩形 ,下面的矩形可以被上面的矩形覆盖,被一个矩形覆盖了的点就不能被其他矩形覆盖 下面给出m个询问,每次给出一个矩形的id,输出这个矩形被几个矩形覆盖过,并由下到上输出这些矩形的编号


分析:
线段树不太好维护 不太好想到dfs 不过由于矩形有顺序关系确实可以按层数递归  


代码:

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
const int maxn=520;

struct node{
	int x1,y1,x2,y2;
	node(){}
	node(int a,int b,int c,int d):x1(a),y1(b),x2(c),y2(d){}
}g[maxn];

int n,m,t;
set<int> s;

/*
分块dfs示意图 
----------------
|    |  3  |   |
|    |-----|   |
|  1 | dep | 2 |
|    |-----|   |
|    |  4  |   |
----------------  也有可能只有1和3部分 或者2和4部分
*/
void dfs(int x1,int y1,int x2,int y2,int dep){  
	while(dep<=n&&(g[dep].x1>=x2||g[dep].x2<=x1||g[dep].y1>=y2||g[dep].y2<=y1)) dep++;
	if(dep>n) return;
	s.insert(dep);
	if(g[dep].x1>x1) {
		dfs(x1,y1,g[dep].x1,y2,dep+1);
		x1=g[dep].x1;
	}
	if(g[dep].x2<x2) {
		dfs(g[dep].x2,y1,x2,y2,dep+1);
		x2=g[dep].x2;
	}
	if(g[dep].y1>y1) dfs(x1,y1,x2,g[dep].y1,dep+1);
	if(g[dep].y2<y2) dfs(x1,g[dep].y2,x2,y2,dep+1);
}

int main(){
	while(scanf("%d",&n)==1){
		for(int i=1;i<=n;i++) scanf("%d%d%d%d",&g[i].x1,&g[i].y1,&g[i].x2,&g[i].y2);
		scanf("%d",&m);
		while(m--) {
			s.clear();
			scanf("%d",&t);
			dfs(g[t].x1,g[t].y1,g[t].x2,g[t].y2,t+1);
			printf("%d",s.size());
			///set默认由小到大排序 
			for(set<int>::iterator it=s.begin();it!=s.end();it++) printf(" %d",*it);
			puts("");
		}
		puts("");
	}
	return 0;
}




Bacteria Colony

Time Limit: 2 Seconds       Memory Limit: 65536 KB

The biologist Mr.R likes cultivating bacteria on the culture medium. What is interesting is that the new cultivated bacterial colonies always form a rectangle, whose sides are parallel with the x-axis or the y-axis of the plane.

One day, Mr.R cultivated a lot of bacteria on the culture medium. Some of the colonies were overlapping. To his surprise, he found all the new cultivated bacteria defeat the old ones, which means when the new bacteria had been cultivated, the old bacteria lived in the new bacteria's colony disappeared.

Mr.R has picked out some of the bacteria and he wants to know which bacteria have defeated them.

Input

The problem has multiple test cases.

For each test case, the first line contains a integer n (1 <= n <= 500), which represents the total number of bacteria.

The next n lines describe the colonies of the bacteria. Each line has four integers x1 y1 x2 y2 (x1 < x2, y1 < y2, -100,000 <= x1, x2, y1, y2 <= 100,000). They are the coordinates of the left-top and the right-bottom points of the rectangle. The bacteria are numbered from 1 to n. The first rectangle is the colony of the first cultivated bacteria; the second rectangle is the colony of the second cultivated one, etc.

The following line contains a number m ( m <= n ), which means the number of the bacteria that Mr.R picked out.

The last line of the test case contains m numbers (between 1 and n) indicating the picked out bacteria.

Output

For each test case, there are m lines. The first number of each line is the number of the bacteria that have defeated the selected one, and then list them out in increasing order. The first line contains the answer to the first selected bacteria; the second line contains the answer to the second, etc.

Output a blank line after each test case.

Example Input

3
1 1 5 6
2 2 5 5
3 3 4 4
3
3 2 1

3
1 1 5 5
2 2 4 4
3 3 5 5
3
1 2 3

3
3 3 5 5
2 2 5 5
3 3 5 5
3
1 2 3

Example Output

0
1 3
1 2

2 2 3
1 3
0

1 2
1 3
0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值