题目链接:

D. Vika and Segments

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.

Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.

Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.

 
Output

Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.

Examples
input
3
0 1 2 1
1 4 1 2
0 3 2 3
output
8
input
4
-2 -1 2 -1
2 1 -2 1
-1 -2 -1 2
1 2 1 -2
output
16
Note

In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).

题意:

给了这么多线段,问它们一共包含了多少个点;

思路:

把线段变成宽为1的矩形,然后用扫描线算法求面积;

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+4;
int n,x1,x2,y3,y2,rec[2*N],num;
struct no
{
int l,r,h,flag;
};
no line[8*N];
struct nod
{
int l,r,cover;
ll sum;
};
nod tree[8*N];
int cmp(no x,no y)
{
return x.h<y.h;
}
void build(int node,int L,int R)
{
tree[node].l=L,tree[node].r=R;
tree[node].cover=tree[node].sum=0;
if(L>=R)return ;
int mid=(L+R)>>1;
build(2*node,L,mid);
build(2*node+1,mid+1,R);
}
void Pushup(int node)
{
if(tree[node].cover)
{
tree[node].sum=rec[tree[node].r+1]-rec[tree[node].l];
}
else
{
if(tree[node].l==tree[node].r)tree[node].sum=0;
else tree[node].sum=tree[2*node].sum+tree[2*node+1].sum;
}
}
void update(int node,int L,int R,int x)
{
if(L<=tree[node].l&&R>=tree[node].r)
{
tree[node].cover+=x;
Pushup(node);
return ;
}
int mid=(tree[node].l+tree[node].r)>>1;
if(L>mid) update(2*node+1,L,R,x);
else if(R<=mid)update(2*node,L,R,x);
else
{
update(2*node,L,mid,x);
update(2*node+1,mid+1,R,x);
}
Pushup(node);
}
int bi(int x)
{
int L=1,R=num-1,mid;
while(L<=R)
{
mid=(L+R)>>1;
if(rec[mid]==x)return mid;
else if(rec[mid]>x)R=mid-1;
else L=mid+1;
}
return -1;
}
int main()
{
scanf("%d",&n);
int cnt=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&x1,&y3,&x2,&y2);
if(x1>x2)swap(x1,x2);
if(y3>y2)swap(y3,y2);
rec[cnt] = line[cnt].l = x1;
line[cnt].r = x2+1;
line[cnt].h = y3;
line[cnt++].flag = 1;
line[cnt].l = x1;
rec[cnt] = line[cnt].r = x2+1;
line[cnt].h = y2+1;
line[cnt++].flag = -1;
}
sort(line+1,line+cnt,cmp);
sort(rec+1,rec+cnt);
num = 2;
for(int i = 2;i < cnt;i++)
{
if(rec[i]!=rec[i-1])rec[num++]=rec[i];
}
build(1,1,num-1);
ll ans=0;
for(int i = 1;i < cnt-1;i++)
{
int fx = bi(line[i].l);
int fy = bi(line[i].r)-1;
if(fx <= fy)
{
update(1,fx,fy,line[i].flag);
}
ans+=tree[1].sum*(ll)(line[i+1].h-line[i].h);
}
cout<<ans<<"\n";
return 0;
}

  

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

  1. hdu-1542 Atlantis(离散化+线段树+扫描线算法)

    题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others) ...

  2. 610D - Vika and Segments(线段树+扫描线+离散化)

    扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 看图,图中的数字是横坐标离散后对应的下标,计算时左端点不 ...

  3. 【20.51%】【codeforces 610D】Vika and Segments

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. Educational Codeforces Round 23 F. MEX Queries 离散化+线段树

    F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. 南阳理工 题目9:posters(离散化+线段树)

    posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 The citizens of Bytetown, AB, could not stand that ...

  6. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  7. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  8. hpu校赛--雪人的高度(离散化线段树)

    1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec  内存限制: 128 MB 提交: 81  解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...

  9. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

随机推荐

  1. 面试宝典之预处理、const与sizeof

    #include <stdio.h> #define SUB(x, y) x - y #define ACCESS_BEFORE(element, offset, value) *SUB( ...

  2. Java 调用OPENOFFIC 转换文档类型

    public static void office2PDF(String sourceFile, String destFile) { try { File inputFile = new File( ...

  3. anaconda3.5 3.6 2.7

    https://repo.continuum.io/archive/ Filename Size Last Modified MD5 Anaconda2-5.0.1-Linux-x86.sh 413. ...

  4. 数据挖掘之pandas

    sdata={'语文':89,'数学':96,'音乐':39,'英语':78,'化学':88} #字典向Series转化 @@ >>> studata=Series(sdata) & ...

  5. Spark源码分析之三:Stage划分

    继上篇<Spark源码分析之Job的调度模型与运行反馈>之后,我们继续来看第二阶段--Stage划分. Stage划分的大体流程如下图所示: 前面提到,对于JobSubmitted事件,我 ...

  6. 【WPF学习笔记】之如何传递第一个登录界面的值到下一个页面显示:动画系列之(三)

    ... ... 承接系列(二) 在之前的登录后台已设置发送到主界面: 在主界面接收传递的值: using System; using System.Collections.Generic; using ...

  7. 部署mongodb中需要注意的调参

    部署mongodb的生产服务器,给出如下相关建议: 使用虚拟化环境: 系统配置 1)推荐RAID配置 RAID(Redundant Array of Independent Disk,独立磁盘冗余阵列 ...

  8. Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现

    看到cocos2d-x推出了3.1版本号,真是每月一次新版本号,速度. 另一个好消息就是http://cn.cocos2d-x.org/上线了,祝贺!啥时候把我的视频和教程放上去呢?!! . 视频下载 ...

  9. Android开发:LocationManager获取经纬度及定位过程(附demo)

    在Android开发其中.常常须要用到定位功能,尤其是依赖于地理位置功能的应用.非常多人喜欢使用百度地图,高德地图提供的sdk.开放API,可是在只须要经纬度,或者城市,街道地址等信息.并不须要提供预 ...

  10. Java知识点梳理——读写分离

    1.读写分离:可以通过Spring提供的AbstractRoutingDataSource类,重写determineCurrentLookupKey方法,实现动态切换数据源的功能:读写分离可以有效减轻 ...