题目链接:http://ac.nbutoj.com/Problem/view.xhtml?id=1463
题意:给出一个数列,每次询问区间[L,R]内数字出现次数的立方和。注意是次数的立方和。
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <ctime>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-10
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n) while(scanf("%d",&n)!=-1)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%d",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(i64 x) {printf("%I64d\n",x);}
void PR(u32 x) {printf("%u\n",x);}
void PR(double x) {printf("%.4lf\n",x);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
const double dINF=1e50;
const i64 inf=((i64)1)<<60;
const int INF=2147483647;
const int N=100005;
struct node
{
int id,L,R;
node(){}
node(int _id,int _L,int _R)
{
id=_id;
L=_L;
R=_R;
}
};
int n,m,a[N],K,len,num[N];
vector<node> V[N];
int b[N];
i64 ans[N];
int cmp(node a,node b)
{
return a.R<b.R;
}
int find(int low,int high,int x)
{
int mid;
while(low<=high)
{
mid=(low+high)>>1;
if(b[mid]==x) return mid;
if(b[mid]>x) high=mid-1;
else low=mid+1;
}
}
void init()
{
int i;
FOR0(i,n) b[i]=a[i];
sort(b,b+n);
int sz=unique(b,b+n)-b;
FOR0(i,n) a[i]=find(0,sz-1,a[i]);
}
i64 cal(int x)
{
return (i64)x*x*x;
}
int main()
{
Rush(n)
{
int i,j;
FOR0(i,n) RD(a[i]);
init();
RD(m);
for(len=0;sqr(len+1)<=n;len++);
if(n%len==0) K=n/len;
else K=n/len+1;
FOR0(i,K) V[i].clear();
int x,y;
FOR0(i,m)
{
RD(x,y);
x--; y--;
V[x/len].pb(node(i,x,y));
}
int L,R;
node p;
i64 temp;
FOR0(i,K)
{
clr(num,0);
sort(V[i].begin(),V[i].end(),cmp);
L=i*len; R=L-1; temp=0;
FOR0(j,SZ(V[i]))
{
p=V[i][j];
while(R<p.R)
{
R++;
temp-=cal(num[a[R]]);
num[a[R]]++;
temp+=cal(num[a[R]]);
}
while(L<p.L)
{
temp-=cal(num[a[L]]);
num[a[L]]--;
temp+=cal(num[a[L]]);
L++;
}
while(L>p.L)
{
L--;
temp-=cal(num[a[L]]);
num[a[L]]++;
temp+=cal(num[a[L]]);
}
ans[p.id]=temp;
}
}
FOR0(i,m) PR(ans[i]);
}
return 0;
}