• BZOJ 3781: 小B的询问 莫队


    3781: 小B的询问

    >原题链接<

    Description

    小B有一个序列,包含N个1~K之间的整数。他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数。小B请你帮助他回答询问。

    Input

    第一行,三个整数N、M、K。
    第二行,N个整数,表示小B的序列。
    接下来的M行,每行两个整数L、R。

    Output

    M行,每行一个整数,其中第i行的整数表示第i个询问的答案。
     

    Sample Input

    6 4 3
    1 3 2 1 1 3
    1 4
    2 6
    3 5
    5 6

    Sample Output

    6
    9
    5
    2

    思路:

      莫队,对于每次更新, 先减去当前颜色之前的平方和贡献,再更新桶和现在数量的平方和贡献。

    #include <cmath>
    #include <cstdio>
    #include <cctype>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 51000;
    const int M = 50100;
    int bkt[M];
    int pos[N], a[N];
    int n, m;
    int nl, nr, na;
    inline char nc() {
        static char buf[100000], *p1, *p2;
        return p1==p2&&(p2=(p1=buf)+fread(buf, 1, 100000, stdin), p1==p2)?EOF:*p1++;
    }
    int rd() {
        int x = 0;char c = nc();
        while(!isdigit(c)) c = nc();
        while(isdigit(c)) x=x*10+c-48, c=nc();
        return x;
    }
    struct Query {
        int l, r, id, ans;
        bool operator < (const Query x) const {
            if(pos[l] == pos[x.l])
                return pos[l]&1?r<x.r:r>x.r;
            return l<x.l;
        }
    }q[N<<2];
    bool cmp(Query a, Query b) {
        return a.id < b.id;
    }
    char pbuf[10000000] , *pp = pbuf;
    inline void write(int x) {
        static int sta[35];
        int top = 0;
        if(!x)sta[++top]=0;
        while(x) sta[++top] = x % 10 , x /= 10;
        while(top)*pp++=sta[top--]^'0';
    }
    void del(int posi) {
        int nowk = bkt[a[posi]];
        na -= nowk*nowk;
        bkt[a[posi]]--;
        nowk--;
        na += nowk*nowk;
    }
    void add(int posi) {
        int nowk = bkt[a[posi]];
        na -= nowk*nowk;
        bkt[a[posi]]++;
        nowk++;
        na += nowk*nowk;
    }
    int main() {
        n=rd();m=rd();a[1]=rd();for(int i=1;i<=n;i++)a[i]=rd();
        int siz=sqrt(n);int blk=n/siz,p=0;
        for(int i=1;i<=blk;i++)for(int j=1;j<=siz;j++)pos[++p]=i;
        if(p<n)for(int i=p+1;i<=n;i++)pos[i]=blk+1;
        for(int i=1;i<=m;i++)q[i].l=rd(),q[i].r=rd(),q[i].id=i;
        sort(q+1, q+m+1);
        for(int i=1;i<=m;i++) {
            while(nl<q[i].l) {del(nl++);}
            while(nl>q[i].l) {add(--nl);}
            while(nr<q[i].r) {add(++nr);}
            while(nr>q[i].r) {del(nr--);}
            q[i].ans = na;
        }
        sort(q+1, q+m+1, cmp);for(int i=1;i<=m;i++)write(q[i].ans-1), *pp++='
    ';
        fwrite(pbuf,1,pp-pbuf,stdout);
    }
    View Code
  • 相关阅读:
    springEL单引号默认值
    vscode支持c99标准
    Error loading class 'solr.HMMChineseTokenizerFactory'
    spring security通过UserDetailsService方式当找不到用户时兼容i18n方法
    springboot普通类中获取i18对应的值
    docker-compose安装后执行报错
    springboot日志
    mysql登录报错:mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
    springboot统一ajax返回数据格式,并且jquery ajax success函数修改
    spring security jquery ajax重定向问题解决
  • 原文地址:https://www.cnblogs.com/Tobichi/p/9282212.html
Copyright © 2020-2023  润新知