zoukankan      html  css  js  c++  java
  • 【填坑向】bzoj2038小Z的袜子 莫队

    学莫队必做题,,,但是懒得写。今天来填个坑

    莫队水题

    莫队实际上就是按一个玄学顺序来离线计算询问,保证复杂度只会多一个n1/2,感觉是玄学(离线算法都很玄学)

    易错点:要开long long(卡我半天)

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <algorithm>
     4 using namespace std;
     5 int n,m,N,c[50001],a[50001];
     6 long long ans[50001],an[50001];
     7 struct que{int l,r,id;}q[50001];
     8 bool operator<(que a,que b){return ((a.l/N)<(b.l/N)) || ((a.l/N==b.l/n)&&(a.r/N<b.r/N));}
     9 long long gcd(long long a,long long b)
    10 {
    11     if(b==0)    return a;
    12     else    return(gcd(b,a%b));
    13 }
    14 int main()
    15 {
    16     scanf("%d%d",&n,&m);N=(int)sqrt(n);
    17     for(int i=1;i<=n;i++)
    18         scanf("%d",&c[i]);
    19     for(int i=1;i<=m;i++)
    20         scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
    21     sort(q+1,q+m+1);
    22     int l=q[1].l,r=q[1].l;long long s=0;
    23     a[c[l]]=1;
    24     for(int i=1;i<=m;i++)
    25     {
    26         while(l>q[i].l)    s+=a[c[--l]],a[c[l]]++;
    27         while(l<q[i].l)    a[c[l]]--,s-=a[c[l++]];
    28         while(r<q[i].r)    s+=a[c[++r]],a[c[r]]++;
    29         while(r>q[i].r)    a[c[r]]--,s-=a[c[r--]];
    30         ans[q[i].id]=s;an[q[i].id]=(long long)(q[i].r-q[i].l)*(q[i].r-q[i].l+1)/2;
    31     }
    32     for(int i=1;i<=m;i++)
    33         printf("%lld/%lld
    ",ans[i]/gcd(an[i],ans[i]),an[i]/gcd(an[i],ans[i]));
    34     return 0;
    35 } 
  • 相关阅读:
    modf()函数
    面向对象编程五大原则
    .Net网络资源
    整理CentOS常用命令
    在RHEL5上安装oracle10gLinux
    strchr()函数
    swab函数
    Strstr()函数
    tmpnam函数
    strdup ()函数
  • 原文地址:https://www.cnblogs.com/wanglichao/p/5752368.html
Copyright © 2011-2022 走看看