102125: [AtCoder]ABC212 F - Greedy Takahashi

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

There are $N$ cities numbered $1$ through $N$, and $M$ buses that go between these cities. The $i$-th bus $(1 \leq i \leq M)$ departs from City $A_i$ at time $S_i+0.5$ and arrive at City $B_i$ at time $T_i+0.5$.

Takahashi will travel between these $N$ cities. When he is in City $p$ at time $t$, he will do the following.

  1. If there is a bus that departs from City $p$ not earlier than time $t$, take the bus that departs the earliest among those buses to get to another city.
  2. If there is no such bus, do nothing and stay in City $p$.

Takahashi repeats doing the above until there is no bus to take. It is guaranteed that all $M$ buses depart at different times, so the bus to take is always uniquely determined. Additionally, the time needed to change buses is negligible.

Here is your task: process $Q$ queries, the $i$-th $(1 \leq i \leq Q)$ of which is as follows.

  • If Takahashi begins his travel in City $Y_i$ at time $X_i$, in which city or on which bus will he be at time $Z_i$?

Constraints

  • $2 \leq N \leq 10^5$
  • $1 \leq M \leq 10^5$
  • $1 \leq Q \leq 10^5$
  • $1 \leq A_i,B_i \leq N\ (1 \leq i \leq M)$
  • $A_i \neq B_i\ (1 \leq i \leq M)$
  • $1 \leq S_i \lt T_i \leq 10^9\ (1 \leq i \leq M)$
  • $S_i \neq S_j\ (i \neq j)$
  • $1 \leq X_i \lt Z_i \leq 10^9\ (1 \leq i \leq Q)$
  • $1 \leq Y_i \leq N\ (1 \leq i \leq Q)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $M$ $Q$
$A_1$ $B_1$ $S_1$ $T_1$
$A_2$ $B_2$ $S_2$ $T_2$
$\hspace{1.8cm}\vdots$
$A_M$ $B_M$ $S_M$ $T_M$
$X_1$ $Y_1$ $Z_1$
$X_2$ $Y_2$ $Z_2$
$\hspace{1.2cm}\vdots$
$X_Q$ $Y_Q$ $Z_Q$

Output

Print $Q$ lines. The $i$-th line should contain the response to the $i$-th query as follows.

  • If Takashi is on some bus at time $Z_i$, print two integers representing the city from which the bus departs and the city at which the bus arrives, in this order, with a space between them.
  • Otherwise, that is, if Takahashi is in some city at time $Z_i$, print an integer representing that city.

Sample Input 1

3 2 3
1 2 1 3
2 3 3 5
1 1 5
2 2 3
1 3 2

Sample Output 1

2 3
2
3

In the first query, Takahashi will travel as follows.

  1. Start in City $1$ at time $1$.
  2. Take the bus that departs from City $1$ at time $1.5$ and arrives at City $2$ at time $3.5$.
  3. Take the bus that departs from City $2$ at time $3.5$ and arrives at City $3$ at time $5.5$.
  4. Since no bus departs from City $3$ at time $5.5$ or later, stay in City $3$ (forever).

At time $5$, he will be on the bus that departs from City $2$ and arrives at City $3$. Thus, as specified in the Output section, we should print $2$ and $3$ with a space between them.


Sample Input 2

8 10 10
4 3 329982133 872113932
6 8 101082040 756263297
4 7 515073851 793074419
8 7 899017043 941751547
5 7 295510441 597348810
7 2 688716395 890599546
6 1 414221915 748470452
6 4 810915860 904512496
3 1 497469654 973509612
4 1 307142272 872178157
374358788 4 509276232
243448834 6 585993193
156350864 4 682491610
131643541 8 836902943
152874385 6 495945159
382276121 1 481368090
552433623 2 884584430
580376205 2 639442239
108790644 7 879874292
883275610 1 994982498

Sample Output 2

4
6 1
4 1
8
6 1
1
2
2
7 2
1

