题意:给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,线段树的更多相关文章

  1. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  2. 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 ...

  3. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  4. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 创建一个线程池(java)

    private ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("billService-poo ...

  2. [WPF] Caliburn Micro学习二 Infrastructure

    Caliburn Micro学习一 Installation http://blog.csdn.net/alvachien/article/details/12985415 Step 1. 无论是通过 ...

  3. 使用iis反向代理.net core应用程序

    .net core 其实是自宿主性质的web应用程序,而不再是web网站,所以.net core是可以直接单独作为系统服务部署.但是实际情况中,为了同个一个端口能支持多个web应用和统一管理,还是应该 ...

  4. ASP.NET购物车Cookie获取,创建,添加,更新,删除的用法

    #region 添加购物车 public void GetShoppingCart(int skuId, int quanlity) { HttpCookie cookie = HttpContext ...

  5. Java,你告诉我 fail-fast 是什么鬼?

    本篇我们来聊聊 Java 的 fail-fast 机制,文字一如既往的有趣哦. 01.前言 说起来真特么惭愧:十年 IT 老兵,Java 菜鸟一枚.今天我才了解到 Java 还有 fail-fast ...

  6. C语言|博客作业09

    这个作业属于哪个课程 C语言程序设计II 这个作业的要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/10027 我在这个课程 ...

  7. Python开发面试集锦

    我正在编写一套python面试开发集锦,可以帮忙star一下,谢谢! 地址:GitHub专栏

  8. .NET core3.0 使用Jwt保护api

    摘要: 本文演示如何向有效用户提供jwt,以及如何在webapi中使用该token通过JwtBearerMiddleware中间件对用户进行身份认证. 认证和授权区别? 首先我们要弄清楚认证(Auth ...

  9. python容器类型集合的操作

    集合(set):集合是一个无序的序列,集合中的元素可以是任意数据类型:表现形式是set(集合的元素),能够实现自动去重:集合传入的必须是一个hashable类型值,(不能存储字典格式的值):并且创建集 ...

  10. tcp和udp的网络编程(发送消息及回复)

    一.UDP  无连接的  高效的  基于数据报的  不可靠 的连接 主要的应用场景: 需要资源少,网络情况稳定的内网,或者对于丢包不敏感的应用,比如 DHCP 就是基于 UDP 协议的.不需要一对一沟 ...