Bzoj 2038 2009 Countries Training Team Small Z socks Mo team algorithm

Source: Internet
Author: User
Tags gcd

The main topic: give some socks in the order, each time asked how many of the same color in the socks pair.


Idea: Mo team algorithm is really a magic algorithm. First, the brute force enumeration is the time complexity of O (n^2), which is certainly not possible. If the interval is guaranteed not to overlap, then the total time transfer complexity can be reduced to O (n). Unfortunately, there is no such guarantee in the title. Thus, the mysterious team invented a magic algorithm.

For each inquiry, we regard it as a point on a plane (x1,y1), and there are other points distributed in the plane. If there is another point (x2,y2), then the total number of elements that we need to change from the first interval to the second interval is |x1-x2| + |y1-y2|, which is the distance between two points in Manhattan. So we put all the questions on the abstract Narihira surface, then make the smallest spanning tree in Manhattan, and then facilitate the tree, and by the way, all the answers are counted.


CODE:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 50010#        Define INF 0x3f3f3f3fusing namespace std;struct edge{int x,y,length; Edge (int _,int __,int ___): X (_), Y (__), Length (___) {} Edge () {} BOOL operator < (const Edge &a) Const {R    Eturn length < A.length;        }}edge[max << 3];struct point{int x,y,_id;        BOOL operator < (const point &a) Const {if (x = = a.x) return y < a.y;    return x < a.x;    } void Read () {scanf ("%d%d", &x,&y); }}point[max];struct ask{int x, y;} Ask[max];int t,points,edges;int Src[max];p air<int,int *> xx[max];int y_x[max];int fenwick[MAX];int Father[MAX]; int head[max],total;int _next[max << 1],aim[max << 1];unsigned int cnt[max],now;unsigned int ans[max],down[    max];inline void Add (int x,int y) {_next[++total] = head[x];    Aim[total] = y; HEAD[X] = total;} inline int CALCM (const point &a,const Point &b) {return abs (a.x-b.x) + ABS (A.Y-B.Y);}    inline int GetPos (int x) {int re = 0;            for (; x <= points; x + = X&-x) if (point[fenwick[x]].x + point[fenwick[x]].y < point[re].x + point[re].y)    Re = fenwick[x]; return re;} inline void Fix (int x,int pos) {for (; x; x = x&-x) if (point[fenwick[x]].x + point[fenwick[x]].y > point[ pos].x + point[pos].y) fenwick[x] = pos;} void Makegraph () {for (int dir = 1; dir <= 4; ++dir) {if (dir! = 1) for (int i = 1; I <= points;        ++i) Point[i].y *=-1;                if (dir = = 3) for (int i = 1; I <= points; ++i) swap (POINT[I].X,POINT[I].Y);        Memset (Fenwick,0,sizeof (Fenwick));            for (int i = 1; I <= points; ++i) {xx[i].first = point[i].y-point[i].x;        Xx[i].second = &y_x[i];        } sort (xx + 1,xx + points + 1);        int t = 0;        Xx[0].first = INF; for (int i = 1;i <= points;            ++i) {if (Xx[i].first! = Xx[i-1].first) ++t;        *xx[i].second = t;        } sort (Point + 1,point + points + 1);            for (int i = points; i; i.) {int temp = GetPos (Y_x[i]);            if (temp) edge[++edges] = Edge (POINT[I]._ID,POINT[TEMP]._ID,CALCM (point[i],point[temp));        Fix (Y_x[i],i);    }}}int Find (int x) {if (father[x] = = x) return x; return father[x] = Find (Father[x]);}    void MST () {sort (edge + 1,edge + edges + 1);        for (int i = 1; I <= edges; ++i) {int fx = Find (edge[i].x);        int fy = Find (EDGE[I].Y);            if (FX! = FY) {father[fx] = FY;            ADD (EDGE[I].X,EDGE[I].Y);        ADD (edge[i].y,edge[i].x);    }}}void DFS (int x,int last) {static int l = 1,r = 0;    while (R < ask[x].y) Now + = cnt[src[++r]]++;    while (L > ask[x].x) Now + = cnt[src[--l]]++;    while (R > Ask[x].y) Now-=--cnt[src[r--]]; while (L < ask[x].x) Now-=--cnt[src[l++]];    DOWN[X] = (unsigned int) (r-l + 1) * (r-l) >> 1;        ANS[X] = now;        for (int i = head[x]; I, i = _next[i]) {if (aim[i] = = last) continue;                DFS (AIM[I],X);        while (R < ask[x].y) Now + = cnt[src[++r]]++;        while (L > ask[x].x) Now + = cnt[src[--l]]++;        while (R > Ask[x].y) Now-=--cnt[src[r--]];    while (L < ask[x].x) now-=--cnt[src[l++]];    }}int Main () {cin >> T >> points;    for (int i = 1; I <= t; ++i) scanf ("%d", &src[i]);    point[0].x = Point[0].y = INF; for (int i = 1; I <= points; ++i) {point[i].        Read ();        ask[i].x = point[i].x;        Ask[i].y = POINT[I].Y;        point[i]._id = i;    Father[i] = i;    } makegraph ();    MST ();    DFS (1,0);        for (int i = 1; I <= points; ++i) {unsigned int u = ans[i],d = Down[i];        if (!u) puts ("0/1");            else {unsigned int gcd = __GCD (u,d); printf ("%u/%u\n", U/GCD,D/GCD); }} return 0;}


Bzoj 2038 2009 Countries Training Team Small Z socks 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.