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
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 | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 146 |
| 3 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
| 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