题意

给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k。

分析

从左往右扫每个点\(x\),若覆盖点\(x\)的线段数cnt大于k,则贪心的删去覆盖点\(x\)的线段中\(r_i\)前\(cnt-k\)大的线段,因为点\(x\)左边的点的被覆盖数一定已经小于等于k了,删去\(r_i\)越大的线段越优。可以用个堆来维护覆盖点\(x\)的线段,用树状数组维护覆盖每个点的线段数量。

Code

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=2e5+10;
int n,k;
int b[maxn];
void add(int x,int k){
while(x<maxn) b[x]+=k,x+=x&-x;
}
int dor(int x){
int ret=0;
while(x) ret+=b[x],x-=x&-x;
return ret;
}
struct ppo{
int l,r,i;
bool operator <(const ppo &o) const{
if(r==o.r&&l==o.l) return i<o.i;
if(r==o.r) return l<o.l;
return r<o.r;
}
};
int ans[maxn];
struct node{
int x,i;
};
vector<node>a[maxn];
int main(){
ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>n>>k;
for(int i=1,l,r;i<=n;i++){
cin>>l>>r;
a[l].pb(node{r,i});
add(l,1);add(r+1,-1);
}
int tot=0;
set<ppo>q;
for(int i=1;i<maxn;i++){
for(node x:a[i]) q.insert(ppo{i,x.x,x.i});
int cnt=dor(i);
while(cnt>k){
auto it=q.end();
--it;
add(it->l,-1);add(it->r+1,1);
ans[++tot]=it->i;
q.erase(it);
cnt--;
}
}
cout<<tot<<'\n';
for(int i=1;i<=tot;i++){
cout<<ans[i]<<" ";
}cout<<'\n';
return 0;
}

codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组的更多相关文章

  1. Codeforces 1249 D2. Too Many Segments (hard version)

    传送门 贪心 对于第一个不合法的位置,我们显然要通过删除几个覆盖了它的区间来使这个位置合法 显然删右端点更靠右的区间是更优的,所以就考虑优先删右端点靠右的,然后再考虑下一个不合法位置 用一个 $set ...

  2. Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组

    http://codeforces.com/contest/101/problem/B 给定一个数n,起点是0  终点是n,有m两车,每辆车是从s开去t的,我们只能从[s,s+1,s+2....t-1 ...

  3. Codeforces Round #413 (Div1 + Div. 2) C. Fountains(树状数组维护最大值)

    题目链接:https://codeforces.com/problemset/problem/799/C 题意:有 c 块硬币和 d 块钻石,每种喷泉消耗硬币或钻石中的一种,每个喷泉有一个美丽值,问建 ...

  4. Codeforces 980E The Number Games - 贪心 - 树状数组

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗有$n$个点的树,$i$号点的权值是$2^{i}$要求删去$k$个点,使得剩下的点仍然连通,并且总权值和最大,问删去的所有点的编号. ...

  5. Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  6. Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值

    http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b ...

  7. Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...

  8. Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)

    F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  9. D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

    题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个 ...

随机推荐

  1. 02 servlet基础 生命周期 tomcat web.xml

    新建web项目 – new Web Project – 选择:javaee 5.0 建包 – com.gzsxt.wang 新建class:FirstServlet – 继承:HttpServlet( ...

  2. This is very likely to create a memory leak. Stack trace of thread错误分析

    1.问题描述 启动tomcat部署项目时,报This is very likely to create a memory leak. Stack trace of thread错误. 29-May-2 ...

  3. Unity UGUI动态生成控件

    一. 首先你得先清楚RectTransform组件的一些程序控制 1. 先得到UGUI控件上面的RectTransform组件 RectTransform rtr = gameObject.GetCo ...

  4. hdu 1325

    .,. 还是待整理 #include <stdio.h> const; typedef struct { int num,root,conn;//数据.根.入度 }Node; Node n ...

  5. 21-MySQL DBA笔记-高可用性

    第21章 高可用性 本章将为读者介绍单点故障的处理策略,以及单点故障最为主流的解决方案:MySQL数据库切换. 21.1 概述 可用性定义为系统保持正常运行时间的百分比,高可用可以理解为系统可用时间的 ...

  6. python2.7.5安装docker-compose的方法

    yum -y install epel-release && yum install -y python-pip && pip install --upgrade pi ...

  7. (六)图数据neo4j之cypher(一)

    1.Cypher概述 cypher是一种声明式的图数据库查询语言,能高效的查询和更新图数据库,是依赖于模式的.所谓模式(Patterns)是就是众多节点和关系的任意复杂想法. (1)节点语法 cyph ...

  8. [Python][pythonchallenge][TBC]古老的python在线挑战赛,很有意思 (C0-C4)

    预计阅读时间:15分钟 背景:搜索资料时候偶然发现的,很有意思,每一关都覆盖了很多知识点 Python版本:3.0 Talking is cheap,show me the code 主页: http ...

  9. Vs2017 NetCode Mvc EF Mysql 整合1

    1  运行环境   vs2017   NetCode2.0 2 NuGet  MySql.Data.EntityFrameworkCore 8.0.18 3  源代码 https://github.c ...

  10. django考点

    django考点 1 列举Http请求中常见的请求方式2 谈谈你对HTTP协议的认识.1.1 长连接3 简述MVC模式和MVT模式4 简述Django请求生命周期5 简述什么是FBV和CBV6 谈一谈 ...