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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include <bits/stdc++.h> using namespace std; namespace Legendgod { namespace Read {
#ifdef Fread const int Siz = (1 << 21) + 5; char *iS, *iT, buf[Siz]; #define gc() ( iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iS == iT ? EOF : *iS ++) : *iS ++ ) #define getchar gc #endif template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; !isdigit(c); c = getchar()) if(c == '-') f = -1; for(; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } template <typename T, typename...Args> void r1(T &x, Args&...arg) { r1(x), r1(arg...); } #undef getchar }
using namespace Read;
const int maxn = 2e5 + 5; int n, K;
int ls[maxn], rs[maxn], mn[maxn], mx[maxn], vl[maxn], rd[maxn], sz[maxn], rt, tot; void pushup(int p) { mn[p] = min(min(mn[ls[p]], mn[rs[p]]), vl[p]); mx[p] = max(max(mx[ls[p]], mx[rs[p]]), vl[p]); sz[p] = sz[ls[p]] + sz[rs[p]] + 1; }
int news(int v) { int x = ++ tot; vl[x] = mn[x] = mx[x] = v, sz[x] = 1; return rd[x] = rand(), ls[x] = rs[x] = 0, x; } int merge(int x,int y) { if(!x || !y) return x | y; if(rd[x] > rd[y]) return rs[x] = merge(rs[x], y), pushup(x), x; else return ls[y] = merge(x, ls[y]), pushup(y), y; }
void split(int p,int &x,int &y,int K) { if(!p) return x = y = 0, void(); if(K <= sz[ls[p]]) split(ls[p], x, ls[y = p], K); else split(rs[p], rs[x = p], y, K - sz[ls[p]] - 1); pushup(p); }
void move(int i,int ed) { if(i == ed) return ; int a, b, c, d; split(rt, c, d, i), split(c, b, c, i - 1), split(b, a, b, ed - 1); rt = merge(a, merge(c, merge(b, d))); }
void print(int x) { if(!x) return ; print(ls[x]), printf("%d\n", vl[x]), print(rs[x]); }
void Solve() { int i, j; r1(n, K); mn[0] = 1e9; for(i = 1; i <= n; ++ i) { int h, p, ln, a, tot(0); r1(h); p = rt = merge(rt, news(h)); while(p) { int r = rs[p]; if(mx[r] - h > K || h - mn[r] > K) p = r; else if(vl[p] - h > K || h - vl[p] > K) { tot += sz[r]; break; } else tot += sz[r] + 1, p = ls[p]; } if(tot == 1) continue;
ln = i - tot; split(rt, rt, a, ln), p = a, tot = 0; if(mx[a] == h) { rt = merge(rt, a); continue; } while(p) { int l = ls[p]; if(mx[l] > h) p = l; else if(vl[p] > h) { tot += sz[l]; break; } else tot += sz[l] + 1, p = rs[p]; } rt = merge(rt, a), move(i, ln + tot + 1); } print(rt); }
signed main() { int i, j, T(1);
while(T --) Solve(); return 0; }
}
signed main() { return Legendgod::main(), 0; }
|