RoAlgo Contest #12 - NiceContest | G. Nice Apple Tree

This was the problem page during the contest. Access the current page here.
Time limit: 2.75s Memory limit: 256MB Input: Output:

An apple tree can be represented as a tree with NN nodes, where each node is an apple. Good apples are denoted by 11, while rotten apples are denoted by 00. There are QQ events of the following types:

  • 1 l r1 \ l \ r - all apples in the interval [l,r][l, r] become good;
  • 2 l r2 \ l \ r - all apples in the interval [l,r][l, r] become rotten;
  • 3 l r3 \ l \ r - all apples in the interval [l,r][l, r] change their state (01,10)(0 \rightarrow 1, 1 \rightarrow 0).

Task

Farmer John wonders after each event which are the two good apples AA and BB, such that the number of nodes located on the path between AA and BB is maximized.

Input data

The first line will contain the numbers NN and QQ, as described above.
The following N1N-1 lines will contain the edges of the tree.
The next line will contain a binary string of length NN, where SiS_i represents the state of apple ii (11 = good, 00 = rotten).
The following QQ lines will contain the numbers tt, ll, and rr, representing operation ii.

Output data

You will output QQ lines, line ii containing the numbers aia_i and bib_i, representing the good apples located at the maximum distance from each other. If there is not at least one good apple, print 0 0.

Constraints and clarifications

  • 1N,Q100 0001 \leq N, Q \leq 100 \ 000
  • If there are multiple solutions, any of them will be accepted
  • The apples chosen don't have to be distinct
  • If no good apple exists, 0 0 should be printed
  • It is recommended to use fast I/O
  • If you have issues with the constant, you can try using pragmas
  • For test cases worth 55 points: N,Q20N, Q \leq 20
  • For other test cases worth 55 points: N,Q200N, Q \leq 200
  • For other test cases worth 55 points: N,Q1 000N, Q \leq 1 \ 000
  • For other test cases worth 1111 points: the tree is of the form 12...N1 - 2 - ... - N
  • For other test cases worth 1010 points: there are no updates of type 22 or 33
  • For other test cases worth 1414 points: there are no updates of type 33
  • For other test cases worth 1919 points: N,Q60 000N, Q \leq 60 \ 000

Example

stdin

9 7
1 2
1 3
2 7
7 8
7 9
3 4
3 5
3 6
100011101
1 2 3
2 5 7
3 1 9
3 1 4
3 2 6
1 1 9
2 1 9

stdout

6 9
3 9
5 8
5 8
4 8
8 5
0 0

Explanation

Initially, the tree looks like this:

After the first event, the tree looks like this:

In this case, one solution is 6 96 \ 9, with a distance of 55. Another solution could have been 5 95 \ 9.

After the second event, the tree looks like this:

In this case, the only solution is 3 93 \ 9, with a distance of 44.

At the end of all events, the tree looks like this:

In this case, there is no good apple, so the solution will be 0 00 \ 0.

Log in or sign up to be able to send submissions!