cf 1000c Covered Points Count

【CF1000C】Covered Points Count(离散化+差分) 点此看题面 大致题意:给出nnn条线段,分别求有多少点被覆盖111次、222次…nnn次。 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了半天只想出一个无比麻烦的),这里介绍一个稍微有些复杂的。 考虑离散化每一个出现过的点以及这些点后面的点(之所以要离散化这些后面的点,是为了方便后面的差分)。 假如我们用p[i]p[i]p[i]来表示原来为iii的数离散化后的值... 阅读详情

一 原题

C. Covered Points Count
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given nn segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

Your task is the following: for every k∈[1..n]k∈[1..n], calculate the number of points with integer coordinates such that the number of segments that cover these points equals kk. A segment with endpoints lili and riri covers point xxif and only if li≤x≤rili≤x≤ri.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of segments.

The next nn lines contain segments. The ii-th line contains a pair of integers li,rili,ri (0≤li≤ri≤10180≤li≤ri≤1018) — the endpoints of the ii-th segment.

Output

Print nn space separated integers cnt1,cnt2,…,cntncnt1,cnt2,…,cntn, where cnticnti is equal to the number of points such that the number of segments that cover these points equals to ii.

Examples
input
Copy
3
0 3
1 3
3 8
output
Copy
6 2 1 
input
Copy
3
1 3
2 4
5 7
output
Copy
5 2 0 
Note

The picture describing the first example:

Points with coordinates [0,4,5,6,7,8][0,4,5,6,7,8] are covered by one segment, points [1,2][1,2] are covered by two segments and point [3][3] is covered by three segments.

The picture describing the second example:

Points [1,4,5,6,7][1,4,5,6,7] are covered by one segment, points [2,3][2,3] are covered by two segments and there are no points covered by three segments.


二 分析

给你n条x轴上的线段,问被k条线段覆盖的点的个数,k从1遍历到n。n<=2e5,线段坐标long long范围。

把2n个端点排序,相同的情况下起点在前,维护一下当前区间上有多少条线段(一个整型变量cnt,遇到起点+1,遇到终点-1),比较麻烦的是端点本身有多少条线段覆盖:如果遇到连续的一组重合的点,因为排序时起点在前,遍历完其中所有起点时cnt的值就是覆盖这个端点的线段数。

关于这种排序后扫一遍的题,cf上有篇看起来不错的博客,有空学习一下

PS:这题第一次提交的时候RE了,一开始以为是数组越界,而ans数组显然不会超。后来发现居然是sort的比较函数写的有毛病。关键就在于sort重载的是小于比较,两个相等的数比较应当返回false,附上错误的cmp:

bool cmp(const plb &p1, const plb &p2) {
	if (p1.fi == p2.fi) return p1.se; // first相同,second为true的排在前面
	return p1.fi < p2.fi; // first小的排在前面
}


三 代码

/*
AUTHOR: maxkibble
LANG: c++
PROB: cf 1000c
*/

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define plb pair<long long, bool>

const int maxn = 2e5 + 5;

int n, cnt;
long long l, r, ans[maxn];
vector<plb> v;

bool cmp(const plb &p1, const plb &p2) {
	if (p1.fi == p2.fi) return (p1.se == p2.se)? false: p1.se;
	return p1.fi < p2.fi;
}

int main() {
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> l >> r;
		v.push_back(make_pair(l, true));
		v.push_back(make_pair(r, false));
	}
	sort(v.begin(), v.end(), cmp);
	bool ed = false;
	cnt = 1;
	for (int i = 1; i < v.size(); i++) {
		if (v[i].fi != v[i - 1].fi) {
			ans[cnt] += v[i].fi - (v[i - 1].fi) - 1;
			if (!ed) ans[cnt]++;
			else ed = false;
		}
		if (!ed && !v[i].se) {
			ans[cnt]++;
			ed = true;
		}
		cnt += v[i].se? 1: -1;
	}
	for (int i = 1; i <= n; i++) cout << ans[i] << " \n"[i == n];
	return 0;
}

从零到一:手把手教你用海康VisionMaster完成第一个字符识别项目(附完整流程与避坑点) 本文详细介绍了如何使用海康VisionMaster完成首个字符识别项目,涵盖从环境搭建、图像预处理到系统集成的完整流程。特别分享了Blob分析配置、字符识别优化等实战技巧,帮助开发者避开常见陷阱,快速掌握VisionMaster的基本操作,提升工业自动化场景下的字符识别效率。 阅读详情

