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.

Sample test(s)
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).

分析:

这是一道计数题,方法比较传统,离散、扫描、树状数组、求差。

首先考虑将共线的线段合并,用O(nlogn)排序后线性合并。处理后的线段都是相离的。

再考虑用水平扫描线自顶向下扫描,将线段的左右端点看作是对纵线范围数值求和,事先将纵线按照较高的y值从大大小排列。

那么用树状数组储存那些横跨当前水平扫描线y值得纵线,如果是考虑线段,那么需要同时考虑进队和出队,然而若将线段看成前缀之差,那么只需考虑进队,进行两次计算

作差即可。从而计数交点的数目。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef __int64 ll;
const int maxn = 2e5 + ;
int n, nh, nv;
int base;
map<int, int> mapi;
struct H{
int x1, x2, y;
H(int x1 = , int x2 = , int y = ) : x1(x1), x2(x2), y(y) {}
bool operator < (const H &rhs) const{
if(y < rhs.y) return ;
if(y == rhs.y && x1 < rhs.x1) return ;
if(y == rhs.y && x1 == rhs.x1 && x2 < rhs.x2) return ;
return ;
}
}h[maxn], h1[maxn]; struct V{
int y1, y2, x;
V(int y1 = , int y2 = , int x = ) : y1(y1), y2(y2), x(x) {}
bool operator < (const V &rhs) const{
if(x < rhs.x) return ;
if(x == rhs.x && y1 < rhs.y1) return ;
if(x == rhs.x && y1 == rhs.y1 && y2 < rhs.y2) return ;
return ;
}
}v[maxn]; int inv[maxn];
int buf[maxn], nbuf;
ll x[maxn]; bool cmpv(V p, V q){
if(p.y2 > q.y2) return ;
if(p.y2 == q.y2 && p.y1 > q.y1) return ;
return ;
} bool cmph(H p, H q){
if(p.y > q.y) return ;
if(p.y == q.y && p.x1 < q.x1) return ;
if(p.y == q.y && p.x1 == q.x1 && p.x2 < q.x2) return ;
return ;
} bool cmpvy2(V p, V q){
return p.y2 > q.y2;
} int low_bit(int x) { return x & (-x); } void Push(int i){
int tem = mapi[v[i].x];
while(tem <= base){
x[tem]++;
tem += low_bit(tem);
}
} ll query(int i, int j){
i = mapi[i], j = mapi[j];
ll tem = ;
while(j >= ){
tem += x[j];
j -= low_bit(j);
}
--i;
while(i >= ){
tem -= x[i];
i -= low_bit(i);
}
return tem;
} void solve(){
sort(h, h + nh);
sort(v, v + nv);
mapi.clear();
ll ans = ;
int nh1 = , nv1 = ;
int head, tail;
if(nh){
head = h[].x1, tail = h[].x2;
nbuf = ;
for(int i = ; i < nh; i++){
if(h[i].y != h[i - ].y || h[i].x1 > tail){
ans += tail - head + ;
h[nh1++] = H(head, tail, h[i - ].y);
buf[nbuf++] = head, buf[nbuf++] = tail;
head = h[i].x1, tail = h[i].x2;
}else if(h[i].x2 > tail) tail = h[i].x2;
}
ans += tail - head + ;
h[nh1++] = H(head, tail, h[nh - ].y);
buf[nbuf++] = head, buf[nbuf++] = tail;
}if(nv){
head = v[].y1, tail = v[].y2;
for(int i = ; i < nv; i++){
if(v[i].x != v[i - ].x || v[i].y1 > tail){
ans += tail - head + ;
v[nv1++] = V(head, tail, v[i - ].x);
if(v[i].x != v[i - ].x) buf[nbuf++] = v[i - ].x;
head = v[i].y1, tail = v[i].y2;
}else if(v[i].y2 > tail) tail = v[i].y2;
}
ans += tail - head + ;
v[nv1++] = V(head, tail, v[nv - ].x);
buf[nbuf++] = v[nv - ].x;
}
sort(buf, buf + nbuf);
base = ;
mapi[buf[]] = base, inv[base] = buf[];
for(int i = ; i < nbuf; i++){
if(buf[i] == buf[i - ]) continue;
mapi[buf[i]] = ++base, inv[base] = buf[i];
}
nv = nv1, nh = nh1;
sort(v, v + nv, cmpv);
sort(h, h + nh, cmph);
memset(x, , sizeof x);
int pointer = ;
while(pointer < nv && v[pointer].y2 >= h[].y) Push(pointer), ++pointer;
ll tem = query(h[].x1, h[].x2);
ans -= tem;
for(int i = ; i < nh; i++){
if(h[i].y != h[i - ].y){
while(pointer < nv && v[pointer].y2 >= h[i].y) Push(pointer), ++pointer;
}
tem = query(h[i].x1, h[i].x2);
ans -= tem;
}
for(int i = ; i < nv; i++) v[i].y2 = v[i].y1 - ;
pointer = ;
sort(v, v + nv, cmpvy2);
memset(x, , sizeof x);
while(pointer < nv && v[pointer].y2 >= h[].y) Push(pointer), ++pointer;
tem = query(h[].x1, h[].x2);
ans += tem;
for(int i = ; i < nh; i++){
if(h[i].y != h[i - ].y){
while(pointer < nv && v[pointer].y2 >= h[i].y) Push(pointer), ++pointer;
}
tem = query(h[i].x1, h[i].x2);
ans += tem;
}
printf("%I64d\n", ans);
} int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n)){
int x1, y1, x2, y2;
nh = nv = ;
for(int i = ; i < n; i++){
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if(y1 == y2) h[nh++] = H(min(x1, x2), max(x1, x2), y1);
else v[nv++] = V(min(y1, y2), max(y1, y2), x1);
}
solve();
}
return ;
}

