洛谷 1937 [USACO10MAR]仓配置Barn Allocation

【题解】
贪心。 把区间按照右端点从小到大排序,右端点相同的按照长度从小到大排序,然后按顺序考虑,能放就放下去。 维护能不能放下去用线段树即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define rg register
#define N 100010
#define ls (u<<1)
#define rs (u<<1|1)
using namespace std;
int n,m,ans,mn[N<<],del[N<<];
struct seg{
int l,r;
}a[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
void build(int u,int l,int r){
if(l<r){
int mid=(l+r)>>;
build(ls,l,mid),build(rs,mid+,r),mn[u]=min(mn[ls],mn[rs]);
}
else mn[u]=read();
}
inline void pushdown(int u){
mn[ls]+=del[u]; del[ls]+=del[u];
mn[rs]+=del[u]; del[rs]+=del[u];
del[u]=;
}
void update(int u,int l,int r,int ul,int ur){
if(ul<=l&&r<=ur){
mn[u]--; del[u]-=;
return;
}
int mid=(l+r)>>; pushdown(u);
if(ul<=mid) update(ls,l,mid,ul,ur);
if(ur>mid) update(rs,mid+,r,ul,ur);
mn[u]=min(mn[ls],mn[rs]);
}
int query(int u,int l,int r,int ul,int ur){
if(ul<=l&&r<=ur) return mn[u];
int mid=(l+r)>>,ret=2e9; pushdown(u);
if(ul<=mid) ret=min(ret,query(ls,l,mid,ul,ur));
if(ur>mid) ret=min(ret,query(rs,mid+,r,ul,ur));
return ret;
}
inline bool cmp(seg a,seg b){
if(a.r==b.r) return a.l>b.l;
return a.r<b.r;
}
int main(){
n=read(); m=read(); build(,,n);
for(rg int i=;i<=m;i++) a[i].l=read(),a[i].r=read();
sort(a+,a++m,cmp);
for(rg int i=;i<=m;i++){
int tmp=query(,,n,a[i].l,a[i].r);
if(tmp>){
ans++;
update(,,n,a[i].l,a[i].r);
}
}
printf("%d\n",ans);
return ;
}
洛谷 1937 [USACO10MAR]仓配置Barn Allocation的更多相关文章
- 洛谷P1937 [USACO10MAR]仓配置Barn Allocation
题目描述 Farmer John recently opened up a new barn and is now accepting stall allocation requests from t ...
- AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937
[USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; ...
- LUOGU P1937 [USACO10MAR]仓配置Barn Allocation
传送门 解题思路 扫了一眼觉得是贪心+线段树,结果贪心的时候刚开始按区间长度排的序..这还有82分,后来叉了自己,换成按右端点排序过了. 代码 #include<iostream> #in ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)
P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集(树形动规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 洛谷P3066 [USACO12DEC]逃跑的Barn (线段树合并)
题目描述It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to ro ...
- 洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]
题目传送门 逃跑的Barn 题目描述 It's milking time at Farmer John's farm, but the cows have all run away! Farmer J ...
- Java实现 洛谷 P6183 [USACO10MAR]The Rock Game S(DFS)
P6183 [USACO10MAR]The Rock Game S 输入输出样例 输入 3 输出 OOO OXO OXX OOX XOX XXX XXO XOO OOO PS: 因为每一位只有两种可能 ...
随机推荐
- JEECG框架使用Tomcat启动报ClassNotFound
JEECG框架缺少一个类,名为AnnotationProcessor,包名为:org.apache package org.apache; import java.lang.reflect.Invoc ...
- bzoj 1673: [Usaco2005 Dec]Scales 天平【dfs】
真是神奇 根据斐波那契数列,这个a[i]<=c的最大的i<=45,所以直接搜索即可 #include<iostream> #include<cstdio> usin ...
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】
二分图最大点覆盖模型,因为对于一个点(x,y)显然只要选x或者y就好了,于是连边,跑最大匹配=最大点覆盖(不会证) #include<iostream> #include<cstdi ...
- bzoj 1634: [Usaco2007 Jan]Protecting the Flowers 护花【贪心】
因为交换相邻两头牛对其他牛没有影响,所以可以通过交换相邻两头来使答案变小.按照a.t*b.f排降序,模拟着计算答案 #include<iostream> #include<cstdi ...
- laravel生命周期和核心思想
工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...
- 服务器出现 nginx 502 Bad Gateway
发生原因1.PHP FastCGI进程数不够用 当网站并发访问巨大时,php fastcgi的进程数不有一定的保障,因为cgi是单线程多进程工作的,也就是说cgi需要处理完一个页面后再继续下一个页面. ...
- 2017青岛网络赛1011 A Cubic number and A Cubic Number
A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/3276 ...
- 数据传递-------@ModelAttribute
package com.wh.handler; /** * @ModelAttribute绑定请求参数到命令对象 * @ModelAttribute一个具有如下三个作用: * * ①绑定请求参数到命令 ...
- 398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...
- Spring Boot (32) Lock 本地锁
平时开发中,有时会双击提交表单造成重复提交,或者网速比较慢时还没有响应又点击了按钮,我们在开发中必须防止重复提交 一般在前台进行处理,定义个变量,发送请求前判断变量值为true,然后把变量设置为fal ...