Entrenamiento OIE Nivel Experto - Semana 12
A. Parking
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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,

- X X - - X - - - - X
there are exactly 12 different ways of parking vehicles in the 7 empty parking spots. These are just 4 of these ways, where c is a car, sss is an SUV, and ttttttt is an 18-wheeler.
c X X c c X c c c c X c X X sss X c c c c X c X X c c X c sss c X c X X sss X ttttttt X
Input

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.

Output

For each input case, output a line with the total number of different configurations. No output will be larger than $$$10^{18}$$$.

Examples
Input
-
X
--
-X
XX
---
----
Output
1
1
2
1
1
3
6
Input
-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
Output
12
990
69988378
2

B. Bigint
time limit per test
0.4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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

Output $$$k$$$ lines with the results of the $$$k$$$ sums, again in base 10.

Examples
Input
1
1
1
2 1 1
Output
2
Input
1
999999999
4
1 1
2 1 1
3 1 1 1
4 1 1 1 1
Output
999999999
1999999998
2999999997
3999999996
Input
4
1
999999999
1000000000
1000000001
3
2 1 2
2 1 3
4 1 2 3 4
Output
1000000000
1000000001
3000000001
Input
4
1234567890123456789012345678901234
11234567890123456789012345678901234
111234567890123456789012345678901234
1111234567890123456789012345678901234
3
1 1
4 1 2 3 4
4 4 3 2 1
Output
1234567890123456789012345678901234
1234938271560493827156049382715604936
1234938271560493827156049382715604936

Statement is not available in English language
Statement is not available in English language
E. Tao of Trees (more treaps)
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

  • ADD $$$k$$$ $$$h$$$: Add a node $$$K$$$ with key $$$k$$$ and height $$$h$$$ into the treap. Use 1 split and 2 joins.
  • DEL $$$k$$$: Delete the node with key $$$k$$$ from the treap $$$T$$$, using 2 splits and 1 join.
  • ORDER_OF_KEY $$$k$$$: Find the order of the key $$$k$$$, or $$$-1$$$ if it does not exist.
  • FIND_BY_ORDER $$$i$$$: Find the key of order $$$i\geq 0$$$, or $$$-1$$$ if the treap has $$$i$$$ elements or fewer.
Input

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.

Output

For each command ORDER_OF_KEY or FIND_BY_ORDER, output a line with the corresponding answer.

Examples
Input
ADD 1 23
ADD 2 19
ADD 4 95
ADD 6 13
ADD 8 25
ADD 9 71
ORDER_OF_KEY 1
ORDER_OF_KEY 2
ORDER_OF_KEY 4
ORDER_OF_KEY 6
ORDER_OF_KEY 8
ORDER_OF_KEY 9
ORDER_OF_KEY 10
FIND_BY_ORDER 0
FIND_BY_ORDER 1
FIND_BY_ORDER 2
FIND_BY_ORDER 3
FIND_BY_ORDER 4
FIND_BY_ORDER 5
FIND_BY_ORDER 6
Output
0
1
2
3
4
5
-1
1
2
4
6
8
9
-1
Input
ADD 0 50
ADD 1 74
ADD 2 23
DEL 1
ADD 3 91
ADD 4 43
ADD 5 17
DEL 3
ADD 9 65
ADD 8 35
ADD 1 74
ADD 7 92
DEL 9
ADD 6 36
ADD 3 19
ADD 9 66
ORDER_OF_KEY 3
ORDER_OF_KEY 5
ORDER_OF_KEY 9
ORDER_OF_KEY 0
FIND_BY_ORDER 4
FIND_BY_ORDER 7
FIND_BY_ORDER 2
FIND_BY_ORDER 1
Output
3
5
9
0
4
7
2
1