Given a tree of n vertices, count the number of ways to choose a connected subgraph of the tree so that all the vertices in that
subgraph consists of consecutive integers when sorted. Thanks!
N <= 3e5
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 155 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 145 |
| 5 | Um_nik | 143 |
| 6 | Errichto | 139 |
| 7 | BledDest | 136 |
| 8 | adamant | 134 |
| 9 | maroonrk | 132 |
| 10 | cry | 130 |
Given a tree of n vertices, count the number of ways to choose a connected subgraph of the tree so that all the vertices in that
subgraph consists of consecutive integers when sorted. Thanks!
N <= 3e5
| Name |
|---|



let mx[i] = largest vertice on the path ($$$i, i + 1$$$).
mn[i] = smallest vertice on the path ($$$i, i + 1$$$).
calculate number of pairs ($$$l \lt r$$$) such that $$$r - l$$$ = max(mx[ $$$l$$$ ..($$$r - 1$$$)]) — min(mn[ $$$l$$$ ..($$$r - 1$$$)]);
nice bbc
incorrect solution
you can look into my algorithm, not sure if it's completely correct though
define $$$S(i)$$$ for $$$1\leq i \lt n$$$ as the set of nodes on the simple path between $$$i$$$ and $$$i+1$$$, $$$mn(i)$$$ and $$$mx(i)$$$ respectively the minimum and maximum index node in $$$S(i)$$$
obviously intervals of length 1 satisfy, we then need to count the number of pairs $$$(l,r)$$$ $$$(1\leq l \lt r\leq n)$$$ satisfying
$$$\min_{i=l}^{r-1} {mn(i)}\geq l$$$ and $$$\max_{i=l}^{r-1} {mx(i)}\leq r$$$
which can be done by precomputation and kquery here is the problem link (statement in vietnamese) and my implementation (which got ac) https://coder.husc.edu.vn/problem/olpicpc049c11e
https://ideone.com/GizYa3
sorry for the dirty code, hope you get the idea behind the algo
Likes Pudding Monster but with linear graph