【CF】474E Pillars

时间:2024-08-27 09:07:32

H的范围是10^15,DP方程很容易想到。但是因为H的范围太大了,而n的范围还算可以接受。因此,对高度排序排重后。使用新的索引建立线段树,使用线段树查询当前高度区间内的最大值,以及该最大值的前趋索引。线段树中的结点索引一定满足i<j的条件,因为采用从n向1更新线段树结点。每次线段树查询操作就可以得到argmax(dp[L, R]),很据不等式很容易得到L和R的范围。

 /* 474E */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std; #define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
__int64 mx;
int fa;
} node_t; const int maxn = 1e5+;
node_t nd[maxn<<];
__int64 h[maxn], hh[maxn];
int fa[maxn], dp[maxn]; void PushUp(int rt) {
int lb = rt<<;
int rb = rt<<|; if (nd[lb].mx >= nd[rb].mx) {
nd[rt].fa = nd[lb].fa;
nd[rt].mx = nd[lb].mx;
} else {
nd[rt].fa = nd[rb].fa;
nd[rt].mx = nd[rb].mx;
}
} void build(int l, int r, int rt) {
nd[rt].mx = -;
nd[rt].fa = ;
if (l == r)
return ;
int mid = (l+r)>>;
build(lson);
build(rson);
} void update(int x, int in, int l, int r, int rt) {
if (l == r) {
if (nd[rt].mx < dp[in]) {
nd[rt].mx = dp[in];
nd[rt].fa = in;
}
return ;
}
int mid = (l+r)>>;
if (x <= mid)
update(x, in, lson);
else
update(x, in, rson);
PushUp(rt);
} node_t query(int L, int R, int l, int r, int rt) {
node_t d;
if (L<=l && R>=r)
return nd[rt];
int mid = (l+r)>>;
if (R <= mid) {
return query(L, R, lson);
} else if (L > mid) {
return query(L, R, rson);
} else {
node_t ln = query(L, R, lson);
node_t rn = query(L, R, rson);
return rn.mx>ln.mx ? rn:ln;
}
} int main() {
int i, j, k;
int n, m, d;
int ans, v;
int l, r;
node_t node;
__int64 tmp; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d %d", &n, &d);
for (i=; i<=n; ++i) {
scanf("%I64d", &h[i]);
hh[i] = h[i];
}
sort(h+, h++n);
m = unique(h+, h++n) - (h+);
build(, m, ); for (i=n; i>; --i) {
dp[i] = ;
fa[i] = ; tmp = hh[i] + d;
if (tmp <= h[m]) {
l = lower_bound(h+, h++m, tmp) - h;
node = query(l, m, , m, );
if (node.mx+ > dp[i]) {
dp[i] = node.mx + ;
fa[i] = node.fa;
}
} tmp = hh[i] - d;
if (tmp >= h[]) {
r = m+;
if (tmp < h[m])
r = upper_bound(h+, h++m, tmp) - h;
node = query(, r-, , m, );
if (node.mx+ > dp[i]) {
dp[i] = node.mx + ;
fa[i] = node.fa;
}
} l = lower_bound(h+, h++m, hh[i]) - h;
update(l, i, , m, );
} ans = dp[];
v = ;
for (i=; i<=n; ++i) {
if (dp[i] > ans) {
ans = dp[i];
v = i;
}
} printf("%d\n", ans);
printf("%d", v);
while (fa[v]) {
v = fa[v];
printf(" %d", v);
}
putchar('\n'); #ifndef ONLINE_JUDGE
printf("%d\n", (int)clock());
#endif return ;
}