版本比较
[JOISC2017] 做题记录
正文
增加 72 行删除 72 行
23 行未改动24252627282930313233348 行未改动43444546474849505152535455565758596061626364654 行未改动707172737475767778798081827 行未改动9091929394959697984 行未改动1031041051061071081091101115 行未改动11711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916011 行未改动17217317417517617717817918018 行未改动1992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362375 行未改动2432442452462472482492502512522532542552562572582592606 行未改动2672682692702712722732742758 行未改动2842852862872882892902912928 行未改动3013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403415 行未改动34734834935035135235335435535635735835936036136236336436536636736836937037137237337437537620 行未改动39739839940040140240340440540640740840941041141241341441541641741841942042142242342442512 行未改动43843944044144244344444544644744844931 行未改动48148248348448548648748848917 行未改动5075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425439 行未改动5535545555565575585595605612 行未改动5645655665675685695705715725735743 行未改动5785795805815825835845855866 行未改动5935945955965975985996006016026036046056066076086 行未改动23 行未改动```cpp#define int long longconst int maxn = 605;int r, c, n, ans, L;struct Point{ int x, y; } a[maxn];inline bool operator < (const Point& x, const Point& y) { return x.x < y.x; }struct Queue{8 行未改动int sta[maxn], top;inline int calc(int l){int res = c + c, p1 = 1, p2 = 1, p3; top = 0;while (p1 <= n || p2 <= n){if (p1 <= n && (p2 > n || a[p1].x <= a[p2].x + l)) sta[++top] = a[p1++].x;else sta[++top] = a[p2++].x + l + 1;}top = unique(sta + 1, sta + top + 1) - sta;p1 = p2 = p3 = 1; while (p1 <= top){while (p2 <= n && a[p2].x + l < sta[p1]) ++p2;while (p3 < n && a[p3 + 1].x <= sta[p1]) ++p3;if (p2 > p3) tmp[p1] = {0, c, 0}; else tmp[p1] = s[p2][p3];++p1;}q1.init(); q2.init(); q3.init();for (int i = 1, j = 1; i <= top; ++i){while (j <= top && sta[i] + r > sta[j]) { q1.push(j, tmp[j].f); q2.push(j, tmp[j].g); q3.push(j, tmp[j].h); ++j; }if (j > top) break;4 行未改动}signed main(){read(r, c, n); ans = r + c;rep(i, 1, n) read(a[i].x, a[i].y); sort(a + 1, a + n + 1);rep(i, 1, n){vector<int> b; b.reserve(n);rep(j, i, n){b.insert(lower_bound(b.begin(), b.end(), a[j].y), a[j].y);s[i][j] = {b[0] - 1, 0, c - b.back()};7 行未改动if (a[i].x - a[j].x - 1 > L) chkmn(ans, a[i].x - a[j].x - 1 + calc(a[i].x - a[j].x - 1));if (a[i].x - 1 + r - a[j].x > L) chkmn(ans, a[i].x - 1 + r - a[j].x + calc(a[i].x - 1 + r - a[j].x));}write(ans); pc(0xa);return 0;}```### [P14383 [JOISC 2017] 港口设施 / Port Facility - 洛谷](https://www.luogu.com.cn/problem/P14383)4 行未改动这样做是 $O(n^2)$ 的,能拿到 $22\text{pts}$ 的好成绩。```cppconst int maxn = 2005, mod = 1e9 + 7;int n; vector<int> e[maxn];struct Seg{ int l, r; } a[maxn];int vis[maxn], cnt, flg = 1;5 行未改动vis[u] = vis[fa] ^ 1;for (int v : e[u]){if (!~vis[v]) dfs(v, u);else flg&= (vis[u] ^ vis[v]);}}int main(){read(n); rep(i, 1, n) read(a[i].l, a[i].r);rep(i, 1, n) rep(j, i + 1, n) if (chk(i, j)) e[i].pbk(j), e[j].pbk(i);rep(i, 1, n) vis[i] = -1;rep(i, 1, n) if (!~vis[i]) ++cnt, dfs(i, 0);write(flg ? qpow(2, cnt) : 0); pc(0xa);return 0;}```考虑按右端点从小到大扫描所有线段,并维护右端点大于目前扫描区间的线段集合 $S$,那么当前扫描到的端点对应线段应该和 $S$ 中连续一段线段连边。考虑维护 $\text{nxt}_i$ 数组记录 $i$ 所在同色块能延伸到的右端点,修改时暴力访问,用并查集可以做到 $O(n\alpha(n))$。```cppconst int maxn = 2000005, mod = 1e9 + 7;int n; vector<int> e[maxn];int a[maxn], lst[maxn], sta[maxn], top;int vis[maxn], cnt, flg = 1;inline ll qpow(ll x, ll y) { ll z = 1; for (; y; y>>= 1, x = x * x % mod) if (y & 1) z = z * x % mod; return z; }int fa[maxn], nxt[maxn];inline int find(int x) { return (x ^ fa[x]) ? fa[x] = find(fa[x]) : x; }inline void dfs(int u, int ff){vis[u] = vis[ff] ^ 1;for (int v : e[u]){if (!~vis[v]) dfs(v, u);else flg&= (vis[u] ^ vis[v]);}}int main()11 行未改动}}rep(i, 1, n) if (!~vis[i]) dfs(i, 0), ++cnt;write(flg ? qpow(2, cnt) : 0); pc(0xa);return 0;}```### [P14384 [JOISC 2017] 烟花棒 / Sparklers - 洛谷](https://www.luogu.com.cn/problem/P14384)18 行未改动ll b[maxn], sumb[maxn], limb[maxn]; int cntb, posb[maxn], totb;inline bool chk(ll v){int p; ll sum, lim;t = T * v * 2; cnta = cntb = tota = totb = 0;fep(i, K - 1, 1) a[++cnta] = X[i + 1] - X[i];rep(i, K + 1, n) b[++cntb] = X[i] - X[i - 1];p = 1; sum = 0; while (p <= cnta) { sum = -a[p] + t; while (p < cnta && sum < 0) sum+= -a[++p] + t; posa[++tota] = p++; suma[tota] = sum; }p = 1; sum = 0; while (p <= cntb) { sum = -b[p] + t; while (p < cntb && sum < 0) sum+= -b[++p] + t; posb[++totb] = p++; sumb[totb] = sum; }rep(i, 1, tota) { lim = sum = 0; rep(j, posa[i - 1] + 1, posa[i]) chkmx(lim, (sum+= a[j])), sum-= t; lima[i] = lim; }rep(i, 1, totb) { lim = sum = 0; rep(j, posb[i - 1] + 1, posb[i]) chkmx(lim, (sum+= b[j])), sum-= t; limb[i] = lim; }ll cur = t; int pa, pb; pa = pb = 1;while (pa <= tota && suma[pa] >= 0 && pb <= totb && sumb[pb] >= 0){if (lima[pa] <= cur && limb[pb] <= cur){if (suma[pa] > sumb[pb]) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= a[i]; if (cur < 0) return 0; cur+= t; } ++pa; }else { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= b[i]; if (cur < 0) return 0; cur+= t; } ++pb; }}else if (lima[pa] <= cur) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= a[i]; if (cur < 0) return 0; cur+= t; } ++pa; }else if (limb[pb] <= cur) { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= b[i]; if (cur < 0) return 0; cur+= t; } ++pb; }else return 0;}while (pa <= tota && suma[pa] >= 0) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= a[i]; if (cur < 0) return 0; cur+= t; } ++pa; }while (pb <= totb && sumb[pb] >= 0) { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= b[i]; if (cur < 0) return 0; cur+= t; } ++pb; }ll fnl = n * t - (X[n] - X[1]);reverse(a + 1, a + cnta + 1); cnta = cnta - posa[pa - 1]; pa = 1; tota = 0;reverse(b + 1, b + cntb + 1); cntb = cntb - posb[pb - 1]; pb = 1; totb = 0;p = 1; sum = 0; while (p <= cnta) { sum = a[p] - t; while (p < cnta && sum < 0) sum+= a[++p] - t; posa[++tota] = p++; suma[tota] = sum; }p = 1; sum = 0; while (p <= cntb) { sum = b[p] - t; while (p < cntb && sum < 0) sum+= b[++p] - t; posb[++totb] = p++; sumb[totb] = sum; }rep(i, 1, tota) { lim = sum = 0; rep(j, posa[i - 1] + 1, posa[i]) chkmx(lim, (sum+= t)), sum-= a[j]; lima[i] = lim; }rep(i, 1, totb) { lim = sum = 0; rep(j, posb[i - 1] + 1, posb[i]) chkmx(lim, (sum+= t)), sum-= b[j]; limb[i] = lim; }cur = fnl;while (pa <= tota && pb <= totb){if (lima[pa] <= cur && limb[pb] <= cur){5 行未改动else return 0;}while (pa <= tota) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= t; if (cur < 0) return 0; cur+= a[i]; } ++pa; }while (pb <= totb) { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= t; if (cur < 0) return 0; cur+= b[i]; } ++pb; }return 1;}signed main(){read(n, K, T); rep(i, 1, n) read(X[i]);ll l = 0, r = inf, mid, res;while (l <= r) { chk(mid = l + r >> 1) ? r = mid - 1, res = mid : l = mid + 1; }write(res); pc(0xa);return 0;}```### [P14385 [JOISC 2017] 门票安排 / Arranging Tickets - 洛谷](https://www.luogu.com.cn/problem/P14385)6 行未改动设确定要反转的区间 $[l,r]$ 的集合为 $S$,发现如果 $S$ 中存在两个区间没有交集,那么这两个区间不反转不劣。> 证明:>> 如果存在区间 $[l_1,r_1]\cup[l_2,r_2]=\emptyset$,那么反转后会在 $([1,l_1)\cup(r_1,n])\cap([1,l_2)\cup(r_2,n])$ 的加 $2$,其余地方加 $1$,显然不优于不反转。于是 $S$ 中所有区间的交一定不为空,即存在一个点被所有 $S$ 中的区间覆盖。8 行未改动设 $p$ 点为 $S$ 中所有区间交集中的一点,于是有:$$b_i=\begin{cases}a_i-c_i+(c_p-c_i), &i<p\\a_i-c_i, &i=p\\a_i-c_i+(c_p-c_i), &i>p\end{cases}$$8 行未改动观察 [题解](https://www2.ioi-jp.org/camp/2017/2017-sp-tasks/2017-sp-d2-arranging_tickets-review.pdf) 发现,一定存在一组最优解,使得 $b_p\in\{\max b_i,\max b_i-1\}$。> 证明:>> 假设 $S$ 中所有区间的交为 $[l,r]$,考虑不在这个范围内的点 $i$,如果 $b_i\ge b_p+2$,那么由于一定存在 $S$ 中一个区间以 $l$ 为左端点,另一个区间以$r$ 为右端点。把这两个区间从 $S$ 中去除后,$b_p+2\le b_i$,$b_i$ 至少 $-1$。重复该操作,最终 $[l',r']$ 中一定有一个 $b_p\in \{\max b_i,\max b_i-1\}$。于是枚举 $c_p$ 等同于枚举 $b_p$ 只需要枚举 $O(1)$ 个。可以得到 $O(n^2\log n\log V)$ 的做法。再观察 [题解](https://www2.ioi-jp.org/camp/2017/2017-sp-tasks/2017-sp-d2-arranging_tickets-review.pdf) 发现,一定种最优的选择,使得 $a_p=\max a_i$。> 证明:>> 考虑对不在 $[l,r]$ 中的 $i$,有 $c_i\le c_p$。因为 $b_p\ge b_i-1$,于是 $c_i-b_p+b_i-c_p\le 1$。>> 考虑 $b_i=a_i-2c_i+c_p,b_p=a_p-c_p$,因此:>> $$> \begin{split}> c_i-a_p+c_p+a_i-2c_i+c_p-c_p &\le 1\\> -c_i-a_p+a_i+c_p &\le 1\\> a_i &\le a_p+c_i-c_p+1> \end{split}> $$>> 由于 $i$ 不在 $[l,r]$ 中,所以 $c_i<c_p$,所以 $c_i-c_p+1\le 0$,因此 $a_i\le a_p$ 得证。于是 $p$ 也无需枚举,直接在 $a_p$ 最大的中任意挑一个。时间复杂度 $O(n\log n\log V)$。```cppconst int maxn = 2e5 + 5; const ll inf = 1e18;int n, m, p; vector<pil> line[maxn];ll a[maxn], b[maxn], c[maxn], cnt;struct Line{ int l, r; ll w; }; priority_queue<Line> pq;inline bool operator < (const Line& x, const Line& y) { return x.r < y.r; }5 行未改动{for (auto [r, w] : line[i]) if (r >= p) pq.push({i, r, w});while (pq.size() && cnt < b[i]){auto [l, r, w] = pq.top(); pq.pop();if (w + cnt > b[i]){int nw = min(w, b[i] - cnt); pq.push({l, r, w - nw});cnt+= nw; c[1]+= nw; c[l]-= 2ll * nw; c[r + 1]+= 2ll * nw; break;}cnt+= w; c[1]+= w; c[l]-= 2ll * w; c[r + 1]+= 2ll * w;}if (cnt < b[i]) return 0;}rep(i, 1, n) c[i]+= c[i - 1];rep(i, 1, n) if (c[i] + a[i] > x) return 0; return 1;}int main(){read(n, m); rep(i, 1, m) { int x, y, z; read(x, y, z); if (x > y) swap(x, y); --y; line[x].pbk({y, z}); a[x]+= z; a[y + 1]-= z; }p = 1; rep(i, 2, n) if ((a[i]+= a[i - 1]) > a[p]) p = i;ll l = 0, r = inf, mid, res;while (l <= r) { mid = l + r >> 1; (chk(mid, 0) || chk(mid, 1)) ? r = mid - 1, res = mid : l = mid + 1; }write(res); pc(0xa);return 0;}```### [P14386 [JOISC 2017] 故障机器 / Broken Device - 洛谷](https://www.luogu.com.cn/problem/P14386)20 行未改动时间复杂度 $O(n+q\log n)$。```cppconst int maxn = 1e5 + 5;int n, K, q, v[maxn], l[maxn][20], r[maxn][20], ans;int sta[maxn], top;int main(){read(n, K, q); rep(i, 1, n) read(v[i]);sta[top = 1] = 1; rep(i, 1, n) { while (top > 1 && v[sta[top]] < v[i]) --top; l[i][0] = sta[top]; while (top && v[sta[top]] <= v[i]) --top; sta[++top] = i; }sta[top = 1] = n; fep(i, n, 1) { while (top > 1 && v[sta[top]] < v[i]) --top; r[i][0] = sta[top]; while (top && v[sta[top]] <= v[i]) --top; sta[++top] = i; }rep(t, 0, 18) rep(i, 1, n) l[i][t + 1] = min(l[l[i][t]][t], l[r[i][t]][t]), r[i][t + 1] = max(r[r[i][t]][t], r[l[i][t]][t]);rep(i, 1, q){int a, b; read(a, b); if (a > b) swap(a, b);int p1 = a, p2 = a, n1, n2; ans = 0;fep(t, 19, 0) if (max(r[p1][t], r[p2][t]) < b) { ans+= (1 << t); n1 = min(l[p1][t], l[p2][t]), n2 = max(r[p1][t], r[p2][t]); p1 = n1; p2 = n2; }a = p2, p1 = p2 = b;fep(t, 19, 0) if (min(l[p1][t], l[p2][t]) > a) { ans+= (1 << t); n1 = min(l[p1][t], l[p2][t]), n2 = max(r[p1][t], r[p2][t]); p1 = n1; p2 = n2; }write(ans); pc(0xa);}return 0;}```### [P14388 [JOISC 2017] 长途巴士 / Long Distance Coach - 洛谷](https://www.luogu.com.cn/problem/P14388)12 行未改动$$f_i\leftarrow\min\{f_i,f_{i-1}+W\cdot \text{cst}_i\}$$其中 $\text{cst}_i$ 表示 $i$ 全程坐车需要和的总水量。- 钦定 $i$ 离开,且 $[j,i]$ 在同一段离开,且 $i+1$ 不离开$$f_i\leftarrow \min\left\{f_{j-1}+W\cdot (i-j+1)\cdot k_i+\sum_{k=j}^i C_k\right\}$$31 行未改动read(X, n, m, W, T);rep(i, 1, n) read(S[i]); S[++n] = X;rep(i, 1, m) read(a[i].d, a[i].c), K[i] = -1;sort(a + 1, a + m + 1);rep(i, 1, m) sum[i] = sum[i - 1] + a[i].c;rep(i, 1, n){ll num = S[i] / T, ret = S[i] % T;int id = lower_bound(a + 1, a + m + 1, (Pass){ret, 0}) - a - 1;17 行未改动注意到每把钥匙能无限使用。直接记搜,计算出每个位置能到达的左端点和右端点。均摊 $O(n)$。```cppconst int maxn = 5e5 + 5;int n, q, c[maxn]; vector<int> a[maxn];int lst[maxn], L[maxn], R[maxn];pii f[maxn];inline void ex(int x){if (~f[x].fst) return;int &l = f[x].fst, &r = f[x].sec, flg = 1; l = r = x;while (flg){flg = 0;if (l > 1 && R[l - 1] <= r && (ex(l - 1), chkmn(l, f[l - 1].fst), chkmx(r, f[l - 1].sec), flg = 1, 0));if (r < n && L[r] >= l && (ex(r + 1), chkmn(l, f[r + 1].fst), chkmx(r, f[r + 1].sec), flg = 1, 0));}}int main(){read(n); rep(i, 1, n - 1) read(c[i]);rep(i, 1, n) { int x; read(x); a[i].resize(x); rep(j, 0, x - 1) read(a[i][j]); }rep(i, 1, n) { for (int x : a[i]) lst[x] = i; L[i] = lst[c[i]]; }rep(i, 1, n) lst[i] = n + 1;fep(i, n, 1) { R[i] = lst[c[i]]; for (int x : a[i]) lst[x] = i; }rep(i, 1, n) f[i] = {-1, -1};rep(i, 1, n) ex(i);read(q);rep(i, 1, q) { int x, y; read(x, y); puts(f[x].fst <= y && y <= f[x].sec ? "YES" : "NO"); }return 0;}```### [P14390 [JOISC 2017] 自然公园 / Natural Park - 洛谷](https://www.luogu.com.cn/problem/P14390)9 行未改动时间复杂度 $O(n+\log n+m\log m+q(n+m))$。```cppconst int maxn = 5e4 + 5;int n, m, q, a[maxn], b[maxn];pii st1[maxn][20], st2[maxn][20];inline pii qry1(int l, int r) { if (l > r) return {-1, 0}; int k = log2(r - l + 1); return max(st1[l][k], st1[r - (1 << k) + 1][k]); }inline pii qry2(int l, int r) { if (l > r) return {-1, 0}; int k = log2(r - l + 1); return max(st2[l][k], st2[r - (1 << k) + 1][k]); }2 行未改动inline void divide(int x, int y, int xl, int xr, int yl, int yr, ll& res){if (x == xl) return chkmx(res, k[0][0] + y - yl + 1), chkmx(res, k[0][1] + yr - y + 1), void();if (x == xr) return chkmx(res, k[1][0] + y - yl + 1), chkmx(res, k[1][1] + yr - y + 1), void();if (y == yl) return chkmx(res, k[2][0] + x - xl + 1), chkmx(res, k[2][1] + xr - x + 1), void();if (y == yr) return chkmx(res, k[3][0] + x - xl + 1), chkmx(res, k[3][1] + xr - x + 1), void();auto [v1, p1] = qry1(xl + 1, xr - 1); auto [v2, p2] = qry2(yl + 1, yr - 1); int nx = p1, ny = p2;if (v1 < v2){3 行未改动else { k[2][0] = k1, k[2][1] = k2, k[0][0]+= ny - yl, k[1][0]+= ny - yl; divide(x, y, xl, xr, ny, yr, res); }}else{ll k1 = max({0ll, k[2][0] + nx - xl, k[2][1] + xr - nx}), k2 = max({0ll, k[3][0] + nx - xl, k[3][1] + xr - nx});if (X == nx && x == nx) { return (Y >= y) ? chkmx(res, (ll)Y - yl + k1), void() : chkmx(res, (ll)yr - Y + k2), void(); }if (x <= nx) { k[1][0] = k1, k[1][1] = k2, k[2][1]+= xr - nx, k[3][1]+= xr - nx; divide(x, y, xl, nx, yl, yr, res); }else { k[0][0] = k1, k[0][1] = k2, k[2][0]+= nx - xl, k[3][0]+= nx - xl; divide(x, y, nx, xr, yl, yr, res); }}6 行未改动rep(i, 1, m) read(b[i]), st2[i][0] = {b[i], i}; rep(t, 0, 18) for (int l = 1, r = 1 << t + 1; r <= m; ++l, ++r) st2[l][t + 1] = max(st2[l][t], st2[l + (1 << t)][t]);rep(i, 1, q){read(X, Y); ll ans = 1;mems(k, -0x3f); divide(X, Y + 1, 0, n + 1, 0, m + 1, ans);mems(k, -0x3f); divide(X, Y - 1, 0, n + 1, 0, m + 1, ans);mems(k, -0x3f); divide(X + 1, Y, 0, n + 1, 0, m + 1, ans);mems(k, -0x3f); divide(X - 1, Y, 0, n + 1, 0, m + 1, ans);write(ans - 1); pc(0xa);}return 0;}```### [P14392 [JOISC 2017] 都市 / City - 洛谷](https://www.luogu.com.cn/problem/P14392)6 行未改动23 行未改动24252627282930313233348 行未改动43444546474849505152535455565758596061626364654 行未改动707172737475767778798081827 行未改动9091929394959697984 行未改动1031041051061071081091101115 行未改动11711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916011 行未改动17217317417517617717817918018 行未改动1992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362375 行未改动2432442452462472482492502512522532542552562572582592606 行未改动2672682692702712722732742758 行未改动2842852862872882892902912928 行未改动3013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403415 行未改动34734834935035135235335435535635735835936036136236336436536636736836937037137237337437537620 行未改动39739839940040140240340440540640740840941041141241341441541641741841942042142242342442512 行未改动43843944044144244344444544644744844931 行未改动48148248348448548648748848917 行未改动5075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425439 行未改动5535545555565575585595605612 行未改动5645655665675685695705715725735743 行未改动5785795805815825835845855866 行未改动5935945955965975985996006016026036046056066076086 行未改动23 行未改动```cpp#define int long longconst int maxn = 605;int r, c, n, ans, L;struct Point{ int x, y; } a[maxn];inline bool operator < (const Point& x, const Point& y) { return x.x < y.x; }struct Queue{8 行未改动int sta[maxn], top;inline int calc(int l){int res = c + c, p1 = 1, p2 = 1, p3; top = 0;while (p1 <= n || p2 <= n){if (p1 <= n && (p2 > n || a[p1].x <= a[p2].x + l)) sta[++top] = a[p1++].x;else sta[++top] = a[p2++].x + l + 1;}top = unique(sta + 1, sta + top + 1) - sta;p1 = p2 = p3 = 1; while (p1 <= top){while (p2 <= n && a[p2].x + l < sta[p1]) ++p2;while (p3 < n && a[p3 + 1].x <= sta[p1]) ++p3;if (p2 > p3) tmp[p1] = {0, c, 0}; else tmp[p1] = s[p2][p3];++p1;}q1.init(); q2.init(); q3.init();for (int i = 1, j = 1; i <= top; ++i){while (j <= top && sta[i] + r > sta[j]) { q1.push(j, tmp[j].f); q2.push(j, tmp[j].g); q3.push(j, tmp[j].h); ++j; }if (j > top) break;4 行未改动}signed main(){read(r, c, n); ans = r + c;rep(i, 1, n) read(a[i].x, a[i].y); sort(a + 1, a + n + 1);rep(i, 1, n){vector<int> b; b.reserve(n);rep(j, i, n){b.insert(lower_bound(b.begin(), b.end(), a[j].y), a[j].y);s[i][j] = {b[0] - 1, 0, c - b.back()};7 行未改动if (a[i].x - a[j].x - 1 > L) chkmn(ans, a[i].x - a[j].x - 1 + calc(a[i].x - a[j].x - 1));if (a[i].x - 1 + r - a[j].x > L) chkmn(ans, a[i].x - 1 + r - a[j].x + calc(a[i].x - 1 + r - a[j].x));}write(ans); pc(0xa);return 0;}```### [P14383 [JOISC 2017] 港口设施 / Port Facility - 洛谷](https://www.luogu.com.cn/problem/P14383)4 行未改动这样做是 $O(n^2)$ 的,能拿到 $22\text{pts}$ 的好成绩。```cppconst int maxn = 2005, mod = 1e9 + 7;int n; vector<int> e[maxn];struct Seg{ int l, r; } a[maxn];int vis[maxn], cnt, flg = 1;5 行未改动vis[u] = vis[fa] ^ 1;for (int v : e[u]){if (!~vis[v]) dfs(v, u);else flg&= (vis[u] ^ vis[v]);}}int main(){read(n); rep(i, 1, n) read(a[i].l, a[i].r);rep(i, 1, n) rep(j, i + 1, n) if (chk(i, j)) e[i].pbk(j), e[j].pbk(i);rep(i, 1, n) vis[i] = -1;rep(i, 1, n) if (!~vis[i]) ++cnt, dfs(i, 0);write(flg ? qpow(2, cnt) : 0); pc(0xa);return 0;}```考虑按右端点从小到大扫描所有线段,并维护右端点大于目前扫描区间的线段集合 $S$,那么当前扫描到的端点对应线段应该和 $S$ 中连续一段线段连边。考虑维护 $\text{nxt}_i$ 数组记录 $i$ 所在同色块能延伸到的右端点,修改时暴力访问,用并查集可以做到 $O(n\alpha(n))$。```cppconst int maxn = 2000005, mod = 1e9 + 7;int n; vector<int> e[maxn];int a[maxn], lst[maxn], sta[maxn], top;int vis[maxn], cnt, flg = 1;inline ll qpow(ll x, ll y) { ll z = 1; for (; y; y>>= 1, x = x * x % mod) if (y & 1) z = z * x % mod; return z; }int fa[maxn], nxt[maxn];inline int find(int x) { return (x ^ fa[x]) ? fa[x] = find(fa[x]) : x; }inline void dfs(int u, int ff){vis[u] = vis[ff] ^ 1;for (int v : e[u]){if (!~vis[v]) dfs(v, u);else flg&= (vis[u] ^ vis[v]);}}int main()11 行未改动}}rep(i, 1, n) if (!~vis[i]) dfs(i, 0), ++cnt;write(flg ? qpow(2, cnt) : 0); pc(0xa);return 0;}```### [P14384 [JOISC 2017] 烟花棒 / Sparklers - 洛谷](https://www.luogu.com.cn/problem/P14384)18 行未改动ll b[maxn], sumb[maxn], limb[maxn]; int cntb, posb[maxn], totb;inline bool chk(ll v){int p; ll sum, lim;t = T * v * 2; cnta = cntb = tota = totb = 0;fep(i, K - 1, 1) a[++cnta] = X[i + 1] - X[i];rep(i, K + 1, n) b[++cntb] = X[i] - X[i - 1];p = 1; sum = 0; while (p <= cnta) { sum = -a[p] + t; while (p < cnta && sum < 0) sum+= -a[++p] + t; posa[++tota] = p++; suma[tota] = sum; }p = 1; sum = 0; while (p <= cntb) { sum = -b[p] + t; while (p < cntb && sum < 0) sum+= -b[++p] + t; posb[++totb] = p++; sumb[totb] = sum; }rep(i, 1, tota) { lim = sum = 0; rep(j, posa[i - 1] + 1, posa[i]) chkmx(lim, (sum+= a[j])), sum-= t; lima[i] = lim; }rep(i, 1, totb) { lim = sum = 0; rep(j, posb[i - 1] + 1, posb[i]) chkmx(lim, (sum+= b[j])), sum-= t; limb[i] = lim; }ll cur = t; int pa, pb; pa = pb = 1;while (pa <= tota && suma[pa] >= 0 && pb <= totb && sumb[pb] >= 0){if (lima[pa] <= cur && limb[pb] <= cur){if (suma[pa] > sumb[pb]) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= a[i]; if (cur < 0) return 0; cur+= t; } ++pa; }else { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= b[i]; if (cur < 0) return 0; cur+= t; } ++pb; }}else if (lima[pa] <= cur) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= a[i]; if (cur < 0) return 0; cur+= t; } ++pa; }else if (limb[pb] <= cur) { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= b[i]; if (cur < 0) return 0; cur+= t; } ++pb; }else return 0;}while (pa <= tota && suma[pa] >= 0) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= a[i]; if (cur < 0) return 0; cur+= t; } ++pa; }while (pb <= totb && sumb[pb] >= 0) { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= b[i]; if (cur < 0) return 0; cur+= t; } ++pb; }ll fnl = n * t - (X[n] - X[1]);reverse(a + 1, a + cnta + 1); cnta = cnta - posa[pa - 1]; pa = 1; tota = 0;reverse(b + 1, b + cntb + 1); cntb = cntb - posb[pb - 1]; pb = 1; totb = 0;p = 1; sum = 0; while (p <= cnta) { sum = a[p] - t; while (p < cnta && sum < 0) sum+= a[++p] - t; posa[++tota] = p++; suma[tota] = sum; }p = 1; sum = 0; while (p <= cntb) { sum = b[p] - t; while (p < cntb && sum < 0) sum+= b[++p] - t; posb[++totb] = p++; sumb[totb] = sum; }rep(i, 1, tota) { lim = sum = 0; rep(j, posa[i - 1] + 1, posa[i]) chkmx(lim, (sum+= t)), sum-= a[j]; lima[i] = lim; }rep(i, 1, totb) { lim = sum = 0; rep(j, posb[i - 1] + 1, posb[i]) chkmx(lim, (sum+= t)), sum-= b[j]; limb[i] = lim; }cur = fnl;while (pa <= tota && pb <= totb){if (lima[pa] <= cur && limb[pb] <= cur){5 行未改动else return 0;}while (pa <= tota) { rep(i, posa[pa - 1] + 1, posa[pa]) { cur-= t; if (cur < 0) return 0; cur+= a[i]; } ++pa; }while (pb <= totb) { rep(i, posb[pb - 1] + 1, posb[pb]) { cur-= t; if (cur < 0) return 0; cur+= b[i]; } ++pb; }return 1;}signed main(){read(n, K, T); rep(i, 1, n) read(X[i]);ll l = 0, r = inf, mid, res;while (l <= r) { chk(mid = l + r >> 1) ? r = mid - 1, res = mid : l = mid + 1; }write(res); pc(0xa);return 0;}```### [P14385 [JOISC 2017] 门票安排 / Arranging Tickets - 洛谷](https://www.luogu.com.cn/problem/P14385)6 行未改动设确定要反转的区间 $[l,r]$ 的集合为 $S$,发现如果 $S$ 中存在两个区间没有交集,那么这两个区间不反转不劣。> 证明:>> 如果存在区间 $[l_1,r_1]\cup[l_2,r_2]=\emptyset$,那么反转后会在 $([1,l_1)\cup(r_1,n])\cap([1,l_2)\cup(r_2,n])$ 的加 $2$,其余地方加 $1$,显然不优于不反转。于是 $S$ 中所有区间的交一定不为空,即存在一个点被所有 $S$ 中的区间覆盖。8 行未改动设 $p$ 点为 $S$ 中所有区间交集中的一点,于是有:$$b_i=\begin{cases}a_i-c_i+(c_p-c_i), &i<p\\a_i-c_i, &i=p\\a_i-c_i+(c_p-c_i), &i>p\end{cases}$$8 行未改动观察 [题解](https://www2.ioi-jp.org/camp/2017/2017-sp-tasks/2017-sp-d2-arranging_tickets-review.pdf) 发现,一定存在一组最优解,使得 $b_p\in\{\max b_i,\max b_i-1\}$。> 证明:>> 假设 $S$ 中所有区间的交为 $[l,r]$,考虑不在这个范围内的点 $i$,如果 $b_i\ge b_p+2$,那么由于一定存在 $S$ 中一个区间以 $l$ 为左端点,另一个区间以$r$ 为右端点。把这两个区间从 $S$ 中去除后,$b_p+2\le b_i$,$b_i$ 至少 $-1$。重复该操作,最终 $[l',r']$ 中一定有一个 $b_p\in \{\max b_i,\max b_i-1\}$。于是枚举 $c_p$ 等同于枚举 $b_p$ 只需要枚举 $O(1)$ 个。可以得到 $O(n^2\log n\log V)$ 的做法。再观察 [题解](https://www2.ioi-jp.org/camp/2017/2017-sp-tasks/2017-sp-d2-arranging_tickets-review.pdf) 发现,一定种最优的选择,使得 $a_p=\max a_i$。> 证明:>> 考虑对不在 $[l,r]$ 中的 $i$,有 $c_i\le c_p$。因为 $b_p\ge b_i-1$,于是 $c_i-b_p+b_i-c_p\le 1$。>> 考虑 $b_i=a_i-2c_i+c_p,b_p=a_p-c_p$,因此:>> $$> \begin{split}> c_i-a_p+c_p+a_i-2c_i+c_p-c_p &\le 1\\> -c_i-a_p+a_i+c_p &\le 1\\> a_i &\le a_p+c_i-c_p+1> \end{split}> $$>> 由于 $i$ 不在 $[l,r]$ 中,所以 $c_i<c_p$,所以 $c_i-c_p+1\le 0$,因此 $a_i\le a_p$ 得证。于是 $p$ 也无需枚举,直接在 $a_p$ 最大的中任意挑一个。时间复杂度 $O(n\log n\log V)$。```cppconst int maxn = 2e5 + 5; const ll inf = 1e18;int n, m, p; vector<pil> line[maxn];ll a[maxn], b[maxn], c[maxn], cnt;struct Line{ int l, r; ll w; }; priority_queue<Line> pq;inline bool operator < (const Line& x, const Line& y) { return x.r < y.r; }5 行未改动{for (auto [r, w] : line[i]) if (r >= p) pq.push({i, r, w});while (pq.size() && cnt < b[i]){auto [l, r, w] = pq.top(); pq.pop();if (w + cnt > b[i]){int nw = min(w, b[i] - cnt); pq.push({l, r, w - nw});cnt+= nw; c[1]+= nw; c[l]-= 2ll * nw; c[r + 1]+= 2ll * nw; break;}cnt+= w; c[1]+= w; c[l]-= 2ll * w; c[r + 1]+= 2ll * w;}if (cnt < b[i]) return 0;}rep(i, 1, n) c[i]+= c[i - 1];rep(i, 1, n) if (c[i] + a[i] > x) return 0; return 1;}int main(){read(n, m); rep(i, 1, m) { int x, y, z; read(x, y, z); if (x > y) swap(x, y); --y; line[x].pbk({y, z}); a[x]+= z; a[y + 1]-= z; }p = 1; rep(i, 2, n) if ((a[i]+= a[i - 1]) > a[p]) p = i;ll l = 0, r = inf, mid, res;while (l <= r) { mid = l + r >> 1; (chk(mid, 0) || chk(mid, 1)) ? r = mid - 1, res = mid : l = mid + 1; }write(res); pc(0xa);return 0;}```### [P14386 [JOISC 2017] 故障机器 / Broken Device - 洛谷](https://www.luogu.com.cn/problem/P14386)20 行未改动时间复杂度 $O(n+q\log n)$。```cppconst int maxn = 1e5 + 5;int n, K, q, v[maxn], l[maxn][20], r[maxn][20], ans;int sta[maxn], top;int main(){read(n, K, q); rep(i, 1, n) read(v[i]);sta[top = 1] = 1; rep(i, 1, n) { while (top > 1 && v[sta[top]] < v[i]) --top; l[i][0] = sta[top]; while (top && v[sta[top]] <= v[i]) --top; sta[++top] = i; }sta[top = 1] = n; fep(i, n, 1) { while (top > 1 && v[sta[top]] < v[i]) --top; r[i][0] = sta[top]; while (top && v[sta[top]] <= v[i]) --top; sta[++top] = i; }rep(t, 0, 18) rep(i, 1, n) l[i][t + 1] = min(l[l[i][t]][t], l[r[i][t]][t]), r[i][t + 1] = max(r[r[i][t]][t], r[l[i][t]][t]);rep(i, 1, q){int a, b; read(a, b); if (a > b) swap(a, b);int p1 = a, p2 = a, n1, n2; ans = 0;fep(t, 19, 0) if (max(r[p1][t], r[p2][t]) < b) { ans+= (1 << t); n1 = min(l[p1][t], l[p2][t]), n2 = max(r[p1][t], r[p2][t]); p1 = n1; p2 = n2; }a = p2, p1 = p2 = b;fep(t, 19, 0) if (min(l[p1][t], l[p2][t]) > a) { ans+= (1 << t); n1 = min(l[p1][t], l[p2][t]), n2 = max(r[p1][t], r[p2][t]); p1 = n1; p2 = n2; }write(ans); pc(0xa);}return 0;}```### [P14388 [JOISC 2017] 长途巴士 / Long Distance Coach - 洛谷](https://www.luogu.com.cn/problem/P14388)12 行未改动$$f_i\leftarrow\min\{f_i,f_{i-1}+W\cdot \text{cst}_i\}$$其中 $\text{cst}_i$ 表示 $i$ 全程坐车需要和的总水量。- 钦定 $i$ 离开,且 $[j,i]$ 在同一段离开,且 $i+1$ 不离开$$f_i\leftarrow \min\left\{f_{j-1}+W\cdot (i-j+1)\cdot k_i+\sum_{k=j}^i C_k\right\}$$31 行未改动read(X, n, m, W, T);rep(i, 1, n) read(S[i]); S[++n] = X;rep(i, 1, m) read(a[i].d, a[i].c), K[i] = -1;sort(a + 1, a + m + 1);rep(i, 1, m) sum[i] = sum[i - 1] + a[i].c;rep(i, 1, n){ll num = S[i] / T, ret = S[i] % T;int id = lower_bound(a + 1, a + m + 1, (Pass){ret, 0}) - a - 1;17 行未改动注意到每把钥匙能无限使用。直接记搜,计算出每个位置能到达的左端点和右端点。均摊 $O(n)$。```cppconst int maxn = 5e5 + 5;int n, q, c[maxn]; vector<int> a[maxn];int lst[maxn], L[maxn], R[maxn];pii f[maxn];inline void ex(int x){if (~f[x].fst) return;int &l = f[x].fst, &r = f[x].sec, flg = 1; l = r = x;while (flg){flg = 0;if (l > 1 && R[l - 1] <= r && (ex(l - 1), chkmn(l, f[l - 1].fst), chkmx(r, f[l - 1].sec), flg = 1, 0));if (r < n && L[r] >= l && (ex(r + 1), chkmn(l, f[r + 1].fst), chkmx(r, f[r + 1].sec), flg = 1, 0));}}int main(){read(n); rep(i, 1, n - 1) read(c[i]);rep(i, 1, n) { int x; read(x); a[i].resize(x); rep(j, 0, x - 1) read(a[i][j]); }rep(i, 1, n) { for (int x : a[i]) lst[x] = i; L[i] = lst[c[i]]; }rep(i, 1, n) lst[i] = n + 1;fep(i, n, 1) { R[i] = lst[c[i]]; for (int x : a[i]) lst[x] = i; }rep(i, 1, n) f[i] = {-1, -1};rep(i, 1, n) ex(i);read(q);rep(i, 1, q) { int x, y; read(x, y); puts(f[x].fst <= y && y <= f[x].sec ? "YES" : "NO"); }return 0;}```### [P14390 [JOISC 2017] 自然公园 / Natural Park - 洛谷](https://www.luogu.com.cn/problem/P14390)9 行未改动时间复杂度 $O(n+\log n+m\log m+q(n+m))$。```cppconst int maxn = 5e4 + 5;int n, m, q, a[maxn], b[maxn];pii st1[maxn][20], st2[maxn][20];inline pii qry1(int l, int r) { if (l > r) return {-1, 0}; int k = log2(r - l + 1); return max(st1[l][k], st1[r - (1 << k) + 1][k]); }inline pii qry2(int l, int r) { if (l > r) return {-1, 0}; int k = log2(r - l + 1); return max(st2[l][k], st2[r - (1 << k) + 1][k]); }2 行未改动inline void divide(int x, int y, int xl, int xr, int yl, int yr, ll& res){if (x == xl) return chkmx(res, k[0][0] + y - yl + 1), chkmx(res, k[0][1] + yr - y + 1), void();if (x == xr) return chkmx(res, k[1][0] + y - yl + 1), chkmx(res, k[1][1] + yr - y + 1), void();if (y == yl) return chkmx(res, k[2][0] + x - xl + 1), chkmx(res, k[2][1] + xr - x + 1), void();if (y == yr) return chkmx(res, k[3][0] + x - xl + 1), chkmx(res, k[3][1] + xr - x + 1), void();auto [v1, p1] = qry1(xl + 1, xr - 1); auto [v2, p2] = qry2(yl + 1, yr - 1); int nx = p1, ny = p2;if (v1 < v2){3 行未改动else { k[2][0] = k1, k[2][1] = k2, k[0][0]+= ny - yl, k[1][0]+= ny - yl; divide(x, y, xl, xr, ny, yr, res); }}else{ll k1 = max({0ll, k[2][0] + nx - xl, k[2][1] + xr - nx}), k2 = max({0ll, k[3][0] + nx - xl, k[3][1] + xr - nx});if (X == nx && x == nx) { return (Y >= y) ? chkmx(res, (ll)Y - yl + k1), void() : chkmx(res, (ll)yr - Y + k2), void(); }if (x <= nx) { k[1][0] = k1, k[1][1] = k2, k[2][1]+= xr - nx, k[3][1]+= xr - nx; divide(x, y, xl, nx, yl, yr, res); }else { k[0][0] = k1, k[0][1] = k2, k[2][0]+= nx - xl, k[3][0]+= nx - xl; divide(x, y, nx, xr, yl, yr, res); }}6 行未改动rep(i, 1, m) read(b[i]), st2[i][0] = {b[i], i}; rep(t, 0, 18) for (int l = 1, r = 1 << t + 1; r <= m; ++l, ++r) st2[l][t + 1] = max(st2[l][t], st2[l + (1 << t)][t]);rep(i, 1, q){read(X, Y); ll ans = 1;mems(k, -0x3f); divide(X, Y + 1, 0, n + 1, 0, m + 1, ans);mems(k, -0x3f); divide(X, Y - 1, 0, n + 1, 0, m + 1, ans);mems(k, -0x3f); divide(X + 1, Y, 0, n + 1, 0, m + 1, ans);mems(k, -0x3f); divide(X - 1, Y, 0, n + 1, 0, m + 1, ans);write(ans - 1); pc(0xa);}return 0;}```### [P14392 [JOISC 2017] 都市 / City - 洛谷](https://www.luogu.com.cn/problem/P14392)6 行未改动