Surface
Topic Portal
Solution
With bzoj2716
I CDQ write or ugly ah, someone else a dropped me T fly
Code
#include <bits/stdc++.h> #define INF 1 << 30#define N 1000010using namespace Std;template <typename node> ; void Chkmax (node &x, node y) {x = max (x, y);} Template <typename node> void chkmin (node &x, node y) {x = min (x, y);} Template <typename node> void Read (node &x) {x = 0; int f = 1; char c = GetChar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = GetChar ();} while (IsDigit (c)) x = x * ten + C-' 0 ', C = GetChar (); x *= F;} struct Node {int op, x, Y, Tim, id;} a[n], T[n];int mx, f[n], Ans[n];bool CMP1 (Node A, Node B) {return a.x < b.x;} BOOL CMP2 (Node A, Node B) {return a.y > b.y;} BOOL Cmp3 (Node A, Node B) {return a.x > b.x;} int lowbit (int x) {return x & x;} void Modify (int x, int v) {for (int i = x; i <= mx; i + = Lowbit (i)) if (v = =-inf) F[i] = v; else Chkmax (F[i], v);} int query (int x) {int ret =-inf; for (int i = x; i; i-= Lowbit (i)) ret = max (ret, f[i]); return ret;} void CDQ1 (int l, int r) {if (L >= R) return; int mid = (L + r) >> 1, tx = L, Ty = mid + 1; for (int i = l; I <= R; i++) if (A[i].tim <= mid) t[tx++] = A[i]; else t[ty++] = a[i]; for (int i = l; I <= R; i++) A[i] = T[i]; tx = L, Ty = mid + 1; while (Ty <= R) {while (TX <= mid && a[tx].x <= a[ty].x) {if (A[tx].op = = 1) Modify (a [Tx].y, a[tx].x + a[tx].y); tx++; } if (A[ty].op = = 2) chkmin (Ans[a[ty].id], a[ty].x + a[ty].y-query (a[ty].y)); ty++; } for (int i = l; i < TX; i++) if (A[i].op = = 1) modify (a[i].y,-inf); CDQ1 (L, mid); CDQ1 (mid + 1, r);} void cdq2 (int l, int r) {if (L >= R) return; int mid = (L + r) >> 1, tx = L, Ty = mid + 1; for (int i = l; I <= R; i++) if (A[i].tim <= mid) t[tx++] = A[i]; else t[ty++] = a[i]; for (int i = l; I <= R; i++) A[i] = T[i]; tx = L, Ty = mid + 1; while (Ty <= r) { while (TX <= mid && a[tx].y >= a[ty].y) {if (A[tx].op = = 1) modify (a[tx].x, A[tx].x-a[tx]. y); tx++; } if (A[ty].op = = 2) chkmin (Ans[a[ty].id], a[ty].x-a[ty].y-query (a[ty].x)); ty++; } for (int i = l; i < TX; i++) if (A[i].op = = 1) modify (a[i].x,-inf); CDQ2 (L, mid); CDQ2 (mid + 1, r);} void cdq3 (int l, int r) {if (L >= R) return; int mid = (L + r) >> 1, tx = L, Ty = mid + 1; for (int i = l; I <= R; i++) if (A[i].tim <= mid) t[tx++] = A[i]; else t[ty++] = a[i]; for (int i = l; I <= R; i++) A[i] = T[i]; tx = L, Ty = mid + 1; while (Ty <= R) {while (TX <= mid && a[tx].x >= a[ty].x) {if (A[tx].op = = 1) Modify (a [Tx].y, a[tx].y-a[tx].x]; tx++; } if (A[ty].op = = 2) chkmin (Ans[a[ty].id], a[ty].y-a[ty].x-query (A[TY].Y)); ty++; } for (int i = l; i < TX; i++) if (A[i].op = = 1) Modify(A[i].y,-inf); CDQ3 (L, mid); CDQ3 (mid + 1, r);} void Cdq4 (int l, int r) {if (L >= R) return; int mid = (L + r) >> 1, tx = L, Ty = mid + 1; for (int i = l; I <= R; i++) if (A[i].tim <= mid) t[tx++] = A[i]; else t[ty++] = a[i]; for (int i = l; I <= R; i++) A[i] = T[i]; tx = L, Ty = mid + 1; while (Ty <= R) {while (TX <= mid && a[tx].x >= a[ty].x) {if (A[tx].op = = 1) Modify (m X-A[TX].Y + 1,-a[tx].x-a[tx].y); tx++; } if (A[ty].op = = 2) chkmin (Ans[a[ty].id],-a[ty].x-a[ty].y-query (mx-a[ty].y + 1)); ty++; } for (int i = l; i < TX; i++) if (A[i].op = = 1) Modify (mx-a[i].y + 1,-inf); Cdq4 (L, mid); Cdq4 (mid + 1, r);} int main () {int n, m, tot = 0; Read (n), read (m); for (int i = 1; I <= n; i++) {int x, y; Read (x), read (y), Chkmax (MX, max (x, y)); A[++tot] = (Node) {1, x, y, tot, 0}; } int cntq = 0; for (int i = 1; i <= m; i++) {Read (A[++TOT].OP), read (a[tot].x), read (A[TOT].Y); Chkmax (MX, max (a[tot].x, a[tot].y)); if (A[tot].op = = 2) a[tot].id = ++CNTQ; A[tot].tim = tot; } for (int i = 1; I <= cntq; i++) ans[i] = inf; for (int i = 1; I <= mx; i++) f[i] =-inf; Sort (A + 1, a + tot + 1, CMP1); CDQ1 (1, tot); for (int i = 1; I <= mx; i++) f[i] =-inf; Sort (A + 1, a + tot + 1, CMP2); CDQ2 (1, tot); for (int i = 1; I <= mx; i++) f[i] =-inf; Sort (A + 1, a + tot + 1, Cmp3); CDQ3 (1, tot); for (int i = 1; I <= mx; i++) f[i] =-inf; Sort (A + 1, a + tot + 1, Cmp3); Cdq4 (1, tot); for (int i = 1; I <= cntq; i++) cout << ans[i] << "\ n"; return 0;}
Bzoj 2648 sjy pendulum piece CDQ split + tree array