Bzoj[Usaco2018 Feb]5194 Snow Boots(线段树)
Description
Input
第一行包含两个空格分隔的整数N和B(1≤N,B≤10^5)。
Output
输出包含N行
Sample Input
0 3 8 5 6 9 0 0
0 5
0 6
6 2
8 1
10 1
5 3
150 7
Sample Output
1
1
0
1
1
1
这题的解法好像挺多,比如并查集?但是我还是用线段树了,因为讲师是这么讲的23333
#include<bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define ull unsigned long long
using namespace std;
const int maxn = ;
struct enkidu {
int di, si, id;
}ask[maxn], a[maxn];
struct shiki {
int l, r, l_lin, r_lin;
int max_cnt;
}tree[maxn << ];
//l_lin表示这个区间从左边开始连续的不能走的长度
//r_lin表示这个区间从右边开始连续的不能走的长度
int n, m;
int ans[maxn]; inline int read() {
int x = , y = ;
char ch = getchar();
while(!isdigit(ch)) {
if(ch == '-') y = -;
ch = getchar();
}
while(isdigit(ch)) {
x = (x << ) + (x << ) + ch - '';
ch = getchar();
}
return x * y;
} inline bool cmp(enkidu a, enkidu b) {
return a.si < b.si;
} inline void maintain(int pos) {
int ls = pos << , rs = pos << | ;
if(tree[ls].r - tree[ls].l + == tree[ls].max_cnt)
tree[pos].l_lin = tree[ls].max_cnt + tree[rs].l_lin;
else tree[pos].l_lin = tree[ls].l_lin;
if(tree[rs].r - tree[rs].l + == tree[rs].max_cnt)
tree[pos].r_lin = tree[ls].r_lin + tree[rs].max_cnt;
else tree[pos].r_lin = tree[rs].r_lin;
tree[pos].max_cnt = max(tree[ls].max_cnt, tree[rs].max_cnt);
tree[pos].max_cnt = max(tree[pos].max_cnt, tree[ls].r_lin + tree[rs].l_lin);
} void build(int pos, int l, int r) {
tree[pos].l = l, tree[pos].r = r;
if(l == r) {
tree[pos].l_lin = tree[pos].r_lin = tree[pos].max_cnt = ;
return;
}
int mid = l + r >> ;
build(pos << , l, mid);
build(pos << | , mid + , r);
maintain-(pos);
} void update(int pos, int aim, int l, int r) {
if(l == r && l == aim) {
tree[pos].max_cnt = tree[pos].l_lin = tree[pos].r_lin = ;
return;
}
int mid = l + r >> ;
if(aim <= mid) update(pos << , aim, l, mid);
else update(pos << | , aim, mid + , r);
maintain(pos);
} int main() {
n = read(), m = read();
for(int i = ; i <= n; ++i) {
a[i].si = read();
a[i].id = i;
}
for(int i = ; i <= m; ++i) {
ask[i].si = read(), ask[i].di = read();
ask[i].id = i;
}
sort(ask + , ask + m + , cmp);
sort(a + , a + n + , cmp);
build(, , n);
int brick = ;//砖,表示第几块砖
for(int i = ; i <= m; ++i) {
while(brick < n && a[brick + ].si <= ask[i].si) {//如果可以走
brick++;
update(, a[brick].id, , n);
}
if(tree[].max_cnt < ask[i].di) ans[ask[i].id] = ;
}
for(int i = ; i <= m; ++i) printf("%d\n", ans[i]);
return ;
}
Bzoj[Usaco2018 Feb]5194 Snow Boots(线段树)的更多相关文章
- bzoj 1593: [Usaco2008 Feb]Hotel 旅馆【线段树】
参考:https://blog.csdn.net/u010336344/article/details/53034372 神一样的线段树 线段树上维护:ll从左开始最长空段:rr从右开始最长空段:le ...
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- [BZOJ 3995] [SDOI2015] 道路修建 【线段树维护连通性】
题目链接:BZOJ - 3995 题目分析 这道题..是我悲伤的回忆.. 线段树维护连通性,与 BZOJ-1018 类似,然而我省选之前并没有做过 1018,即使它在 ProblemSet 的第一页 ...
- [BZOJ 3888] [Usaco2015 Jan] Stampede 【线段树】
题目链接:BZOJ - 3888 题目分析 首先,计算出每个线段在 x 坐标 0 处出现的时间开始点和结束点,就转成了时间轴上的线段. 然后就是看每条线段是否被 y 比它小的线段完全覆盖了.注意求出的 ...
- [BZOJ 3747] [POI 2015] Kinoman【线段树】
Problem Link : BZOJ 3747 题解:ZYF-ZYF 神犇的题解 解题的大致思路是,当区间的右端点向右移动一格时,只有两个区间的左端点对应的答案发生了变化. 从 f[i] + 1 到 ...
- BZOJ.4137.[FJOI2015]火星商店问题(线段树分治 可持久化Trie)
BZOJ 洛谷 一直觉得自己非常zz呢.现在看来是真的=-= 注意题意描述有点问题,可以看BZOJ/洛谷讨论. 每个询问有两个限制区间,一是时间限制\([t-d+1,t]\),二是物品限制\([L,R ...
- BZOJ.1805.[IOI2007]sail船帆(贪心 线段树)
BZOJ 洛谷 首先旗杆的顺序没有影响,答案之和在某一高度帆的总数有关.所以先把旗杆按高度排序. 设高度为\(i\)的帆有\(s_i\)个,那么答案是\(\sum\frac{s_i(s_i-1)}{2 ...
- BZOJ.4825.[AHOI/HNOI2017]单旋(线段树)
BZOJ LOJ 洛谷 这题不难啊,我怎么就那么傻,拿随便一个节点去模拟.. 我们只需要能够维护,将最小值或最大值转到根.模拟一下发现,对于最小值,它的右子树深度不变(如果存在),其余节点深度全部\( ...
- BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)
BZOJ 洛谷 \(Description\) 给定一棵树,每次询问给定\(p,k\),求满足\(p,a\)都是\(b\)的祖先,且\(p,a\)距离不超过\(k\)的三元组\(p,a,b\)个数. ...
随机推荐
- Global Vectors forWord Representation
参考论文: GloVe: Global Vectors forWord Representation 参考博客:https://blog.csdn.net/coderTC/article/detail ...
- Image Scaling using Deep Convolutional Neural Networks
Image Scaling using Deep Convolutional Neural Networks This past summer I interned at Flipboard in P ...
- HTML5文件上传器,纯脚本无插件的客户端文件上传器---Uploader 文件上传器类
概述 客户端完全基于JavaScript的 浏览器文件上传器,不需要任何浏览器插件,但需要和jQuery框架协同工作,支持超大文件上传,其算法是将一个超大文件切片成N个数据块依次提交给服务 端处理,由 ...
- 【BZOJ】4764: 弹飞大爷 LCT
[题意]给定n个数字ai,表示大爷落到i处会被弹飞到i+ai处,弹飞到>n或<1处则落地.m次操作,修改一个ai,或询问大爷落到x处经过几次落地(或-1).n,m<=10^5,|ai ...
- Spring Data JPA笔记
1. Spring Data JPA是什么 Spring Data JPA是Spring Data大家族中的一员,它对对持久层做了简化,用户只需要声明方法的接口,不需要实现该接口,Spring Dat ...
- Android中的通信Volley
1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...
- linux系统分区参考
UPDATE: update is used to download package information from all configured sources. UPGRADE: upgrad ...
- MGR Switch single-Primary to Muti_primary
MGR single_primary 切换 Muti-Primary 模式 root@localhost [(none)]>select * from performance_schema.re ...
- 基于scrapy的分布式爬虫抓取新浪微博个人信息和微博内容存入MySQL
为了学习机器学习深度学习和文本挖掘方面的知识,需要获取一定的数据,新浪微博的大量数据可以作为此次研究历程的对象 一.环境准备 python 2.7 scrapy框架的部署(可以查看上一篇博客的简 ...
- weblogic12.1.3 静默安装 建域
--安装依赖包 yum -y install compat-libcap1 compat-libstdc++ gcc gcc-c++ glibc-devel libaio-devel libstdc+ ...