Submission #126556

# Submission time Handle Problem Language Result Execution time Memory
126556 2019-07-08T05:37:06 Z baluteshih Railway Trip (JOI17_railway_trip) C++14
5 / 100
2000 ms 29948 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);
			}
		}
		//for(int i=1;i<=k;++i,ET)
		//	for(int j=1;j<=n;++j)
		//		cout << "(" << "[" << LK[i][j].F << "," << LK[i][j].S << "],[" << RK[i][j].F << "," << RK[i][j].S << "]) ";
		while(q--)
		{
			cin >> a >> b,ans=INF;
			for(int i=max(val[a],val[b]);i<=k;++i)
			{
				//cout << 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]));//,cout << RK[i][a].S+RK[i][b].S+abs((int)(rv[RK[i][a].F]-rv[RK[i][b].F])) << "(1) ";
				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]));//,cout << LK[i][a].S+RK[i][b].S+abs((int)(rv[LK[i][a].F]-rv[RK[i][b].F])) << "(2) ";
				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]));//,cout << RK[i][a].S+LK[i][b].S+abs((int)(rv[RK[i][a].F]-rv[LK[i][b].F])) << "(3) ";
				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 << LK[i][a].S+LK[i][b].S+abs((int)(rv[LK[i][a].F]-rv[LK[i][b].F])) << "(4) ";
				//ET;
			}
			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";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2680 KB Output is correct
3 Correct 5 ms 2680 KB Output is correct
4 Correct 4 ms 2728 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 2680 KB Output is correct
8 Correct 4 ms 2680 KB Output is correct
9 Correct 4 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2808 KB Output is correct
2 Incorrect 54 ms 22368 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 170 ms 29948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2045 ms 8156 KB Time limit exceeded
2 Halted 0 ms 0 KB -