zoukankan      html  css  js  c++  java
  • luogu1494 [国家集训队]小Z的袜子

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    int n, m, a[50005], blc, bel[50005], cnt[50005];
    long long ansfz[50005], ansfm[50005], ans;
    struct Node{
        int l, r, id;
    }nd[50005];
    bool cmp(Node x, Node y){
        if(bel[x.l]!=bel[y.l])	return bel[x.l]<bel[y.l];
        if(bel[x.l]&1)	return x.r<y.r;
        else	return x.r>y.r;
    }
    void add(int x){
        cnt[a[x]]++;
        if(cnt[a[x]]>1)	ans += cnt[a[x]]*(cnt[a[x]]-1) - (cnt[a[x]]-2)*(cnt[a[x]]-1);
    }
    void del(int x){
        cnt[a[x]]--;
        if(cnt[a[x]]>0)	ans += cnt[a[x]]*(cnt[a[x]]-1) - (cnt[a[x]]+1)*cnt[a[x]];
    }
    //注意:以上add和del中的限制条件要么同时加要么同时不加。
    //因为cnt可能是负值。比如说在[4,4]到[7,7]的过程中
    long long gcd(long long a, long long b){
        return b==0?a:gcd(b,a%b);
    }
    int main(){
        cin>>n>>m;
        blc = sqrt(n);
        for(int i=1; i<=n; i++){
            scanf("%d", &a[i]);
            bel[i] = (i - 1) / blc + 1;
        }
        for(int i=1; i<=m; i++){
            scanf("%d %d", &nd[i].l, &nd[i].r);
            nd[i].id = i;
        }
        sort(nd+1, nd+1+m, cmp);
        int ll=nd[1].l, rr=nd[1].r;
        for(int i=ll; i<=rr; i++)	add(i);
        if(!ans)	ansfz[nd[1].id] = 0, ansfm[nd[1].id] = 1;
        else{
            long long qwq=gcd(ans, (long long)(rr-ll+1)*(rr-ll));
            ansfz[nd[1].id] = ans / qwq;
            ansfm[nd[1].id] = (long long)(rr - ll + 1) * (rr - ll) / qwq;
        }
        for(int i=2; i<=m; i++){
            while(ll<nd[i].l)	del(ll++);
            while(ll>nd[i].l)	add(--ll);
            while(rr<nd[i].r)	add(++rr);
            while(rr>nd[i].r)	del(rr--);
            if(!ans)	ansfz[nd[i].id] = 0, ansfm[nd[i].id] = 1;
            else{
                long long qwq=gcd(ans, (long long)(rr-ll+1)*(rr-ll));
                ansfz[nd[i].id] = ans / qwq;
                ansfm[nd[i].id] = (long long)(rr - ll + 1) * (rr - ll) / qwq;
            }
        }
        for(int i=1; i<=m; i++)
            printf("%lld/%lld
    ", ansfz[i], ansfm[i]);
        return 0;
    }
    
    
  • 相关阅读:
    idea添加类注释和方法注释
    蓝桥杯ALGO-1,区间k大数查询
    personalblog
    ul+li水平居中的几种方法
    前端ps部分
    帝国cms-tab
    帝国cms判断某一字段是否为空
    帝国cms建站总结-(分页)
    Js获取验证码倒计时
    前端截取字符串:JS截取字符串之substring、substr和slice详解
  • 原文地址:https://www.cnblogs.com/poorpool/p/8489064.html
Copyright © 2011-2022 走看看