QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#19132#1877. Matryoshka DollswyzwyzCompile Error//C++142.2kb2022-01-28 11:33:102022-05-18 04:05:42

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-18 04:05:42]
  • 评测
  • [2022-01-28 11:33:10]
  • 提交

answer

#include<cstdio>
#include<cctype>
#include<set>
#include<cmath>
#include<algorithm>
#include<iostream>

using std::cerr;
using std::endl;

#define maxn 505505

template<class T>

inline T read(){
	T r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

template<class T>

inline T abs(T a){
	return a<0?-a:a;
}

int n,m,a[maxn],num[maxn],pos[maxn];

struct Q{
	int l,r,num;
	bool operator <(const Q &q) const{
		return pos[l]^pos[q.l]?l<q.l:r>q.r;
	}
}q[maxn];

int id[maxn],lst[maxn],nxt[maxn],pre[maxn],suf[maxn];

long long Sum,sum,ans[maxn];

inline bool cmp(int x,int y){
	return a[x]<a[y];
}

int top,sta[maxn][3];

inline void del(int x){
	int Pre=pre[x],Suf=suf[x];
	if(Pre&&Suf)sum+=abs(Pre-Suf);
	if(Pre)sum-=abs(x-Pre);
	if(Suf)sum-=abs(x-Suf);
	suf[Pre]=Suf,pre[Suf]=Pre;
}

inline void Del(int x){
	int Pre=lst[x],Suf=nxt[x];
	if(Pre&&Suf)Sum+=abs(Pre-Suf);
	if(Pre)Sum-=abs(x-Pre);
	if(Suf)Sum-=abs(x-Suf);
	nxt[Pre]=Suf,lst[Suf]=Pre;
}

int main(){
	n=read<int>();
	m=read<int>();
	for(int i=1;i<=n;i++)
		a[i]=read<int>(),num[a[i]]=i;
	int B=max(n/sqrt(m)-50,1);
	for(int i=1;i<=n;i++)
		pos[i]=(i-1)/B+1,id[i]=i;
	for(int i=1;i<=m;i++){
		q[i].l=read<int>();
		q[i].r=read<int>();
		q[i].num=i;
	}
	std::sort(q+1,q+1+m);
	std::sort(id+1,id+1+n,cmp);
	for(int i=1;i<=n;i++){
		lst[id[i]]=id[i-1];
		nxt[id[i-1]]=id[i];
		sum+=abs(id[i]-id[i+1]);
	}
	sum-=id[n],Sum=sum;
	for(int i=1,j=1;j<=pos[n];j++){
		if(pos[q[i].l]==j){
			sum=Sum;
			int L=(j-1)*B+1,l=L,r=n;
			for(int k=l;k<=n;k++)
				pre[k]=lst[k],suf[k]=nxt[k];
			for(;pos[q[i].l]==j;i++){
				while(r>q[i].r)del(r--);
				long long lstsum=sum;
				top=0;
				while(l<q[i].l){
					top++,sta[top][1]=l;
					sta[top][0]=pre[l];
					sta[top][2]=suf[l];
					del(l++);
				}
				ans[q[i].num]=sum;
				sum=lstsum,l=L;
				while(top){
					int Pre=sta[top][0];
					int x=sta[top][1];
					int Suf=sta[top][2];
					suf[Pre]=x,pre[Suf]=x;
					top--;
				}
			}
		}
		for(int k=(j-1)*B+1;k<=j*B;k++)Del(k);
	}
	for(int i=1;i<=m;i++)
		printf("%lld\n",ans[i]);
	return 0;
}

詳細信息

answer.code: In function ‘int main()’:
answer.code:69:15: error: ‘max’ was not declared in this scope; did you mean ‘std::max’?
   69 |         int B=max(n/sqrt(m)-50,1);
      |               ^~~
      |               std::max
In file included from /usr/include/c++/11/algorithm:62,
                 from answer.code:5:
/usr/include/c++/11/bits/stl_algo.h:3467:5: note: ‘std::max’ declared here
 3467 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~