Rectangles
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3846   Accepted: 1124

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

题意:给你一些矩形,编号为1~n。有一些询问,每次问你其中的一些矩形的面积并。

题解:

矩形切割

矩形切割裸题。。。当然也可以用线段树(大坑。。。)

直接挂个模版就过了。

看到Discuss中有人说矩形切割会TLE。可能是常数太大了吧。但我没有TLE呀,而且貌似跑的飞快(时间好像排32名,内存也很小。。。)

 #include<bits/stdc++.h>
using namespace std;
struct node
{
int X1,Y1,X2,Y2;
}R[];
int s1,cc[],ans[];
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void Cover(int X1,int Y1,int X2,int Y2,int k,int k1)
{
while(k<=s1&&(X1>=R[cc[k]].X2||X2<=R[cc[k]].X1||Y1>=R[cc[k]].Y2||Y2<=R[cc[k]].Y1))k++;
if(k>=s1+){ans[k1]+=(X2-X1)*(Y2-Y1);return;}
if(X1<R[cc[k]].X1){Cover(X1,Y1,R[cc[k]].X1,Y2,k+,k1);X1=R[cc[k]].X1;}
if(X2>R[cc[k]].X2){Cover(R[cc[k]].X2,Y1,X2,Y2,k+,k1);X2=R[cc[k]].X2;}
if(Y1<R[cc[k]].Y1){Cover(X1,Y1,X2,R[cc[k]].Y1,k+,k1);Y1=R[cc[k]].Y1;}
if(Y2>R[cc[k]].Y2){Cover(X1,R[cc[k]].Y2,X2,Y2,k+,k1);Y2=R[cc[k]].Y2;}
}
int main()
{
int n,m,i,case1=,C=,j,sum;
while()
{
n=read();m=read();if(n==&&m==)break;
for(i=;i<=n;i++){R[i].X1=read();R[i].Y1=read();R[i].X2=read();R[i].Y2=read();}
printf("Case %d:\n",++case1);
C=;
for(i=;i<=m;i++)
{
s1=read();
for(j=;j<=s1;j++)cc[j]=read();
for(j=s1;j>=;j--)Cover(R[cc[j]].X1,R[cc[j]].Y1,R[cc[j]].X2,R[cc[j]].Y2,j+,j);//矩形切割法
sum=;
for(j=;j<=s1;j++){sum+=ans[j];ans[j]=;}
printf("Query %d: %d\n",++C,sum);
}
printf("\n");
}
fclose(stdin);
fclose(stdout);
return ;
}

Poj 3695-Rectangles 矩形切割的更多相关文章

  1. Poj 1151-Atlantis 矩形切割

    题目:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissio ...

  2. [USACO 3.1.4]rect1(漂浮法/矩形切割)

    描述 N个不同的颜色的不透明的长方形(1 <= N <= 1000)被放置在一张横宽为A竖长为B的白纸上. 这些长方形被放置时,保证了它们的边与白纸的边缘平行. 所有的长方形都放置在白纸内 ...

  3. poj 2540 Hotter Colder 切割多边形

    /* poj 2540 Hotter Colder 切割多边形 用两点的中垂线切割多边形,根据冷热来判断要哪一半 然后输出面积 */ #include <stdio.h> #include ...

  4. codeforces round#524 C. Masha and two friends /// 矩形切割

    题目大意: 给定n行m列的黑白棋盘如下 给定矩形的左下点x1 y1和右上点x2 y2将这个区域都涂成白色 再给定矩形的左下点x3 y3和右上点x4 y4将这个区域都涂成黑色 求最后棋盘内有分别多少个白 ...

  5. Java实现第十届蓝桥杯矩形切割

    试题 B: 矩形切割 本题总分:5 分 [问题描述] 小明有一些矩形的材料,他要从这些矩形材料中切割出一些正方形. 当他面对一块矩形材料时,他总是从中间切割一刀,切出一块最大的正方 形,剩下一块矩形, ...

  6. 洛谷P1884 [USACO12FEB]Overplanting S (矩形切割)

    一种矩形切割的做法: 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const in ...

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

    Rectangles Time Limit: 5000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

  9. POJ 2155 Matrix (矩形)

    date:公元2017年7月19日适逢周三: location:清北集训 杭州 point:二维树状数组/二维差分 Matrix Time Limit: 3000MS   Memory Limit:  ...

随机推荐

  1. Gradle实战教程之依赖管理

    这是从我个人网站中复制过来的,原文地址:http://coolshell.info/blog/2015/05/gradle-dependency-management.html,转载请注明出处. 简要 ...

  2. Sql server 浅谈用户定义表类型

    1.1 简介 SQL Server 中,用户定义表类型是指用户所定义的表示表结构定义的类型.您可以使用用户定义表类型为存储过程或函数声明表值参数,或者声明您要在批处理中或在存储过程或函数的主体中使用的 ...

  3. Android Activity 生命周期详解

    学习android开发这么久对于activity的生命周期还没有仔细思考过,所以,我大致的把这些东西整理一下,希望通过这使自己理解的更透彻点吧! 首先看一下Activity生命周期图和它的的四个阶段 ...

  4. js data日期初始化的5种方法new Date()

    var objDate=new Date([arguments list]); 参数形式有以下5种: 1)new Date("month dd,yyyy hh:mm:ss"); 2 ...

  5. js事件对象--DOM中的事件对象/IE中的事件对象/跨浏览器的事件对象

    事件对象    在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含着所有与事件有关的信息.包括导致事件的元素.事件的类型,以及其他与特定事件相关的信息.例如,鼠标操作导致的事件对 ...

  6. 在开发项目中有些常用的的实用代码(ps:平时看着无关紧要的,却很容易忘记)

    1,在客户端使用Cookie document.cookie = "key=1"; document.cookie = "name=zhangsan"; coo ...

  7. bzoj 1815: [Shoi2006]color 有色图 置换群

    1815: [Shoi2006]color 有色图 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 136  Solved: 50[Submit][Stat ...

  8. What does it mean for an algorithm to be fair

    What does it mean for an algorithm to be fair In 2014 the White House commissioned a 90-day study th ...

  9. textarea宽度、高度自动适应处理方法

    textarea自动高度 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http: ...

  10. 3G 2G GPRS 1G的概念

    3G, 第三代移动通信技术(3rd-generation,3G),是指支持高速数据传输的蜂窝移动通讯技术.3G服务 能够同时传送声音及数据信息,速率一般在几百kbps以上.3G标准:它们分别是WCDM ...