Codeforces Round #595 (Div. 3) D2Too Many Segments,线段树
题意:给n个线段,每个线段会覆盖一些点,求删最少的线段,使得每个点覆盖的线段不超过k条。
思路:按右端点排序,之后依次加入每个线段,查询线段覆盖区间内的每个点,覆盖的最大线段数量,如果不超过k,那就可以直接加入。
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+;
int s[maxn<<],col[maxn<<];
struct node
{
int id,l,r;
}p[maxn];
bool cmp(node a,node b)
{
if(a.r==b.r)return a.l<b.l;
else return a.r<b.r;
}
void up(int p)
{
s[p]=max(s[p<<],s[p<<|]);
}
void down(int p,int l,int r)
{
if(col[p])
{
s[p<<]+=col[p];
s[p<<|]+=col[p];
col[p<<]+=col[p];
col[p<<|]+=col[p];
col[p]=;
}
}
void modify(int p,int l,int r,int x,int y,int c)
{
if(x<=l&&r<=y)
{
s[p]+=c;
col[p]+=c;
return;
}
down(p,l,r);
int mid=l+r>>;
if(x<=mid)modify(p<<,l,mid,x,y,c);
if(y>mid) modify(p<<|,mid+,r,x,y,c);
up(p);
}
int query(int p,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
return s[p];
}
down(p,l,r);
int mid=l+r>>,maxn=;
if(x<=mid)maxn=max(maxn,query(p<<,l,mid,x,y));
if(y>mid)maxn=max(maxn,query(p<<|,mid+,r,x,y));
return maxn;
} int main()
{
int n,k,ans[maxn]={},cnt=;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d%d",&p[i].l,&p[i].r);
p[i].id=i;
}
sort(p+,p++n,cmp);
for(int i=;i<=n;i++)
{
int tmp=query(,,maxn,p[i].l,p[i].r);
if(tmp<k) modify(,,maxn,p[i].l,p[i].r,),ans[p[i].id]=,cnt++;
}
printf("%d\n",n-cnt);
int cas=;
for(int i=;i<=n;i++)
{
if(!ans[i])
{
printf("%d",i),cas++;
if(cas==n-cnt)printf("\n");
else printf(" ");
}
}
return ;
}
Codeforces Round #595 (Div. 3) D2Too Many Segments,线段树的更多相关文章
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #546 (Div. 2) E 推公式 + 线段树
https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...
- Codeforces Round #271 (Div. 2) F. Ant colony 线段树
F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
D. Developing Game Pavel is going to make a game of his dream. However, he knows that he can't mak ...
- Codeforces Round #275 Div.1 B Interesting Array --线段树
题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...
- Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP
D. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
随机推荐
- [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法
如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...
- VsCode Python配置安装教程
1.软件下载地址 Python官网: https://www.python.org/downloads/windows/ Python下载地址: https://www.python.org/ftp/ ...
- Apache中AllowOverride的详细配置使用
我们通常利用Apache的rewrite模块对URL进行重写,rewrite规则会写在 .htaccess 文件里.但要使 apache 能够正常的读取.htaccess 文件的内容,就必须对.hta ...
- 领扣(LeetCode)各位相加 个人题解
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...
- .NET做人脸识别并分类
.NET做人脸识别并分类 在游乐场.玻璃天桥.滑雪场等娱乐场所,经常能看到有摄影师在拍照片,令这些经营者发愁的一件事就是照片太多了,客户在成千上万张照片中找到自己可不是件容易的事.在一次游玩等活动或家 ...
- Android 8.1 自定义热点的时候设置了热点maxLength="32",但是在希伯来语等状态下还是发现在没到32个字符之前就无法把热点设置成功了
初步认为应该是与热点名称的字节数有关. 然后开始查看源码. /Settings/res/xml/tether_prefs.xml 中的 <Preference android:key=" ...
- SpringMvc demo示例及源码详细分析
三层架构介绍 我们的开发架构一般都是基于两种形式,一种C/S架构,也就是客户端/服务器,另一种是B/S架构,也就是浏览器/服务器.在JavaEE开发中,几乎全部都是基于B/S架构的开发.那么在B/S架 ...
- 如何解决Redis缓存雪崩、缓存穿透、缓存并发等5大难题
缓存雪崩 数据未加载到缓存中,或者缓存同一时间大面积的失效,从而导致所有请求都去查数据库,导致数据库CPU和内存负载过高,甚至宕机. 比如一个雪崩的简单过程: 1.redis集群大面积故障 2.缓存失 ...
- 2019-11-28:ssrf基础学习,笔记
ssrf服务端请求伪造ssrf是一种由恶意访问者构造形成由服务端发起请求的一个安全漏洞,一般情况下,ssrf访问的目标是从外网无法访问的内部系统,正式因为它是由服务端发起的,所以它能请求到它相连而外网 ...
- Docker从门外到入门使用
取材 第一本Docker书.原作者:James Turnbull 安装 这里只说明Windows环境的安装(Windows7以上) 使用Docker Toolbox工具即可:http://mirror ...