Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance After Road Addition Queries II

Try to solve the Shortest Distance After Road Addition Queries II problem.

Statement

You are given an integer n and a 2D2 D integer array queries.

There are n cities numbered from 00 to n −1- 1. Initially, there is a unidirectional road from city i to city i +1+ 1 for all 0≤0 \le i << n −1- 1.

Each query queries[i] =[ui,vi]= [u_i, v_i] represents the addition of a new unidirectional road from city uiu_i to city viv_i. After processing each query, you need to find the length of the shortest path from city 00 to city n −1- 1.

Return an array answer of length queries.length, where answer[i] is the length of the shortest path from city 00 to city n −1- 1 after processing the first i +1+ 1 queries.

Constraints:

  • 3≤3 \le n ≤105\le 10^5

  • 1≤1 \le queries.length ≤105\le 10^5

  • queries[i].length =2= 2

  • 0≤0 \le queries[i][0] << queries[i][1] << n

  • 1<1 < queries[i][1] −- queries[i][0]

  • There are no repeated roads among the queries.

  • There are no two queries such that i ≠\neq j and queries[i][0] << queries[j][0] << queries[i][1] << queries[j][1].

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance After Road Addition Queries II

Try to solve the Shortest Distance After Road Addition Queries II problem.

Statement

You are given an integer n and a 2D2 D integer array queries.

There are n cities numbered from 00 to n −1- 1. Initially, there is a unidirectional road from city i to city i +1+ 1 for all 0≤0 \le i << n −1- 1.

Each query queries[i] =[ui,vi]= [u_i, v_i] represents the addition of a new unidirectional road from city uiu_i to city viv_i. After processing each query, you need to find the length of the shortest path from city 00 to city n −1- 1.

Return an array answer of length queries.length, where answer[i] is the length of the shortest path from city 00 to city n −1- 1 after processing the first i +1+ 1 queries.

Constraints:

  • 3≤3 \le n ≤105\le 10^5

  • 1≤1 \le queries.length ≤105\le 10^5

  • queries[i].length =2= 2

  • 0≤0 \le queries[i][0] << queries[i][1] << n

  • 1<1 < queries[i][1] −- queries[i][0]

  • There are no repeated roads among the queries.

  • There are no two queries such that i ≠\neq j and queries[i][0] << queries[j][0] << queries[i][1] << queries[j][1].