bzoj2038 : [2009国家集训队]小Z的袜子(hose) 莫队算法

转自 http://www.cnblogs.com/kuangbin/archive/2013/08/16/3263483.html

2038: [2009国家集训队]小Z的袜子(hose)

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 966  Solved: 472
[Submit][Status]

Description

作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… 具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L

Input

输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。

Output

包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)

Sample Input

6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6

Sample Output

2/5
0/1
1/1
4/15
【样例解释】
询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。
询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。
询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。
注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。
【数据规模和约定】
30%的数据中 N,M ≤ 5000;
60%的数据中 N,M ≤ 25000;
100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。

HINT

 

Source

[Submit][Status]


HOME Back


 

 

 

莫队算法可以解决一类不修改、离线查询问题。

 

构造曼哈顿最小生成树的做法还没有写。

写了个直接分段解决的办法。

 

把1~n分成sqrt(n)段。

unit = sqrt(n)

m个查询先按照第几个块排序,再按照 R排序。

 

然后直接求解。

#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cstdio>
#include<cmath>
using namespace std;
const int maxn =60005;
typedef long long LL;
struct node{
  int L,R,id; LL a,b;
}P[maxn],T[maxn];
int C[maxn],b[maxn],block;
bool cmp1(node A, node B){
   return b[A.L]!=b[B.L]? b[A.L]<b[B.L]:A.R<B.R;
}
LL ans;
LL num[maxn];
bool cmp2(node A, node B){
   return A.id<B.id;
}
void update(int x, int add){
     ans -= num[x]*num[x];
     num[x]+=add;
     ans+=num[x]*num[x];
}
LL gcd(LL a, LL b){
    if(b==0)return a;
    else return gcd(b,a%b);
}
void solve(int M){
         int L=1,R=0;
         for(int i=1; i<=M; i++){
              while(R<P[i].R){
                    R++;
                    update(C[R],1);
              }
              while(R>P[i].R){
                   update(C[R],-1); R--;
              }

              while(L<P[i].L){

                    update(C[L],-1);L++;
              }
              while(L>P[i].L){
                     L--;
                     update(C[L],1);
              }
              if(P[i].L==P[i].R){
                   P[i].a=0; P[i].b=1; continue;
              }
              LL a = ans - (P[i].R-P[i].L+1);
              LL b = (long long)(P[i].R-P[i].L+1)*((P[i].R-P[i].L));
              LL k = gcd(a,b);


               T[P[i].id].a=a/k; T[P[i].id].b=b/k;

         }
}
int main()
{
    int N,M;
    C[0]=0;
    while(scanf("%d%d",&N,&M)==2){
         ans=0;
         for(int i=1; i<=N; i++)scanf("%d",&C[i]);
         for(int i=1; i<=M; i++){
             scanf("%d%d",&P[i].L,&P[i].R);
             //if(P[i].L>P[i].R) swap(P[i].L,P[i].R);
             P[i].id=i;
         }
         memset(num,0,sizeof(num));
         block=(int)sqrt(N);
         for(int i=1; i<=N; i++)
            b[i]=(i-1)/block+1;
         sort(P+1,P+M+1,cmp1);
         solve(M);
        for(int i=1; i<=M; i++){
          printf("%lld/%lld\n",T[i].a,T[i].b);
        }
    }

    return 0;
}

 

posted @ 2015-04-24 21:57  来自大山深处的菜鸟  阅读(190)  评论(0编辑  收藏  举报