MO Team Algorithm Template

Source: Internet
Author: User

Shaped like Hdu 5381: Click to open link

The function complexity of add (int x, int y) is O (|x-y|)

Del similarly

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include &LT;LIMITS.H&G  T #include <vector> #include <string> #include <time.h> #include <math.h> #include &LT;IOMANIP&G  T #include <queue> #include <stack> #include <set> #include <map> const int inf = 1e9;const Doub Le EPS = 1e-8;const double pi = ACOs ( -1.0); template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = Get char (), c = = EOF) return 0;while (c! = '-' && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} Using namespace Std;const int N = 1e5 + 10;typedef long long ll;ll gcd (ll x, ll y) {if (x > Y) Swap (x, y), while (x) y%= x, Swap (x, y); return y;} Vector<int>g[n];class MST {struct Edge {int from, to, dis; Edge (int _from = 0, int _to = 0, int _dis = 0): from (_from), to (_to), dis (_dis) {}bool operator < (const Edge &AMP;X) C onst {return dis < X.dis;}} Edge[n << 3];int F[n], tot;int find (int x) {return x = = F[x]? x:f[x] = Find (F[x]);} BOOL Union (int x, int y) {x = find (x); y = find (y); if (x = = y) return false;if (x > Y) Swap (x, y); F[x] = Y;return true;} Public:void init (int n) {for (int i = 0; I <= N; i++) F[i] = I;tot = 0;} void Add (int u, int v, int dis) {edge[tot++] = Edge (U, v, dis);} ll work () {//Calculate minimum spanning tree, return cost sort (edge, Edge + tot); ll costs = 0;for (int i = 0; i < tot; i++) if (Union (Edge[i].from, Edge [i].to)] {cost + = Edge[i].dis; G[edge[i].from].push_back (edge[i].to); G[edge[i].to].push_back (Edge[i].from);} return cost;}} Mst;struct Point {//two-dimensional plane dot int x, y, Id;boOL operator < (const point&a) const {return x = = a.x? y < a.y:x < a.x;}} P[n];bool cmp_id (const point&a, const POINT&AMP;B) {return a.id < b.id;} Class BIT {//tree array int c[n], id[n], maxn;int lowbit (int x) {return x&-x;} Public:void init (int n) {MAXN = n + 10;fill (c, C + MAXN + 1, INF), Fill (ID, id + MAXN + 1,-1);} void Updata (int x, int val, int _id) {while (x) {if (Val < c[x]) {c[x] = val; id[x] = _id;} X-= Lowbit (x);}} int query (int x) {int val = inf, _id = -1;while (x <= maxn) {if (val > C[x]) {val = c[x]; _id = id[x];} x + = Lowbit (x);} return _id;}} tree;inline BOOL CMP (int *x, int *y) {return *x < *y;} Class Manhattan_mst {int a[n], b[n];p ublic:ll work (int L, int. r) {Mst.init (R); for (int dir = 1; dir <= 4; dir++) {if (d Ir% 2 = = 0) for (int i = l; I <= R; i++) swap (p[i].x, p[i].y), and else if (dir = = 3) for (int i = l; I <= R; i++) P[i].y = -p[i].y;sort (P + L, p + R + 1); for (int i = l; I <= R; i++) A[i] = b[i] = p[i].y-p[i].x; Discretization of SORT (b + 1, B + n + 1), int sz = unique (b + 1, B + n + 1)-b-1;//Initialize the inverse tree array tree.init (SZ); for (int i = r; I >= l; i--) {i NT pos = lower_bound (b + 1, B + sz + 1, a[i])-B;int id = tree.query (POS); if (id! =-1) mst.add (p[i].id, P[id].id, ABS (p[i ].x-p[id].x) + ABS (P[I].Y-P[ID].Y)) Tree.updata (POS, p[i].x + p[i].y, i);}} for (int i = l; I <= R; i++) P[i].y =-p[i].y;return mst.work ();}} M_mst;int N, M, A[n];int L[n], r[n];ll ans[n], now;void Add (int x, int y) {for (int i = x; i <= y; i++) {}}void del (int x, int y) {}void dfs (int u, int fa) {if (FA! = u) {Add (L[u], r[u]);} Else{if (L[u] < L[FA]) Add (L[u], L[fa]-1), if (R[u] > R[fa]) Add (R[fa] + 1, r[u]), if (L[u] > L[fa]) del (L[FA], l [u]-1), if (R[u] < R[FA]) del (R[u] + 1, R[FA]);} for (int v:g[u]) if (v = FA) Dfs (V, u), if (FA! = u) {if (L[u] < L[FA]) del (L[u], L[fa]-1), if (R[u] > R[fa]) del (r [FA] + 1, r[u]), if (L[u] > L[fa]) Add (L[FA], L[u]-1), if (R[u] < R[FA]) Add (R[u] + 1, R[FA]);}} int main () {int T; RD (T), while (t--) {rd (n), for (int i = 1; I <= n; i++) Rd (A[i]), RD (M); for (int i = 1; I <= m; i++) {rd (p[i].x); Rd (P[I].Y); P[i].id = i;l[i] = p[i].x; R[i] = p[i].y;} M_mst.work (1, m);d FS (1, 1), for (int i = 1; I <= m; i++) pt (Ans[i]), puts ("");} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MO Team Algorithm Template

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.