The street in front of you has some empty parking spots. A car needs 1 parking spot to park; an SUV needs 2 parking spots; and an 18-wheeler truck needs 4. Some parking spots are already taken. Given a description of the parking spots that are free and taken in the street currently, compute in how many different ways you can fill up the street with vehicles (cars, SUVs, or 18-wheelers).
For instance, in the following street, where a dash (-) means an empty spot and an X means a spot already taken,
The input consists of an arbitrary number of lines. Every line is a non-empty string with symbols - or X. No line will be larger than 70 symbols.
For each input case, output a line with the total number of different configurations. No output will be larger than $$$10^{18}$$$.
-X---XXX-------
1 1 2 1 1 3 6
-XX--X----X---X--XX--------X-X---XXXXXXXX-XX----------------------------------X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X
12 990 69988378 2
You might already know that, in most modern C++ compilers, int is a 32-bit integer, so it can store numbers from $$$-2^{31}$$$ (-2147483648) to $$$2^{31}-1$$$ (2147483647). If you use long long you can store 64-bit integers (around 18 digits long), but if you want to work with numbers longer than that, you are on your own.
In this problem we ask you to write code that can add arbitrarily long positive integers, and that can read and write them in the standard form (base 10 representation).
Your first instinct might be to use strings: every digit in your number is a char from '0' to '9'. This works great for input and output, but sums are kind of slow because your program must add digit by digit.
Your second instinct might be to use vector<int>, and make every number of your vector a "big-digit" in base $$$2^{31}$$$ (or $$$2^{32}$$$ if you know how to use unsigned ints). You can add two "big-digits" by transforming them first into "long long". Now sums are fast: every time you add up two "big-digits" you are adding a bit more than 9 of the old base-10 digits. But reading and writing requires translating base 10 numbers into base $$$2^{31}$$$ or $$$2^{32}$$$, which is not a trivial task.
Let's hope you have a third instinct that will allow you to solve this problem.
The input consists of a number $$$n$$$, followed by $$$n$$$ arbitrary long numbers $$$x_1, \ldots, x_n$$$ given in base-10.
Then, the number $$$k$$$ of sums of the form $$$x_{i_1} + \ldots + x_{i_t}$$$ that your program must perform. For each sum, you will be given the number $$$t$$$ followed by $$$t$$$ indexes $$$i_1, \ldots, i_k$$$, all separated by spaces.
We guarantee that the size of the input or the output won't be larger than 1 megabyte. Make sure, though, that your program is using fast I/O for competitive programming.
Output $$$k$$$ lines with the results of the $$$k$$$ sums, again in base 10.
1112 1 1
2
199999999941 12 1 13 1 1 14 1 1 1 1
999999999 1999999998 2999999997 3999999996
419999999991000000000100000000132 1 22 1 34 1 2 3 4
1000000000 1000000001 3000000001
4123456789012345678901234567890123411234567890123456789012345678901234111234567890123456789012345678901234111123456789012345678901234567890123431 14 1 2 3 44 4 3 2 1
1234567890123456789012345678901234 1234938271560493827156049382715604936 1234938271560493827156049382715604936
Did you solve the previous problem? You surely realized that treaps don't seem more useful than a regular std::set. This is going to change now.
Since we have implemented the treaps ourselves, we can easily extend them to do more things. The situation is very similar to segment trees: they are not just useful to store individual elements, but they can also store additional information that allows us to do bulk queries or updates faster.
We will ask you to implement the easiest of those extensions for treaps: compute the order of an element, and find the element with a given order. The order of an element of key $$$k$$$ is its (0-based) index when you traverse the treap in preorder (that is, from smaller to largest key, or from left to right). If you are familiar with GNU C++ STL PBDS containers, you might know these operations as order_of_key and find_by_order.
How can we implement this in logarithmic time? Make every node have its size precomputed, where the size of a node is just the number of nodes of the subtreap rooted at it. To find the element of order $$$t$$$ in a treap, compare $$$t$$$ with the size of the root's left child: if the size is larger than $$$t$$$, the element must be in the left child; if the size is exactly $$$t$$$, the element is the root; if the size is less than $$$t$$$, the element is in the right child.
The only difficulty is to make sure that, whenever split and join operations modify the treaps, these precomputed sizes don't get outdated. To show us that you can handle that, we will ask you to write a program that implements the following operations efficiently.
Starting with an empty treap, execute a sequence of orders, as described. Integers $$$k$$$ will be from $$$0$$$ to $$$10^5$$$, heights $$$h$$$ will be from $$$0$$$ to $$$10^9$$$. All requested indexes and orders will be non-negative numbers.
At all times the nodes in the tree will not have repeated keys or heights. We will not ask you to remove nodes that do not exist.
For each command ORDER_OF_KEY or FIND_BY_ORDER, output a line with the corresponding answer.
ADD 1 23ADD 2 19ADD 4 95ADD 6 13ADD 8 25ADD 9 71ORDER_OF_KEY 1ORDER_OF_KEY 2ORDER_OF_KEY 4ORDER_OF_KEY 6ORDER_OF_KEY 8ORDER_OF_KEY 9ORDER_OF_KEY 10FIND_BY_ORDER 0FIND_BY_ORDER 1FIND_BY_ORDER 2FIND_BY_ORDER 3FIND_BY_ORDER 4FIND_BY_ORDER 5FIND_BY_ORDER 6
0 1 2 3 4 5 -1 1 2 4 6 8 9 -1
ADD 0 50ADD 1 74ADD 2 23DEL 1ADD 3 91ADD 4 43ADD 5 17DEL 3ADD 9 65ADD 8 35ADD 1 74ADD 7 92DEL 9ADD 6 36ADD 3 19ADD 9 66ORDER_OF_KEY 3ORDER_OF_KEY 5ORDER_OF_KEY 9ORDER_OF_KEY 0FIND_BY_ORDER 4FIND_BY_ORDER 7FIND_BY_ORDER 2FIND_BY_ORDER 1
3 5 9 0 4 7 2 1