”Happy” is a popular restaurant chain in Bulgaria. Maria traveled from Burgas to the nearby city of Varna – the birthplace of ”Happy”. She met her friends and went to one of the restaurants to celebrate her recent success at IATI. Known for its excellent service, the waitress gave Maria a challenge – if she solves it, the order will be free.
There are Happy restaurants in Varna, numbered from to . They are connected by two-way paths, where each path has a happy energy value. Given two restaurants and , we define a simple route between them as a sequence of paths starting from and ending in without repeating restaurants. The restaurants are connected in such a way so that there is exactly one simple route between each pair of restaurants.
For some unknown reason, the happiness of a simple route is defined as the bitwise XOR of the happy energies of the paths in it.
The challenge is that Maria has to find the sum of the happiness of the simple routes between each pair of open Happy restaurants. Initially, all restaurants are closed but we have updates of two types:
- type 1 – given integers and such that , all restaurants with numbers in become opened (if some restaurants are already opened, then they remain opened);
- type 2 – given integers and such that , all restaurants with numbers in become closed (if some restaurants are already closed, then they remain closed);
Help Maria solve the challenge!
A bitwise XOR (^) is a binary operation on the binary representation of two numbers, applied bit by bit. It returns for each position if the corresponding bits are different, and if they are the same. For example, ^ .
Implementation details
You should implement the function happy:
std::vector<long long int> happy (int N, int Q,
std::vector<int> u, std::vector<int> v, std::vector<int> h,
std::vector<int> t, std::vector<int> l, std::vector<int> r)
- : the number Happy restaurants;
- : the number of updates;
- and : vectors of integers, representing the restaurants and happy energies for the paths;
- : vectors of integers, representing the types and intervals for the updates.
This function will be called once for each test and has to return a vector with numbers - the sum of the happiness of the simple routes between each pair of open restaurants after each update.
Restrictions
- for each
- There is exactly one simple route between each pair of restaurants.
| Subtask | Points | Required subtasks | Other constraints | ||
|---|---|---|---|---|---|
| 0 | 0 | The example. | |||
| 1 | 7 | ||||
| 2 | 8 | ||||
| 3 | 19 | for all and for all | |||
| 4 | 19 | for all and for all | |||
| 5 | 10 | ||||
| 6 | 17 | If then restaurants in were closed before the update; if then restaurants in were opened before the update . | |||
| 7 | 20 |
Example 1
Consider the following call and illustration for the restaurants, for and :
happy(6, 5, {0, 1, 2, 2, 4}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 2},
{1, 1, 2, 2, 1}, {1, 2, 2, 4, 0}, {2, 3, 2, 5, 5})

The updates are the following:
- (restaurants and become opened) – after the update we have only the simple route with happiness . Sum is ;
- (restaurants and become opened) – after the update we have the simple routes between restaurants and , and , and and with corresponding happiness ^ and (^ is the C++ operator for bitwise XOR). Sum is ;
- (restaurant becomes closed) – after the update we have the simple route between restaurants and . Sum is ;
- (restaurants and become closed) – after the update we have the simple route between restaurants and . Sum is ;
- (restaurants to become opened) – after the update we have the simple routes between all pairs of restaurants. Sum is .
Therefore, the call should return {2, 6, 1, 1, 56}.
Sample grader
The input format is the following:
- line : two integers – the values of and .
- line to : three integers – representing a path between restaurants with numbers and with happy energy .
- line to : three integers – representing an update of type for an interval of restaurants with numbers from and .
The output format is the following:
- line : the -th value as returned by the call.