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 ...
随机推荐
- spring-session 共享
Spring session 共享 一.引入依赖 <dependency> <groupId>redis.clients</groupId> <artifac ...
- django框架简介
-------------------MVC与MVT框架-------------------1.MVC MVC框架的核心思想是:解耦.降低各功能模块之间的耦合性,方便将来变化时,更容易重构代码,最大 ...
- Java 随笔记录
1. java对象转json Message msg = generateMessage();ObjectMapper mapper = new ObjectMapper();String json ...
- SSM框架整合,以CRM为例子
Mybatis.SpringMVC练习 CRM系统 回顾 Springmvc 高级参数绑定 数组 List <input type name=ids /& ...
- java 得到以后的日期
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt222 import java.text.ParseException; im ...
- C++IO类&文件输入输出
C++IO类&文件输入输出 istream(输入流)类型,提供输入操作. ostream(输出流)类型,提供输出操作. cin,一个istream对象,从标准输入读取数据. cout,一个os ...
- 这是一款借助chrome 插件的微信机器人
1.chrome kit微信机器人简介 借助chrome 插件 js注入来实现消息的发送 chrome devtool api的调用来监听https请求 打开微信登录界面,在扫码登录前必须先打开too ...
- 201521123115 《Java程序设计》第3周学习总结
Java 第三周总结 1.本周学习总结 {{uploading-image-747934.png(uploading...)}} 2.书面作业 1.代码阅读 public class Test1 { ...
- 201521123067 《Java程序设计》第2周学习总结
1. 本周学习总结 ●本周主要学习了java的基本语法,从中我知道了java中的变量类型以及关于类型转换的问题,而且学会了通过import引用包. ●通过本周的学习,我学会了使用数组,包括对数组的创建 ...
- 201521123038 《Java程序设计》 第十一周学习总结
201521123038 <Java程序设计> 第十一周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多 ...