Clear orange a1206 small Z so (Mo team algorithm)

Source: Internet
Author: User
A1206. small Z sock time limit: 1.0 s memory limit: 512.0 MB total submissions: 744 AC times: 210 average score: 44.44 share this question: view unformatted questions submit questions Discussion Question source 2010 China National Training Team question description as a casual person, it takes a long time for little Z to find a pair of colorful so every morning. One day, Mr. Z could no longer endure the annoying process of searching for so, so he decided to let him go ......
Specifically, little Z numbers the N so from 1 to n and then from number l to R (L although little Z doesn't care whether the two so are full or not, I don't even care whether the two so are either left or right, but he cares about the color of the so. After all, wearing two so of different colors will be embarrassing.
Your task is to tell Mr. Z how likely he is to draw two so in the same color. Of course, Mr. Z wants this probability to be as high as possible, so he may ask multiple (L, R) for his own convenience. The first line of the input file contains two positive integers n and M. N is the number of so, and m is the number of inquiries from small Z.
The next line contains n positive integers (CI), where CI indicates the color of the I-th sock. The same color is represented by the same number.
In the next m row, there are two positive integers (l) in each row. r indicates a query. The output file in the output format contains m rows. For each query, the output score a/B Indicates the range [L, r] the probability that two so have the same color is randomly selected. If the probability is 0, 0/1 is output; otherwise, the output a/B must be the simplest score. (See the example for details.) Input 6 4 in the sample.
1 2 3 3 2
2 6
1 3
3 5
1 6 samples output 2/5
0/1
1/1
4/15 Example 1: a total of 10 possibilities are C (5, 2) =, of which two are possible, and two three are possible, the probability is (1 + 3)/10 = 4/10 = 2/5.
Question 2: C (3, 2) = 3 possibilities. You cannot draw a sock of the same color. The probability is 0/3 = 0/1.
Question 3: C (3, 2) = 3 possibilities in total, all of which are extracted by two 3 with a probability of 3/3 = 1/1.
Note: The preceding C (a, B) indicates the number of combinations. The number of combinations C (A, B) is equivalent to the number of B options selected in a different item. N, m ≤ 30% of the data scale and agreed 5000;
N, m ≤ 60% of the data;
In 100% of the data, n, m ≤, 1 ≤ L <r ≤ n, CI ≤ n.


Click the link to open the question

Question:

The Chinese question is not explained.

Ideas:

Query L and R. Set the color to X, Y, Z... To A, B, and C...

Then the answer is (A * (A-1)/2 + B * (b-1)/2 + C * (c-1)/2 ....) /(R-L + 1) * (R-L)/2)

Simplified: (a ^ 2 + B ^ 2 + C ^ 2 +... x ^ 2-(A + B + C + D + .....)) /(R-L + 1) * (R-L ))

(A ^ 2 + B ^ 2 + C ^ 2 +... x ^ 2-(R-L + 1)/(R-L + 1) * (R-L ))

Therefore, the key to this question is to calculate the sum of squares of each color number in an interval.

But how can we solve the problem quickly?

For general interval maintenance problems, we generally think of using a line segment tree. But this question does not know how to do the line segment tree, Baidu. I know it's the mo team algorithm.

So I learned it. Write your learning experience.

Mo Tao invented the mo team algorithm. I think this person is awesome. However, he cannot find his papers on Baidu. I had to learn from others' blogs. The mteams algorithm is an offline algorithm that handles a class of issue without modifying the query range. If you know the answer to [L, R. You can get [L, R-1] and [L, R + 1] and [L-1, R] and [L + 1, r. You can use the mteams algorithm.

