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\)个数. ...
随机推荐
- CSS中em,rem的区别
首先这两个单位一般用在移动端 不太清楚得求证 再记录 1.em w3cschool中给出css中尺寸单位如下: 单位 描述 % 百分比 in 英寸 cm 厘米 mm 毫米 em 1em 等于当前的字 ...
- UVA 10479 The Hendrie Sequence
https://vjudge.net/problem/UVA-10479 打表找规律: 1.根据n可以确定第n项在上表中第i行 2.减去前i-1行,就得到了n在第i行的第j个 3.第i行的规律:1个i ...
- 字符串:KMP
KMP是字符串匹配的经典算法 也是众多字符串基础的重中之重 A. 题意:给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1. 做法:直 ...
- Jugs(回溯法)
ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In ...
- C++ 的getline问题
在用c++的getline函数的时候碰到两个问题,总结如下: 1.有时候写程序的时候我们会发现getline(cin,str);这样的语句是不会执行,而是直接跳过的, 一般的解决方法是getline一 ...
- AlloyTouch 简介
AlloyTouch 是来自于腾讯AlloyTeam团队开发的一个适用用移动端的js组件库. 特性: 1.丰富的组件 选择组件.级联选择组件.轮播组件.全屏滚动组件.下拉刷新组件.上拉刷新任君选择 2 ...
- ASP.NET站点Web部署(一键发布的实现)
在开发过程中经常需要发布到开发环境.测试环境或者预发布环境上给其他同事进行测试验证效果等等,每次发布都要备份,拷贝,修改配置文件等等重复操作非常的麻烦,效率大打折扣,而web部署提供了这样的解决方案: ...
- 【POJ】3177 Redundant Paths
[算法]边双连通分量 [题意&题解]http://blog.csdn.net/geniusluzh/article/details/6619575 (注意第一份代码是错误的) 一些细节: 1. ...
- win10本地搭建php运行环境
一.下载搭建环境所需软件,安装顺序也要按照列表顺序安装 1.Vc2015(根据需要安装Vc2012或者Vc2015) Vc2015:https://www.microsoft.com/zh-CN/do ...
- a 标签传值
转载:http://blog.csdn.net/muyeju/article/details/48594377 .<a>标签传值的形式--参数固定:<a href="地址? ...