题解-CF1401E Divide Square
题面
给一个正方形平面边长为 \(10^6\),给 \(n\) 条横线段和 \(m\) 条竖线段,每条线段都与正方形边缘相交且一条直线上不会有两条线段,求被线段划分后有几个块。
数据范围:\(0\le n,m\le 10^5\),\(0<x,y<10^6\),\(0\le (lx<rx),(ly<ry)\le 10^6\)。
蒟蒻语
前天打小号 \(30\) 分钟切了 \(\tt ABCD\) 以为能 \(\tt AK\),结果 \(\tt E\) 少看了条件开始硬钢幸得爆零。
蒟蒻解
结论:\(ans=(\)内部交点数 \(p)+(10^6\) 长度线段数 \(s)+1\)。
这个结论应该比较好找,可以多画几个图找规律。
蒟蒻本来想了一个很炫酷的证明,但被证伪了,看个逊一点的吧……
证明:
对于 \(s=0\) 的情况,所有线段都贴且仅贴一边又互相不重合,所以每多一个交点多一个块易证。
对于 \(s>0\) 的情况,如果一条长度为 \(10^6\) 的线段不穿过线段,那么固然把正方形分成两块多一块。否则从一条被穿过的线段把它拆成两条线段考虑,答案也会 \(+1\)。
综上,\(ans=p+s+1\)。
至于实现,可以用树状数组扫描线,代码中给出一种新奇的树状数组写法。
代码
#include <bits/stdc++.h>
using namespace std;
//Start
typedef long long ll;
typedef double db;
#define mp(a,b) make_pair((a),(b))
#define x first
#define y second
#define be(a) (a).begin()
#define en(a) (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define R(i,a,b) for(int i=(a),I=(b);i<I;i++)
#define L(i,a,b) for(int i=(a),I=(b);i>I;i--)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
//Data
const int N=1e5,M=1e6;
int n,m; ll ans=1;
vector<int> d;
struct segment{
int op,y,l,r;
segment(){}
segment(int _op,int _y,int _l,int _r){
op=_op,y=_y,l=_l,r=_r;
}
};
vector<segment> a;
//FenwickTree
vector<int> c;
void add(int i,int v){for(;i<sz(c);i|=i+1) c[i]+=v;}
int sum(int i){int v=0;for(;~i;(i&=i+1)--) v+=c[i];return v;}
//Main
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>m;
R(i,0,n){
int y,l,r; cin>>y>>l>>r;
if(r-l==M) ++ans;
d.pb(l),d.pb(r),a.pb(segment(0,y,l,r));
}
R(i,0,m){
int x,l,r; cin>>x>>l>>r;
if(r-l==M) ++ans;
d.pb(x),a.pb(segment(1,l,x,1)),a.pb(segment(1,r,x,-1));
}
sort(be(a),en(a),[&](segment p,segment q){
if(p.y!=q.y) return p.y<q.y;
return p.op*p.r>q.op*q.r;
});
sort(be(d),en(d)),d.erase(unique(be(d),en(d)),en(d));
c.assign(sz(d),0);
// c.assign(M+1,0);
for(auto u:a){
if(u.op){
int i=lower_bound(be(d),en(d),u.l)-be(d);
add(i,u.r);
} else {
int l=lower_bound(be(d),en(d),u.l)-be(d);
int r=lower_bound(be(d),en(d),u.r)-be(d);
ans+=sum(r)-sum(l-1);
}
}
cout<<ans<<'\n';
return 0;
}
祝大家学习愉快!
题解-CF1401E Divide Square的更多相关文章
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- IEEE 754标准
IEEE 754-1985 was an industry standard for representing floating-point numbers in computers, officia ...
- paperOne基于java web的简易四则运算出题网站
项目成员:张金生 张政 需求概要 1.运算数均为正整数 2.包含的运算符有+,-,*,/ 3.除法运算结果为整除运算 4.批量生成题目并判题 核心功能分析 1.题目生成——java后端 题目生 ...
- Codeforces Round #665 (Div. 2)
Codeforces Round #665 (Div. 2) A. Distance and Axis 如果\(B\)在\(O\)左边,那么只能是定值\(OA\) 如果\(B\)在\(OA\)中间 ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
- 【题解】The Great Divide [Uva10256]
[题解]The Great Divide [Uva10256] 传送门:\(\text{The Great Divide [Uva10256]}\) [题目描述] 输入多组数据,每组数据给定 \(n\ ...
- LeetCode题解:(221) Maximal Square
题目说明 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...
随机推荐
- Cache一致性和内存模型
-------------------------------
- CSP-S 2020 Travels
CSP-S 2020 Travels DAY 0 I hit the board in the morning before departure The rest of the time is dec ...
- 从头学起Verilog(一):组合逻辑基础与回顾
引言 该部分主要回顾了本科时数字电路中组合逻辑电路部分,内容相对简单和基础. 内容主要包括:布尔代数相关知识,卡诺图,最大项与最小项,竞争和冒险以及一些常见模块 数字电路中的逻辑 组合逻辑:输出可以表 ...
- Spring接口
FactoryBean接口 Spring中有两种类型的Bean:一种是普通的JavaBean:另一种就是工厂Bean(FactoryBean),这两种Bean都受Spring的IoC容器管理. Fac ...
- 【SpringCloud】consul注册中心注册的服务为内网(局域网)IP
一.前因 最近在做公司的一个微服务项目,技术架构为spring cloud + consul + SSM. 当我写完一个功能要在本地测试时,发现服务运行成功,但是前后端联调报500错误. 当时的第一个 ...
- 面试官:小伙子,你给我讲一下java类加载机制和内存模型吧
类加载机制 虚拟机把描述类的数据从 Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型,这就是虚拟机的类加载机制. 类的生命周期 加载(Loadi ...
- java8的lambda表达式
关于java8的lambda表达式 lambda表达式一般用于接口,因为lambda表达式是函数式编程. 1.有且仅有一个抽象方法被称为函数式接口,函数式接口可以显示的被@FunctionalInte ...
- php bypass disable_function 命令执行 方法汇总简述
1.使用未被禁用的其他函数 exec,shell_exec,system,popen,proc_open,passthru (python_eval?perl_system ? weevely3 wi ...
- CENTOS 7平滑升级PHP到最新版7.3
安装Remi和EPEL数据源(仓库) rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm r ...
- django绕过admin登录设置
在admin.py文件添加以下函数本文是转载:#绕过admin登录def allow_anonymous_user(): from django.contrib.auth.models import ...