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 ...
随机推荐
- INotifyPropertyChanged(监听数据),当数据改变时调用
public class BaseViewModel : INotifyPropertyChanged { public event PropertyChangedEventHan ...
- [WPF]如何调试Data Binding
前言 在WPF开发中,将ViewModel中对象绑定到UI上时,会出现明明已经将数据对象Binding到UI,但是UI上就是不显示等等的问题.这篇博客将介绍WPF Data Binding相关的内容, ...
- spring mvc:属性无法自动注入
在使用spring mvc 3开发一个项目模块时,遇到这样一个奇怪的问题: 前端页面发送的请求中,所有参数都无法自动注入到指定的@ModelAttribute对象中,经过检查,参数名称与接受对象的属性 ...
- leetcode 001 Two Sun
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- JavaScript在应用中的技巧(二)
==,===运算符和强制转换 先看个表达式: "1.0e0" == { valueOf: function() { return true; } }; 是的,这个结果可能有点出乎意 ...
- 四,ESP8266 TCP服务器
我要赶时间赶紧写完所有的内容....朋友的东西答应的还没做完呢!!!!!!!没想到又来了新的事情,,....... 配置模块作为TCP服务器然后呢咱们连接服务器发指令控制继电器吸合和断开 控制的指令呢 ...
- Java 多线程(三) 线程的生命周期及优先级
线程的生命周期 线程的生命周期:一个线程从创建到消亡的过程. 如下图,表示线程生命周期中的各个状态: 线程的生命周期可以分为四个状态: 1.创建状态: 当用new操作符创建一个新的线程对象时,该线程处 ...
- 团队作业4——第一次项目冲刺(Alpha版本)4.27
一.当天站立式会议照片 本次会议主要内容:组长给大家分配新一步任务,分别汇报目前所完成的内容,完善昨日的解压接口代码,并做测试修正. 二.每个人的工 三.燃尽图 横坐标:工作日,以天为单位,一共七天, ...
- 结对编程1---四则运算---GUI
201421123003 黄建英 201421123004 黄美海 题目描述: 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是Window ...
- 团队作业4——第一次项目冲刺(Alpha版本)4.28
团队作业4--第一次项目冲刺(Alpha版本) Day seven: 会议照片 每日站立会议: 项目进展 今天是项目的Alpha敏捷冲刺的第七天,先大概整理下昨天已完成的任务以及今天计划完成的任务.今 ...