全T???
查看原帖
全T???
196522
HuaJi_360楼主2020/5/22 17:13

rt,我莫队全TLE了,有dalao来救救孩子吗?

#include<bits/stdc++.h>
#define reg register int
using namespace std;
struct line{
	int l,r,num,pos;
}q[50010];
long long n,m,k,a[50010],ans[50010],s[50010],size,id[50010];
bool cmp(line x,line y){
	if(id[x.r]==id[y.r])return x.r<y.r;
	else return id[x.r]<id[y.r];
}
int main(){
	cin>>n>>m>>k;
	size=sqrt(n);
	for(reg i=1;i<=n;i++){
		cin>>a[i];
		id[i]=(i-1)/size+1;
	}
	for(reg i=1;i<=m;i++){
		cin>>q[i].l>>q[i].r;
		q[i].num=i;
	}
	sort(q+1,q+m+1,cmp);
	long long l=1,r=0,sum=0;
	for(reg i=1;i<=m;i++){
		while(l<q[i].l){
			s[a[l]]--;
			sum-=s[a[l]]*2+1;
			l++;
		}
		while(l>q[i].l){
			l--;
			s[a[l]]++;
			sum+=s[a[l]]*2-1;
		}
		while(r<q[i].r){
			r++;
			s[a[r]]++;
			sum+=s[a[r]]*2-1;
		}
		while(r>q[i].r){
			s[a[r]]--;
			sum-=s[a[r]]*2+1;
			r--;
		}
		ans[q[i].num]=sum;
	}
	for(reg i=1;i<=m;i++){
		cout<<ans[i]<<endl;
	}
	return 0;
}
2020/5/22 17:13
加载中...