Submission #1871605


Source Code Expand

#include<cstdio>
#include<cstring>
#define LL long long
#define ll LL
#define N 1200000
#define INF 0x3f3f3f3f3f3f3f3f
LL n,m,x,y,ans,mn1,mn2,a[N],seg[2][N],seg0[2][N],seg1[2][N],tag[2][N];
 
ll min(ll x,ll y){ return x<y?x:y; }
ll max(ll x,ll y){ return x>y?x:y; }
ll abs(ll x){ return x>0?x:-x; }
void add(LL op,LL k){
	seg[op][1]+=k; seg0[op][1]+=k; seg1[op][1]+=k; tag[op][1]+=k;
}
 
void pushdown(LL op,LL x){
	tag[op][x<<1]+=tag[op][x]; tag[op][x<<1|1]+=tag[op][x];
	seg[op][x<<1]=min(INF,seg[op][x<<1]+tag[op][x]);
	seg0[op][x<<1]=min(INF,seg0[op][x<<1]+tag[op][x]);
	seg1[op][x<<1]=min(INF,seg1[op][x<<1]+tag[op][x]);
	seg[op][x<<1|1]=min(INF,seg[op][x<<1|1]+tag[op][x]);
	seg0[op][x<<1|1]=min(INF,seg0[op][x<<1|1]+tag[op][x]);
	seg1[op][x<<1|1]=min(INF,seg1[op][x<<1|1]+tag[op][x]);
	tag[op][x]=0;
}
 
void mdf(LL op,LL x,LL l,LL r,LL t,LL k){
	if (l==r){
		if (seg[op][x]>k){seg[op][x]=k; seg0[op][x]=k-t; seg1[op][x]=k+t;}
		return;
	}
	pushdown(op,x);
	LL mid=(l+r)>>1;
	if (t<=mid) mdf(op,x<<1,l,mid,t,k);
	if (t>mid) mdf(op,x<<1|1,mid+1,r,t,k);
	seg[op][x]=min(seg[op][x<<1],seg[op][x<<1|1]);
	seg0[op][x]=min(seg0[op][x<<1],seg0[op][x<<1|1]);
	seg1[op][x]=min(seg1[op][x<<1],seg1[op][x<<1|1]);
}
		
LL qry1(LL op,LL x,LL l,LL r,LL L,LL R){
	if (l>=L && r<=R){
		return seg0[op][x];
	}
	LL mid=(l+r)>>1,tmp=INF;
	pushdown(op,x);
	if (L<=mid) tmp=min(tmp,qry1(op,x<<1,l,mid,L,R));
	if (R>mid) tmp=min(tmp,qry1(op,x<<1|1,mid+1,r,L,R));
	return tmp;
}
 
LL qry2(LL op,LL x,LL l,LL r,LL L,LL R){
	if (l>=L && r<=R){
		return seg1[op][x];
	}
	LL mid=(l+r)>>1,tmp=INF;
	pushdown(op,x);
	if (L<=mid) tmp=min(tmp,qry2(op,x<<1,l,mid,L,R));
	if (R>mid) tmp=min(tmp,qry2(op,x<<1|1,mid+1,r,L,R));
	return tmp;
}
 
int main(){
	scanf("%lld%lld%lld%lld",&n,&m,&x,&y);
	memset(seg,0x3f,sizeof seg);
	memset(seg0,0x3f,sizeof seg0);
	memset(seg1,0x3f,sizeof seg1);
	mdf(0,1,1,n,x,0); mdf(1,1,1,n,y,0);
	for (LL i=1;i<=m;i++){
		scanf("%lld",&a[i]);
		mn1=mn2=INF;
		mn1=qry1(0,1,1,n,1,a[i])+a[i];
		mn1=min(mn1,qry2(0,1,1,n,a[i],n)-a[i]);
		mn2=qry1(1,1,1,n,1,a[i])+a[i];
		mn2=min(mn2,qry2(1,1,1,n,a[i],n)-a[i]);
		add(0,abs(a[i]-(i==1?y:a[i-1])));
		add(1,abs(a[i]-(i==1?x:a[i-1])));
		mdf(0,1,1,n,i==1?x:a[i-1],mn2); mdf(1,1,1,n,i==1?y:a[i-1],mn1);
	}
	printf("%lld\n",min(seg[0][1]+tag[0][1],seg[1][1]+tag[1][1]));
	
	return 0;
}

Submission Info

Submission Time
Task F - Many Moves
User miaom
Language C++14 (GCC 5.4.1)
Score 900
Code Size 2412 Byte
Status AC
Exec Time 460 ms
Memory 71808 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:64:39: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld%lld",&n,&m,&x,&y);
                                       ^
./Main.cpp:70:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld",&a[i]);
                      ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 4
AC × 34
Set Name Test Cases
Sample example0, example1, example2, example3
All example0, example1, example2, example3, flower0, flower1, flower2, flower3, flower4, flower5, flower6, flower7, flower8, flower9, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, smallrand5, smallrand6, smallrand7, smallrand8, smallrand9
Case Name Status Exec Time Memory
example0 AC 15 ms 59520 KB
example1 AC 15 ms 59520 KB
example2 AC 15 ms 59520 KB
example3 AC 15 ms 59520 KB
flower0 AC 433 ms 71808 KB
flower1 AC 424 ms 71808 KB
flower2 AC 425 ms 71808 KB
flower3 AC 429 ms 71808 KB
flower4 AC 428 ms 71808 KB
flower5 AC 427 ms 71808 KB
flower6 AC 426 ms 71808 KB
flower7 AC 428 ms 71808 KB
flower8 AC 427 ms 71808 KB
flower9 AC 426 ms 71808 KB
maxrand0 AC 440 ms 71808 KB
maxrand1 AC 443 ms 71808 KB
maxrand2 AC 443 ms 71808 KB
maxrand3 AC 441 ms 71808 KB
maxrand4 AC 445 ms 71808 KB
maxrand5 AC 460 ms 71808 KB
maxrand6 AC 448 ms 71808 KB
maxrand7 AC 445 ms 71808 KB
maxrand8 AC 445 ms 71808 KB
maxrand9 AC 440 ms 71808 KB
smallrand0 AC 15 ms 59520 KB
smallrand1 AC 15 ms 59520 KB
smallrand2 AC 15 ms 59520 KB
smallrand3 AC 15 ms 59520 KB
smallrand4 AC 15 ms 59520 KB
smallrand5 AC 15 ms 59520 KB
smallrand6 AC 15 ms 59520 KB
smallrand7 AC 15 ms 59520 KB
smallrand8 AC 15 ms 59520 KB
smallrand9 AC 15 ms 59520 KB