hdu 2461(AC) & poj 3695(TLE)(离散化+矩形并)

Rectangles

Time Limit: 5000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1503    Accepted Submission(s): 777

Problem Description
You
are developing a software for painting rectangles on the screen. The
software supports drawing several rectangles and filling some of them
with a color different from the color of the background. You are to
implement an important function. The function answer such queries as
what is the colored area if a subset of rectangles on the screen are
filled.
 
Input
The
input consists of multiple test cases. Each test case starts with a
line containing two integers N(1 ≤ N ≤ 20) and M(1 ≤ M ≤ 100000),
indicating the number of rectangles on the screen and the number of
queries, respectively.
The i-th line of the following N lines
contains four integers X1,Y1,X2,Y2 (0 ≤ X1 < X2 ≤ 1000, 0 ≤ Y1 <
Y2 ≤ 1000), which indicate that the lower-left and upper-right
coordinates of the i-th rectangle are (X1, Y1) and (X2, Y2). Rectangles
are numbered from 1 to N.
The last M lines of each test case describe
M queries. Each query starts with a integer R(1<=R ≤ N), which is
the number of rectangles the query is supposed to fill. The following
list of R integers in the same line gives the rectangles the query is
supposed to fill, each integer of which will be between 1 and N,
inclusive.

The last test case is followed by a line containing two zeros.

 
Output
For each test case, print a line containing the test case number( beginning with 1).
For
each query in the input, print a line containing the query number
(beginning with 1) followed by the corresponding answer for the query.
Print a blank line after the output for each test case.
 
Sample Input
2 2
0 0 2 2
1 1 3 3
1 1
2 1 2
2 1
0 1 1 2
2 1 3 2
2 1 2
0 0
 
Sample Output
Case 1:
Query 1: 4
Query 2: 7

Case 2:
Query 1: 2

 
Source
 题意:给你n个矩形,然后m个询问,每次询问 1-n个矩形的并面积.
题解:我的方法就是每次询问的时候然后再一个个的去查询。。优化了好久,poj还是TLE hdu 1185MS水过
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAAWCAIAAAAtny3jAAAGFElEQVR4nO1bPW6rQBDec+QMLig4QO7gkleZO4Q69KR3lSriBJYsRe4fpeXCMo2lFG6Qq0RxkKXHK4Dd2T9sHDDjZFZfEa13yH7DsB+zO7B1Vr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oKANOSvlpDOinW1V2w58ritc9nmF6MbVFllK2Zo/yLDNnTFdi6rU0yoeSXMPWAr+HyVNRqKpJZUthnZRKSnH48PYmURhSGLDTyUld/98amstOIY34kNG6Qjxx2FqTkn4Krc+73oyCF6EZNeEmX0UmXYQR5/Lc/A6DXmspaQhhc5a8fYVCGFJxKUujYRBlqrXiVN/WjuNalsMzT9Exu/p1TWkN3KAilMGnJZiyGprBlKYqr8ZK7+QK6y89BVyoa1p1URFXP5D4DpTLd3ppc7xL5AicFmL0kOuTCDv6Jn9Oi1nctqv6r9zdILiqqG5NuAn3UuK9XmofAvJqh6BrNP5RRW7ylgwdTO83fyBbehy8fvvPrM1WYo/jU80P35aBNvDRJbyEsSyBjU93p0Kis/obWgxj4XFVHao2sq74l9IVQtH/mOmF7qkKYjOjDY6CVgK9esdowuPGOMXnuNcWUijlqFrbGE+LZy2eYa45tT2baHNL9KZaEQlgD6B2qDqzFn9NjqhHm/Xq7MJxD71a6151MuawD//tV8+KTIp/i+kA+rv+TRS6I6mqGM+sMVqQLR2Gn5EpQ3qDQ2iSq/6gFbrL3ei4tg4p4GTsNOm16MbSoHk67fi9B24Blr9Nq/l1UzXfvH4uqAb0nsFSKhBPxeVhKpm1NZnP79sfhNm72dAH+84Z8hMSXPEN8BuZPKXheksi2BP97wz5CYkmeI74DcSWUJqIE/3vDPkJiSZ4jvgNxJZQmogT/e8M+QmJJniO+A3EllCaiBP97wz5CYkmeI74DcSWUJqIE/3vDPkJiSZ4jvgNxZQe1U+weabczxeDwcDp+fn19fX8fj8Zxr5nl+OBwOh8OZJtQQNooNarZGsUGtbP8BVG5KNY1zCzUAAAAASUVORK5CYII=" alt="" />
///离散化
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int N = ;
struct Rec
{
int x1,y1,x2,y2;
} rec[N];
int x[N*],y[N*];
int vis[N*][N*];
int k,n,t,m;
int binary1(int l,int r,int value)
{
int mid;
while(l<r)
{
mid = (l+r)>>;
if(x[mid]==value) return mid;
if(x[mid]<value) l = mid+;
else r = mid-;
}
return l;
}
int binary2(int l,int r,int value)
{
int mid;
while(l<r)
{
mid = (l+r)>>;
if(y[mid]==value) return mid;
if(y[mid]<value) l = mid+;
else r = mid-;
}
return l;
}
int k1,k2;
void input()
{
k=,t=;
int x1,y1,x2,y2;
for(int i=; i<n; i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
rec[t].x1 = x1,rec[t].y1 = y1,rec[t].x2=x2,rec[t++].y2 = y2;
x[k] = x1,y[k++] = y1;
x[k] = x2,y[k++] = y2;
}
sort(x,x+k);
sort(y,y+k);
k1 = ,k2=;
x[k1] = x[];
y[k2] = y[];
for(int i=;i<k;i++){ ///去重还是TLE
if(x[i]!=x[i-]) x[++k1] = x[i];
if(y[i]!=y[i-]) y[++k2] = y[i];
}
}
int solve(int num)
{
memset(vis,,sizeof(vis));
while(num--)
{
int b;
scanf("%d",&b);
int t1,t2,t3,t4;
t1 = binary1(,k1,rec[b-].x1);
t2 = binary1(,k1,rec[b-].x2);
t3 = binary2(,k2,rec[b-].y1);
t4 = binary2(,k2,rec[b-].y2);
for(int j=t1; j<t2; j++)
{
for(int l = t3; l<t4; l++)
{
vis[j][l] = ;
}
}
}
int area = ;
for(int i=; i<=k1; i++)
{
for(int j=; j<=k2; j++)
{
area+=vis[i][j]*(x[i+]-x[i])*(y[j+]-y[j]);
}
}
return area;
}
int main()
{
int x1,y1,x2,y2;
int cnt = ;
while(scanf("%d%d",&n,&m)!=EOF&&n+m)
{ input();
int a,b;
printf("Case %d:\n",cnt++);
int cas = ;
while(m--)
{
scanf("%d",&a);
printf("Query %d: %d\n",cas++,solve(a));;
}
printf("\n");
}
return ;
}
上一篇:matlab常用小函数(一)


下一篇:数据结构:HDU 2993 MAX Average Problem