LUOGU P1937 [USACO10MAR]仓配置Barn Allocation
解题思路
扫了一眼觉得是贪心+线段树,结果贪心的时候刚开始按区间长度排的序。。这还有82分,后来叉了自己,换成按右端点排序过了。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 100005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f?x:-x;
}
struct Data{
int l,r;
}data[MAXN];
int n,m,ans;
int lazy[MAXN<<2],Min[MAXN<<2];
inline void pushdown(int x){
lazy[x<<1]+=lazy[x];lazy[x<<1|1]+=lazy[x];
Min[x<<1]-=lazy[x];Min[x<<1|1]-=lazy[x];
lazy[x]=0;
}
void build(int x,int l,int r){
if(l==r){
Min[x]=rd();
return;
}
int mid=l+r>>1;
build(x<<1,l,mid);build(x<<1|1,mid+1,r);
Min[x]=min(Min[x<<1],Min[x<<1|1]);
}
int query(int x,int l,int r,int L,int R){
if(L<=l && r<=R) return Min[x];
int mid=l+r>>1,ret=0x3f3f3f3f;
if(lazy[x]) pushdown(x);
if(mid>=L) ret=min(ret,query(x<<1,l,mid,L,R));
if(mid<R) ret=min(ret,query(x<<1|1,mid+1,r,L,R));
return ret;
}
void update(int x,int l,int r,int L,int R,int k){
if(L<=l && r<=R) {
Min[x]-=k;
lazy[x]+=k;
return;
}
int mid=l+r>>1;
if(lazy[x]) pushdown(x);
if(mid>=L) update(x<<1,l,mid,L,R,k);
if(mid<R) update(x<<1|1,mid+1,r,L,R,k);
Min[x]=min(Min[x<<1],Min[x<<1|1]);
}
inline bool cmp(Data A,Data B){
if(A.r==B.r)
return A.l>B.l;
return A.r<B.r;
}
int main(){
n=rd();m=rd();build(1,1,n);
for(register int i=1;i<=m;i++) data[i].l=rd(),data[i].r=rd();
sort(data+1,data+1+m,cmp);
for(register int i=1;i<=m;i++)
if(query(1,1,n,data[i].l,data[i].r)>0) {
ans++;
update(1,1,n,data[i].l,data[i].r,1);
}
cout<<ans<<endl;
return 0;
}
LUOGU P1937 [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; ...
- 洛谷 1937 [USACO10MAR]仓配置Barn Allocation
[题解] 贪心. 把区间按照右端点从小到大排序,右端点相同的按照长度从小到大排序,然后按顺序考虑,能放就放下去. 维护能不能放下去用线段树即可. #include<cstdio> #inc ...
- 【Luogu1973】仓配置(贪心,线段树)
[Luogu1973]仓配置 题面 直接找洛谷把... 题解 很明显的贪心吧 按照线段的右端点为第一关键字,左端点第二关键字排序 然后线段树维护区间最小就可以啦 #include<iostrea ...
- 【Luogu1937】仓配置(贪心,线段树)
[Luogu1937]仓配置 题面 直接找洛谷把... 题解 很明显的贪心吧 按照线段的右端点为第一关键字,左端点第二关键字排序 然后线段树维护区间最小就可以啦 #include<iostrea ...
- 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- Maven私仓配置
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
- BZOJ1828[USACO 2010 Mar Gold 2.Barn Allocation]——贪心+线段树
题目描述 输入 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i 输出 * 第一行: ...
- LUOGU P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
传送门 解题思路 首先第一遍dfs预处理出每个点的子树的siz,然后可以处理出放在根节点的答案,然后递推可得其他答案,递推方程 sum[u]=sum[x]-(val[i]*siz[u])+(siz[1 ...
随机推荐
- 17-7-es6作用域
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Eclipse Unable to install breakpoint in XXX 解决办法
Debug 时偶尔会出现:Eclipse Unable to install breakpoint in XXX 情况一: 清除所有断点就行了,原因是断点打到注释上了. breakpoint 窗口: ...
- linq学习(第二部分)
8.匿名方法 (1)源起 在上面的例子中 为了得到序列中较大的值 我们定义了一个More方法 var d1 = new Predicate<int>(More); 然而这个方法,没有太多逻 ...
- [CF587-F]WI-FI
显然DP题... f[i][0]表示这个点不装路由器,f[i][1]表示装路由器 转移也很简单,在前面一段区间找最小值就好了 但是直接转移是$O(n*k)$的,会T掉 大佬说这个东西有单调性,但是菜鸡 ...
- zabbix被监控端代理设置
zabbix被监控端代理设置 安装zabbix-agent客户端 rpm -ivh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-re ...
- htaccess apache重定向学习
1.推荐博客:http://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html 2.测试工具:https://htaccess.madewi ...
- HttpUrlConnection post 乱码 终极解决方案
今天遇到了java后台模拟http请求,以POST方式传参中文乱码,google了一下,大部分是在打开的HttpURLConnection的输入流的时候设置编码(utf-8),按照设置,试了下并没有解 ...
- elasticsearch配置优化
调整ES内存分配有多种方式,建议调整 elasticsearch 中的设置(还可以直接修改bin下的启动脚本). # Directory where the Elasticsearch binary ...
- PAT甲级——A1006 Sign In and Sign Out
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- 解析Request和Response
简介: Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应 ...