hdu1828(线段树+扫描线)
又知道了线段树的一种用法,除了单点更新,区间更新,还有这种在一段线段上标号但不往下推。 真是神奇
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <stdlib.h>
using namespace std;
#define N 5050 struct RECT
{
int x1,y1,x2,y2;
}g[N]; struct LINE
{
int k,b,d;
int flag;
}line[*N]; int ans=;
int n; int l[],r[],mark[],yb[],yd[],num[]; int cmp(LINE x,LINE y)
{
return x.k<y.k;
} void build(int tl,int tr,int s)
{
l[s]=tl; r[s]=tr;
mark[s]=;yb[s]=; yd[s]=; num[s]=;
if(tl==tr) return ;
int mid=(tl+tr)/;
build(tl,mid,*s);
build(mid+,tr,*s+);
} void up(int s)
{
if(mark[s]>)
{
num[s]=;
yb[s]=;
yd[s]=;
}
else
{
if(l[s]==r[s])
{
num[s]=;
yb[s]=;
yd[s]=;
}
else
{
if(yd[*s]==&&yb[*s+]==)
num[s]=num[*s]+num[*s+]-;
else num[s]=num[*s]+num[*s+];
yb[s]=yb[*s];
yd[s]=yd[*s+];
}
}
} void update(int tl,int tr,int sign,int s)
{
if(tl==l[s]&&tr==r[s])
{
mark[s]+=sign;
up(s);
return ;
}
int mid=(l[s]+r[s])/;
if(tr<=mid) update(tl,tr,sign,*s);
else if(tl>mid) update(tl,tr,sign,*s+);
else
{
update(tl,mid,sign,*s);
update(mid+,tr,sign,*s+);
}
up(s);
} void fuc()
{
int cnt=;
int mix=,mxx=-; for(int i=;i<n;i++)
{
line[cnt].k=g[i].y1;
line[cnt].b=g[i].x1;
line[cnt].d=g[i].x2-;//注意可能为负的
line[cnt].flag=;
cnt++; mix=min(mix,g[i].x1);
mxx=max(mxx,g[i].x2-); line[cnt].k=g[i].y2;
line[cnt].b=g[i].x1;
line[cnt].d=g[i].x2-;
line[cnt].flag=-;
cnt++;
}
sort(line,line+cnt,cmp);
build(mix,mxx,);
for(int i=;i<cnt-;i++)
{
update(line[i].b,line[i].d,line[i].flag,);
ans += (line[i+].k-line[i].k)**num[];
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
{
scanf("%d%d%d%d",&g[i].x1,&g[i].y1,&g[i].x2,&g[i].y2);
g[i].x1+=;
g[i].x2+=;
g[i].y1+=;
g[i].y2+=;
}
if(n==)
{
printf("0\n");
continue;
}
ans=;
fuc();
//
for(int i=;i<n;i++)
{
swap(g[i].x1,g[i].y1);
swap(g[i].x2,g[i].y2);
}
fuc();
printf("%d\n",ans);
}
return ;
}
hdu1828(线段树+扫描线)的更多相关文章
- hdu1828 线段树扫描线求矩形面积的周长
题意: 给你n个矩形,问你这n个矩形所围成的图形的周长是多少. 思路: 线段树的扫描线简单应用,这个题目我用的方法比较笨,就是扫描两次,上下扫描,求出多边形的上下边长和,然后同 ...
- hdu1828 Picture(线段树+扫描线+矩形周长)
看这篇博客前可以看一下扫描线求面积:线段树扫描线(一.Atlantis HDU - 1542(覆盖面积) 二.覆盖的面积 HDU - 1255(重叠两次的面积)) 解法一·:两次扫描线 如图我们可以 ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))
扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...
- 【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 ...
随机推荐
- flex Chrome flash调试时 出现Shockwave flash has crashed的解决办法
在Chrome中输入:chrome://plugins/ PPAPI的Flash Player停用. 使用NPAPI的Flash player. 这里好像没有显示是Debug版本. 但是我在调 ...
- UML教程首页(转载)
UML是一种标准语言,用于指定,可视化,构造和文档的软件系统的文物. UML是OMG在1997年1月提出了创建由对象管理组和UML1.0规范草案. 本教程给出了一个比较完整的学习理解UML,可以方便学 ...
- C#中“貌似”跳出while(true)死循环
当程序第一次执行到Read()函数时,程序会被阻塞,然后输入字符,Enter之后程序被激活,windows平台会自动在输入字符之后加入回车符和换行符,此时输入流中就有三个字符,然而read每次只读取一 ...
- 13test07;字符排序,去重,三三输出
#include<iostream> #include<string> using namespace std; void buddle(char*,int);//对输入字符的 ...
- 转 wince程序 中使用Listview显示图标问题 (C#) .
思路: 1.窗体控件:lstaqgl [Listview控件名称] imageList1[ImageList控件] 2. 图片路径添加到—imageList1——Listview显示图片从 ima ...
- C# 比较方法
public int Compare(Product first, Product second) { return PartialComparer.RefernceCompare(first, se ...
- poj 2068 Nim 博弈论
思路:dp[i][j]:第i个人时还剩j个石头. 当j为0时,有必胜为1: 后继中有必败态的为必胜态!!记忆化搜索下就可以了! 代码如下: #include<iostream> #incl ...
- PHP5.4最新特性
PHP5.4最新特性 官网:ChangeLog-5.php#5.4.0 原文Oracle:LAMP 体系有了新的竞争,但此版本中的特性使 PHP 再次挑战极限. 稍微做了修改.: 概述总结:1. ...
- jquery常见问题
1.下面哪种说法是正确的? 您的回答:jQuery 是 JavaScript 库 2.jQuery 使用 CSS 选择器来选取元素? 您的回答:错误 正确答案:正确 3.jQuery 的简写是? 您的 ...
- jackson set properties to default value (取消让jackson 赋予默认值)
you can define it with Integer rather than int or long. define it with a package type. jackson wont' ...