洛谷 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: 因为每一位只有两种可能 ...
随机推荐
- vue的学习网址
Vue官网:http://cn.vuejs.org/v2/guide/index.html 淘宝镜像:http://npm.taobao.org/ Vue-router:https://router. ...
- java编程中的断言工具类(org.springframework.util.Assert)
转自:https://blog.csdn.net/gokeiryou263/article/details/19612471 断言工具类:Assert类, java.lang.Object ---&g ...
- DNS中的AC、rndc、智能DNS解析和基础排错
bind中的ACL和rndc DNS除了服务器外,还具有一些访问控制和视图功能. 访问控制是指仅对定义的网络进行解析,视图也就是智能解析. 1>访问控制是通过acl函数来实现的,acl把一个或多 ...
- Ubuntu12.04安装JDK(jdk-6u45-linux-x64.bin)(转载)
首先下载jdk-6u45-linux-x64.bin, 如果是32bit系统下载相应的i386即可. 更改文件权限-> chmod a+x jdk-6u45-linux-x64.bin. 创建目 ...
- bzoj 1047: [HAOI2007]理想的正方形【单调队列】
没有复杂结构甚至不长但是写起来就很想死的代码类型 原理非常简单,就是用先用单调队列处理出mn1[i][j]表示i行的j到j+k-1列的最小值,mx1[i][j]表示i行的j到j+k-1列的最大值 然后 ...
- BZOJ4543/BZOJ3522 [POI2014]Hotel加强版(长链剖分)
题目好神仙--这个叫长链剖分的玩意儿更神仙-- 考虑dp,设\(f[i][j]\)表示以\(i\)为根的子树中到\(i\)的距离为\(j\)的点的个数,\(g[i][j]\)表示\(i\)的子树中有\ ...
- ACM_拼接数字
拼接数字 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个正整数数组,现在把数组所有数字都拼接成一个大数字,如何使得拼接后 ...
- Scala-基础-数组(1)
import junit.framework.TestCase import scala.collection.mutable.ArrayBuffer; //数组(1) //知识点-定义数组,变长数组 ...
- 重新学习Java——Java基本的程序设计结构(二)
上一节简单回顾了Java基本的一些程序设计的知识,这一节将继续根据<Java核心技术>这本书,进行这方面知识的复习与探索. 1. 字符串 Java字符串实际上就是Unicode字符序列.例 ...
- 2017-12-04HTML table布局
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...