【Luogu3602】Koishi Loves Segments(贪心)

题面

洛谷

题解

离散区间之后把所有的线段挂在左端点上,从左往右扫一遍。

对于当前点的限制如果不满足显然会删掉右端点最靠右的那根,拿一个堆维护一下就好了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define MAX 400400
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n,m,ans,S[MAX<<1],tot,L[MAX],R[MAX],P[MAX],X[MAX];
int Bound(int x){return lower_bound(&S[1],&S[tot+1],x)-S;}
int c[MAX<<1],lim[MAX<<1];
vector<int> lk[MAX<<1];
struct Heap
{
int H[MAX],top;
void push(int x){H[++top]=x;push_heap(&H[1],&H[top+1]);}
void pop(){pop_heap(&H[1],&H[top--]);}
int Top(){return H[1];}
}Q;
int main()
{
n=read();m=read();
for(int i=1;i<=n;++i)L[i]=read(),R[i]=read(),S[++tot]=L[i],S[++tot]=R[i];
for(int i=1;i<=m;++i)P[i]=read(),X[i]=read(),S[++tot]=P[i];
sort(&S[1],&S[tot+1]);tot=unique(&S[1],&S[tot+1])-S-1;
for(int i=1;i<=n;++i)lk[Bound(L[i])].push_back(Bound(R[i]));
for(int i=1;i<=tot;++i)lim[i]=1e9;
for(int i=1;i<=m;++i)P[i]=Bound(P[i]),lim[P[i]]=min(lim[P[i]],X[i]);
for(int i=1,t=0;i<=tot;++i)
{
t+=c[i];
for(int r:lk[i])c[r+1]-=1,t+=1,Q.push(r),++ans;
while(t>lim[i])c[Q.Top()+1]+=1,t-=1,Q.pop(),--ans;
}
printf("%d\n",ans);
return 0;
}

【Luogu3602】Koishi Loves Segments(贪心)的更多相关文章

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

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

  2. 洛谷P3602 Koishi Loves Segments 贪心

    正解:贪心 解题报告: 传送门! 首先在学习贪心的入门题的时候我们就知道,当x=1的时候,也就是每条线段不能相交的时候的做法——就按右端点排序然后能选就选,也就是会议安排问题,原因显然?就你选右端点更 ...

  3. 洛谷P3602 Koishi Loves Segments(贪心,multiset)

    洛谷题目传送门 贪心小水题. 把线段按左端点从小到大排序,限制点也是从小到大排序,然后一起扫一遍. 对于每一个限制点实时维护覆盖它的所有线段,如果超过限制,则贪心地把右端点最大的线段永远删去,不计入答 ...

  4. LuoguP3602 Koishi Loves Segments

    题面 n个区间和数轴上的m个关键点 (0<=n,m<=4*1e5,数轴范围 \(-1^7\) ~ \(1^7\))每个关键点有被区间区间覆盖的次数上限,求最多能放多少个区间到数轴上 传送门 ...

  5. Luogu P3602 Koishi Loves Segments

    传送门 题解 既然是选取区间,没说顺序 肯定先排遍序 都是套路 那么按什么排序呢??? 为了方便处理 我们把区间按左端点从小到大排序 把关键点也按从小到大排序 假设当扫到 \(i\) 点时,i 点之前 ...

  6. 【题解】P3599 Koishi Loves Construction

    [题解]P3599 Koishi Loves Construction \(\mod n\) 考虑如何构造,发现\(n\)一定在第一位,不然不行.\(n\)一定是偶数或者是\(1\),不然 \(n|\ ...

  7. 题解-Koishi Loves Construction

    题解-Koishi Loves Construction 前缀知识 质数 逆元 暴搜 Koishi Loves Construction 给定 \(X\),\(T\) 组测试数据,每次给一个 \(n\ ...

  8. CF447B DZY Loves Strings 贪心

    DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...

  9. HDU 4882 ZCC Loves Codefires(贪心)

     ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. 牛客OI周赛8-普及组

    https://ac.nowcoder.com/acm/contest/543#question A. 代码: #include <bits/stdc++.h> using namespa ...

  2. jvisualvm中VisualGC插件提示:不受此JVM支持

    Visual GC插件「不受此JVM支持」问题 - djp567的博客 - CSDN博客https://blog.csdn.net/djp567/article/details/78662047 jv ...

  3. Access使用记录

    iif函数 此函数类似编程语言中的双目运算符,官方解释如下: 在任何可以使用表达式的位置均可使用 IIf.您可以使用 IIf 确定另一个表达式为 True 还是 False.如果表达式为 True,则 ...

  4. Django的分页和中间件

    一.分页 Django的分页器(paginator) view.py from django.shortcuts import render,HttpResponse # Create your vi ...

  5. 简单JQuery+AJAX+Servlet的计算器实现

    index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...

  6. C#通过Socket读取大量数据

    在C#中经常会用到Socket去接收和发送数据,而且也是非常方便的,有时候我们会向服务端去请求数据,如果返回的数据量很大,比如超过10M甚至是更多,那么该怎样去接收数据呢?下面以一个在项目中用到的实例 ...

  7. Servlet学习的一些笔记

    Servlet一点笔记 Servlet:Server Applet,全称Java Servlet,是运行在Web服务器或应用服务器上的程序,它是作为来自Web浏览器或其他HTTP客户端的请求和HTTP ...

  8. java JSP自定义标签

    来至: http://blog.csdn.net/jiangwei0910410003/article/details/23915373 http://blog.csdn.net/jiangwei09 ...

  9. 共轭函数Fenchel不等式

    f(x)不一定是凸函数,但他的共轭函数一定是凸函数.是仿射函数的逐点上确界. Fenchel不等式 f(x)+f*(x)>=xTy 如

  10. Span<T>

    Introduction Span<T> is a new type we are adding to the platform to represent contiguous regio ...