95pts 卡常求调(上一个代码挂了,重发)
查看原帖
95pts 卡常求调(上一个代码挂了,重发)
315005
White_gugu楼主2022/11/9 21:44

请大佬们看看有什么可以优化的QAQ

#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
const int N = 500500;
using namespace std;
namespace IO
{
	const int NN=1<<21;
	char bucketR[NN],*frontR=bucketR,*backR=bucketR;
	char getchar()
	{
		return frontR==backR && (backR=bucketR+fread(bucketR,1,NN,stdin))==(frontR=bucketR)?EOF:*frontR++;
	}
	template <class type> type read()
	{
		char ch=getchar(); type x=0; bool sig=0;
		for (;!isdigit(ch);ch=getchar()) if (ch=='-') sig=1;
		for (;isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
		return sig?-x:x;
	}
	template <class type> void read(type &x) {x=read<type>();}
	template <class type,class...args> void read(type &x,args &...y)
		{read(x),read(y...);}
	template <class type> void read(type *x,type *y)
	{
		for (type *i=x;i!=y;++i) read(*i);
	}
	char bucketW[NN],*frontW=bucketW,*backW=bucketW+NN;
	void putchar(char ch)
	{
		if (frontW==backW)
		{
			fwrite(bucketW,1,NN,stdout);
			frontW=bucketW;
		}
		*frontW++ = ch;
	}
	template <class type> inline void write(type x)
	{
		if (x<0) return (void)(putchar('-'),write(-x));
		if (x>9) write(x/10);
		putchar(x%10+'0');
	}
	template <class type,class ... args> void write(type x,args ... y)
	{
		write(x),putchar(' '),write(y...);
	}
	template <class type> void write(type * x, type * y, char c)
	{
		for (type *i=x;i!=y;++i,putchar(c)) write(*i);
	}
	void flush()
	{
		fwrite(bucketW,1,frontW-bucketW,stdout);
	}
}
using namespace IO;
struct Query{
	int l,r,id;
}q[N];
int n,m,block,key[N],st[1010],en[1010],a[N];
int lst[N],nxt[N],cnt[1010],px[1010][1010],Lst,Nxt;
long long val[N];
bool bz[N];
long long an[N];
bool cmp(Query x,Query y){
	return key[x.l]<key[y.l]||(key[x.l]==key[y.l]&&x.r>y.r);
}
long long ab(long long x){
	if(x<0) return -x;
	else return x;
}
int main(){
//	freopen("rrads.in","r",stdin);
//	freopen("rrads.out","w",stdout);
	read(n,m);
	block=sqrt(n*2);
	for(register int i=1;i<=n;i++){
		read(a[i]);
		val[a[i]]=i;
		key[i]=i/block+1;
		if(!st[key[i]]) st[key[i]]=i;
		en[key[i]]=i;
		cnt[key[i]]++;
		px[key[i]][cnt[key[i]]]=a[i];
	}
	for(register int i=1;i<=key[n];i++)
	sort(px[i],px[i]+cnt[i]+1);
	for(register int i=1;i<=m;i++)
	read(q[i].l,q[i].r),q[i].id=i;
	
	sort(q+1,q+m+1,cmp);
	int R=n;
	long long Ans=0;
	for(register int i=1;i<=m;i++){
		if(key[q[i].l]!=key[q[i-1].l]){
			for(register int j=st[key[q[i-1].l]];j<=R;j++) bz[a[j]]=0,lst[a[j]]=nxt[a[j]]=0;
			for(register int j=st[key[q[i].l]];j<=n;j++) bz[a[j]]=1;
			int Lst=0;
			Ans=0;
			for(register int j=1;j<=n;j++){
				if(!bz[j]) continue;
				if(Lst) lst[j]=Lst,nxt[Lst]=j,Ans=Ans+ab(val[j]-val[Lst]);
				Lst=j;
			}
			R=n;
		}
		long long ans=0;
		if(key[q[i].l]==key[q[i].r]){
			Lst=0;
			for(register int j=1;j<=en[key[q[i].l]]-st[key[q[i].l]]+1;j++){
				if(val[px[key[q[i].l]][j]]<q[i].l||val[px[key[q[i].l]][j]]>q[i].r) continue;
				if(Lst) ans=ans+ab(val[Lst]-val[px[key[q[i].l]][j]]);
				Lst=px[key[q[i].l]][j];
			}
			an[q[i].id]=ans;
			continue;
		}
		while(R>q[i].r){
            bz[a[R]]=0; 
			Lst=lst[a[R]],Nxt=nxt[a[R]];
			lst[a[R]]=nxt[a[R]]=0;
			if(Lst) Ans=Ans-ab(val[Lst]-val[a[R]]);
			if(Nxt) Ans=Ans-ab(val[Nxt]-val[a[R]]);
			lst[Nxt]=Lst,nxt[Lst]=Nxt;
			if(Lst&&Nxt) Ans=Ans+ab(val[Lst]-val[Nxt]);
			R--;
		}
		ans=Ans;
		for(register int j=st[key[q[i].l]];j<q[i].l;j++){
			Lst=lst[a[j]],Nxt=nxt[a[j]];
			if(Lst) ans=ans-ab(val[Lst]-val[a[j]]);
			if(Nxt) ans=ans-ab(val[Nxt]-val[a[j]]);
			lst[Nxt]=Lst,nxt[Lst]=Nxt;
			if(Lst&&Nxt) ans=ans+ab(val[Lst]-val[Nxt]);
		}
		an[q[i].id]=ans;
		for(register int j=q[i].l-1;j>=st[key[q[i].l]];j--){
			Lst=lst[a[j]],Nxt=nxt[a[j]];
			nxt[Lst]=a[j],lst[Nxt]=a[j];
		}
	}
	for(register int i=1;i<=m;i++)
	write(an[i]),putchar('\n');
	flush();
}
2022/11/9 21:44
加载中...