happy

Time limit: 3s Memory limit: 512MB Input: Output:

”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 NN Happy restaurants in Varna, numbered from 00 to N1N-1. They are connected by N1N-1 two-way paths, where each path has a happy energy value. Given two restaurants xx and yy, we define a simple route between them as a sequence of paths starting from xx and ending in yy 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 QQ updates of two types:

  • type 1 – given integers l[j]l[j] and r[j]r[j] such that 0l[j]r[j]N10 \leq l[j] \leq r[j] \leq N-1, all restaurants with numbers in [l[j],r[j]][l[j], r[j]] become opened (if some restaurants are already opened, then they remain opened);
  • type 2 – given integers l[j]l[j] and r[j]r[j] such that 0l[j]r[j]N10 \leq l[j] \leq r[j] \leq N-1, all restaurants with numbers in [l[j],r[j]][l[j], r[j]] 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 11 for each position if the corresponding bits are different, and 00 if they are the same. For example, 4 8794 \ 879 ^ 4 772=4274 \ 772 = 427.

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)
  • NN: the number Happy restaurants;
  • QQ: the number of updates;
  • u,vu, v and hh: vectors of N1N-1 integers, representing the restaurants and happy energies for the paths;
  • t,l,rt, l, r: vectors of QQ 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 QQ numbers - the sum of the happiness of the simple routes between each pair of open restaurants after each update.

Restrictions

  • 1N300 0001 \leq N \leq 300 \ 000
  • 1Q300 0001 \leq Q \leq 300 \ 000
  • 1h[i]<2201 \leq h[i] < 2^{20} for each 0i<N10 \leq i < N-1
  • There is exactly one simple route between each pair of restaurants.
Subtask Points Required subtasks NN QQ Other constraints
0 0 - - - The example.
1 7 00 100\leq 100 100\leq 100 -
2 8 010-1 2 000\leq 2 \ 000 2 000\leq 2 \ 000 -
3 19 - 300 000\leq 300 \ 000 300 000\leq 300 \ 000 l[j]=r[j]l[j] = r[j] for all 0j<Q0 \leq j < Q and u[i]=i,v[i]=i+1u[i] = i, v[i] = i+1 for all 0i<N10 \leq i < N-1
4 19 - 300 000\leq 300 \ 000 300 000\leq 300 \ 000 l[j]=r[j]l[j] = r[j] for all 0j<Q0 \leq j < Q and h[i]=1h[i] = 1 for all 0i<N10 \leq i < N-1
5 10 00 300 000\leq 300 \ 000 10\leq 10 -
6 17 - 300 000\leq 300 \ 000 300 000\leq 300 \ 000 If t[j]=1t[j] = 1 then restaurants in [l[j],r[j]][l[j], r[j]] were closed before the update; if t[j]=2t[j] = 2 then restaurants in [l[j],r[j]][l[j], r[j]] were opened before the update (0j<Q)(0 \leq j < Q).
7 20 060-6 300 000\leq 300 \ 000 300 000\leq 300 \ 000 -

Example 1

Consider the following call and illustration for the restaurants, for N=6N=6 and Q=5Q=5:

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 55 updates are the following:

  • 1 1 21 \ 1 \ 2 (restaurants 11 and 22 become opened) – after the update we have only the simple route 1221 \xleftrightarrow{2} 2 with happiness 22. Sum is 22;
  • 1 2 31 \ 2 \ 3 (restaurants 22 and 33 become opened) – after the update we have the simple routes between restaurants 11 and 2 (122)2 \ (1 \xleftrightarrow{2} 2), 11 and 3 (12223)3 \ (1 \xleftrightarrow{2} 2 \xleftrightarrow{2} 3), and 22 and 3 (233)3 \ (2 \xleftrightarrow{3} 3) with corresponding happiness 2,22, 2 ^ 3=13 = 1 and 33 (^ is the C++ operator for bitwise XOR). Sum is 2+1+3=62 + 1 + 3 = 6;
  • 2 2 22 \ 2 \ 2 (restaurant 22 becomes closed) – after the update we have the simple route between restaurants 11 and 33. Sum is 11;
  • 2 4 52 \ 4 \ 5 (restaurants 44 and 55 become closed) – after the update we have the simple route between restaurants 11 and 33. Sum is 11;
  • 1 0 51 \ 0 \ 5 (restaurants 00 to 55 become opened) – after the update we have the simple routes between all pairs of restaurants. Sum is 5656.

Therefore, the call should return {2, 6, 1, 1, 56}.

Sample grader

The input format is the following:

  • line 11: two integers – the values of NN and QQ.
  • line 2+i2+i to 2+N22+N-2: three integers u[i],v[i],h[i]u[i], v[i], h[i] – representing a path between restaurants with numbers u[i]u[i] and v[i]v[i] with happy energy h[i]h[i].
  • line N+1+jN+1+j to N+1+Q1N+1+Q-1: three integers t[j],l[j],r[j]t[j],l[j],r[j] – representing an update of type t[j]t[j] for an interval of restaurants with numbers from l[j]l[j] and r[j]r[j].

The output format is the following:

  • line ii: the ii-th value as returned by the call.

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