Input

题意翻译

### 题目大意 有 $n$ 座城市和 $m$ 辆公交车,第 $i$ 辆公交车会在 $S_i+0.5$ 时从城市 $A_i$ 出发,在 $T_i+0.5$ 时到达城市 $B_i$。 Takahashi 想要在这些城市中旅行。具体的说,当他在 $t$ 时刻时位于城市 $p$ 时,他会按照如下方案移动: 若存在在 $t$ 时刻后从城市 $p$ 出发的公交车,那么选择其中离 $t$ 时刻最近的一辆并乘坐。否则停留在城市 $p$ 不移动。 现在 Takahashi 想要问你 $Q$ 个问题,每个问题的格式如下: 如果 Takahashi 在 $X$ 时刻从城市 $Y$ 出发,那么 $Z$ 时刻时 Takahashi 位于哪辆公交车上或者哪个城市中? ### 输入格式 第一行三个正整数 $n,m,Q$。 接下来 $m$ 行,每行四个正整数 $A_i,B_i,S_i,T_i$,描述一辆公交车。 接下来 $Q$ 行,每行三个正整数 $X,Y,Z$,描述一个询问。 ### 输出格式 输出共 $Q$ 行,每行一个或两个正整数。 如果是城市,输出城市编号,如果是公交车,输出其起点和终点的城市编号。 Translated by \_Ponder_

Output

分数:500分 部分 问题描述 有编号为1到N的N个城市,以及在这之间运行的M辆公共汽车。第i辆公共汽车(1≤i≤M)在时间S_i+0.5从城市A_i出发,并在时间T_i+0.5到达城市B_i。 Takahashi将在这N个城市之间旅行。当他于时间t在城市p时,他将执行以下操作。 1. 如果有从城市p不早于时间t出发的公共汽车,那么乘坐出发最早的那些公共汽车到达另一个城市。 2. 如果没有这样的公共汽车,那么不做任何事情,留在城市p。 Takahashi重复执行上述操作,直到没有公共汽车可乘坐。由于所有的M辆公共汽车的出发时间都不同,因此要乘坐的公共汽车总是唯一确定的。另外,换乘公共汽车所需的时间可以忽略不计。 您的任务是处理Q个查询,其中第i个(1≤i≤Q)查询如下。 如果Takahashi在城市Y_i于时间X_i开始旅行,他将在时间Z_i时在哪个城市或哪辆公共汽车上? 部分 约束 * 2≤N≤10^5 * 1≤M≤10^5 * 1≤Q≤10^5 * 1≤A_i,B_i≤N(1≤i≤M) * A_i≠B_i(1≤i≤M) * 1≤S_i<T_i≤10^9(1≤i≤M) * S_i≠S_j(i≠j) * 1≤X_i<Z_i≤10^9(1≤i≤Q) * 1≤Y_i≤N(1≤i≤Q) * 输入中的所有值都是整数。 输入格式 标准输入按以下格式给出输入: $N$ $M$ $Q$ $A_1$ $B_1$ $S_1$ $T_1$ $A_2$ $B_2$ $S_2$ $T_2$ $\hspace{1.8cm}\vdots$ $A_M$ $B_M$ $S_M$ $T_M$ $X_1$ $Y_1$ $Z_1$ $X_2$ $Y_2$ $Z_2$ $\hspace{1.2cm}\vdots$ $X_Q$ $Y_Q$ $Z_Q$ 输出格式 打印Q行。第i行应包含对第i个查询的响应,如下所示。 * 如果Takashi在时间Z_i时在某辆公共汽车上,那么打印两个整数,表示公共汽车出发的城市和公共汽车到达的城市,中间用空格分隔。 * 否则,即如果Takahashi在时间Z_i时在某个城市,那么打印一个整数,表示该城市。 样例输入1 3 2 3 1 2 1 3 2 3 3 5 1 1 5 2 2 3 1 3 2

加入题单

上一题 下一题 算法标签: