经过assert测试,所有测试点的n均等于m,这不仅直接导致我打错的代码AC,而且还可能使得那些按照询问排序的程序通过(本人理解,如果不对请大佬们指正)。
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+5;
int n,m,k,a[N];
struct ask{
int l,r,id;
}s[N];
int blen,btot,cnt[N];
inline int L(int block) {
if(block==1) return 1;
else return (block-1)*blen+1;
}
inline int R(int block) {
if(block==btot) return m;
else return block*blen;
}
inline int where(int idx) {
return (idx-1)/blen+1;
}
void build(){
cin>>n>>m>>k;
blen=sqrt(m);
btot=(m/blen)+(m%blen?1:0);
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++){
cin>>s[i].l>>s[i].r;
s[i].id=i;
}
}
bool cmpA(ask a,ask b){
return a.l<b.l;
}
bool cmpP(ask a,ask b){
return a.r<b.r;
}
long long out[N],ans;
void work(int x,int c){
ans+=2*cnt[x]*c+c*c;
cnt[x]+=c;
}
int main(){
build();
sort(s+1,s+1+n,cmpA);
for(int i=1;i<=btot;i++){
sort(s+L(i),s+1+R(i),cmpP);
}
int l=1,r=0;
for(int i=1;i<=n;i++){//注意这里n打错了
while(l>s[i].l) l--,work(a[l],1);
while(r<s[i].r) r++,work(a[r],1);
while(l<s[i].l) work(a[l],-1),l++;
while(r>s[i].r) work(a[r],-1),r--;
out[s[i].id]=ans;
}
for(int i=1;i<=m;i++){
cout<<out[i]<<endl;
}
return 0;
}