Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D
就是给你宽度为1的n个线段,然你求总共有多少单位的长度。
相当于用线段树求面积并,只不过宽为1,注意y和x的最大都要+1,这样才相当于求面积。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int MAXN = 4e5 + ;
typedef __int64 LL;
struct data {
LL x1 , x2 , y , flag;
bool operator <(const data& cmp) const {
return y < cmp.y;
}
}a[MAXN];
struct segtree {
LL l , r , lazy , val;
}T[MAXN << ];
map <LL , LL> mp;
LL x[MAXN]; void build(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r , T[p].val = T[p].lazy = ;
if(r - l == ) {
return ;
}
build(p << , l , mid);
build((p << )| , mid , r);
} void pushup(int p) {
if(T[p].lazy) {
T[p].val = (x[T[p].r] - x[T[p].l]);
}
else if(T[p].r - T[p].l == ) {
T[p].val = ;
}
else {
T[p].val = T[p << ].val + T[(p << )|].val;
}
} void updata(int p , int l , int r , int val) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
T[p].lazy += val;
pushup(p);
return ;
}
if(r <= mid) {
updata(p << , l , r , val);
}
else if(l >= mid) {
updata((p << )| , l , r , val);
}
else {
updata(p << , l , mid , val);
updata((p << )| , mid , r , val);
}
pushup(p);
} int main()
{
int n , f = , cnt = ;
LL x1 , x2 , y1 , y2;
scanf("%d" , &n);
for(int i = ; i < n ; i++) {
scanf("%I64d %I64d %I64d %I64d" , &x1 , &y1 , &x2 , &y2);
if(x1 > x2)
swap(x1 , x2);
x2++;
if(y1 > y2)
swap(y1 , y2);
y2++;
a[f].x1 = x1 , a[f].x2 = x2 , a[f].y = y1 , a[f].flag = ;
f++;
a[f].x1 = x1 , a[f].x2 = x2 , a[f].y = y2 , a[f].flag = -;
f++;
if(!mp[x1]) {
mp[x1] = ;
x[++cnt] = x1;
}
if(!mp[x2]) {
mp[x2] = ;
x[++cnt] = x2;
}
}
build( , , cnt);
sort(a , a + f);
sort(x + , x + cnt + );
for(int i = ; i < f ; i++) {
int pos = lower_bound(x + , x + cnt + , a[i].x1) - x;
a[i].x1 = pos;
pos = lower_bound(x + , x + cnt + , a[i].x2) - x;
a[i].x2 = pos;
}
LL res = ;
updata( , a[].x1 , a[].x2 , a[].flag);
for(int i = ; i < f ; i++) {
res += (LL)(a[i].y - a[i - ].y) * T[].val;
updata( , a[i].x1 , a[i].x2 , a[i].flag);
}
printf("%I64d\n" , res);
}
Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
随机推荐
- ZJOI2006物流运输
唉,没想出来…… 注意到预处理的作用.还有CLJ大牛说的话:这么小的数据,想干什么都可以. SPFA预处理+DP 够经典 var f:..,..]of longint; a:..,..]of bool ...
- hdu 4609 3-idiots(快速傅里叶FFT)
比较裸的FFT(快速傅里叶变换),也是为了这道题而去学的,厚的白书上有简单提到,不过还是推荐看算法导论,讲的很详细. 代码的话是照着别人敲的,推荐:http://www.cnblogs.com/kua ...
- WebService 出现因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。
要在webservice的web.config文件中的 <system.web> 节点下加入: <webServices> <protocols> ...
- Mybatis学习——一对一关联表查询
1.SQL语句建表 CREATE TABLE teacher( t_id ) ); CREATE TABLE class( c_id ), teacher_id INT ); ALTER TABLE ...
- “FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”。如果是有意隐藏,请使用关键字 new。
一旦运行就显示:“FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”.如果是有意隐藏,请使用关键字 ne ...
- HDU 5875 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU5045-Contest(状压dp)
题意: 有n个学生,m道题,给出每个同学解出m个问题的概率,在解题过程中每个学生的解题数的差不大于1,求最大能解出题目数的期望 分析: n很小,知道用状压,但是比赛没做出来(脑子太死了,有一个限制条件 ...
- 2016计蒜之道复赛 菜鸟物流的运输网络 网络流EK
题源:https://nanti.jisuanke.com/t/11215 分析:这题是一个比较经典的网络流模型.把中间节点当做源,两端节点当做汇,对节点进行拆点,做一个流量为 22 的流即可. 吐槽 ...
- 向Window BCD 文件添加VHD开机启动项的相关笔记
******************************************************************************** * BCD_YE_MIN文件说明:(精 ...
- 《Nagios系统监控实践》勘误
在翻译的过程中,虽然反反复复的检查了很多遍,但依然有所遗漏——这不,今天就收到了 @我是晓梦 的回复,指出了书中的一些错误. 从今天起,建立勘误表,记录这些错误,以便在下一次印刷时纠正,并对广大读者致 ...