Codeforces Round #337 Vika and Segments的更多相关文章

  1. Codeforces Round #535 E2-Array and Segments (Hard version)

    Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...

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

  4. Codeforces Round #337 (Div. 2)

    水 A - Pasha and Stick #include <bits/stdc++.h> using namespace std; typedef long long ll; cons ...

  5. codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)

    题目链接: D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input s ...

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

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

  7. Codeforces Round #337 (Div. 2) B. Vika and Squares 贪心

    B. Vika and Squares 题目连接: http://www.codeforces.com/contest/610/problem/B Description Vika has n jar ...

  8. Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

    B. Vika and Squares   Vika has n jars with paints of distinct colors. All the jars are numbered from ...

  9. Codeforces Round #337 (Div. 2) B. Vika and Squares

    B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. 到底UDP和TCP是什么个概念?

    今天在论坛看到一牛人对tcp和udp的解释和区分,突然间恍然大悟. 以下全为拷贝. 在现实生活中,“要想富,先修路”:同时人总要“居有定所”,于是盖起了N多的房子.但是当你和同事商量好去做客的时候却发 ...

  2. 【转】轻量级分布式 RPC 框架

    第一步:编写服务接口 第二步:编写服务接口的实现类 第三步:配置服务端 第四步:启动服务器并发布服务 第五步:实现服务注册 第六步:实现 RPC 服务器 第七步:配置客户端 第八步:实现服务发现 第九 ...

  3. SQL 数据库 子查询、主外键

    子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在 ...

  4. Codeforce Round #214 Div2

    我是不是快要滚蛋了,这次CF爆0? 居然第一题都过不去了,妈蛋附近有没有神经病医院,我要去看看! 精力憔悴! 第一题,我以为要恰好这么多钱,不能多余,想想这也没必要,不符合逻辑,及自己就是这么傻逼! ...

  5. codeforces-Glass Carving(527C)std::set用法

    C. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. XML节点名称中有小数点处理(deal with dot)导致使用xpath时报错解决方法

    <?xml version="1.0"?> <ModifyFiles> <_Layout.cshtml>123456</_Layout.c ...

  7. .NET: C#: Datetime

    比较简单的类,一般用到它的属性.经常会用到的是DateTime.Now和DateTime.Now.TimeOfDay; using System; using System.Collections.G ...

  8. SharedPreferences 轻型的数据存储方式

    //初始化,(名字,隐私或公开) SharedPreferences openTimes=getSharedPreferences("openTimes",0); //提交保存数据 ...

  9. Uploadify在MVC中使用方法案例(一个视图多次上传单张图片)

    Controller 中代码和 上一节文章(http://www.cnblogs.com/yechangzhong-826217795/p/3785842.html )一样 视图中代码如下: < ...

  10. 夺命雷公狗ThinkPHP项目之----企业网站21之网站前台二级分类显示名称(TP自定义函数展示无限极分类)

    我们实现网站二级分类的显示的时候,先要考虑的是直接取出顶级栏目,控制器代码如下所示: <?php namespace Home\Controller; use Think\Controller; ...