2014
03-16

# Windows

Emma is not very tidy with the desktop of her computer. She has the habit of opening windows on the screen and then not closing the application that created them. The result, of course, is a very cluttered desktop with some windows just peeking out from behind others and some completely hidden. Given that Emma doesn’t log off for days, this is a formidable mess. Your job is to determine which window (if any) gets selected when Emma clicks on a certain position of the screen.

Emma’s screen has a resolution of 106 by 106. When each window opens its position is given by the upper-left-hand corner, its width, and its height. (Assume position (0,0) is the location of the pixel in the upper-left-hand corner of her desktop. So, the lower-right-hand pixel has location (999999, 999999).)

Input for each test case is a sequence of desktop descriptions. Each description consists of a line containing a positive integer n, the number of windows, followed by n lines, n ≤ 100, describing windows in the order in which Emma opened them, followed by a line containing a positive integer m, the number of queries, followed by m lines, each describing a query. Each of the n window description lines contains four integers r, c, w, and h, where (r, c) is the row and column of the upper left pixel of the window, 0 ≤ r, c ≤ 999999, and w and h are the width and height of the window, in pixels, 1 ≤ w, h. All windows will lie entirely on the desktop (i.e., no cropping). Each of the m query description lines contains two integers cr and cc, the row and column number of the location (which will be on the desktop). The last test case is followed by a line containing 0.

Input for each test case is a sequence of desktop descriptions. Each description consists of a line containing a positive integer n, the number of windows, followed by n lines, n ≤ 100, describing windows in the order in which Emma opened them, followed by a line containing a positive integer m, the number of queries, followed by m lines, each describing a query. Each of the n window description lines contains four integers r, c, w, and h, where (r, c) is the row and column of the upper left pixel of the window, 0 ≤ r, c ≤ 999999, and w and h are the width and height of the window, in pixels, 1 ≤ w, h. All windows will lie entirely on the desktop (i.e., no cropping). Each of the m query description lines contains two integers cr and cc, the row and column number of the location (which will be on the desktop). The last test case is followed by a line containing 0.

3
1 2 3 3
2 3 2 2
3 4 2 2
4
3 5
1 2
4 2
3 3
2
5 10 2 10
100 100 100 100
2
5 13
100 101
0

Desktop 1:
window 3
window 1
background
window 2
Desktop 2:
background
window 2

#include<iostream>
using namespace std;

int cnt=1;

struct jsj
{
int x1,y1;
int x2,y2;
}a[101];

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
for(int i=0;i<n;i++)
{
int c,d;
scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&c,&d);
a[i].x2=a[i].x1+d-1;
a[i].y2=a[i].y1+c-1;
}

int m;
scanf("%d",&m);
printf("Desktop %d:/n",cnt++);
for(int i=0;i<m;i++)
{
bool f=false;
int q1,q2;
scanf("%d%d",&q1,&q2);
for(int j=n-1;j>=0;j--)
{
if(q1>=a[j].x1 && q1<=a[j].x2 && q2>=a[j].y1 && q2<=a[j].y2)
{
f=true;
printf("window %d/n",j+1);
break;
}
}
if(!f) printf("background/n");
}
}
return 0;
}

1. 给你一组数据吧：29 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1000。此时的数据量还是很小的，耗时却不短。这种方法确实可以，当然或许还有其他的优化方案，但是优化只能针对某些数据，不太可能在所有情况下都能在可接受的时间内求解出答案。