ARC072E Alice in linear land < DP >

Problem

Alice in linear land

Time  limit:  2  Sec\mathrm{Time\;limit:\;2\;Sec}
Memory  limit:  256  MB\mathrm{Memory\;limit:\;256\;MB}

Statement

Alice lives on a line. Today, she will travel to some place in a mysterious vehicle. Initially, the distance between Alice and her destination is DD. When she input a number xx to the vehicle, it will travel in the direction of the destination by a distance of xx if this move would shorten the distance between the vehicle and the destination, and it will stay at its position otherwise. Note that the vehicle may go past the destination when the distance between the vehicle and the destination is less than xx.
Alice made a list of NN numbers. The ithi^{th} number in this list is did_i. She will insert these numbers to the vehicle one by one.
However, a mischievous witch appeared. She is thinking of rewriting one number in the list so that Alice will not reach the destination after NN moves.
She has QQ plans to do this, as follows:
Rewrite only the qithq_i^{th} number in the list with some integer so that Alice will not reach the destination.
Write a program to determine whether each plan is feasible.

Constraints

1N5×1051\le N\le5\times10^5
1Q5×1051\le Q\le5\times10^5
1D1091\le D\le10^9
1di109  (1iN)1\le d_i\le10^9\;(1\le i\le N)
1qiN  (1iQ)1\le q_i\le N\;(1\le i\le Q)
DD and each did_i are integers.

Input

Input is given from Standard Input in the following format:
N  DN\;D
d1  d2    dNd_1\;d_2\;\cdots\;d_N
QQ
q1  q2    qQq_1\;q_2\;\cdots\;q_Q

Output

Print QQ lines. The ithi^{th} line should contain YES if the ithi^{th} plan is feasible, and NO otherwise.

Sample

Input #1

1
2
3
4
4 10
3 4 3 3
2
4 3

Output #1

1
NO

Explanation #1
For the first plan, Alice will already arrive at the destination by the first three moves, and therefore the answer is NO. For the second plan, rewriting the third number in the list with 55 will prevent Alice from reaching the destination as shown in the following figure, and thus the answer is YES.

Input #2

1
2
3
4
5 9
4 4 2 3 2
5
1 4 2 3 5

Output #2

1
2
3
4
5
YES
YES
YES
YES
YES

Explanation #2
Alice will not reach the destination as it is, and therefore all the plans are feasible.
Input #3

1
2
3
4
6 15
4 3 5 4 2 1
6
1 2 3 4 5 6

Output #3

1
2
3
4
5
6
NO
NO
YES
NO
NO
YES

标签:DP

Translation

有一个无限长的数轴,你现在在DD处,有NN条指令,每条指令为一个整数did_i。顺次处理每条指令,如果你当前在xx处,你会走到min(x,xdi)\min(x,|x-d_i|)处。有QQ条询问,对于每条询问,回答能否通过改变dqid_{q_i}的值使得最后不能到达00

Solution

精妙的做法,有点类似构造,又有点像DP\mathrm{DP}

首先处理出数组AA,其中AiA_i表示在没有更改指令的情况下,处理前ii条指令后的位置。那么如果能够修改did_i,我们可以在处理完did_i后到达[0,Ai1][0,A_{i-1}]中的所有位置。我们需要找到一个数组BB,其中BiB_i表示修改并处理完did_i后,至少要在哪个位置才能使处理完后面的指令后不能到达00。显然,如果Aq1BqA_{q-1}\ge B_q,一定能找到可行方案,否则一定不能。

考虑如何得到BB。我们倒序计算BB。首先,显然有Bn=1B_n=1。不难发现BB一定是递减的,于是倒序计算时我们用前面的值作为基础值来处理出现在处理到的BB。对于Bi  (i[1,n))B_i\;(i\in[1,n))BiB_i不小于Bi+1B_{i+1},如果2Bi+1di+12B_{i+1}\le d_{i+1},那么从Bi+1B_{i+1}位置进行di+1d_{i+1}指令一定不会走到更小的点上,因此BiB_i可以取到其最小值Bi+1B_{i+1};否则,需要使其操作di+1d_{i+1}后不小于Bi+1B_{i+1},因此Bi=Bi+1+di+1B_i=B_{i+1}+d_{i+1}

处理出A,BA,B后判断即可。时间复杂度O(N+Q)O(N+Q)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
#define MAX_N 500000
using namespace std;
template <class T> inline void read(T &x) {
x = 0; int c = getchar(), f = 1;
for (; !isdigit(c); c = getchar()) if (c == 45) f = -1;
for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0');
}
int n, d[MAX_N+5], a[MAX_N+5], b[MAX_N+5];
int main() {
read(n), read(a[0]), b[n] = 1;
for (int i = 1; i <= n; i++) read(d[i]);
for (int i = 1; i <= n; i++)
a[i] = min(a[i-1], abs(a[i-1]-d[i]));
for (int i = n-1; i; i--)
if (b[i+1] <= d[i+1]/2) b[i] = b[i+1];
else b[i] = b[i+1]+d[i+1];
int m; read(m);
while (m--) {
int p; read(p);
if (b[p] <= a[p-1]) puts("YES");
else puts("NO");
}
return 0;
}