Submission #3878535


Source Code Expand

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
#define chmax(a,b) a=max(a,b);
typedef long long ll;

using namespace std;
#define N 100010
#define D 20
class RMQxRMQ{
private:
    ll dat[2*N],laz[2*N];
    ll eval(ll k){
	if(k<N){
	    chmax(laz[k*2],laz[k]);
	    chmax(laz[k*2+1],laz[k]);
	}
	chmax(dat[k],laz[k]); laz[k]=-1;
	return dat[k];
    }
public:
    void Init(){
	lol(i,2*N)dat[i]=laz[i]=-1;
    }
    void Upd(ll l,ll r,ll x){
	l+=N,r+=N+1;
	for(ll a=l,b=r;a<b;a>>=1,b>>=1){
	    if(a&1){chmax(laz[a],x);a++;}
	    if(b&1){b--;chmax(laz[b],x);}
	}
	for(ll a=l,b=r;a+b;a>>=1,b>>=1){
	    dat[a/2]=max(eval(a),eval(a^1));
	    dat[b/2]=max(eval(b),eval(b^1));
	}
    }
    ll Qry(ll l,ll r){
	l+=N,r+=N+1;
	for(ll i=D;i;i--)eval(l>>i),eval(r>>i);
	ll res=0;
	for(ll a=l,b=r;a<b;a>>=1,b>>=1){
	    if(a&1)res=max(res,eval(a++));
	    if(b&1)res=max(res,eval(--b));
	}
	return res;
    }
};

ll s[N][D][2],n,k,q,a[N];
void MakeTree(ll ind){
    RMQxRMQ seg;seg.Init();
    lol(i,n){
	ll to=seg.Qry(a[i],N-2);
	if(!ind)s[i][0][0]=to;
	else s[n-1-i][0][1]=n-1-to;
	seg.Upd(a[i],a[i],i);
    }
    s[0][0][0]=s[0][0][1]=0;
    s[n-1][0][0]=s[n-1][0][1]=n-1;
}
ll $0(ll a,ll b,ll d){
    return min(s[a][d][0],s[b][d][0]);
}
ll $1(ll a,ll b,ll d){
    return max(s[a][d][1],s[b][d][1]);
}
void MakeTable(){
    for(ll d=0;d<D-1;d++)lol(i,n){
	ll a=s[i][d][0],b=s[i][d][1];
	s[i][d+1][0]=$0(a,b,d);
	s[i][d+1][1]=$1(a,b,d);
    }
}
void Solve(ll a,ll b){
    ll l=a,r=b,ans=0;
    a=b=r;
    for(ll d=D-2;~d;d--){
	ll v0=$0(a,b,d),v1=$1(a,b,d);
	if(v0<=l)continue;
	a=v0,b=v1,ans+=(1LL<<d);
    }
    r=a,a=b=l;
    for(ll d=D-2;~d;d--){
	ll v0=$0(a,b,d),v1=$1(a,b,d);
	if(v1>=r)continue;
	a=v0,b=v1,ans+=(1LL<<d);
    }
    printf("%lld\n",ans);
}
int main(){
    cin>>n>>k>>q;
    lol(i,n)scanf("%lld",&a[i+1]);
    n+=2,a[0]=a[n-1]=N-2;
    lol(r,2){
	MakeTree(r);
	lol(i,n/2)swap(a[i],a[n-i-1]);
    }
    MakeTable();
    lol(u,q){
	ll a,b;cin>>a>>b;
	Solve(min(a,b),max(a,b));
    }
    return 0;
}

Submission Info

Submission Time
Task F - 鉄道旅行 (Railway Trip)
User ynymxiaolongbao
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2201 Byte
Status AC
Exec Time 480 ms
Memory 35968 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:94:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     lol(i,n)scanf("%lld",&a[i+1]);
                                  ^

Judge Result

