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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| int n, m; int st; int a[500059], b[500050], f[500050];
struct data { int mx, mx2, mn, mn2, cmx, cmn, tmx, tmn, tad; int sum; }; data t[4000050];
void pushup(int u) { const int lu = u << 1, ru = u << 1 | 1; t[u].sum = t[lu].sum + t[ru].sum; if (t[lu].mx == t[ru].mx) { t[u].mx = t[lu].mx, t[u].cmx = t[lu].cmx + t[ru].cmx; t[u].mx2 = max(t[lu].mx2, t[ru].mx2); } else if (t[lu].mx > t[ru].mx) { t[u].mx = t[lu].mx, t[u].cmx = t[lu].cmx; t[u].mx2 = max(t[lu].mx2, t[ru].mx); } else { t[u].mx = t[ru].mx, t[u].cmx = t[ru].cmx; t[u].mx2 = max(t[lu].mx, t[ru].mx2); } if (t[lu].mn == t[ru].mn) { t[u].mn = t[lu].mn, t[u].cmn = t[lu].cmn + t[ru].cmn; t[u].mn2 = min(t[lu].mn2, t[ru].mn2); } else if (t[lu].mn < t[ru].mn) { t[u].mn = t[lu].mn, t[u].cmn = t[lu].cmn; t[u].mn2 = min(t[lu].mn2, t[ru].mn); } else { t[u].mn = t[ru].mn, t[u].cmn = t[ru].cmn; t[u].mn2 = min(t[lu].mn, t[ru].mn2); } } void push_add(int u, int l, int r, int v) { t[u].sum += (r - l + 1ll) * v; t[u].mx += v, t[u].mn += v; if (t[u].mx2 != -INF) t[u].mx2 += v; if (t[u].mn2 != INF) t[u].mn2 += v; if (t[u].tmx != -INF) t[u].tmx += v; if (t[u].tmn != INF) t[u].tmn += v; t[u].tad += v; } void push_min(int u, int tg) { if (t[u].mx <= tg) return; t[u].sum += (tg * 1ll - t[u].mx) * t[u].cmx; if (t[u].mn2 == t[u].mx) t[u].mn2 = tg; if (t[u].mn == t[u].mx) t[u].mn = tg; if (t[u].tmx > tg) t[u].tmx = tg; t[u].mx = tg, t[u].tmn = tg; } void push_max(int u, int tg) { if (t[u].mn > tg) return; t[u].sum += (tg * 1ll - t[u].mn) * t[u].cmn; if (t[u].mx2 == t[u].mn) t[u].mx2 = tg; if (t[u].mx == t[u].mn) t[u].mx = tg; if (t[u].tmn < tg) t[u].tmn = tg; t[u].mn = tg, t[u].tmx = tg; } void pushdown(int u, int l, int r) { const int lu = u << 1, ru = u << 1 | 1, mid = (l + r) >> 1; if (t[u].tad) push_add(lu, l, mid, t[u].tad), push_add(ru, mid + 1, r, t[u].tad); if (t[u].tmx != -INF) push_max(lu, t[u].tmx), push_max(ru, t[u].tmx); if (t[u].tmn != INF) push_min(lu, t[u].tmn), push_min(ru, t[u].tmn); t[u].tad = 0, t[u].tmx = -INF, t[u].tmn = INF; } void build(int u, int l, int r) { t[u].tmn = INF, t[u].tmx = -INF; if (l == r) { t[u].sum = t[u].mx = t[u].mn = INF; t[u].mx2 = -INF, t[u].mn2 = INF; t[u].cmx = t[u].cmn = 1; return; } int mid = (l + r) >> 1; build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); pushup(u); } void add(int L, int R, int v, int u, int l, int r) { if (R < l || r < L) return; if (L <= l && r <= R) return push_add(u, l, r, v); int mid = (l + r) >> 1; pushdown(u, l, r); add(L, R, v, u << 1, l, mid), add(L, R, v, u << 1 | 1, mid + 1, r); pushup(u); } void tomin(int L, int R, int v, int u, int l, int r) { if (R < l || r < L || t[u].mx <= v) return; if (L <= l && r <= R && t[u].mx2 < v) return push_min(u, v); int mid = (l + r) >> 1; pushdown(u, l, r); tomin(L, R, v, u << 1, l, mid), tomin(L, R, v, u << 1 | 1, mid + 1, r); pushup(u); } void tomax(int L, int R, int v, int u, int l, int r) { if (R < l || r < L || t[u].mn >= v) return; if (L <= l && r <= R && t[u].mn2 > v) return push_max(u, v); int mid = (l + r) >> 1; pushdown(u, l, r); tomax(L, R, v, u << 1, l, mid), tomax(L, R, v, u << 1 | 1, mid + 1, r); pushup(u); } int qsum(int L, int R, int u, int l, int r) { if (R < l || r < L) return 0; if (L <= l && r <= R) return t[u].sum; int mid = (l + r) >> 1; pushdown(u, l, r); return qsum(L, R, u << 1, l, mid) + qsum(L, R, u << 1 | 1, mid + 1, r); } int qmax(int L, int R, int u, int l, int r) { if (R < l || r < L) return -INF; if (L <= l && r <= R) return t[u].mx; int mid = (l + r) >> 1; pushdown(u, l, r); return max(qmax(L, R, u << 1, l, mid), qmax(L, R, u << 1 | 1, mid + 1, r)); } int qmin(int L, int R, int u, int l, int r) { if (R < l || r < L) return INF; if (L <= l && r <= R) return t[u].mn; int mid = (l + r) >> 1; pushdown(u, l, r); return min(qmin(L, R, u << 1, l, mid), qmin(L, R, u << 1 | 1, mid + 1, r)); } void solve() { cin >> n >> m; lep(i, 1, n) cin >> a[i]; lep(i, 1, m) cin >> b[i]; sort(b + 1, b + 1 + m); a[++n] = INF; st = n + 1; build(1, 1, n + m + 1); tomin(st + 1, n + m + 1, 1, 1, 1, n + m + 1); lep(i, 1, n) { if (a[i] > a[i - 1]) f[i] = f[i-1]; else f[i] = INF; int id = lower_bound(b + 1, b + 1 + m, a[i]) - b - 1; f[i] = min(f[i], qmin(st + 1, st + id, 1, 1, n + m + 1)); st--; add(st + 1, st + m, 1, 1, 1, n + m + 1); id = lower_bound(b + 1, b + 1 + m, a[i - 1]) - b - 1; tomin(st + id + 1, st + m, f[i - 1] + 1, 1, 1, n + m + 1); } cout << (f[n] < 1e9 ? f[n] : -1) << endl; }
|