codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)
题目链接:
2 seconds
256 megabytes
standard input
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.
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.
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.
3
0 1 2 1
1 4 1 2
0 3 2 3
8
4
-2 -1 2 -1
2 1 -2 1
-1 -2 -1 2
1 2 1 -2
16
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(离散化+线段树+扫描线算法)的更多相关文章
- hdu-1542 Atlantis(离散化+线段树+扫描线算法)
题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 610D - Vika and Segments(线段树+扫描线+离散化)
扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 看图,图中的数字是横坐标离散后对应的下标,计算时左端点不 ...
- 【20.51%】【codeforces 610D】Vika and Segments
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- hpu校赛--雪人的高度(离散化线段树)
1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...
- 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
[BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...
随机推荐
- 安装配置 Kafka Manager 分布式管理工具
Kafka Manager 特性,它支持以下内容(官方译解): 管理多个群集容易检查集群状态(主题,消费者,偏移量,经纪人,副本分发,分区分配)运行首选副本选举使用选项生成分区分配,以选择要使用的代理 ...
- detect——point_in_polygon
/******************实现功能:判断平面任一点是否在指定多边形内********************/ #include <string> #include <v ...
- Android之Intent和Activity
Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...
- Java中的Enum的继承
public interface Icolor{ int apply(int x,int y); } public enum color implements Icolor{ plus("+ ...
- Codeforces 467D Fedor and Essay bfs
题目链接: 题意: 给定n个单词. 以下有m个替换方式.左边的单词能变成右边的单词. 替换随意次后使得最后字母r个数最少,在r最少的情况下单词总长度最短 输出字母r的个数和单词长度. 思路: 我们觉得 ...
- 开源项目WebImageView载入图片
项目地址:https://github.com/ZaBlanc/WebImageView 作者对载入图片,以及图片的内存缓存和磁盘缓存做了封装. 代码量不多.可是可以满足一般的载入图片. 先看下项目结 ...
- JS 之 数据类型转换
首先我们来简单了解一下JS中的数据类型,JavaScript拥有字符串.数字.布尔.数组.对象.Null.Undefiend 6中数据类型.同一时候,JavaScript拥有动态类型. 也 ...
- Python中urllib2总结
使用Python访问网页主要有三种方式: urllib, urllib2, httpliburllib比较简单,功能相对也比较弱,httplib简单强大,但好像不支持session1. 最简单的页面访 ...
- EasyPlayerPro(Windows)流媒体播放器开发之跨语言调用
下面我们来讲解一下关于EasyPlayerPro接口的调用,主要分为C++和C#两种语言,C++也可以基于VC和QT进行开发,C++以VC MFC框架为例进行讲解,C#以Winform框架为例进行讲解 ...
- Netty聊天室-源码
目录 Netty聊天室 源码工程 写在前面 [百万级流量 聊天室实战]: [分布式 聊天室] [Spring +Netty]: [Netty 原理] 死磕 系列 [提升篇]: [内力大增篇]: 疯狂创 ...