相关推荐

SOI技术解析:绝缘体上硅的结构优势与应用场景

本文深入解析了SOI(绝缘体上硅)技术的核心结构、性能优势与应用场景。SOI通过在硅衬底与器件层之间引入绝缘的埋氧层,实现了优异的电隔离,从而显著降低芯片功耗、提升工作频率并增强抗干扰能力。该技术已广泛应用于5G射频前端、高性能计算、汽车电子及MEMS传感器等关键领域,是推动半导体性能持续提升的重要技术路径。

jj890的博客 402

E. Covered Points (线段上的整点数)

题目链接:https://codeforces.com/contest/1036/problem/E 思路:学会了一个在线段上的整数点等于 GCD(x1 - x2, y1 - y2) + 1,然后去重线段相交的重复整点。 AC代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef lo...

weixin_30698297的博客 252

Camera metadata

Camera metadata

weixin_42136255的博客 2528

C. Covered Points Count (离散化 + 前缀和)

题目链接:http://codeforces.com/contest/1000/problem/C 题意:给你n组l 和 r,分别表示n个闭区间,分别求被1~n个区间覆盖的点数 算法:前缀和 思路:对于每组l和r,用mp[l]++ 表示从 l 往右的区间覆盖数+1, 用mp[r+1]–表示从 r+1 往右的区间覆盖数-1。 #include &amp;lt;bits/stdc++.h&amp;gt;...

凝望的博客 475

安卓逆向实战:用Batch Apktool 3.8.0快速汉化APK的完整流程

本文详细解析了如何使用Batch Apktool 3.8.0进行安卓APK汉化的完整流程,包括环境配置、反编译、资源解析、深度汉化、重构建与签名优化等关键步骤。通过这款集成化逆向工具,开发者可以高效完成APK汉化,提升工作效率。

s1t2u3的博客 1098

【网络】PFC背景和原理 、文档(DCB=PFC + ETS,DCBX=DCB扩展)

目录 零、背景 一、PFC 1.概述 2.报文格式 3.工作机制 二、ETS 产生原因 基本原理 优先级组的调度 优先级的调度 三、DCBX 产生原因 基本原理 DCBX TLV介绍 五、PFC 拥塞研究(文档收集) 六、DC-QCN算法 (网络工程师必会) 7.1 α更新 7.2 降速 7.3 提速 8.总结 ...

小鱼菜鸟的博客 2165

T - Covered Points Count (差分)

You are given n segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide. Your task is the following: for

weixin_52115456的博客 224

HINT: High-quality INpainting Transformer with Mask-Aware Encoding and Enhanced Attention 训练自己的数据集

HINT(高质量图像修复变换器)通过创新的掩码感知像素洗牌下采样模块(MPD)和空间激活通道注意力层(SCAL),显著提高了图像修复的质量,尤其在大面积缺失区域的情况下。实验表明,HINT在CelebA、CelebA-HQ、Places2和敦煌数据集上超越了现有的最先进模型,提升了图像的细节和结构一致性。

LJ1147517021的博客 312

C. Covered Points Count (线段问题---差分、排序)

题目 两种解法: 1.用差分思想,l—r所有点加1用map[l]+1,mdp[r+1]-1表示,前缀和即是当前点所对应覆盖线段数。 Code: #include<iostream> #include<stack> #include<map> #include<string> #include<algorithm> using namespace std; typedef long long ll; const int Max = 1e6 + 5;

Rikkaの博客 377

基于逆向工程的中国裁判文书网爬虫技术深度解析

本文详细介绍了中国裁判文书网爬虫的开发过程,涵盖了从基础爬取到高级功能的完整解决方案。逆向工程分析:理解网站的反爬机制和加密逻辑浏览器自动化:使用Playwright模拟真实用户行为参数加密:通过JavaScript执行环境生成加密参数智能调度:控制请求频率,避免被检测错误处理:完善的异常处理和重试机制。

2201_76125261的博客 2145

UG NX二次开发(Python)-API函数介绍与应用实例(三)-UFLayer类操作

