can anyone help me with the chgoram problem of august challenge

# | User | Rating |
---|---|---|

1 | MiFaFaOvO | 3681 |

2 | Um_nik | 3544 |

3 | maroonrk | 3431 |

4 | tourist | 3409 |

5 | apiadu | 3397 |

6 | 300iq | 3317 |

7 | ecnerwala | 3260 |

7 | Benq | 3260 |

9 | LHiC | 3229 |

10 | TLE | 3223 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 194 |

2 | antontrygubO_o | 191 |

3 | vovuh | 178 |

4 | pikmike | 177 |

5 | tourist | 166 |

6 | Um_nik | 164 |

6 | McDic | 164 |

8 | ko_osaga | 163 |

9 | Radewoosh | 162 |

10 | 300iq | 156 |

can anyone help me with the chgoram problem of august challenge

↑

↓

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/28/2020 18:23:34 (g2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

Here is a link for some quick explanation.

For example, let's suppose that restaurants must be in increasing order: 1, 2, 3.

Assume that you are checking all the vertexes as the middle one. Now you want to know how many can be the first and how many can be the last, of course, they must belong to different subtrees of our current vertex.

For every vertex

`i`

, go through each vertex`j`

that share an edge with`i`

. Try to visualize it as a splitting the tree into two subsets, where subset`A`

is from`j`

to all other vertexes connected to it except from`i`

and subset`B`

the remaining vertexes.Also, let

`val`

be the value of our current vertex. We need to count how many`(x, y)`

pairs we have, where:`x`

is in`A`

`y`

is in`B`

`x < val < y`

`L`

= how many values are less than`val`

in`A`

`R`

= how many values are greater than`val`

in`B`

`current_answer`

=`L`

*`R`

If the middle value is not 2, after you sum the answer for every vertex, you need to divide the

`total_answer`

by two, to remove some repeted pairs.The biggest problem now is: how to do fast queries on how many values are less/greater than

`val`

in a subtree?I have solved this with persistent segment tree, where I do a query in an interval that represent this subtree. Like:

`st[current_version]->query(x, y) - st[current_version - size_of_this_subtree]->query(x, y)`

The problem have a tight time limit if you code with persistent segment tree, you can use other faster methods to do this offline, like the small to large trick.