求助莫队
查看原帖
求助莫队
126957
酱紫林楼主2021/11/17 09:22
#include<bits/stdc++.h>
#define N 51999
#define int long long
using namespace std;

int n,m;
int a[N];
int sqr,cnt[N];
struct node{
	int l,r,id;
	bool operator < (const node &s){
		if(l/sqr==s.l/sqr) return r<s.r;
		else return l/sqr<s.l/sqr;
	}
}q[N];
int anss[N][2];
int c(int x){
	return x*(x-1)/2;
}

signed main(){
	 
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	sqr=sqrt(n);
	for(int i=1;i<=m;i++){
		scanf("%lld%lld",&q[i].l,&q[i].r);
		q[i].id=i;
	}
	sort(q+1,q+m+1);
	for(int i=q[1].l;i<=q[1].r;i++){
		cnt[a[i]]++;
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		ans+=c(cnt[a[i]]);
	}
	int x=q[1].l,y=q[1].r;
	for(int i=1;i<=m;i++){
		//先扩大后缩小!!! 
		while(x>q[i].l){   //x,y  ->  x-1,y
			int p=a[x-1];
			ans=ans-c(cnt[p]);
			cnt[p]++;
			ans=ans+c(cnt[p]);
			x--;
		}
		while(y<q[i].r){   //x,y  ->  x,y+1
			int p=a[y+1];
			ans=ans-c(cnt[p]);
			cnt[p]++;
			ans=ans+c(cnt[p]);
			y++;
		}
		while(y>q[i].r){   //x,y  ->  x,y-1
			int p=a[y];
			ans=ans-c(cnt[p]);
			cnt[p]--;
			ans=ans+c(cnt[p]);
			y--;
		}
		while(x<q[i].l){   //x,y  ->  x+1,y
			int p=a[x];
			ans=ans-c(cnt[p]);
			cnt[p]--;
			ans=ans+c(cnt[p]);
			x++;
		}
		if(q[i].l==q[i].r){
		    anss[q[i].id][0]=0;
		    anss[q[i].id][1]=1;
		    continue;
		}
		int zong=ans==0||zong==0?1:c(q[i].r-q[i].l+1),gg=ans==0?1:__gcd(ans,zong);
		anss[q[i].id][0]=ans/gg;
		anss[q[i].id][1]=zong/gg;
	}
	for(int i=1;i<=m;i++){
		printf("%lld/%lld\n",anss[i][0],anss[i][1]);
	}
	return 0;
}

昨天还好好的,今天本机编译都过不了,交一直70分,分别是1,5,9的点,大佬们帮帮我吧

2021/11/17 09:22
加载中...