QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#20581#1877. Matryoshka Dollsjr_zlwWA 9ms5344kbC++172.2kb2022-02-16 18:02:172022-05-03 10:39:25

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-03 10:39:25]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:5344kb
  • [2022-02-16 18:02:17]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#define rep(a,b,c) for(int c=(a);c<=(b);++c)
inline int read()
{
	int res=0;char ch=getchar();while(ch<'0'||ch>'9')ch=getchar();
	while(ch<='9'&&ch>='0')res=res*10+(ch^48),ch=getchar();return res;
}
const int BK=333,N=1e5+10,QN=5e5+10;int loc[N],a[N],w[N],n,Q,lc[N],rc[N],stk[N],blk,B[N],top;long long Ans,ans[QN];
struct Query{int l,r,idx;inline bool operator<(const Query &Q)const{return loc[l]==loc[Q.l]?r>Q.r:l<Q.l;};}t[QN];
inline int Abs(const int &x){return x<0?-x:x;}
inline void del(const int &x)
{
//	if(!lc[x]&&!rc[x])return;
	if(!lc[x]){Ans-=Abs(w[rc[x]]-w[x]);lc[rc[x]]=0;stk[++top]=x;return;}
	if(!rc[x]){Ans-=Abs(w[lc[x]]-w[x]);rc[lc[x]]=0;stk[++top]=x;return;}
	Ans+=Abs(w[lc[x]]-w[rc[x]])-Abs(w[lc[x]]-w[x])-Abs(w[rc[x]]-w[x]);
	rc[lc[x]]=rc[x];lc[rc[x]]=lc[x];stk[++top]=x;
}
inline void Del(const int &x)
{
//	if(!lc[x]&&!rc[x]){return;}
	if(!lc[x]){Ans-=Abs(w[rc[x]]-w[x]);lc[rc[x]]=0;stk[++top]=x;return;}
	if(!rc[x]){Ans-=Abs(w[lc[x]]-w[x]);rc[lc[x]]=0;stk[++top]=x;return;}
	Ans+=Abs(w[lc[x]]-w[rc[x]])-Abs(w[lc[x]]-w[x])-Abs(w[rc[x]]-w[x]);
	rc[lc[x]]=rc[x];lc[rc[x]]=lc[x];
}
inline void ins(const int &x)
{
//	if(!lc[x]&&!rc[x])return;
	if(!lc[x]){Ans+=Abs(w[x]-w[rc[x]]);lc[rc[x]]=x;return;}
	if(!rc[x]){Ans+=Abs(w[x]-w[lc[x]]);rc[lc[x]]=x;return;}
	Ans-=Abs(w[lc[x]]-w[rc[x]])-Abs(w[lc[x]]-w[x])-Abs(w[rc[x]]-w[x]);
	rc[lc[x]]=lc[rc[x]]=x;return;
}
inline void BackTo(const int &x){while(top>x){ins(stk[top--]);}}
inline void Init(){rep(1,n,i){loc[i]=(i-1)/BK+1;if(loc[i]!=loc[i-1])B[++blk]=i;}}
int main()
{
	n=read();Q=read();rep(1,n,i)w[a[i]=read()]=i;Init();
	rep(1,Q,i)t[i].l=read(),t[i].r=read(),t[i].idx=i;std::sort(t+1,t+Q+1);
	for(int i=1;i<n;++i)lc[i]=i-1,rc[i]=i+1,Ans+=Abs(w[i]-w[i+1]);lc[n]=n-1;
//	rep(1,Q,i)printf("%d :: [%d,%d]\n",i,t[i].l,t[i].r);
	int b=1,R=n;rep(1,Q,i)
	{
		if(loc[t[i].l]!=loc[t[i-1].l]){BackTo(0);for(int j=B[b];j<B[b+1];++j)Del(j);++b;R=n;}
		while(R>t[i].r)del(a[R--]);int tmp=top;for(int j=B[b];j<t[i].l;++j)del(a[j]);ans[t[i].idx]=Ans;BackTo(tmp);
//		printf("%d :: ",i);rep(1,n,i)printf("(%d,%d) ",lc[i],rc[i]);putchar('\n');
	}
	rep(1,Q,i)printf("%lld\n",ans[i]);return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3664kb

input:

5 5
1 5 2 4 3
1 5
1 4
1 3
1 2
1 1

output:

7
5
3
1
0

result:

ok 5 number(s): "7 5 3 1 0"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3584kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 9ms
memory: 5344kb

input:

100000 1
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...

output:

4966705611

result:

wrong answer 1st numbers differ - expected: '4999950000', found: '4966705611'