POJ1177+线段树+扫描线
思路:
以y的值进行离散化
根据x的值 对每一条y轴边进行处理,如果是"左边"则插入,是"右边"则删除。
/*
扫描线+线段树+离散化
求多个矩形的周长
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
using namespace std;
const int maxn = ;
const int maxm = ;
struct SegTree{
int l,r;
int len;//区间代表的长度
int segnum;//区间被分成的段数
int cover;//区间被覆盖的次数
int sum;//区间被覆盖的总长度
bool lcover,rcover;
}ST[ maxm<< ];
struct Line{
int st,ed,x;//竖边的两个y值
bool InOut;//是否为左边
bool operator < (Line L) const{
return x<L.x;
}
}; Line yLine[ maxm ];
int yIndex[ maxm ];
int n; void build( int L,int R,int n ){
ST[ n ].l = L;
ST[ n ].r = R;
ST[ n ].len = yIndex[ R ]-yIndex[ L ];
ST[ n ].sum = ST[ n ].cover = ST[ n ].segnum = ;
ST[ n ].lcover = ST[ n ].rcover = false;
if( R-L> ){
int mid = (L+R)/;
build( L,mid,*n );
build( mid,R,*n+ );
}
return ;
} void Update_Len( int n ){
if( ST[n].cover> ){
ST[n].sum = ST[n].len;
}
else if( ST[n].r-ST[n].l> ){
ST[n].sum = ST[*n].sum+ST[*n+].sum;
}
else
ST[n].sum = ;
} void Update_Segnum( int n ){
if( ST[n].cover> ){
ST[n].lcover = ST[n].rcover = true;
ST[n].segnum = ;
}
else if( ST[n].r-ST[n].l> ){
ST[n].lcover = ST[*n].lcover;
ST[n].rcover = ST[*n+].rcover;
ST[n].segnum = ST[*n].segnum+ST[*n+].segnum-ST[*n].rcover*ST[*n+].lcover;
}
else{
ST[n].segnum = ;
ST[n].lcover = ST[n].rcover = false;
}
} void PushUp ( int n ){
Update_Len( n );//求节点包含的线段总长度
Update_Segnum( n );
} void Insert( int left,int right,int n ){
if( ST[ n ].l==left&&ST[ n ].r==right ){
ST[ n ].cover++;
}
else {
int mid = (ST[ n ].l+ST[ n ].r)/;
if( right<=mid )
Insert( left,right,*n );
else if( left>=mid )
Insert( left,right,*n+ );
else{
Insert( left,mid,*n );
Insert( mid,right,*n+ );
}
}
PushUp( n );
} void Delete( int left,int right,int n ){//删除矩形的右边
if( ST[ n ].l==left&&ST[ n ].r==right ){
ST[ n ].cover--;
}
else {
int mid = (ST[ n ].l+ST[ n ].r)/;
if( right<=mid )
Delete( left,right,*n );
else if( left>=mid )
Delete( left,right,*n+ );
else{
Delete( left,mid,*n );
Delete( mid,right,*n+ );
}
}
PushUp( n );
} int GetIndex( int value ,int cnt ){
return lower_bound(yIndex,yIndex+cnt,value )-yIndex;
} int main(){
while( scanf("%d",&n)== ){
int cnt = ;
int x1,y1,x2,y2;
for( int i=;i<n;i++ ){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
yLine[ *i ].x = x1;
yLine[ *i+ ].x = x2;
yLine[ *i ].st = yLine[ *i+ ].st = y1;
yLine[ *i ].ed = yLine[ *i+ ].ed = y2;
yLine[ *i ].InOut = true;
yLine[ *i+ ].InOut = false;
yIndex[ *i ] = y1;
yIndex[ *i+ ] = y2;
}
sort( yIndex,yIndex+*n );
sort( yLine,yLine+*n );
for( int i=;i<*n;i++ ){
if( yIndex[i]!=yIndex[i-] )
yIndex[cnt++] = yIndex[i-];
}
yIndex[cnt++] = yIndex[*n-];
build( ,cnt-, );
int Ans = ;
int PreSum = ;;//上一次记录的长度
for( int i=;i<*n-;i++ ){
if( yLine[i].InOut ){
Insert( GetIndex(yLine[i].st,cnt),GetIndex(yLine[i].ed,cnt), );
}
else{
Delete( GetIndex(yLine[i].st,cnt),GetIndex(yLine[i].ed,cnt), );
}
Ans += ST[].segnum**(yLine[i+].x-yLine[i].x);
Ans += abs(ST[].sum-PreSum);
PreSum = ST[].sum;
}
Delete( GetIndex(yLine[*n-].st,cnt),GetIndex(yLine[*n-].ed,cnt), );
//特殊处理最后一条出边,因为没有下一条竖边了
Ans += abs(ST[].sum-PreSum);
printf("%d\n",Ans);
}
return ;
}
POJ1177+线段树+扫描线的更多相关文章
- 【学习笔记】线段树—扫描线补充 (IC_QQQ)
[学习笔记]线段树-扫描线补充 (IC_QQQ) (感谢 \(IC\)_\(QQQ\) 大佬授以本内容的著作权.此人超然于世外,仅有 \(Luogu\) 账号 尚可膜拜) [学习笔记]线段树详解(全) ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤
3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...
- BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞
看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...
- hdu 5091(线段树+扫描线)
上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...
- POJ1151+线段树+扫描线
/* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
随机推荐
- C# is 强制转换
在平时开发中,经常遇上强制转换,在这过程中经常遇上null对象转换为值类型,如果不判断的情况下在编译的时候不会出错,但程序一运行就抛出错误.好在C#为我们提供了is ,它判断一个对象如果成立就转换,如 ...
- 查看Unix系统是32位还是64位
#getconf查看OS位数 以下经过测试了HP: getconf KERNEL_BITSLinux: getconf LONG_BITAIX: getconf KERNEL_BITMODE #AIX ...
- dapper关联关系查询小测试
测试实体类(表结构) public class User { public int user_id { get; set; } public string user_name { get; set; ...
- HDU 1087 Super Jumping! Jumping! Jumping!(动态规划)
Super Jumping! Jumping! Jumping! Problem Description Nowadays, a kind of chess game called “Super Ju ...
- 阅读 Linux 内核源码
阅读Linux Kernel Source Code 假如你在Linux系统下面阅读Linux内核源代码,那么需要准备一些工具. ①Linux的内核源码 内核源码的下载地址:Index of /pub ...
- 洛谷 P1108 低价购买
P1108 低价购买 标签 动态规划 难度 提高+/省选- 题目描述 "低价购买"这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:& ...
- [GeekBand] C++学习笔记(1)——以复数类为例
本篇笔记以复数类(不含指针的类)为例进行面向对象的学习 ========================================================= 复数类的声明: class ...
- (转)MySql可视化工具MySQL Workbench使用教程
转自:http://www.cnblogs.com/daimage/archive/2012/02/25/2367534.html 1. MySQL Workbench MySQL Workbench ...
- [转]select模型的一种技巧运用-libevent
参见网址 http://www.cppblog.com/xvsdf100/archive/2013/12/10/204689.html
- Sublime Text 3的快捷键
Sublime Text 3是一个非常了不起的软件,它不仅具有令人难以置信的内置功能(多行编辑和VIM模式),而且还支持插件.代码片段和其它许多东西. 今天,我们来总结一下Sublime Text 3 ...