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
题意:已知多个矩形(平行于x,y轴)的位置,求指定矩形被覆盖的次数(注意:矩形被覆盖的部分在覆盖之后无效)
#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
const int maxn=500+10;
struct rect{
int x1,y1,x2,y2;
rect() {}
}g[maxn];
int n;
set<int> s;
void run(int x1,int y1,int x2,int y2,int k)
{
while(k<=n&&(g[k].x1>=x2||g[k].x2<=x1||g[k].y1>=y2||g[k].y2<=y1)) k++;
if(k>n) return;
if(g[k].x1>x1) {
run(x1,y1,g[k].x1,y2,k+1);
x1=g[k].x1;
}
if(g[k].x2<x2) {
run(g[k].x2,y1,x2,y2,k+1);
x2=g[k].x2;
}
if(g[k].y1>y1) run(x1,y1,x2,g[k].y1,k+1);
if(g[k].y2<y2) run(x1,g[k].y2,x2,y2,k+1);
s.insert(k);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i,m,t;
for(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);
run(g[t].x1,g[t].y1,g[t].x2,g[t].y2,t+1);
printf("%d",s.size());
for(set<int>::iterator it=s.begin();it!=s.end();it++)
printf(" %d",*it);
printf("\n");
}
printf("\n");
}
return 0;
}
心得:当问题可以分成相似的子问题,可以考虑深搜