Poj 3695-Rectangles 矩形切割
Rectangles
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 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). Sample Input 2 2 Sample Output Case 1: 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 矩形切割的更多相关文章
- Poj 1151-Atlantis 矩形切割
题目:http://poj.org/problem?id=1151 Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- [USACO 3.1.4]rect1(漂浮法/矩形切割)
描述 N个不同的颜色的不透明的长方形(1 <= N <= 1000)被放置在一张横宽为A竖长为B的白纸上. 这些长方形被放置时,保证了它们的边与白纸的边缘平行. 所有的长方形都放置在白纸内 ...
- poj 2540 Hotter Colder 切割多边形
/* poj 2540 Hotter Colder 切割多边形 用两点的中垂线切割多边形,根据冷热来判断要哪一半 然后输出面积 */ #include <stdio.h> #include ...
- codeforces round#524 C. Masha and two friends /// 矩形切割
题目大意: 给定n行m列的黑白棋盘如下 给定矩形的左下点x1 y1和右上点x2 y2将这个区域都涂成白色 再给定矩形的左下点x3 y3和右上点x4 y4将这个区域都涂成黑色 求最后棋盘内有分别多少个白 ...
- Java实现第十届蓝桥杯矩形切割
试题 B: 矩形切割 本题总分:5 分 [问题描述] 小明有一些矩形的材料,他要从这些矩形材料中切割出一些正方形. 当他面对一块矩形材料时,他总是从中间切割一刀,切出一块最大的正方 形,剩下一块矩形, ...
- 洛谷P1884 [USACO12FEB]Overplanting S (矩形切割)
一种矩形切割的做法: 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const in ...
- hdu 2461(AC) & poj 3695(TLE)(离散化+矩形并)
Rectangles Time Limit: 5000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- POJ 2155 Matrix (矩形)
date:公元2017年7月19日适逢周三: location:清北集训 杭州 point:二维树状数组/二维差分 Matrix Time Limit: 3000MS Memory Limit: ...
随机推荐
- ubuntu nginx 伪静态 设置
简单的静态设置 1 vim nginx.conf // 修改nginx配置文件 server { .... root /usr/local/nginx/html; #nginx网站根目录 #下面这个 ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 系统监控的一些工具w , vmstat
w 命令:--w 查看的是系统整体上的负载 # w 15:23:46 up 3:34, 2 users, load average: 0.03, 0.05, 0.00 USER TTY FROM LO ...
- [DevExpress][TreeList]节点互斥
关键代码: /// <summary> /// 节点互斥同步 /// 说明 /// eg: ///TreeListNode _node = e.Node; ///_node.SyncMut ...
- DIV+CSS 网页布局之:一列布局
1.网页布局 布局(layout)即对事物的全面规划和安排,页面布局是对页面的文字.图像或表格进行格式化版式排列.网页布局对改善网站的外观非常重要,又称版式布局,大多数网站会把内容安排到多个列中,就像 ...
- PHP程序实现利用rand(1,100)函数产生10个1~100之间的随机数
//echo rand(1,100);$max=0;$min=100;for($i=0;$i<=9;$i++){ $rand[$i]=rand(1,100); if($rand[$i]>$ ...
- CoreGraphics --- CGContext
CGContext又叫图形上下文,相当于一块画布,以堆栈形式存放,只有在当前context上绘图才有效.iOS有分多种图形上下文,其中UIView自带提供的在drawRect:方法中通过UIGraph ...
- C++程序的构成和书写形式
C++程序的结构和书写格式归纳如下: (1) 一个C++程序可以由一个程序单位或多个程序单位构成.每一个程序单位作为一个文件.在程序编译时,编译系统分别对各个文件进行编译,因此,一个文件是一个编译单 ...
- [BZOJ 2721] [Violet 5] 樱花 【线性筛】
题目链接:BZOJ - 2721 题目分析 题目大意:求出 1 / x + 1 / y = 1 / n! 的正整数解 (x, y) 的个数. 显然,要求出正整数解 (x, y) 的个数,只要求出使 y ...
- 一周一话题之一(EF-CodeFirst、MEF、T4框架搭建学习)
本话题是根据郭明峰博客<MVC实用架构系列>的搭建学习总结. -->目录导航 一.数据仓储访问的构建 1.UnitOfWork的构建 2.Repository的构建 ...