zoukankan      html  css  js  c++  java
  • 【luogu 1494 / BZOJ 2038】 小Z的袜子

    【原题题面】传送门

    【题解大意】

    莫队入门。

    这里

    或者直接看代码。

    要分别维护分子和分母,这个比较特殊。

    还有这个题不是第一次写了,上一次写疯狂TLE,洛谷上开了O2才过。

    这次写没有将每一个点属于哪个块的信息储存,所以跑得飞快。

    写法get。

    【code】

    #include<bits/stdc++.h>
    using namespace std;
    #define File "test"
    #define ll long long
    #define ull unsigned long long
    inline void file(){
        freopen(File".in","r",stdin);
        freopen(File".out","w",stdout);
    }
    inline int read(){
        int x=0,f=1;   char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
        return x*f;
    }
    const int mxn = 5e4+5;
    int n,m,Block;
    int c[mxn],cnt[mxn]/*维护每个点*/;
    struct Q{
        int l,r,id;
        ll a,b;
    }q[mxn];
    inline bool cmp(Q x,Q y){
        if((x.l/Block)^(y.l/Block)) return x.l < y.l;
        else if((x.l/Block)&1) return x.r < y.r;
        else return x.r > y.r;
    }
    inline ll gcd(ll x,ll y){
        return y ? gcd(y,x % y) : x;
    }
    inline bool cmp1(Q x, Q y){
        return x.id < y.id;
    }
    inline ll C(int x){
        return 1ll*x*x;
    }
    ll ans(0);
    inline void mov(int x,int d){
        ans -= C(cnt[c[x]]);
        cnt[c[x]] += d;
        ans += C(cnt[c[x]]);
    }
    int main(){
    //    file();
        n = read(),m = read();
        for(int i = 1;i <= n; ++i) c[i] = read();
        for(int i = 1;i <= m; ++i)
            q[i].l = read(),q[i].r = read(),q[i].id = i;
        Block = sqrt(n);
        sort(q+1,q+m+1,cmp);
        int l(1),r(0);
        for(int i = 1;i <= m; ++i){
            int ql = q[i].l,qr = q[i].r;
            if(ql==qr){
                q[i].a = 0,q[i].b = 1;
                continue;
            }
            while(ql < l) mov(l-1,1),l--;
            while(ql > l) mov(l,-1),l++;//往右舍去已知
            while(qr < r) mov(r,-1),r--;
            while(qr > r) mov(r+1,1),r++;
            q[i].a = ans - (qr-ql+1);
            q[i].b = 1ll*(qr-ql)*(qr-ql+1);
            ll gcd_ = gcd(q[i].a,q[i].b);
            q[i].a /= gcd_,q[i].b /= gcd_;//分别处理分母和分子
        }
        sort(q+1,q+m+1,cmp1);
        for(int i = 1; i <= m; ++i)
            printf("%lld/%lld
    ",q[i].a,q[i].b);
        return 0;
    }
    /*
    6 4
    1 2 3 3 3 2
    2 6
    1 3
    3 5
    1 6
    */
    View Code
  • 相关阅读:
    This is a thoughtful essay
    MSSQL 模糊搜索全文(过程、函数、触发器等)
    MSSQL 高并发下生成连续不重复的订单号
    MSSQL sql numeric转字符串显示不补0
    iOS 开发之UIStackView的应用
    Java day 5
    Java day 4
    Java day 3-1
    Java day 3
    Java day 2
  • 原文地址:https://www.cnblogs.com/ve-2021/p/10896439.html
Copyright © 2011-2022 走看看