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 ...
随机推荐
- vm虚拟机安装linux centos教程
1 下载64btnhttp://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso 2 vm注意选择cen ...
- 怎样判断有没有SQL注入漏洞及原理?
来源:实验楼 最为经典的单引号判断法: 在参数后面加上单引号,比如: http://xxx/abc.php?id=1' 如果页面返回错误,则存在 Sql 注入. 原因是无论字符型还是整型都会因为单引号 ...
- Pandas进阶笔记 (一) Groupby 重难点总结
如果Pandas只是能把一些数据变成 dataframe 这样优美的格式,那么Pandas绝不会成为叱咤风云的数据分析中心组件.因为在数据分析过程中,描述数据是通过一些列的统计指标实现的,分析结果也需 ...
- 6.2.2 辅助类GenericOptionsParser,Tool和ToolRunner深入解析
辅助类GenericOptionsParser,Tool和ToolRunner (1)为什么要用ToolRunner 将MapReduce Job配置参数写到java代码里,一旦变更意味着修改java ...
- linux系统LNMP环境部署
源码安装 nginx# yum -y install gcc openssl-devel# useradd -s /sbin/nologin nginx# tar xf nginx-1.14.0.ta ...
- jenkins里的定时构建
1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月的第几天,取值1~31第四个*表示第几月,取 ...
- SpringBoot 整合NoSql
通用配置 maven依赖 添加Spring-Web和Spring-Security依赖,使用Spring-Security是因为使用SpringBoot的Redis依赖时,必须添加Spring-Sec ...
- 自建yum仓库,该仓库为默认仓库
YUM REPO: http://content.example.com/rhel7.0/x86_64/dvd 创建自建yum REPO文件: vim /etc/yum.repos.d/redhat. ...
- hdu 2063 过山车 (二分图,最大匹配)
过山车Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- nyoj 599-奋斗的小蜗牛 (double ceil(); (temp - 1) / 5)
599-奋斗的小蜗牛 内存限制:64MB 时间限制:1000ms 特判: No 通过数:0 提交数:96 难度:1 题目描述: 传说中能站在金字塔顶的只有两种动物,一种是鹰,一种是蜗牛.一只小蜗牛听了 ...