답안 #126566

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
126566 2019-07-08T05:57:48 Z baluteshih Railway Trip (JOI17_railway_trip) C++14
5 / 100
2000 ms 29320 KB
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define MEM(i,j) memset(i,j,sizeof i)
#define ALL(v) v.begin(),v.end()
#define ET cout << "\n"
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const int INF=1e9;
vector<int> G[100005];
int val[100005],boss[100005],tp[100005],rv[100005];
vector<int> st;
vector<pii> QQ;
queue<int> qu;
int dis[100005];
pii LK[25][100005],RK[25][100005],L[100005],R[100005];

int finds(int x)
{
	if(x==boss[x]) return x;
	return boss[x]=finds(boss[x]);
}

void Union(int x,int y)
{
	x=finds(x),y=finds(y);
	boss[x]=y;
}
 
int main()
{jizz
	int n,k,q,a,b,ans;
	cin >> n >> k >> q;
	for(int i=1;i<=n;++i)
		cin >> val[i],boss[i]=i;
	if(k<=20)
	{
		for(int i=1;i<=n;++i)
		{
			while(!st.empty()&&val[i]>val[st.back()]) st.pop_back();
			if(!st.empty()&&val[st.back()]==val[i])
				Union(st.back(),i),st.back()=i;
			st.pb(i),QQ.pb(MP(val[i],i));
		}
		st.clear();
		for(int i=1;i<=n;++i)
		{
			LK[val[i]][i]=RK[val[i]][i]=MP(i,0);
			rv[i]=++tp[val[i]];
			while(!st.empty()&&val[st.back()]<val[i]) st.pop_back();
			if(!st.empty())
			{
				if(val[st.back()]>val[i])
					LK[val[st.back()]][i]=L[i]=MP(st.back(),1);
				else
					L[i]=L[st.back()],++L[i].S;
				for(int j=val[st.back()]+1;j<=k;++j)
					LK[j][i]=LK[j][st.back()],++LK[j][i].S;
				if(val[st.back()]>val[i]) st.pb(i);
				else st.back()=i;
			}
			else
				st.pb(i);
		}
		st.clear();
		for(int i=n;i>0;--i)
		{
			while(!st.empty()&&val[st.back()]<val[i]) st.pop_back();
			if(!st.empty())
			{
				if(val[st.back()]>val[i])
					RK[val[st.back()]][i]=R[i]=MP(st.back(),1);
				else
					R[i]=R[st.back()],++R[i].S;
				for(int j=val[st.back()]+1;j<=k;++j)
					RK[j][i]=RK[j][st.back()],++RK[j][i].S;
				if(val[st.back()]>val[i]) st.pb(i);
				else st.back()=i;
			}
			else
				st.pb(i);
		}
		sort(ALL(QQ));
		for(auto i:QQ)
		{
			for(int j=i.F+1;j<=k;++j)
			{
				if(L[i.S].F&&RK[j][i.S].F&&RK[j][L[i.S].F].F==RK[j][i.S].F)
					RK[j][i.S].S=min(RK[j][i.S].S,L[i.S].S+RK[j][L[i.S].F].S);
				if(R[i.S].F&&LK[j][i.S].F&&LK[j][R[i.S].F].F==LK[j][i.S].F)
					LK[j][i.S].S=min(LK[j][i.S].S,R[i.S].S+LK[j][R[i.S].F].S);
				if(L[i.S].F&&LK[j][i.S].F&&LK[j][L[i.S].F].F==LK[j][i.S].F)
					LK[j][i.S].S=min(LK[j][i.S].S,L[i.S].S+LK[j][L[i.S].F].S);
				if(R[i.S].F&&RK[j][i.S].F&&RK[j][L[i.S].F].F==RK[j][i.S].F)
					RK[j][i.S].S=min(RK[j][i.S].S,R[i.S].S+RK[j][R[i.S].F].S);
			}
		}
		while(q--)
		{
			cin >> a >> b,ans=INF;
			for(int i=max(val[a],val[b]);i<=k;++i)
			{
				if(RK[i][a].F&&RK[i][b].F&&finds(RK[i][a].F)==finds(RK[i][b].F))
					ans=min(ans,RK[i][a].S+RK[i][b].S+abs(rv[RK[i][a].F]-rv[RK[i][b].F]));
				if(LK[i][a].F&&RK[i][b].F&&finds(LK[i][a].F)==finds(RK[i][b].F))
					ans=min(ans,LK[i][a].S+RK[i][b].S+abs(rv[LK[i][a].F]-rv[RK[i][b].F]));
				if(RK[i][a].F&&LK[i][b].F&&finds(RK[i][a].F)==finds(LK[i][b].F))
					ans=min(ans,RK[i][a].S+LK[i][b].S+abs(rv[RK[i][a].F]-rv[LK[i][b].F]));
				if(LK[i][a].F&&LK[i][b].F&&finds(LK[i][a].F)==finds(LK[i][b].F))
					ans=min(ans,LK[i][a].S+LK[i][b].S+abs(rv[LK[i][a].F]-rv[LK[i][b].F]));
			}
			cout << ans-1 << "\n";
		}
	}
	else
	{
		for(int i=1;i<=n;++i)
		{
			while(!st.empty()&&val[i]>val[st.back()]) 
				G[i].pb(st.back()),G[st.back()].pb(i),st.pop_back();
			if(!st.empty())
				if(val[st.back()]==val[i])
					G[i].pb(st.back()),G[st.back()].pb(i),st.back()=i;
				else
					G[i].pb(st.back()),G[st.back()].pb(i),st.pb(i);
			else
				st.pb(i);
		}
		while(q--)
		{
			cin >> a >> b,fill(dis+1,dis+n+1,n+1);
			while(!qu.empty()) qu.pop();
			qu.push(a),dis[a]=0;
			while(!qu.empty())
			{
				int u=qu.front();
				qu.pop();
				if(u==b) break;
				for(int i:G[u])
					if(dis[i]>dis[u]+1)
						dis[i]=dis[u]+1,qu.push(i);
			}
			cout << dis[b]-1 << "\n";
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 20 ms 2680 KB Output is correct
3 Correct 5 ms 2680 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 4 ms 2684 KB Output is correct
8 Correct 4 ms 2680 KB Output is correct
9 Correct 4 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2808 KB Output is correct
2 Incorrect 55 ms 22400 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 163 ms 29320 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2036 ms 8060 KB Time limit exceeded
2 Halted 0 ms 0 KB -