Vika and Segments - CF610D
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).
简单题意
给你很多条与坐标轴平行的线段,求线段覆盖的点数是多少
胡说题解
首先先分成两类,平行x轴的和平行y轴的线段,然后排序,再合并线段,使得相同类型的线段没有交集,然后计算ans(这个时候还没完,因为横纵相交的点没有去掉
然后我们要计算横纵相交的点数
然后这是比较经典的双关键字的限制的求和了,可以用cdq分治,或者排序按序加入然后维护区间和之类的
脑残错误
一开始RE几发,最后查出原因是因为sort的cmp没打好,不能判断出来相等(a<b是true,b<a也是true)然后就鬼畜了,所以打cmp的时候正确的姿势是每个关键字都要比较(AC代码里面并没有完全改过来,懒。。。
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std; struct point{
bool q;
int h,d,l,r;
}; const int maxn=; int n,s[maxn*],x[maxn*],tot;
point a[maxn*];
long long ans; bool compare(point a,point b){
if(a.q^b.q)return a.q;
if(a.q){
if(a.l!=b.l)return a.l<b.l;
if(a.d!=b.d)return a.d<b.d;
return a.h<b.h;
}
else{
if(a.d!=b.d)return a.d<b.d;
if(a.l!=b.l)return a.l<b.l;
return a.r<b.r;
}
} bool cmp2(point a,point b){
if(a.h!=b.h)return a.h>b.h;
if(a.q^b.q)return a.q>b.q;
return a.l<b.l;
} int find(int i){
int l=,r=tot,mid;
while(l!=r){
mid=(l+r)/;
if(x[mid]>=i)r=mid;
else l=mid+;
}
return l;
} int lowbit(int x){
return x&-x;
} int sum(int x){
int ss=;
while(x>){
ss+=s[x];
x-=lowbit(x);
}
return ss;
} void add(int x,int y){
while(x<=tot){
s[x]+=y;
x+=lowbit(x);
}
} int main(){
scanf("%d",&n);
int i;
for(i=;i<=n;i++){
scanf("%d%d%d%d",&a[i].l,&a[i].h,&a[i].r,&a[i].d);
if(a[i].r<a[i].l)swap(a[i].l,a[i].r);
if(a[i].h<a[i].d)swap(a[i].h,a[i].d);
if(a[i].l==a[i].r)a[i].q=true;
}
sort(a+,a++n,compare);
for(i=;i<n;i++)
if(a[i].q==a[i+].q){
if(a[i].q){
if(a[i].l==a[i+].l)
if(a[i+].d<=a[i].h+){
a[i+].d=a[i].d;
a[i+].h=fmax(a[i+].h,a[i].h);
a[i].l=;a[i].r=-;
}
}
else{
if(a[i].h==a[i+].h)
if(a[i+].l<=a[i].r+){
a[i+].l=a[i].l;
a[i+].r=fmax(a[i+].r,a[i].r);
a[i].l=;a[i].r=-;
}
}
}
for(i=;i<=n;i++)ans+=(a[i].r-a[i].l+)*(a[i].h-a[i].d+);
for(i=;i<=n;i++)
if(a[i].l<=a[i].r)x[++tot]=a[i].l,x[++tot]=a[i].r;
sort(x+,x++tot);
int tmp=n;
for(i=;i<=tmp;i++)if(a[i].q && a[i].l<=a[i].r){
++n;
a[n].q=true;
a[n].h=a[i].h;
a[n].d=a[i].l;
a[n].l=;a[n].r=;
++n;
a[n].q=true;
a[n].h=a[i].d-;
a[n].d=a[i].l;
a[n].l=-;
a[i].l=;a[i].r=-;
}
sort(a+,a++n,cmp2);
for(i=;i<=n;i++){
if(a[i].l<=a[i].r){
if(a[i].q)add(find(a[i].d),a[i].l);
else ans-=sum(find(a[i].r))-sum(find(a[i].l)-);
}
}
printf("%I64d\n",ans);
return ;
}
AC代码
Vika and Segments - CF610D的更多相关文章
- 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 ...
- Codeforces Round #337 Vika and Segments
D. Vika and Segments time limit per test: 2 seconds memory limit per test: 256 megabytes input ...
- 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 ...
- codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)
题目链接: D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 【20.51%】【codeforces 610D】Vika and Segments
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- CodeForces 610D Vika and Segments
模板题,矩形面积并 #include <iostream> #include <cstring> #include <cstdio> #include <al ...
- 610D - Vika and Segments(线段树+扫描线+离散化)
扫描线:http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 看图,图中的数字是横坐标离散后对应的下标,计算时左端点不 ...
- Codeforces 610D Vika and Segments 线段树+离散化+扫描线
可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...
随机推荐
- 2 进程multiprocessing [mʌltɪ'prəʊsesɪŋ] time模块
1.multiprocessing模块 multiprocessing模块就是跨平台版本的多进程模块. multiprocessing模块提供了一个Process类来代表一个进程对象, 2.Proce ...
- 11、Java并发编程:并发容器之CopyOnWriteArrayList
Java并发编程:并发容器之CopyOnWriteArrayList(转载) 原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW ...
- 使用QUIC
QUIC是Google新开发的一个基于UDP的协议,它提供了像TCP一样的传输可靠性保证,可以实现数据传输的0-RTT延迟,灵活的设计使我们可以对它的拥塞控制及流量控制做更多的定制,它还提供了传输的安 ...
- android 学习四 ContentProvider
1.系统自带的许多数据(联系人,本地信息等)保存在sqllite数据库,然后封装成许多ContentProvider来供其他程序访问. 2.对sqllite数据库的操作,可以在命令行通过adb工具登录 ...
- yield学习
如果要控制内存占用,最好不要用list来保存中间结果,而是通过iterable对象(range, xrange, generator等)来迭代. yield 使函数变为generator,返回对象 ...
- Linux系统中ElasticSearch搜索引擎安装配置Head插件
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- 安装SQLSEVER与MySQL
昨天装了一整填的SQLSEVER,今天是把昨天遗留的问题给重新整合一下,上午安装MySQL的时候,是在网上找的帖子通过压缩包安装的,捣鼓了一上午,下午花不到一个小时, 去安装好了,我觉得通过压缩包安装 ...
- <cctype>
头文件名称: <cctype> (ctype.h) 头文件描述: 这是一个拥有许多字符串处理函数声明的头文件,这些函数可以用来对单独字符串进行分类和转换: 其中的函数描述: 这些函数传入 ...
- nginx 重启报错
错误信息: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or d ...
- NodeJs学习笔记01-你好Node
如果你对NodeJs略知一二,不禁会感叹,使用JS的语法和代码习惯就能开发一个网站的后台,实现复杂的数据交互,牛! 对于学习java和php就夹生的小码农来说,简直就是靡靡之音呐~~~ 今晚带着忐忑的 ...