histcat

histcat

[JOI 2022 Final] Railway Trip 2

Title#

The IOI Railway Company operates lines on a railway track. The track is a straight line with $N$ stations, numbered from $1$ to $N$. Station $i$ is directly connected to station $i + 1$ by a track.

The IOI Railway Company operates $M$ lines, numbered from $1$ to $M$. The starting point of line $j$ is $A_j$, and the endpoint is $B_j$. The train stops at each station, meaning if $A_j < B_j$, a train on line $j$ will stop in order at stations $A_j, A_j + 1, \ldots, B_j$. If $A_j > B_j$, a train on line $j$ will stop in order at stations $A_j, A_j - 1, \ldots, B_j$.

JOI is a traveler. He has $Q$ travel plans. In the $k$-th travel plan, he plans to travel from station $S_k$ to station $T_k$ by train.

However, JOI is exhausted from his long journey. He hopes that the trains he takes have empty seats for him to rest. Therefore, JOI decides that he will only take a train on a line if the starting station is the $K$-th station or earlier. In other words, for line $j$, if $A_j < B_j$, he will only board the train on line $j$ at stations $A_j, A_j + 1, \ldots, \min { A_j + K - 1, B_j - 1 }$. If $A_j > B_j$, he will only board the train on line $j$ at stations $A_j, A_j - 1, \ldots, \max { A_j - K + 1, B_j + 1 }$.

Under these conditions, JOI hopes to minimize the number of train rides he takes.

Given the line information of the IOI Railway Company and JOI's plans, write a program to calculate the minimum number of train rides required for each of JOI's plans.

For $100%$ of the data, $2 \le N \le {10}^5$, $1 \le K \le N - 1$, $1 \le M \le 2 \times {10}^5$, $1 \le Q \le 5 \times {10}^4$, $1 \le A_j, B_j, S_k, T_k \le N$, $A_j \ne B_j$, $S_k \ne T_k$, $(A_j, B_j) \ne (A_k, B_k)$ ($j \ne k$), $(S_k, T_k) \ne (S_l, T_l)$ ($k \ne l$).

Solution#

This problem was written to learn algorithms, and the solution was copied

Brute Force Method#

Start from the starting point and perform a depth-first search, leading to explosive complexity.

Slight Optimization#

Let f[i][j] represent the farthest point that can be reached from point i by taking j routes (pair, left and right endpoints).

Preprocess all f[i][j], and enumerate all points k in f[i][j - 1] to see the maximum left and right values that can be reached by f[k][1].

Complexity $O(n^2m)$, which will explode.

Further Optimization#

Seeing this kind of walking~~ stopping~~ problem, consider whether doubling can be applied.

Let f[i][j] represent the farthest point that can be reached from point i by taking $2^j$ routes (pair, left and right endpoints).

Consider the transition, enumerate each position k in f[i][j - 1], and find the maximum left and right values that can be reached by f[k][j - 1].

Clearly, brute force enumeration to find the maximum value is not feasible, but for each 2^j representing j, a segment tree can be established (since there are no modifications), and then it can be done.

How to handle result queries?

It is very similar to LCA, enumerate f[start][w], where w is enumerated from large to small. If the endpoint can be reached, continue to decrease w until it cannot be reached. Finally, if the ending point cannot be reached, output -1; otherwise, increment res by 1.

Code#

#include<bits/stdc++.h> 
#define PII pair<int, int>
#define l first
#define r second
using namespace std;
const int N = 1e5 + 10;
const int M = 2e5 + 10;
PII up_route[M], down_route[M];
int upcnt, downcnt;
int fl[N][20], fr[N][20];
int n, m, k;

int qwq[M], hh = 1, tt = 0;

bool cmp(PII A, PII B)
{
	return A.second > B.second;
}
const int LOG = 20;
struct ST
{
	int st[N][LOG];

