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++ ...
随机推荐
- 优步UBER司机全国各地奖励政策汇总 (4月4日-4月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(4月2、3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- JDK11安装后,环境变量的坑
安装了最新的JDK11,安装完后设置环境变量,打开CMD,没生效 检查了3遍,都没发现问题,在PATH中将JAVA设置移到第一也不行 最后偶然发现,在点击如图右下的‘编辑文本’,用文本方式编辑时,发现 ...
- python编程os、os.path 模块中关于文件、目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名( ...
- C++11 type_traits 之is_pointer,is_member_function_pointer源码分析
源码如下: template<typename> struct __is_pointer_helper : public false_type { }; template<typen ...
- LeeCode第一次刷题(两数相加)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
- github 使用“git commit -m"命令时候出现的一个小问题
git commit -m 使用问题 今天提交文件到github,步骤是: git add abc.py (abc.py是我当前随意写的一个文件名) git commit -m 'add codes ...
- 【RL系列】Multi-Armed Bandit笔记——UCB策略与Gradient策略
本篇主要是为了记录UCB策略与Gradient策略在解决Multi-Armed Bandit问题时的实现方法,涉及理论部分较少,所以请先阅读Reinforcement Learning: An Int ...
- Kali渗透测试-SNMP
1.snmpwalk -v指定snmpwalk版本 -c指定密码 2.snmp-check 获取系统信息,主机名,操作系统及架构 获取用户账户信息 获取网络信息 获取网络接口信息 IP信息 路由信息 ...
- 在 CentOS 下手工安装 Docker v1.1x
Docker在 centos 6.x 下面默认最新的版本是1.7, 然而这个并不符合我的实际需求, 尤其我需要 docker-compose 来作为编配工具部署swarm, 所以我只有手工安装了. 首 ...