采用Python语言进行UG NX二次开发的帮助材料很少,采用录制的方法是一种比较容易实现的方式,但是使用UFun函数更容易上手,我们以后将逐渐的将Python-UFun的功能写进博客中,以便于有需要的人采纳和研究。本博客是为了入门需要,采用简单的创建长方体的功能来讲一下怎么使用Python语言开发。我们今天以移动长方体到某一个图层为例,讲一下UFLayer类中部分函数的使用。另外,我们长期提供UG NX二次开发的功能开发和咨询服务,有疑问可以私信博主或者在评论区讨论。

GimiGimmy 708

C. Covered Points Count

time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given nn segments on a coordinate line; each endpoint of every segment has integer coordin...

xiaoxiede_wo的博客 492

第77步 时间序列建模实战:多因素预测 vol-2(以决策树回归为例)

第77步 时间序列建模实战:多因素预测 vol-2(以决策树回归为例)

Jet4505的博客 1007

CF1000C Covered Points Count

刚刚看到这题时感觉特别难…经过大约10分钟的思考… 呀 水题QAQ 不多说了上代码呀QAQ #include&lt;iostream&gt;//标准模板呀QAQ #include&lt;cstdio&gt; #include&lt;cmath&gt; #include&lt;string.h&gt; #include&lt;algorithm&gt; #define For(i,f,l) int ...

sxy__orz的博客 426

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

【CF1000C】 Covered Points Count

题目 题意翻译 题目大意: 给你n个区间,求被这些区间覆盖层数为k(k&lt;=n)k(k&lt;=n)的点的个数 输入格式: 第一行一个整数,nn,n&lt;=2*10^5n&lt;=2∗10 5 以下nn行,每行有两个整数,即这个区间的左右端点l,r(0&lt;=l&lt;=r&lt;=10^{18})l,r(0&lt;=l&lt;=r&lt;=10 18 ) 输出格式: nn个整数,即每个被...

Brute♂force 487

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

cf1000 C. Covered Points Count

#include<bits/stdc++.h> using namespace std; typedef long long ll; map<ll ,int > mp; ll cnt[200010]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++)...

weixin_30629977的博客 105

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

CF1000C Covered Points Count (差分)

You are given nn segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each ...

Chap13629754032的博客 239

CF1000C Covered Points Count(拆分思想,分成2种类型)

You are givennnsegments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide. Your task is the following: f..

胜神明半子 277

CF--1000C. Covered Points Count

http://codeforces.com/contest/1000/problem/C 题意:n条线,数轴上,求被1,2,3,,,,n条线覆盖的点的个数 思路:类似于扫描线,不过是一维的,对于n个点的2n个端点储存,并由1,-1记录是起始点,还是线的终点,表示后面加一条线段,还是减少一条直线。再就是要注意当端点是同一个点时,排序起点优先与终点,因为,各条线都覆盖了这个点。最后一定注意long...

AAAAAAAC的博客 362

【题解】CF1000C Covered Points Count

我在做这道题时,乍一看,诶,这不是一个前缀和扫一遍吗?于是我就非常愉快的打了一遍。。。然后就 GG 了。 RE 完之后,我再去看了一遍数据范围 – WOC,为什么 l,r 是 1e18?好吧,那这道题还没有想象中的那么水。。。 不过很快大家就能注意到:n 只有 20 万。这说明了什么呢?说明前缀和改变的节点最多只有 40 万个。那么我们把这些节点存起来再一排序,然后扫一遍不就得了? 那么我们...

SSH_oj的博客 433

(前缀和)【CF - 1000C】C. Covered Points Count

题目链接:http://codeforces.com/contest/1000/problem/C题意:n条线段。依次输出被0..n条线段覆盖的点的个数。将点记录并排序,从0点开始模拟,cnt记录当前有几条线段覆盖,碰到起点,说明又多了一条线段覆盖之后的点,所以cnt+1.碰到终点,说明有一条线段结束了,cnt-1./* * @Author: Samson * @Date: 2018-07-0...

Samson的博客 656

Covered Points Count CF1000C 思维 前缀和 贪心

Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output You are givennnsegments on a coordinate lin...

weixin_33872660的博客 167
上一篇: Hulu的一道面试题
下一篇: cf 997c Sky Full of Stars
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值