http://codeforces.com/contest/610/problem/D
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).
题意:在坐标系上画n条线段,求经过了多少个点?
题解:我们把右上角的坐标都加1转换为求面积。用扫描线。下面这篇博客可以http://blog.csdn.net/harlow_cheng/article/details/53027415
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define pii pair<int,int>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn=1e5+;
int sum[maxn<<];
vector<int>a;
int cnt[maxn<<],n;
struct node
{
int x1,x2,h,k;
bool operator<(const node b)const
{
return h<b.h;
}
}bian[maxn<<+]; void push_up(int rt,int l,int r)
{
if(cnt[rt])
sum[rt]=a[r+]-a[l];
else
if(l==r)
sum[rt]=;
else
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
cnt[rt]+=c;
push_up(rt,l,r);
return;
}
int m=(l+r)>>;
if(L<=m)update(L,R,c,l,m,rt<<);
if(m<R)update(L,R,c,m+,r,rt<<|);
push_up(rt,l,r);
}
int main()
{
scanf("%d",&n);
int num=;
for(int i=;i<n;i++)
{
int x1,x2,y1,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
x2++;y2++;
a.pb(x1);a.pb(x2);
num++;
bian[num].x1=x1;bian[num].x2=x2;bian[num].h=y1;bian[num].k=;
num++;
bian[num].x1=x1;bian[num].x2=x2;bian[num].h=y2;bian[num].k=-;
}
sort(a.begin(),a.end());
a.erase(unique(a.begin(),a.end()),a.end());
sort(bian+,bian++num);
ll ans=;
for(int i=;i<=num-;i++)
{
int l=lower_bound(a.begin(),a.end(),bian[i].x1)-a.begin();
int r=lower_bound(a.begin(),a.end(),bian[i].x2)-a.begin()-;
update(l,r,bian[i].k,,a.size()-,);
ans+=(ll)sum[]*(bian[i+].h-bian[i].h);
}
printf("%I64d\n",ans);
return ;
}
http://codeforces.com/contest/610/problem/D的更多相关文章
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环
E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- http://codeforces.com/contest/612/problem/D
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- http://codeforces.com/contest/536/problem/B
B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/535/problem/C
C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- http://codeforces.com/contest/838/problem/A
A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- http://codeforces.com/contest/402/problem/E
E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces.com/contest/251/problem/C
C. Number Transformation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
随机推荐
- 图标字体库(用CSS样式生成搜索、购物车等图标)
参考网址:http://fontawesome.dashgame.com/ 基本图标: 您可以将Font Awesome图标使用在几乎任何地方,只需要使用CSS前缀 fa ,再加上图标名称. Font ...
- Centos6.6下安装Python3.5
centos6.6自带的Python2.6,如果想要安装新版本的Python例如Python2.7+或者Python3.5,不能够用yum安装,那么只能从源码编译安装. Step 1: 安装依赖库和编 ...
- Linux系统下C语言如何调用scalapack中的函数
在并行计算中经常需要调用scalapck(并行化的lapack)函数库里面的函数进行编程,这里简单介绍在C语言如何调用scalapck中的矩阵向量乘的函数. 注意:scalapack中的函数是用for ...
- OpenSSL中的大数接口与基于其的自用RSA加密接口设计
本文记录了初次接触OpenSSL中的大数模块,重温了RSA加密流程,使用OpenSSL的接口包装成自用RSA加密接口,并且利用自己的接口演示了Alice与Bob通过RSA加密进行通讯的一个示例. 概览 ...
- Swiper+JS 上拉刷新
JS // 上拉刷新 var page = 2; var isAjax = true;//加载数据前状态 $( ...
- C# IComparable 和 IComparer 区别
理解很重要: 开始对这两个接口的区别一直是很模糊,看到很多书后,终于知道了区别,形成了个人的理解: 关于 IComparable 比喻一个类person实现了 IComparable,那么它就要重写C ...
- 结对编程1 —— 基于GUI和Swing的四则运算题目生成器
合作伙伴 201421123102 王艳秋 201421123106 陈 雄 代码地址 题目描述 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI( ...
- 团队作业4——第一次项目冲刺 tHe LaSt dAy
项目冲刺--终点 敏捷冲刺最后一天,没想到前一天就上榜了,我也很无奈啊. 那今天就老老实实写一篇博客好了. Mission 这次敏捷冲刺呢,我们主要做了前端页面,后台的数据库和添加了基本的增删查改的功 ...
- 第二次项目冲刺(Beta阶段)5.21
1.提供当天站立式会议照片一张 会议内容: ①检查前一天的任务情况,做出自我反省. ②制定新一轮的任务计划. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 王婧 #53实现多对多查重 # ...
- 201521123014 《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 1.2 可选:使用常规方法总结其他上课内容. GUI与Sw ...