codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

696A.md (3119B)


      1 Source: https://codeforces.com/problemset/problem/696/A
      2 
      3 A. Lorenzo Von Matterhorn
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections *i* and 2*i* and another road between *i* and 2*i* + 1 for every positive integer *i*. You can clearly see that there exists a unique shortest path between any two intersections.
     22 
     23  ![](https://espresso.codeforces.com/4903884e67e4df5c9318cac4bec37adc381fc790.png) 
     24 
     25 Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will *q* consecutive events happen soon. There are two types of events:
     26 
     27 1. Government makes a new rule. A rule can be denoted by integers *v*, *u* and *w*. As the result of this action, the passing fee of all roads on the shortest path from *u* to *v* increases by *w* dollars.
     28 
     29 2. Barney starts moving from some intersection *v* and goes to intersection *u* where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
     30 
     31 Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
     32 
     33 Input
     34 
     35 The first line of input contains a single integer *q* (1 ≤ *q* ≤ 1 000).
     36 
     37 The next *q* lines contain the information about the events in chronological order. Each event is described in form 1 *v* *u* *w* if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from *u* to *v* by *w* dollars, or in form 2 *v* *u* if it's an event when Barnie goes to cuddle from the intersection *v* to the intersection *u*.
     38 
     39 1 ≤ *v*, *u* ≤ 1018, *v* ≠ *u*, 1 ≤ *w* ≤ 109 states for every description line.
     40 
     41 Output
     42 
     43 For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
     44 
     45 Example
     46 
     47 Input
     48 
     49 ```
     50 7  
     51 1 3 4 30  
     52 1 4 1 2  
     53 1 3 6 8  
     54 2 4 3  
     55 1 6 1 40  
     56 2 3 7  
     57 2 2 4
     58 ```
     59 
     60 Output
     61 
     62 ```
     63 94  
     64 0  
     65 32
     66 ```
     67 
     68 Note
     69 
     70 In the example testcase:
     71 
     72 Here are the intersections used:
     73 
     74  ![](https://espresso.codeforces.com/c2675a26082e819949e289f83ac78a7e27afde40.png) 
     75 
     76 1. Intersections on the path are 3, 1, 2 and 4.
     77 2. Intersections on the path are 4, 2 and 1.
     78 3. Intersections on the path are only 3 and 6.
     79 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94.
     80 5. Intersections on the path are 6, 3 and 1.
     81 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0.
     82 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).