Bzoj 3236 AHOI 2013 Operation Mo Team algorithm

Source: Internet
Author: User

Question: Give some numbers, ask how many different values are in an interval, and how many different values are in an interval.


Idea: Because there is no modification, so I think of the MO team algorithm. Then I wrote the 5k+ Manhattan distance to the smallest spanning tree and then decisively T. (100s time limit ah, brush the status of the brush is crazy.), the result of the last added handwriting read-in also failed a). Later decisively give up, wrote a block version of the MO team algorithm. 84sAC ... This card's. It seems that Mo team is not a positive solution.

In fact, it is very easy to write the team in chunks. Only the interval of all queries is ordered, the block of the left endpoint is the first key value, the right end point is ordered as the second-quarter key value, and then it can be transferred. In theory, or Manhattan, the minimum spanning tree is faster, but the constants are too large to be seen everywhere in the pair,sort. It's not too good to write. Alas.. The tears of the Times ...


CODE (84816AC):


#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <  Algorithm> #define MAX 1100010using namespace std;struct ask{int x,y,_id;int block;int a,b;bool operator < (const ASK &a) Const {if (block = = A.block) return y < A.y;return block < A.block;} void Read (int p) {scanf ("%d%d%d%d", &x,&y,&a,&b); _id = P;}} Ask[max];int cnt,asks;pair<int,int *> xx[max << 1];int src[max];int t;int fenwick[max << 1],_fenwick[  Max];int Num[max];p air<int,int> ans[max];inline int getsum (int fenwick[],int x) {int re = 0;for (; x; x-= X&-x) re + = Fenwick[x];return Re;} inline void Fix (int fenwick[],int x,int c) {for (; x <= t; x + = X&-x) fenwick[x] + = C;} int main () {cin >> cnt >> Asks;int temp = 0;for (int i = 1; I <= cnt; ++i) scanf ("%d", &xx[++temp].first), X X[temp].second = &src[i];for (int i = 1; I <= asks; ++i) {ask[i]. Read (i); xx[++temp] = Make_pair (ASK[I].A,&AMP;ASK[I].A); Xx[++temp] = Make_pair (ask[i].b,&ask[i].b);} Sort (xx + 1,xx + temp + 1); for (int i = 1; I <= temp; ++i) {if (!t | | Xx[i].first! = xx[i-1].first) ++t;*xx[i].second = t;} int block = static_cast<int> (sqrt (CNT)), for (int i = 1; I <= asks; ++i) Ask[i].block = Ask[i].x/block;sort (ask + 1,ask + asks + 1); int L = 1,r = 0;for (int x = 1; x <= asks; ++x) {while (R < ask[x].y) {++num[src[++r]]; Fix (fenwick,src[r],1), if (num[src[r] = = 1) fix (_fenwick,src[r],1);} while (L > ask[x].x) {++num[src[--l]]; Fix (fenwick,src[l],1), if (num[src[l] = = 1) fix (_fenwick,src[l],1);} while (R > Ask[x].y) {--num[src[r]]; Fix (Fenwick,src[r],-1), if (!num[src[r]) fix (_fenwick,src[r],-1);--r;} while (L < ask[x].x) {--num[src[l]]; Fix (Fenwick,src[l],-1), if (!num[src[l]) fix (_fenwick,src[l],-1); ++l;} int p = Ask[x]._id;ans[p].first = Getsum (fenwick,ask[x].b)-getsum (fenwick,ask[x].a-1); ans[p].second = GetSum (_fenwick , ask[x].b)-getsum (_fenwick,ask[x].a-1);} for (int i = 1; I <= asks; ++i) printf ("%d%d\n", ans[i].fiRst,ans[i].second); return 0;} 