I feel like I am violent with the mo team algorithm. I only know all the inquiries in advance. The order of each query can be calculated reasonably to reduce the complexity. You need to know the answer to [L, R] After calculating the answer to [L ', r. Since [L, R-1] and [L, R + 1] and [L-1, R] and [L + 1, r. so the time spent calculating the answer to [L ', R'] is | L-L' | + | R-R '|. If you think of [L, R] as a point A (L, R) on the plane, then ask [L ', R'] As point B (L', R. The time overhead is the distance between two Manhattan points. Therefore, each query is considered as a point. We need to calculate each value in a certain order. The overhead is the sum of the distance between Manhattan. Calculate each vertex. The path should be at least a tree. So the problem becomes to finding the minimum Manhattan distance spanning tree of a two-dimensional plane.

About the two-dimensional plane, the minimum Manhattan distance spanning tree. For more information, see click to open the link.

In this way, you only need to calculate the value along the tree. I won't prove that the time complexity is N * SQRT (n.

However, the programming complexity of this method is slightly higher. Therefore, there is a more elegant alternative. That is, block the sequence first. Then all the queries are sorted by the size of the block where L is located. In the same case, sort by R. Then, it is calculated in the sorted order. Why can this computation reduce the complexity.

I. In the same I + 1, R increases monotonically, so R is O (n. Because there are n ^ 0.5 blocks, the time complexity of this part is n ^ 1.5.
2. I and I + 1 span a piece. r changes N at most. Due to N ^ 0.5 blocks, the time complexity of this part is n ^ 1.5.
3. I and I + 1 cannot change more than N ^ 0.5 in the same partition, nor exceed 2 * n ^ 0.5 in each partition. It may be considered as N ^ 0.5. Because there are n numbers, the time complexity is n ^ 1.5
So it becomes O (N ^ 1.5.

For detailed procedures, see the code:

#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>#include<math.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=50010;typedef long long ll;ll num[maxn],up[maxn],dw[maxn],ans,aa,bb,cc;int col[maxn],pos[maxn];struct qnode{    int l,r,id;} qu[maxn];bool cmp(qnode a,qnode b){    if(pos[a.l]==pos[b.l])        return a.r<b.r;    return pos[a.l]<pos[b.l];}ll gcd(ll x,ll y){    ll tp;    while(tp=x%y)    {        x=y;        y=tp;    }    return y;}void update(int x,int d){    ans-=num[col[x]]*num[col[x]];    num[col[x]]+=d;    ans+=num[col[x]]*num[col[x]];}int main(){    int n,m,i,j,bk,pl,pr,id;    freopen("in.txt","r",stdin);    while(~scanf("%d%d",&n,&m))    {        memset(num,0,sizeof num);        bk=ceil(sqrt(1.0*n));        for(i=1;i<=n;i++)        {            scanf("%d",&col[i]);            pos[i]=(i-1)/bk;        }        for(i=0;i<m;i++)        {            scanf("%d%d",&qu[i].l,&qu[i].r);            qu[i].id=i;        }        sort(qu,qu+m,cmp);        pl=1,pr=0;        ans=0;        for(i=0;i<m;i++)        {            id=qu[i].id;            if(qu[i].l==qu[i].r)            {                up[id]=0,dw[id]=1;                continue;            }            if(pr<qu[i].r)            {                for(j=pr+1;j<=qu[i].r;j++)                    update(j,1);            }            else            {                for(j=pr;j>qu[i].r;j--)                    update(j,-1);            }            pr=qu[i].r;            if(pl<qu[i].l)            {                for(j=pl;j<qu[i].l;j++)                    update(j,-1);            }            else            {                for(j=pl-1;j>=qu[i].l;j--)                    update(j,1);            }            pl=qu[i].l;            aa=ans-qu[i].r+qu[i].l-1;            bb=(ll)(qu[i].r-qu[i].l+1)*(qu[i].r-qu[i].l);            cc=gcd(aa,bb);            aa/=cc,bb/=cc;            up[id]=aa,dw[id]=bb;        }        for(i=0;i<m;i++)            printf("%I64d/%I64d\n",up[i],dw[i]);    }    return 0;}


    Clear orange a1206 small Z so (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.