| <p> | |
| The starship Enterprise, bravely captained by Jean-Luc Picard, is on yet another mission to explore strange new worlds, seek out new life and new civilizations, | |
| and boldly go where no one has gone before! Equipped with a state-of-the-art warp drive capable of attaining warp factor 11 and revising the Enterprise's space-time coordinates almost at will | |
| (even sending the ship back in time), not much can stand in the explorers' way. Though, they <em>are</em> low on medical supplies, so they will need to first stock up on neurozine gas for anesthetic purposes. | |
| </p> | |
| <p> | |
| The Enterprise is heading to the Alpha Omicron solar system, which consists of <strong>N</strong> planets, numbered from 1 to <strong>N</strong>. | |
| It also features <strong>M</strong> space conduits, the <em>i</em>th of which allows the Enterprise to travel in either direction between two different planets | |
| <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>. | |
| No two conduits directly link the same unordered pair of planets, and <strong>each planet is reachable from each other planet</strong> by following a sequence of conduits. | |
| </p> | |
| <p> | |
| There's a geyser capable of emitting neurozine located on each planet, though all <strong>N</strong> geysers are initially inactive. | |
| A sequence of <strong>K</strong> events will then take place, one per hour. The event at hour <em>i</em> is described by integers <strong>E<sub>i</sub></strong> and <strong>V<sub>i</sub></strong>, | |
| with <strong>E<sub>i</sub></strong> indicating the event's type, which is one of the following: | |
| </p> | |
| <ul style="list-style-type:disc; padding-inline-start: 30px;"> | |
| <li><strong>E<sub>i</sub></strong> = 1: The <strong>V<sub>i</sub></strong>th conduit (1 ≤ <strong>V<sub>i</sub></strong> ≤ <strong>M</strong>) collapses, | |
| and can no longer be used from that moment onwards. Each conduit collapses at most once. </li> | |
| <li><strong>E<sub>i</sub></strong> = 2: The geyser on planet <strong>V<sub>i</sub></strong> (1 ≤ <strong>V<sub>i</sub></strong> ≤ <strong>N</strong>) activates, | |
| and begins emitting neurozine. Each geyser is activated at most once. </li> | |
| <li><strong>E<sub>i</sub></strong> = 3: The geyser on planet <strong>V<sub>i</sub></strong> (1 ≤ <strong>V<sub>i</sub></strong> ≤ <strong>N</strong>) deactivates, | |
| and no longer emits neurozine from that moment onwards. Each geyser is deactivated at most once, and is guaranteed to not be deactivated before it has been activated. </li> | |
| </ul> | |
| <p> | |
| The Enterprise will arrive in the Alpha Omicron system at some planet <em>x</em> and just before some hour <em>y</em>. | |
| When the starship is currently at a certain planet (and a certain time), Captain Picard may issue any of the following commands to his crew: | |
| </p> | |
| <ul style="list-style-type:disc; padding-inline-start: 30px;"> | |
| <li>Remain at that planet and wait until any future time. </li> | |
| <li>Travel through an uncollapsed space conduit directly from that planet to another one. Thanks to warp technology, this may be done instantly. </li> | |
| <li>Collect neurozine from that planet's geyser, if it's currently active. This may be done instantly. </li> | |
| <li>Remain at that planet while travelling backwards to any past time which is <strong>at most 24 hours earlier than the Enterprise's original arrival time in the solar system</strong> | |
| (in other words, the Enterprise may end up just before hour (<em>y</em> - 24), but no earlier). However, <strong>this may only be done at most once</strong>. The Enterprise retains any neurozine that it had collected before this "temporal revision". | |
| </ul> | |
| <p> | |
| Picard wants his crew to collect neurozine from as many <em>different</em> geysers as possible; there's no additional value in collecting neurozine from any given geyser multiple times, | |
| including both before and after travelling back in time. However, Picard hasn't yet decided where and when the Enterprise should arrive in the Alpha Omicron system. | |
| He has <strong>S</strong> such possible starting situations in mind, the <em>i</em>th of which would have the Enterprise arrive at planet | |
| <strong>X<sub>i</sub></strong> just before hour <strong>Y<sub>i</sub></strong>. | |
| For each hypothetical starting situation, please help Picard determine the maximum number of different geysers from which the Enterprise could then proceed to collect neurozine! | |
| </p> | |
| <p> | |
| Letting <strong>ans<sub>i</sub></strong> be the answer for the <em>i</em>th starting situation, you must output the sum of <strong>ans<sub>1..S</sub></strong> | |
| in order to minimize the size of the output. Please note that this sum may not fit within a 32-bit integer. | |
| </p> | |
| <p> | |
| The starting situations must be considered one after another. In order to enforce this, rather than being given <strong>X<sub>1..S</sub></strong> and <strong>Y<sub>1..S</sub></strong> | |
| explicitly, you must compute them based on given values <strong>X'<sub>1..S</sub></strong> and <strong>Y'<sub>1..S</sub></strong>. | |
| For the first starting situation, <strong>X<sub>1</sub></strong> = <strong>X'<sub>1</sub></strong> and <strong>Y<sub>1</sub></strong> = <strong>Y'<sub>1</sub></strong>, | |
| while for each subsequent starting situation <em>i</em> (2 ≤ <em>i</em> ≤ <strong>S</strong>), | |
| <strong>X<sub>i</sub></strong> = <strong>X'<sub>i</sub></strong> xor <strong>ans<sub>i-1</sub></strong> and | |
| <strong>Y<sub>i</sub></strong> = <strong>Y'<sub>i</sub></strong> xor <strong>ans<sub>i-1</sub></strong> (where "xor" is the bitwise xor operator, "^" in most programming languages). | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of missions. | |
| <br />For each mission, there is first a line containing the space-separated integers <strong>N</strong>, <strong>M</strong>, <strong>K</strong> and <strong>S</strong>. | |
| <br />Then, <strong>M</strong> lines follow, the <em>i</em>th of which contains the space-separated integers <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>. | |
| <br />Then, <strong>K</strong> lines follow, the <em>i</em>th of which contains the space-separated integers <strong>E<sub>i</sub></strong> and <strong>V<sub>i</sub></strong>. | |
| <br />Then, <strong>S</strong> lines follow, the <em>i</em>th of which contains the space-separated integers <strong>X'<sub>i</sub></strong> and <strong>Y'<sub>i</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th mission, print a line containing "Case #<em>i</em>: " followed by | |
| one integer, the sum of the answers for the <strong>S</strong> starting situations. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 100 <br /> | |
| 2 ≤ <strong>N</strong> ≤ 800,000 <br /> | |
| 1 ≤ <strong>M</strong>, <strong>K</strong>, <strong>S</strong> ≤ 800,000 <br /> | |
| 1 ≤ <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong> ≤ <strong>N</strong> <br /> | |
| 1 ≤ <strong>E<sub>i</sub></strong> ≤ 3 <br /> | |
| 1 ≤ <strong>X<sub>i</sub></strong> ≤ <strong>N</strong> <br /> | |
| 1 ≤ <strong>Y<sub>i</sub></strong> ≤ <strong>K</strong> <br /> | |
| 0 ≤ <strong>X'<sub>i</sub></strong>, <strong>Y'<sub>i</sub></strong> ≤ 1,000,000,000 <br /> | |
| </p> | |
| <p> | |
| The sum of <strong>N</strong> across all <strong>T</strong> test cases is no greater than 2,000,000. <br /> | |
| The sum of <strong>M</strong> across all <strong>T</strong> test cases is no greater than 2,000,000. <br /> | |
| The sum of <strong>K</strong> across all <strong>T</strong> test cases is no greater than 2,000,000. <br /> | |
| The sum of <strong>S</strong> across all <strong>T</strong> test cases is no greater than 2,000,000. | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, if the Enterprise arrives at planet 1 just before hour 3, Picard could issue the following sequence of orders to help his crew collect neurozine from both planets' geysers: | |
| <ol> | |
| <li>Travel through the 1st space conduit to planet 2. | |
| <li>Wait until after hour 3. | |
| <li>Collect neurozine from planet 2's now-active geyser. | |
| <li>Travel back in time to just before hour 2. | |
| <li>Travel through the 1st space conduit to planet 1. | |
| <li>Collected neurozine from planet 1's active geyser. | |
| </ol> | |
| <p> | |
| In the second case, the starting situations and corresponding answers are as follows: | |
| </p> | |
| <pre> | |
| i | X<sub>i</sub> | Y<sub>i</sub> | ans<sub>i</sub> | |
| ------------------ | |
| 1 | 2 | 1 | 3 | |
| 2 | 1 | 6 | 2 | |
| 3 | 3 | 5 | 3 | |
| </pre> | |
| <p> | |
| For the first starting situation, the Enterprise could remain on planet 2 until its geyser activates at hour 6, collect its neurozine, travel back in time to just before hour 2, travel to planet 1 and collect its neurozine, travel to planet 2 and then to planet 3, and remain there to collect its neurozine after hour 5. On the other hand, for the second starting situation, neurozine from all 3 geysers cannot be collected. | |
| </p> | |
| <p> | |
| In the third case, the starting situations and corresponding answers are as follows: | |
| </p> | |
| <pre> | |
| i | X<sub>i</sub> | Y<sub>i</sub> | ans<sub>i</sub> | |
| ------------------ | |
| 1 | 1 | 4 | 4 | |
| 2 | 5 | 8 | 3 | |
| 3 | 2 | 9 | 3 | |
| 4 | 3 | 6 | 4 | |
| </pre> | |
| <p> | |
| In the fourth case, the starting situations and corresponding answers are as follows: | |
| </p> | |
| <pre> | |
| i | X<sub>i</sub> | Y<sub>i</sub> | ans<sub>i</sub> | |
| ------------------ | |
| 1 | 6 | 16 | 7 | |
| 2 | 4 | 6 | 8 | |
| 3 | 10 | 22 | 7 | |
| 4 | 3 | 13 | 7 | |
| 5 | 6 | 11 | 8 | |
| 6 | 5 | 17 | 6 | |
| 7 | 2 | 21 | 7 | |
| </pre> | |
| <p> | |
| In the fifth case, the first 5 starting situations and corresponding answers are as follows: | |
| </p> | |
| <pre> | |
| i | X<sub>i</sub> | Y<sub>i</sub> | ans<sub>i</sub> | |
| ------------------ | |
| 1 | 20 | 47 | 2 | |
| 2 | 4 | 49 | 7 | |
| 3 | 24 | 47 | 1 | |
| 4 | 20 | 9 | 13 | |
| 5 | 3 | 38 | 9 | |
| </pre> | |