zoukankan      html  css  js  c++  java
  • bzoj2038 [2009国家集训队]小Z的袜子(hose) 莫队

    Description

    作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……
    具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
    你的任务便是告诉小Z,他有多大的概率抽到两只颜色相同的袜子。当然,小Z希望这个概率尽量高,所以他可能会询问多个(L,R)以方便自己选择。

    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。
     
    题解:
      莫队模板题,按照左端点的块排序,在同一块内按右端点排序,
      可以保证时间复杂度为n log n 膜拜hgz,大佬。
     1 #include<cstring>
     2 #include<cmath>
     3 #include<iostream>
     4 #include<cstdio>
     5 #include<algorithm>
     6 
     7 #define N 50007
     8 #define ll long long
     9 using namespace std;
    10 inline int read()
    11 {
    12     int x=0,f=1;char ch=getchar();
    13     while(ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
    14     while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    15     return f*x;
    16 }
    17 
    18 int n,m;
    19 int pos[N],c[N];
    20 ll sum[N],ans;
    21 struct Node
    22 {
    23     int l,r,id;
    24     ll a,b;
    25 }a[N];
    26 
    27 ll sqr(ll x)
    28 {
    29     return x*x;
    30 }
    31 ll gcd(ll a,ll b)
    32 {
    33     return b?gcd(b,a%b):a;
    34 }
    35 bool cmp(Node x,Node y)
    36 {
    37     if (pos[x.l]==pos[y.l]) return x.r<y.r;
    38     else return x.l<y.l;
    39 }
    40 bool cmp_id(Node x,Node y)
    41 {
    42     return x.id<y.id;
    43 }
    44 void modify(int p,int z)
    45 {
    46     ans-=sqr(sum[c[p]]);
    47     sum[c[p]]+=z;
    48     ans+=sqr(sum[c[p]]);
    49 }
    50 void solve()
    51 {
    52     for (int i=1,l=1,r=0;i<=m;i++)
    53     {
    54         for (;r<a[i].r;r++)
    55             modify(r+1,1);
    56         for (;r>a[i].r;r--)
    57             modify(r,-1);
    58         for (;l<a[i].l;l++)
    59             modify(l,-1);
    60         for (;l>a[i].l;l--)
    61             modify(l-1,1);
    62         if (a[i].l==a[i].r)
    63         {
    64             a[i].a=0,a[i].b=1;
    65             continue;
    66         }
    67         a[i].a=ans-(a[i].r-a[i].l+1);
    68         a[i].b=(ll)(a[i].r-a[i].l+1)*(a[i].r-a[i].l);
    69         ll k=gcd(a[i].a,a[i].b);
    70         a[i].a/=k,a[i].b/=k;
    71     }
    72 }
    73 int main()
    74 {
    75     n=read(),m=read();
    76     for (int i=1;i<=n;i++)
    77         c[i]=read();
    78     int block=int(sqrt(n));
    79     for (int i=1;i<=n;i++)
    80         pos[i]=(i-1)/block+1;
    81     for (int i=1;i<=m;i++)
    82     {
    83         a[i].l=read(),a[i].r=read();
    84         a[i].id=i;
    85     }
    86     sort(a+1,a+m+1,cmp);
    87     solve();
    88     sort(a+1,a+m+1,cmp_id);
    89     for (int i=1;i<=m;i++)
    90         printf("%lld/%lld
    ",a[i].a,a[i].b);
    91 }
  • 相关阅读:
    Spring boot unable to determine jdbc url from datasouce
    Unable to create initial connections of pool. spring boot mysql
    spring boot MySQL Public Key Retrieval is not allowed
    spring boot no identifier specified for entity
    Establishing SSL connection without server's identity verification is not recommended
    eclipse unable to start within 45 seconds
    Oracle 数据库,远程访问 ora-12541:TNS:无监听程序
    macOS 下安装tomcat
    在macOS 上添加 JAVA_HOME 环境变量
    Maven2: Missing artifact but jars are in place
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8142869.html
Copyright © 2011-2022 走看看