题目链接: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 (线段树+扫描线+离散化)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. 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. 这题 ...

  6. 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 ...

  7. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  8. 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所 ...

  9. 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 ...

随机推荐

  1. ireport制作小技巧

    ireport制作小技巧 首先ireport中大小写问题: 1.parameter中如果小写,引用也小写 2.$F{},一般都大写 3.子报表中引用父报表中查询出来的值时,只需要小写即可,即在子报表的 ...

  2. javascript实现map的功能(转载)

    /* * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, v ...

  3. Ejabberd源码解析前奏--配置

    一.基本配置     配置文件将在你第一次启动ejabberd时加载,从该文件中获得的内容将被解析并存储到内部的ejabberd数据库中,以后的配置将从数据库加载,并且任何配置文件里的命令都会被添加到 ...

  4. 新闻类App使用的组件

    UI SlidingMenu:com.jeremyfeinstein.slidingmenu:滑动菜单 ActionBarSherlock:com.actionbarsherlock:Action B ...

  5. git push冲突解决

    1. 首先,可以试图用git push origin branch-name推送自己的修改:2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并:如果git pull提示 ...

  6. SQL Server 高性能写入的一些总结(转)

    1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...

  7. Jacoco远程统计代码覆盖率

    Jacoco   什么是Jacoco? Jacoco是一个开源的Java代码覆盖率工具,Jacoco可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAg ...

  8. 我的EC-final总结

    by.Max EC-final正式结束,也预示着我大学ICPC旅程的结束.回来睡了一天,现在也可以总结一下了 被告知参赛: 本来以为就会这样告别ACM-ICPC,没想到半个月前徐老师告诉我们SHU给我 ...

  9. codevs1796-最小完全图

    表示第一篇就是水题. 根据Prim的思想,我们可以证明:dis ( a , b ) > max { a b 最小生成树路径上的边权 } 把所有边sort一遍用并查集维护就可以了 #include ...

  10. 【转】Yahoo!团队:网站性能优化的35条黄金守则

    Yahoo!的 Exceptional Performance团队为改善 Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客并在各种会议上参与探讨.最佳实践的核心 ...