Set Name Subtask01 Subtask02 Subtask03 Subtask04
Score / Max Score 5 / 5 15 / 15 25 / 25 55 / 55
Status
AC × 12
AC × 31
AC × 16
AC × 70
Set Name Test Cases
Subtask01 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, sample-01.txt, sample-02.txt, sample-03.txt
Subtask02 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, 02-15.txt, 02-16.txt, 02-17.txt, 02-18.txt, 02-19.txt, sample-01.txt, sample-02.txt, sample-03.txt
Subtask03 03-01.txt, 03-02.txt, 03-03.txt, 03-04.txt, 03-05.txt, 03-06.txt, 03-07.txt, 03-08.txt, 03-09.txt, 03-10.txt, 03-11.txt, 03-12.txt, 03-13.txt, 03-14.txt, 03-15.txt, 03-16.txt, sample-02
Subtask04 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, 02-15.txt, 02-16.txt, 02-17.txt, 02-18.txt, 02-19.txt, 03-01.txt, 03-02.txt, 03-03.txt, 03-04.txt, 03-05.txt, 03-06.txt, 03-07.txt, 03-08.txt, 03-09.txt, 03-10.txt, 03-11.txt, 03-12.txt, 03-13.txt, 03-14.txt, 03-15.txt, 03-16.txt, 04-01.txt, 04-02.txt, 04-03.txt, 04-04.txt, 04-05.txt, 04-06.txt, 04-07.txt, 04-08.txt, 04-09.txt, 04-10.txt, 04-11.txt, 04-12.txt, 04-13.txt, 04-14.txt, 04-15.txt, 04-16.txt, 04-17.txt, 04-18.txt, 04-19.txt, 04-20.txt, 04-21.txt, 04-22.txt, 04-23.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 3 ms 3456 KB
01-02.txt AC 3 ms 3456 KB
01-03.txt AC 3 ms 3456 KB
01-04.txt AC 4 ms 3456 KB
01-05.txt AC 4 ms 3456 KB
01-06.txt AC 4 ms 3456 KB
01-07.txt AC 4 ms 3456 KB
01-08.txt AC 4 ms 3456 KB
01-09.txt AC 3 ms 3328 KB
02-01.txt AC 5 ms 3712 KB
02-02.txt AC 140 ms 35456 KB
02-03.txt AC 141 ms 35456 KB
02-04.txt AC 138 ms 35456 KB
02-05.txt AC 140 ms 35456 KB
02-06.txt AC 143 ms 35456 KB
02-07.txt AC 158 ms 35456 KB
02-08.txt AC 135 ms 35456 KB
02-09.txt AC 146 ms 35456 KB
02-10.txt AC 142 ms 35328 KB
02-11.txt AC 146 ms 35456 KB
02-12.txt AC 151 ms 35456 KB
02-13.txt AC 146 ms 35456 KB
02-14.txt AC 146 ms 35456 KB
02-15.txt AC 145 ms 35456 KB
02-16.txt AC 144 ms 35456 KB
02-17.txt AC 135 ms 35456 KB
02-18.txt AC 133 ms 35456 KB
02-19.txt AC 148 ms 35456 KB
03-01.txt AC 416 ms 35840 KB
03-02.txt AC 416 ms 35840 KB
03-03.txt AC 418 ms 35840 KB
03-04.txt AC 411 ms 35840 KB
03-05.txt AC 413 ms 35840 KB
03-06.txt AC 412 ms 35840 KB
03-07.txt AC 411 ms 35840 KB
03-08.txt AC 424 ms 35968 KB
03-09.txt AC 466 ms 35968 KB
03-10.txt AC 464 ms 35968 KB
03-11.txt AC 471 ms 35968 KB
03-12.txt AC 480 ms 35968 KB
03-13.txt AC 472 ms 35968 KB
03-14.txt AC 446 ms 35968 KB
03-15.txt AC 428 ms 35840 KB
03-16.txt AC 413 ms 35840 KB
04-01.txt AC 422 ms 35712 KB
04-02.txt AC 420 ms 35712 KB
04-03.txt AC 408 ms 35712 KB
04-04.txt AC 416 ms 35712 KB
04-05.txt AC 480 ms 35968 KB
04-06.txt AC 465 ms 35968 KB
04-07.txt AC 451 ms 35968 KB
04-08.txt AC 452 ms 35968 KB
04-09.txt AC 434 ms 35840 KB
04-10.txt AC 437 ms 35840 KB
04-11.txt AC 420 ms 35840 KB
04-12.txt AC 428 ms 35840 KB
04-13.txt AC 425 ms 35840 KB
04-14.txt AC 444 ms 35968 KB
04-15.txt AC 443 ms 35968 KB
04-16.txt AC 445 ms 35968 KB
04-17.txt AC 453 ms 35968 KB
04-18.txt AC 459 ms 35968 KB
04-19.txt AC 445 ms 35840 KB
04-20.txt AC 424 ms 35840 KB
04-21.txt AC 396 ms 35712 KB
04-22.txt AC 389 ms 35712 KB
04-23.txt AC 411 ms 35712 KB
sample-01.txt AC 3 ms 3328 KB
sample-02.txt AC 3 ms 3328 KB
sample-03.txt AC 3 ms 3328 KB