2038: [2009 countries Training team] small Z socks (hose) MO Team Algorithm

Source: Internet
Author: User
Tags gcd

Topic Link: Click to open the link

First, the query is processed into Manhattan's smallest spanning tree.

Then you can run violently on the tree.

Can use MO team situation should be for inquiry [L,r], [l ', R '] cost must be ABS (L-L ') + ABS (R-R ')


#include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include < stdlib.h> #include <string.h> #include <limits.h> #include <vector> #include <string># Include <time.h> #include <math.h> #include <iomanip> #include <queue> #include <stack># Include <set> #include <map>const int inf = 1e9;const double eps = 1e-8;const double pi = ACOs ( -1.0); Template &  Lt;class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), 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 &Gt 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) CO Nst{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 planar dots 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&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 (dir% 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 Sort (b + 1, B + n + 1), int sz = unique (b + 1, B + n + 1)-b-1;//initializes the inverse Tree array tree.init (SZ); for (int i = r; I >= l; i --) {int 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;ll Up[n], Now;int l[n], r[n];int N, Query, Col[n], siz[n];void Add (int x, int y) {for (int i = x; i <= y; i++) {Now + = siz[col[i]];siz[col[i]]++;}} void del (int x, int y) {for (int i = x; i <= y; i++) {now-= Siz[col[i]]-1;siz[col[i]]--;}} void Dfs (int u, int fa) {if (FA = =-1) Add (L[u], r[u]), Else{if (L[u] < L[FA]) Add (L[u], L[fa]-1); if (R[u] > R[FA]) A DD (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]);} Up[u] = now;for (int i = 0; i < g[u].size (); i++) if (g[u][i]! = FA) Dfs (g[u][i), u); if (FA! =-1) {if (L[u] < L[FA]) d El (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 () {while (CIN >> n >> query) {for (int i = 1; I <= n; i++) Rd (Col[i]), for (int i = 1; I <= query; i++) {rd (L[i]), RD (R[i]);p [i].x = L[i]; p[i].y = R[i]; p[i].id = i;} for (int i = 1; I <= query; i++) g[i].clear (); m_mst.work (1, query); now = 0;memset (siz, 0, sizeof siz);d FS (1,-1); t i = 1; I <= query; i++) {ll down = (LL) (R[i]-l[i] + 1) * (R[i] – l[i])/2;ll g = gcd (Up[i], down);p t (Up[i]/g), Putchar ('/'); Pt (down/g); Putchar (' \ n ');}} return 0;}


2038: [2009 countries Training team] small Z socks (hose) MO Team Algorithm

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.