	void init(int k,int lorr)
	{
		if(lorr == 0)
		{
			memset(st, 0x3f, sizeof st);
		}
		for(int i = 1;i <= n;i++)
			st[i][0] = ((!lorr) ? (fl[i][k]) : (fr[i][k]));
	}
	void prepare(int lorr)
	{
		for(int j = 1;j < LOG;j++)
		{
			for(int i = 1;i + (1 << j) - 1 <= n;i++)
			{
				st[i][j] = ((!lorr) ? min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]) : max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]));
			}
		}
	}

	int query(int l, int r, int lorr)
	{
		int lllooojjj = log2(r - l + 1);
		return ((!lorr) ? min(st[l][lllooojjj], st[r - (1 << lllooojjj) + 1][lllooojjj]) : max(st[l][lllooojjj], st[r - (1 << lllooojjj) + 1][lllooojjj]));
	}
}s[20][2];//0 represents fl, 1 represents fr 

int ask(int b, int t)
{
	int res = 0;
	int l = b, r = b;
	for(int k = 19;k >= 0;k--)
	{
		int L = s[k][0].query(l, r, 0);
		int R = s[k][1].query(l, r, 1);
		if(L <= t && t <= R) continue;
//		cout << k << " " << L  << " " << R << endl;
		res += (1 << k);
		l = L, r = R;
	}

	int L = s[0][0].query(l, r, 0);
	int R = s[0][1].query(l, r, 1);
	if(L <= t && t <= R) return res + 1;
	return -1;
}


int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	cin >> n >> k;

	cin >> m;
	int u, v;
	for(int i = 1;i <= m;i++)
	{
		cin >> u >> v;
		if(u < v)
		{
			up_route[++upcnt] = make_pair(u, v);
		}
		else
		{
			down_route[++downcnt] = make_pair(v, u);
		}
	}
	if(upcnt >= 1)
	sort(up_route + 1, up_route + upcnt + 1);
	if(downcnt >= 1)
	sort(down_route + 1, down_route + downcnt + 1, cmp);

	for(int i = 1;i <= n;i++)
		fl[i][0] = fr[i][0] = i;
	if(upcnt >= 1)
	for(int i = 1, j = 1;i <= n;i++)
	{
		while(j <= upcnt && up_route[j].l <= i)
		{
			while(hh <= tt && up_route[j].r >= up_route[qwq[tt]].r)
			{
				tt --;
			}
	
			qwq[++tt] = j;
			j++;
		}

		while(hh <= tt && up_route[qwq[hh]].l + k - 1 < i/*!!!*/)
		{
			hh++;
		} 
		if(hh > tt) continue;/*!!!*/
		fr[i][0] = max(fr[i][0], up_route[qwq[hh]].r);
	}
	memset(qwq, 0, sizeof qwq); 
	hh = 1, tt = 0;
	if(downcnt >= 1)
	for(int i = n, j = 1;i >= 1;i--)
	{
		while(j <= downcnt && down_route[j].r >= i)
		{
			while(hh <= tt && down_route[j].l <= down_route[qwq[tt]].l)
			{
				tt --;
			}
	
			qwq[++tt] = j;
			j++;
		}

		while(hh <= tt && down_route[qwq[hh]].r - k + 1 > i/*!!!*/)
		{
			hh++;
		}

		if(hh > tt) continue;/*!!!*/
		fl[i][0] = min(fl[i][0], down_route[qwq[hh]].l);
	}

	s[0][0].init(0, 0);
	s[0][0].prepare(0);
	s[0][1].init(0, 1);
	s[0][1].prepare(1);

	for(int k = 1;k < 20;k++)
	{
		for(int i = 1;i <= n;i++)
		{
			fl[i][k] = s[k - 1][0].query(fl[i][k - 1], fr[i][k - 1], 0);
			fr[i][k] = s[k - 1][1].query(fl[i][k - 1], fr[i][k - 1], 1);
		}
		s[k][0].init(k, 0);
		s[k][0].prepare(0);
		s[k][1].init(k, 1);
		s[k][1].prepare(1);
	}
	int Q;
	cin >> Q;
	int s, t;
	while(Q -- )
	{
		cin >> s >> t;
		cout << ask(s, t) << "\n";
	}

	return 0;
}

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.