zoukankan      html  css  js  c++  java
  • 【luogu 2709 / BZOJ 3781】小B的询问

    【原题题面】传送门

    【题解大意】

    莫队。这题未免太果。

    【code】

    #include<bits/stdc++.h>
    using namespace std;
    #define File ""
    #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,k;
    int Block;
    int a[mxn],ans[mxn],cnt[mxn];
    struct Q{
        int l,r,id;
    }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;
    }
    ll answer(0);
    inline void mov(int x,int d){
        answer -= cnt[a[x]]*cnt[a[x]];
        cnt[a[x]] += d;
        answer += cnt[a[x]]*cnt[a[x]];
    }
    int main(){
    //    file();
        n = read(),m = read(),k = read();
        Block = sqrt(n);
        for(int i = 1;i <= n; ++i) a[i] = read();
        for(int i = 1;i <= m; ++i) q[i].l = read(),q[i].r = read(),q[i].id = i;
        sort(q+1,q+1+m,cmp);
        int l(1),r(0);
        for(int i = 1;i <= m; ++i){
            int ql = q[i].l,qr = q[i].r;
            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++;
            ans[q[i].id] = answer;
        }
        for(int i = 1;i <= m; ++i) printf("%d
    ",ans[i]);
        return 0;
    }
    /*
    6 4 3
    1 3 2 1 1 3
    1 4
    2 6
    3 5
    5 6
    */
    View Code
  • 相关阅读:
    yii2的安装
    php Laravel windows安装
    vimrc for windows
    Magento学习
    php Ajax 局部刷新
    php用于URL的base64
    分享自建的 Jrebel License Server 激活 Jrebel
    Windows下安装Redis服务(zip)
    Windows下安装Redis服务
    MySQL中有关TIMESTAMP和DATETIME的总结
  • 原文地址:https://www.cnblogs.com/ve-2021/p/10900728.html
Copyright © 2011-2022 走看看