• 【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
  • 相关阅读:
    如何用消息系统避免分布式事务?
    jvm调休,监控
    ClassLoader原理
    JVM结构、GC工作机制详解
    单链表倒置
    hashSet
    HashMap
    hashcode
    深入理解HTTP协议、HTTP协议原理分析
    HTTP协议(详解一)
  • 原文地址:https://www.cnblogs.com/ve-2021/p/10900728.html
Copyright © 2020-2023  润新知