题意:给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. 深入理解计算机系统 第二章 信息的表示和处理 Part2 第二遍

    <深入理解计算机系统> 第三版 第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了 29 ~ 34 页) 第一遍对应笔记链接 https://www.cnblogs.com/s ...

  2. 解决 scp 和rsync 同步失败【rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.2]】

    解决 scp 和rsync 同步失败 报错信息截图: 解决问题的步骤: 1.检查对方的scp和rsync 是否能使用. rsync 在使用的时候,需要客户端和服务端都有rsync工具.scp 和 rs ...

  3. 浅析ORACLE中NVL/NVL2/DECODE/CASE WHEN的用法

    使用NVL的时候只能对值进行是否为空的判断,基本语法是NVL( 值1, ,结果2).它的功能是如果值1为空,则此函数返回结果2,不为空的话直接输出值1(如果两个参数都为空,那么还是返回空): NVL2 ...

  4. (二十一)golang--字符串中的函数

    golang中ascii对应的字符占一个字节,而汉字占三个字节. (1)统计字符串的长度len (2)字符串遍历,同时处理有中文的问题r:=[]rune(str) (3)字符串转整数:n,err:= ...

  5. C语音中最简单的排序冒泡排序和选择排序代码实现(非指针)

    #include<stdio.h> int main() { int a[5] = { 2,5,7,3,-1 }; int n = sizeof(a) / sizeof(a[0]);//元 ...

  6. 易语言 史诗级Json处理 烁_Json模块!!!!

    大家好,我是键盘上的魔手 * “************************”* “** 欢迎使用烁Json模块 **”* “** 作者:键盘上的魔手 **”* “**  微信号:codervip ...

  7. Linux内核版本 uname命令 GNU项目 Linux发行版

    1.内核版本由linux内核社区统一编码和发布,格式如下图: major.minor.patch-build.desc 主版本号.次版本号.对次版本号的修订次数-编译次数.当前版本的特殊信息 次版本号 ...

  8. 源码包的安装、rsync同步、inotify监测

    一.源码包的安装 1.源码包的作用:yum 使用的是rpm包,rpm包安装的不能指定安装位置 源码包可以按需选择/定制,及时修复bug ,适用于各种平台 2.大致过程:源码包——>make gc ...

  9. Java数组定义及初始化

    数组定义及初始化 数组这玩意是一种用于存放数据最常见数据结构. 数组的的概念及注意点 数组要求所有的数组元素具有相同的数据类型,且只能存在一种数据类型,要多专一有多专一. 数据类型既可以是基本类型也可 ...

  10. css优先级之important

    css优先级之important