CODE (TLE):


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 1100010#define INF 0x3f3f3f3fusing namespace std;    inline int GetInt () {int res=0;    Char T=getchar (); For (;t> ' 9 ' | |    t< ' 0 '; T=getchar ());    for (; t>= ' 0 ' &&t<= ' 9 '; T=getchar ()) res=res*10+t-' 0 '; return res;}    struct ask{int x,y,_id;         int A, B;        BOOL operator < (const ASK &a) Const {if (x = = a.x) return y < a.y;    return x < a.x;        } void Read () {x = GetInt ();        y = GetInt ();        A = GetInt ();    b = GetInt (); }}ask[max];         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]; int Cnt,asks;int Src[max]; int y_x[max],t,gt,edges;pair<int,int *> Xx[max << 1];int fenwick[max << 1],_fenwick[max]; int Father[max]; int HeaD[max],total;int Next[max << 1],aim[max << 1]; int Num[max];p air<int,int> Ans[max];     inline int CALCM (const ask &a,const ask &b) {return abs (a.x-b.x) + ABS (A.Y-B.Y);} inline int GetPos (int x) {    int re = 0; for (; x <= t; x + = X&-x) if (ask[fenwick[x]].x + ask[fenwick[x]].y < ask[re].x + ask[re].y) re    = Fenwick[x]; return re;}  inline void Fix (int x,int pos) {for (; x; x = x&-x) if (ask[fenwick[x]].x + ask[fenwick[x]].y > ask[pos].x + ask[pos].y) fenwick[x] = pos;} void Makegraph () {for (int dir = 1; dir <= 4, ++dir) {if (dir = = 2 | | dir = = 4) for (int i = 1; I &l T;= asks;        ++i) swap (ASK[I].X,ASK[I].Y);        if (dir = = 3) for (int i = 1; I <= asks; ++i) Ask[i].y *=-1;        Sort (ask + 1,ask + asks + 1);        for (int i = 1; I <= asks; ++i) xx[i] = Make_pair (Ask[i].y-ask[i].x,&y_x[i]); Sort (xx + 1,xx + ASKS + 1);        t = 0;            for (int i = 1; I <= asks; ++i) {if (!t | | Xx[i].first! = xx[i-1].first) ++t;        *xx[i].second = t;        } memset (Fenwick,0,sizeof (Fenwick));            for (int i = asks; i; i.) {int temp = GetPos (Y_x[i]);            if (temp) edge[++edges] = Edge (ASK[TEMP]._ID,ASK[I]._ID,CALCM (ask[temp],ask[i));        Fix (Y_x[i],i); }} for (int i = 1; I <= asks; ++i) ask[i].x *=-1;}    int Find (int x) {if (father[x] = = x) return x; return father[x] = Find (Father[x]);}    inline void Add (int x,int y) {next[++total] = head[x];    Aim[total] = y; HEAD[X] = total;}    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 (Fx,fy), add (FY,FX);    }}} inline int getsum (int fenwick[],int x) {int re = 0;for (; x; x = x&-x) Re + = fenwick[x];      return re; } inline void Fix (int fenwick[],int x,int c) {for (; x <= gt; x + = X&-x) fenwick[x] + = c;} void DFS (int x,         int last) {static int l = 1,r = 0;        while (R < ask[x].y) {++num[src[++r]];        Fix (fenwick,src[r],1);    if (num[src[r]] = = 1) Fix (_fenwick,src[r],1);        } while (L > ask[x].x) {++num[src[--l]];        Fix (fenwick,src[l],1);    if (num[src[l]] = = 1) Fix (_fenwick,src[l],1);        } while (R > Ask[x].y) {--num[src[r]];        Fix (fenwick,src[r],-1);        if (!num[src[r]]) Fix (_fenwick,src[r],-1);    --r;        } while (L < ask[x].x) {--num[src[l]];        Fix (fenwick,src[l],-1);        if (!num[src[l]]) Fix (_fenwick,src[l],-1);    ++l;    } int p = ask[x]._id;    Ans[p].first = Getsum (fenwick,ask[x].b)-getsum (Fenwick,ask[x].a-1);   Ans[p].second = Getsum (_fenwick,ask[x].b)-getsum (_fenwick,ask[x].a-1);      for (int i = head[x]; I, i = Next[i]) {if (aim[i] = = last) continue;             DFS (AIM[I],X);            while (R < ask[x].y) {++num[src[++r]];            Fix (fenwick,src[r],1);        if (num[src[r]] = = 1) Fix (_fenwick,src[r],1);            } while (L > ask[x].x) {++num[src[--l]];            Fix (fenwick,src[l],1);        if (num[src[l]] = = 1) Fix (_fenwick,src[l],1);            } while (R > Ask[x].y) {--num[src[r]];            Fix (fenwick,src[r],-1);            if (!num[src[r]]) Fix (_fenwick,src[r],-1);        --r;            } while (L < ask[x].x) {--num[src[l]];            Fix (fenwick,src[l],-1);            if (!num[src[l]]) Fix (_fenwick,src[l],-1);        ++l;    }}} int main () {ask[0].x = Ask[0].y = INF;    CNT = GetInt ();    asks = GetInt ();    int temp = 0;        for (int i = 1; I <= cnt; ++i) {Xx[++temp].first = GetInt ();    Xx[temp].second = &src[i];  } for (int i = 1; I <= asks; ++i) {ask[i].        Read ();        Xx[++temp] = Make_pair (ASK[I].A,&AMP;ASK[I].A);        Xx[++temp] = Make_pair (ask[i].b,&ask[i].b);        ask[i]._id = i;    Father[i] = i;    } sort (xx + 1,xx + temp + 1);        for (int i = 1; I <= temp; ++i) {if (!GT | | Xx[i].first! = xx[i-1].first) ++gt;    *xx[i].second = gt;    } makegraph ();    MST ();    Memset (Fenwick,0,sizeof (Fenwick));    DFS (1,0);    for (int i = 1; I <= asks; ++i) printf ("%d%d\n", ans[i].first,ans[i].second); return 0;}


Bzoj 3236 AHOI 2013 Operation 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.