Bzoj 2038 small Z socks & MO team algorithm (not just a brute force ...)

Source: Internet
Author: User
Tags gcd

Test instructions

Give a sequence, ask an interval, find the interval ... woc! Stick to the original question!

As a rambling person, little Z spends a lot of time every morning looking for a pair to wear from a bunch of colorful socks. Finally one day, little z can no longer endure this annoying to find socks process, so he decided to resign to fate ...
Specifically, small z to this n socks from 1 to n number, and then from the number L to R (L Although small z does not care about two socks is not a complete pair, even do not care about two socks whether a left and right, he is very concerned about the color of socks, after all, wearing two different color socks will be very embarrassing.
Your task is to tell Little Z how much he has the chance to draw two socks of the same color. Of course, Little Z wants this probability to be as high as possible, so he may ask multiple (l,r) to facilitate his choice.

Sol

Mo team Dafa Good, MO team is a way to fight violence, there is a very 6 of the paper, extremely clear and understandable ... (Dig a hole first, can't find ...)

Then directly on the code ... (first forget to open ll ....) It has been so many times ....

CODE:

  

/*==========================================================================# last modified:2016-02-27 16:55# filename:2038.cpp# Description: ==========================================================================*/# Define me Acrossthesky #include <cstdio> #include <cmath> #include <ctime> #include <string> #inc (in)  Lude <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <queue> #include <vector> #define LOWBIT (x) (x) & (-X ) #define for (I,A,B) for ((i) = (a);(i) <= (b);(i) + +) #define FORP (I,A,B) for (int i= (a); i<= (b); i++) #define FORM (i,a,b ) for (int i= (a); i>= (b); i--) #define LS (A, a) (((a) + (b)) << 1) #define RS (A, B) (((a) + (c)) >> 1) #define GETLC (a) ch[(a)][0] #define GETRC (a) ch[(a)][1] #define MAXN 100000 #define MAXM 100000 #define PI 3.1415926535898 #define _e 2.718281828459 #define INF 1070000000 using Namespace std; typedef long Long LL;  typedef unsigned long long ull;     Template<class t> inline void read (t& num) {bool start=false,neg=false;     char c;     num=0;         while ((C=getchar ())!=eof) {if (c== '-') start=neg=true;             else if (c>= ' 0 ' && c<= ' 9 ') {start=true;         num=num*10+c-' 0 ';     } else if (start) break; } if (neg) num=-num; }/*==================split line==================*/int l,r;struct query{int L,r,id,op;} Q[maxm];struct answer{ll X,tot;} Ans[maxm];bool CMP (const query &x,const query &y) {if (X.OP==Y.OP) return x.r<y.r; else return x.op<y.op;} ll GCD (ll A,ll b) {return b==0?a:gcd (b,a%b);} ll a[maxn],cnt[maxn];ll tmp=0;void Add (int i) {tmp+=cnt[a[i]]; cnt[a[i]]++;} void Remove (int i) {cnt[a[i]]--;tmp-=cnt[a[i]];} void Solve (int l,int r,int ID) {while (l>l) {remove (L); l++;} while (l<l) {l--; add (l);} while (r<r) {remove (R); r--;} while (r>r) {r++; add (r);} Ans[id].x=tmp;//forp (i,l,r) Ans[id]. x+= (cnt[i]* (cnt[i-1])/2);} int main () {int n,m; read (n); read (m); int Sz=trunc (sqrt (n));//num=n/sz+n%sz?0:1; Forp (i,1,n) read (A[i]); Forp (i,1,m) {read (Q[I].L); Read (Q[I].R); q[i].id=i;q[i].op=q[i].l/sz+ (q[i].l%sz?1:0);} Sort (q+1,q+1+m,cmp); Tmp=0,l=q[1].l,r=q[1].r;ll num=r-l+1; Forp (i,l,r) Add (i); ans[q[1].id].x=tmp; ans[q[1].id].tot=num* (num-1)/2; Forp (i,2,m) {num=q[i].r-q[i].l+1;//if (i==1) l=q[i].l,r=q[i].r;ans[q[i].id].tot=num* (num-1)/2;solve (Q[I].L,Q[I].R , q[i].id);} Forp (i,1,m) {if (ans[i].x==0) {printf ("0/1\n"); continue;} ll T=GCD (Ans[i].x,ans[i].tot);p rintf ("%lld/%lld\n", ans[i].x/t,ans[i].tot/t);}}

Bzoj 2038 small Z socks & MO team algorithm (not just a brute force ...)

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.