小B的询问

题面

莫队版子吧......

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

#define maxn 1010000
#define maxb 1010
int aa[maxn],cnt[maxn],belong[maxn];
int n,m,k,size,bnum,now,ans[maxn];

int cf(int jl)
{
	return jl*jl;
}

struct query
{
    int l;
	int r;
	int id;
}q[maxn];

int cmp(query a, query b) 
{
    return (belong[a.l]^belong[b.l])?belong[a.l]<belong[b.l] : ((belong[a.l] & 1) ? a.r<b.r:a.r>b.r);
}

void add(int pos) 
{
    ++cnt[aa[pos]];
    now+=cf(cnt[aa[pos]])-cf(cnt[aa[pos]]-1);
}

void del(int pos) 
{
    --cnt[aa[pos]];
    now-=cf(cnt[aa[pos]]+1)-cf(cnt[aa[pos]]);
}

#define isdigit(x)((x)>='0'&&(x)<='9')
int read() {
    int res = 0;
    char c = getchar();
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) res = (res << 1) + (res << 3) + c - 48, c = getchar();
    return res;
}
void printi(int x) {
    if(x / 10) printi(x / 10);
    putchar(x % 10 + '0');
}

int main() {
    scanf("%d%d%d",&n,&m,&k);
    
    size = sqrt(n);
    bnum = ceil((double)n/size);
    
    for(int i=1;i<=bnum;++i) 
    for(int j=(i-1)*size+1;j<=i*size;j++)
    belong[j]=i;
    
    for(int i=1;i<=n;i++)
	aa[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+m+1,cmp);
    int l=1,r=0;
    for(int i=1;i<=m;++i){
        int ql=q[i].l,qr=q[i].r;
        while(l<ql)del(l++);
        while(l>ql)add(--l);
        while(r<qr)add(++r);
        while(r>qr)del(r--);
        ans[q[i].id]=now;
    }
    for(int i=1;i<=m;++i)
	printf("%d\n",ans[i]);
    return 0;
} 

  

posted @ 2019-07-23 16:46  [jackeylove]  阅读(139)  评论(0编辑  收藏  举报