codeforces 1000C - Covered Points Count 【差分】
题目:戳这里
题意:给出n个线段,问被1~n个线段覆盖的点分别有多少。
解题思路:
这题很容易想到排序后维护每个端点被覆盖的线段数,关键是端点值不好处理。比较好的做法是用差分的思想,把闭区间的线段改为前闭后开,同时在求总点数的时候,也按前闭后开的区间来求,这样就巧妙避开了两个端点之间的讨论,只用维护好一个端点就行了。
代码比文字更容易理解:
1 #include <bits/stdc++.h>
2 #define lowbit(x) x&-x;
3 typedef long long ll;
4 const int maxn = 4e5+10;
5 const int inf = 0x3f3f3f3f;
6 const ll mod = 998244353;
7 using namespace std;
8 ll n;
9 struct nod {
10 ll x;
11 int f;
12 }co[maxn];
13 ll cnt[maxn];
14 bool cmp(nod a, nod b) {
15 if(a.x == b.x) return a.f > b.f;
16 return a.x < b.x;
17 }
18 int main(){
19 ll l,r;
20 scanf("%lld", &n);
21 for(ll i = 0ll; i < n; ++i) {
22 l=2ll*i+1ll; r=2ll*i+2ll;
23 scanf("%lld %lld", &co[l].x, &co[r].x);
24 co[r].x++;//转换为前闭后开
25 co[l].f = 1;
26 co[r].f = -1;
27 }
28 int now = 1;
29 sort(co + 1, co + 1 + 2*n, cmp);
30 for(ll i = 2ll; i <= 2ll * n; ++i) {
31 cnt[now] += co[i].x - co[i-1].x;
32 now += co[i].f;
33 }
34 for(ll i = 1; i <= n; ++i) {
35 printf("%lld ", cnt[i]);
36 }
37 return 0;
38 }
codeforces 1000C - Covered Points Count 【差分】的更多相关文章
- CodeForces 1000C Covered Points Count(区间线段覆盖问题,差分)
https://codeforces.com/problemset/problem/1000/C 题意: 有n个线段,覆盖[li,ri],最后依次输出覆盖层数为1~n的点的个数. 思路: 区间线段覆盖 ...
- cf1000C Covered Points Count (差分+map)
考虑如果数字范围没有这么大的话,直接做一个差分数组就可以了 但现在变大了 所以要用一个map来维护 #include<bits/stdc++.h> #define pa pair<i ...
- C - Covered Points Count CodeForces - 1000C (差分,离散化,统计)
C - Covered Points Count CodeForces - 1000C You are given nn segments on a coordinate line; each end ...
- 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 ...
- Covered Points Count CF1000C 思维 前缀和 贪心
Covered Points Count time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- 【CF1000C】Covered Points Count(离散化+差分)
点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ...
- Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
Bryce1010模板 http://codeforces.com/problemset/problem/1000/C 题意:问你从[l,r]区间的被多少条线覆盖,列出所有答案. 思路:类似括号匹配的 ...
- Codeforces 1036E. Covered Points
又一次写起了几何.... 特殊处理在于有可能出现多条线段交于一点的情况,每次考虑时,对每条线段与其他所有线段的交点存在一个set里,对每一个set,每次删除set.size()即可 重点在于判断两条线 ...
随机推荐
- Netty学习:EventLoop事件机制
目录 EventLoop是什么 EventLoop适用的场景 Netty中的EventLoop Netty中的大量inEventLoop判断 Netty是如何建立连接并监听端口的-NIOSocketC ...
- [java]文件上传下载删除与图片预览
图片预览 @GetMapping("/image") @ResponseBody public Result image(@RequestParam("imageName ...
- git 基本命令和操作
设置全局用户名+密码 $ git config --global user.name 'runoob' $ git config --global user.email test@runoob.com ...
- 阿里云VPC网络内网实例通过SNAT连接外网
场景: 1.有多个ECS实例,其中A实例有公网IP,可以上外网 其它实例没有公网IP,不能上外网 2.所有实例在一个交换机,也就是一个网络(172.16.0.0/16) 实例 内网IP 外网IP A ...
- Eclipse在线安装FatJar插件失败解决方案
在线安装fatjar(URL:http://kurucz-grafika.de/fatjar) 快要安装完的时候报错如下: 找了很久解决方法,终于有了下文:很是粗乎意料呃,下载一个eclipse2.0 ...
- 【转】使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录
[原]http://blog.chinaunix.net/uid-26284395-id-2949145.html ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制 ...
- The OAuth 2.0 Authorization Framework OAuth2.0的核心角色code 扫码登录
RFC 6749 - The OAuth 2.0 Authorization Framework https://tools.ietf.org/html/rfc6749 The OAuth 2.0 a ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- linux shell 条件判断if else, if elif else....
在linux的shell中 if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... ...
- cocos2d-x 调试问题
1.昨天一个新功能,在xcode模拟器上测试没问题.后来打包安卓后,一直有问题 就又添加日志功能 # define CCLOGFUNC(s) ...