Bzoj[Usaco2018 Feb]5194 Snow Boots(线段树)

Description

到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1…N,第i块地砖上积了fi英尺的雪
。在Farmer John的农舍的地窖中,总共有B双靴子,编号为1…B。其中某些比另一些结实,某些比另一些轻便。具
体地说,第i双靴子能够让FJ在至多si英尺深的积雪中行走,能够让FJ每步至多前进di。Farmer John从1号地砖出
发,他必须到达N号地砖才能叫醒奶牛们。1号地砖在农舍的屋檐下,N号地砖在牛棚的屋檐下,所以这两块地砖都
没有积雪。帮助Farmer John求出哪些靴子可以帮助他走完这段艰辛的路程。

Input

第一行包含两个空格分隔的整数N和B(1≤N,B≤10^5)。

第二行包含N个空格分隔的整数;第i个整数为fi,即i号地砖的积雪深度(0≤fi≤10^9)。输入保证f1=fN=0
下面B行,每行包含两个空格分隔的整数。第i+2行的第一个数为si,表示第i双靴子能够承受的最大积雪深度。
第i+2行的第二个数为di,表示第i双靴子的最大步长。输入保证0≤si≤10^9以及1≤di≤N-1

Output

输出包含N行

第i行包含一个整数:如果Farmer John能够穿着第i双靴子从1号地砖走到N号地砖,为1,否则为0

Sample Input

8 7
0 3 8 5 6 9 0 0
0 5
0 6
6 2
8 1
10 1
5 3
150 7

Sample Output

0
1
1
0
1
1
1
---------------------------------------------------------分割线-------------------------------------------------------------
题意:
给定一个长度为n的序列。有m次询问,每次询问给定两个数sidi一开始站在0这个位置,
每次你可以走不超过di的距离,但你到达的位置的数不能超过si,问能否走到n+1
 

这题的解法好像挺多,比如并查集?但是我还是用线段树了,因为讲师是这么讲的23333
思考对问题模型做一些转化,假如说有连续的长度为di的距离都大于si,那么显然你是无论如何都不能走过去
否则显然你是无论如何都能够走过去的
这样问题就变成了考虑一个长度为n的序列中,最长的连续的不能通行的长度是多少。
但是这个东西对于m次询问我们显然是不能暴力去搞的,这样肯定会T.
那么考虑一下我们是否可以把si的顺序调一下然后很方便的去搞。
因为我们很容易想到一个贪心就是如果我们将si按照从大到小排序时,我们每次只需要在原来的基础上删掉一些选择要走的砖即可
反之若是从小到大排,就是加上一些选择要走的砖
而如果我们再把砖的大小排一下序,那么在进行处理时我们插入砖的顺序就也是单调的了,这样遍历一遍复杂度O(n)
那么下面的问题就是处理插入或是删除以维护序列最长的不能通行的长度了
于是我们可以想到利用线段树维护这个信息,然后将si和序列从大到小或是从小到大排序,这样总复杂度是O((n+m)logn)
具体操作可以看看代码 
 
 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define uint unsigned int
 4 #define ull unsigned long long
 5 using namespace std;
 6 const int maxn = 100010;
 7 struct enkidu {
 8     int di, si, id;
 9 }ask[maxn], a[maxn];
10 struct shiki {
11     int l, r, l_lin, r_lin;
12     int max_cnt;
13 }tree[maxn << 2];
14 //l_lin表示这个区间从左边开始连续的不能走的长度
15 //r_lin表示这个区间从右边开始连续的不能走的长度 
16 int n, m; 
17 int ans[maxn];
18 
19 inline int read() {
20     int x = 0, y = 1;
21     char ch = getchar();
22     while(!isdigit(ch)) {
23         if(ch == '-') y = -1;
24         ch = getchar();
25     }
26     while(isdigit(ch)) {
27         x = (x << 1) + (x << 3) + ch - '0';
28         ch = getchar();
29     }
30     return x * y;
31 }
32 
33 inline bool cmp(enkidu a, enkidu b) {
34     return a.si < b.si;
35 }
36 
37 inline void maintain(int pos) {
38     int ls = pos << 1, rs = pos << 1 | 1;
39     if(tree[ls].r - tree[ls].l + 1 == tree[ls].max_cnt) 
40         tree[pos].l_lin = tree[ls].max_cnt + tree[rs].l_lin;
41     else tree[pos].l_lin = tree[ls].l_lin;
42     if(tree[rs].r - tree[rs].l + 1 == tree[rs].max_cnt)
43         tree[pos].r_lin = tree[ls].r_lin + tree[rs].max_cnt;
44     else tree[pos].r_lin = tree[rs].r_lin;
45     tree[pos].max_cnt = max(tree[ls].max_cnt, tree[rs].max_cnt);
46     tree[pos].max_cnt = max(tree[pos].max_cnt, tree[ls].r_lin + tree[rs].l_lin);
47 }
48 
49 void build(int pos, int l, int r) {
50     tree[pos].l = l, tree[pos].r = r; 
51     if(l == r) {
52         tree[pos].l_lin = tree[pos].r_lin = tree[pos].max_cnt = 1;
53         return;
54     }
55     int  mid = l + r >> 1;
56     build(pos << 1, l, mid);
57     build(pos << 1 | 1, mid + 1, r);
58     maintain-(pos);
59 }
60 
61 void update(int pos, int aim, int l, int r) {
62     if(l == r && l == aim) {
63         tree[pos].max_cnt = tree[pos].l_lin = tree[pos].r_lin = 0;
64         return; 
65     }
66     int mid = l + r >> 1;
67     if(aim <= mid) update(pos << 1, aim, l, mid);
68     else update(pos << 1 | 1, aim, mid + 1, r);
69     maintain(pos);
70 }
71 
72 int main() {
73     n = read(), m = read();
74     for(int i = 1; i <= n; ++i) {
75         a[i].si = read();
76         a[i].id = i;
77     }
78     for(int i = 1; i <= m; ++i) {
79         ask[i].si = read(), ask[i].di = read();
80         ask[i].id = i;
81     }
82     sort(ask + 1, ask + m + 1, cmp);
83     sort(a + 1, a + n + 1, cmp);
84     build(1, 1, n);
85     int brick = 0;//砖,表示第几块砖
86     for(int i = 1; i <= m; ++i) {
87         while(brick < n && a[brick + 1].si <= ask[i].si) {//如果可以走 
88             brick++;
89             update(1, a[brick].id, 1, n);
90         }
91         if(tree[1].max_cnt < ask[i].di) ans[ask[i].id] = 1;
92     } 
93     for(int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
94     return 0;
95 }

猜你喜欢

转载自www.cnblogs.com/ywjblog/p/10388749.html