离线树状数组32pts求调
查看原帖
离线树状数组32pts求调
858608
BreadCakeCheese楼主2024/7/19 21:26

大号祭得惨烈

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
int n,m,a[N],c[N],ans[N];
struct node{
	int l,r,i;
}q[N];
bool cmp(node x,node y){ //以右端点为第一关键字排序 
	return x.r<y.r;
}
int lowbit(int x){
	return x&(-x);
}
void update(int x,int v){ //单点修改
	while(x<=n){
		c[x]+=v;
		x+=lowbit(x);
	}
}
int getsum(int x){ //求前缀和 
	int res=0;
	while(x>=1){
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	cin>>m;
	for(int i=1;i<=m;i++){
		scanf("%d %d",&q[i].l,&q[i].r);
		q[i].i=i;
	}
	sort(q+1,q+1+m,cmp);
	int nx=1; 
	int v[N]; //记录每个数字上次出现的位置 
	for(int i=1;i<=m;i++){
		for(int j=nx;j<=q[i].r;j++){
			if(v[a[j]]) update(v[a[j]],-1);
			update(j,1);
			v[a[j]]=j;
		}
		nx=q[i].r+1;
		ans[q[i].i]=getsum(q[i].r)-getsum(q[i].l-1);
	}
	for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
	return 0;
}

调了两个小时了,从8分到了32分,求调

另外有没有dalao可以解释一下为什么数组从1e6+10开到1e7就可以从8分到32分吗

2024/7/19 21:26
加载中...