【CF1000C】Covered Points Count(离散化+差分)
大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次、\(2\)次...\(n\)次。
正常的算法
好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了半天只想出一个无比麻烦的),这里介绍一个稍微有些复杂的。
不正常的算法(我的算法)
考虑离散化每一个出现过的点以及这些点后面的点(之所以要离散化这些后面的点,是为了方便后面的差分)。
假如我们用\(p[i]\)来表示原来为\(i\)的数离散化后的值,并用\(q[i]\)表示离散化后值为\(i\)的数原来的值,则:
对于一条线段,假设它的左端点为\(l\),右端点为\(r\),那么我们就将\(f[p[l]]\)加1,并将\(f[p[r+1]]\)减1(经典的差分套路),最后求出其前缀和,就可以求出每一块被覆盖的次数,从而将其还原计算出答案。
代码
#include<bits/stdc++.h>
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define LL long long
#define N 200000
using namespace std;
int n;
LL x[N+5],y[N+5],z[4*N+5],f[4*N+5],ans[N+5];
map<LL,LL> p,q;
LL read()
{
LL x=0,f=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') f=-1,ch=getchar();
while(ch>='0'&&ch<='9') (x*=10)+=ch-'0',ch=getchar();
return x*=f;
}
void write(LL x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
int main()
{
n=read();
for(int i=1;i<=n;i++) z[4*i-3]=x[i]=read(),z[4*i-2]=x[i]+1,z[4*i-1]=y[i]=read(),z[4*i]=y[i]+1;
int w=4*n,k=0;
sort(z+1,z+w+1);
for(int i=1;i<=w;i++) if(z[i]!=z[i-1]||i==1) p[z[i]]=i-k,q[i-k]=z[i];else k++;//离散化
for(int i=1;i<=n;i++) f[p[x[i]]]++,f[p[y[i]]+1]--;//差分
for(int i=1;i<w;i++) ans[f[i]+=f[i-1]]+=q[i+1]-q[i];//前缀和累加,然后还原计算答案
for(int i=1;i<=n;i++) write(ans[i]),putchar(' ');
return 0;
}
【CF1000C】Covered Points Count(离散化+差分)的更多相关文章
- cf1000C Covered Points Count (差分+map)
考虑如果数字范围没有这么大的话,直接做一个差分数组就可以了 但现在变大了 所以要用一个map来维护 #include<bits/stdc++.h> #define pa pair<i ...
- codeforces 1000C - Covered Points Count 【差分】
题目:戳这里 题意:给出n个线段,问被1~n个线段覆盖的点分别有多少. 解题思路: 这题很容易想到排序后维护每个端点被覆盖的线段数,关键是端点值不好处理.比较好的做法是用差分的思想,把闭区间的线段改为 ...
- C - Covered Points Count CodeForces - 1000C (差分,离散化,统计)
C - Covered Points Count CodeForces - 1000C You are given nn segments on a coordinate line; each end ...
- Covered Points Count CF1000C 思维 前缀和 贪心
Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- Educational Codeforces Round 46 C - Covered Points Count
C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...
- Covered Points Count(思维题)
C. Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 1000C Covered Points Count(区间线段覆盖问题,差分)
https://codeforces.com/problemset/problem/1000/C 题意: 有n个线段,覆盖[li,ri],最后依次输出覆盖层数为1~n的点的个数. 思路: 区间线段覆盖 ...
- Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
Bryce1010模板 http://codeforces.com/problemset/problem/1000/C 题意:问你从[l,r]区间的被多少条线覆盖,列出所有答案. 思路:类似括号匹配的 ...
- EDU 50 E. Covered Points 利用克莱姆法则计算线段交点
E. Covered Points 利用克莱姆法则计算线段交点.n^2枚举,最后把个数开方,从ans中减去. ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算. #include ...
随机推荐
- KONG 安装 (在 CentOS 7 中)
1. 下载安装包: https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong ...
- 51nod1489(dfs)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1489 题意:中文题诶- 思路:dfs 首先我们要通过攻击第1 ...
- Jmeter如何提取响应头部的JSESSIONID【转】
一.测试前准备 1.测试地址: 登录:http://XXXX:8080/futureloan/mvc/api/member/login 充值:http://XXXX:8080/futureloan/m ...
- iOS常用设计模式——工厂方法(简单工厂模式,工厂方法模式, 抽象工厂模式)
1. 简单工厂模式 如何理解简单工厂,工厂方法, 抽象工厂三种设计模式? 简单工厂方法包含:父类拥有共同基础接口,具体子类实现子类特殊功能,工厂类根据参数区分创建不同子类实例.该场景对应的UML图如下 ...
- linux命令ln
创建软连接 ln -s 源文件 目标文件(指向源文件) ln -s /home/test/ /wang
- 74th LeetCode Weekly Contest Valid Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数
Given an array of integers, every element appears three times except for one, which appears exactly ...
- CSU 1453: 平衡序列 学会线段树后必做
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1453. 题目:给定一个大小为100000的数组,里面的数字最大也是100000.现在叫你求出一段子 ...
- ElasticSearch 全文检索— ElasticSearch 核心概念
ElasticSearch核心概念-Cluster 1)代表一个集群,集群中有多个节点,其中有一个为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的.es的一个概念就是去中心化,字 ...
- SpringMVC核心技术---转发和重定向
@Controller public class Mycontroller { //转发 @RequestMapping("/adduser") public String add ...