扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html

看图,图中的数字是横坐标离散后对应的下标,计算时左端点不变,右端点加1,所以总的更新的区间是l到r-1。

也可以理解为1代表的是(1到2这一段),2代表的是(2到3这一段),3代表的是(3到4这一段)。。。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e5+;
struct line
{
int x1,x2;
int h;
int cover;
bool operator < (const line &t)
{
return h<t.h;
}
};
vector<line>l;
vector<int>w;
struct Tree
{
int l,r;
int sum;
int cover;
}tree[N*]; void push_up(int rt)
{
if(tree[rt].cover)
{
tree[rt].sum=w[tree[rt].r+]-w[tree[rt].l];
}
else
{
if(tree[rt].l==tree[rt].r)tree[rt].sum=;
else tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
} void build(int rt,int l,int r)
{
tree[rt].sum=tree[rt].cover=;
tree[rt].l=l,tree[rt].r=r;
if(l==r)return ;
int m=(l+r)>>;
build(ls);
build(rs);
} void Update(int L,int R,int delta,int rt,int l,int r)
{
if(L<=l&&r<=R)
{
tree[rt].cover+=delta;
push_up(rt);
return ;
}
int m=(l+r)>>;
if(L<=m)Update(L,R,delta,ls);
if(R>m)Update(L,R,delta,rs);
push_up(rt);
} int binasrh(int val,int l,int r)
{
int m;
while(l<=r)
{
m=(l+r)>>;
if(w[m]==val)return m;
else if(w[m]<val)l=m+;
else r=m-;
}
return -;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,x1,x2,y1,y2;
cin>>n;
for(int i=;i<n;i++)
{
cin>>x1>>y1>>x2>>y2;
if(x1>x2||y1>y2)
{
swap(x1,x2);
swap(y1,y2);
}
x2++;
y2++;
l.pb(line{x1,x2,y1,});
l.pb(line{x1,x2,y2,-});
w.pb(x1);
w.pb(x2);
}
sort(w.begin(),w.end());
sort(l.begin(),l.end());
w.erase(unique(w.begin(),w.end()),w.end()); ll ans=;
build(,,w.size()-);
for(int i=;i<l.size()-;i++)
{
int L=binasrh(l[i].x1,,w.size()-);
int R=binasrh(l[i].x2,,w.size()-);
if(L<R)Update(L,R-,l[i].cover,,,w.size()-);
ans+=(ll)tree[].sum*(l[i+].h-l[i].h);
} cout<<ans<<endl;
return ;
}

610D - Vika and Segments(线段树+扫描线+离散化)的更多相关文章

  1. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  2. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  3. Codeforces 610D Vika and Segments 线段树+离散化+扫描线

    可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...

  4. hdu1542 Atlantis (线段树+扫描线+离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  6. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...

  7. poj1151 Atlantis (线段树+扫描线+离散化)

    有点难,扫描线易懂,离散化然后线段树处理有点不太好理解. 因为这里是一个区间,所有在线段树中更新时,必须是一个长度大于1的区间才是有效的,比如[l,l]这是一根线段,而不是区间了. AC代码 #inc ...

  8. hdu 4419 线段树 扫描线 离散化 矩形面积

    //离散化 + 扫描线 + 线段树 //这个线段树跟平常不太一样的地方在于记录了区间两个信息,len[i]表示颜色为i的被覆盖的长度为len[i], num[i]表示颜色i 『完全』覆盖了该区间几层. ...

  9. POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...

  10. HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. dedecms开启多站点

    dedecms开启多站点后,填写域名才能正确的地址 if ( ! function_exists('GetFileUrl')) { function GetFileUrl($aid,$typeid,$ ...

  2. Echarts使用及动态加载图表数据 折线图X轴数据动态加载

    Echarts简介 echarts,缩写来自Enterprise Charts,商业级数据图表,一个纯JavaScript的图表库,来自百度...我想应该够简洁了 使用Echarts 目前,就官网的文 ...

  3. json 的相互 转换

    using System.Runtime.Serialization.Json; //json 转化为List集合 public List<T> JSONStringToList<T ...

  4. C++飞机大战

    #include<windows.h> #include"resource.h" #include<stdlib.h> #include<time.h ...

  5. Python: 读文件,写文件

    读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...

  6. centos7 centos-home 磁盘转移至centos-root下

    1.查看分区 df -h (centos-home和centos-root每人的名字可能不一样) vgdisplay (查看空闲磁盘大小) 2.备份home分区文件 tar cvf /tmp/home ...

  7. 使用token实现在有效期内APP自动登录功能

    实现此功能的场景是在当下用户对手机APP体验要求高,并且相对安全前提的推动下诞生:当你下载了一个QQ,微信第一次进行了账号和密码的登录,你从此以后打开应用免去了你每日打开应用都要输入账号跟密码的痛苦过 ...

  8. (八)git更改提交操作

    1.git reset --hard + hash值 2.git reflog 查看当前仓库的操作日志 3.git commit --amend 修改提交信息(上一条) 4.git rebase -i ...

  9. 02:zabbix-agent安装配置 及 web界面管理

    目录:Django其他篇 01: 安装zabbix server 02:zabbix-agent安装配置 及 web界面管理 03: zabbix API接口 对 主机.主机组.模板.应用集.监控项. ...

  10. Starting MySQL...The server quit without updating PID file [失败]local/mysql/data/localhost.localdomain.pid报错

    在添加命令自动补全的时候mysql启动失败 这是原配 # For advice on how to change settings please see # http://dev.mysql.com/ ...