codeforces-scrape

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

1066C.md (3420B)


      1 Source: https://codeforces.com/problemset/problem/1066/C
      2 
      3 C. Books Queries
      4 
      5 time limit per test
      6 
      7 2 seconds
      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 You have got a shelf and want to put some books on it.
     22 
     23 You are given $q$ queries of three types:
     24 
     25 1. L $id$ — put a book having index $id$ on the shelf to the left from the leftmost existing book;
     26 2. R $id$ — put a book having index $id$ on the shelf to the right from the rightmost existing book;
     27 3. ? $id$ — calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index $id$ will be leftmost or rightmost.
     28 
     29 You can assume that the first book you will put can have any position (it does not matter) and queries of type $3$ are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so $id$s don't repeat in queries of first two types.
     30 
     31 Your problem is to answer all the queries of type $3$ in order they appear in the input.
     32 
     33 Note that after answering the query of type $3$ all the books remain on the shelf and the relative order of books does not change.
     34 
     35 If you are Python programmer, consider using PyPy instead of Python when you submit your code.
     36 
     37 Input
     38 
     39 The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) — the number of queries.
     40 
     41 Then $q$ lines follow. The $i$-th line contains the $i$-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type $3$, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).
     42 
     43 It is guaranteed that there is at least one query of type $3$ in the input.
     44 
     45 In each query the constraint $1 \le id \le 2 \cdot 10^5$ is met.
     46 
     47 Output
     48 
     49 Print answers to queries of the type $3$ in order they appear in the input.
     50 
     51 Examples
     52 
     53 Input
     54 
     55 ```
     56 8  
     57 L 1  
     58 R 2  
     59 R 3  
     60 ? 2  
     61 L 4  
     62 ? 1  
     63 L 5  
     64 ? 1
     65 ```
     66 
     67 Output
     68 
     69 ```
     70 1  
     71 1  
     72 2
     73 ```
     74 
     75 Input
     76 
     77 ```
     78 10  
     79 L 100  
     80 R 100000  
     81 R 123  
     82 L 101  
     83 ? 123  
     84 L 10  
     85 R 115  
     86 ? 100  
     87 R 110  
     88 ? 115
     89 ```
     90 
     91 Output
     92 
     93 ```
     94 0  
     95 2  
     96 1
     97 ```
     98 
     99 Note
    100 
    101 Let's take a look at the first example and let's consider queries:
    102 
    103 1. The shelf will look like $[1]$;
    104 2. The shelf will look like $[1, 2]$;
    105 3. The shelf will look like $[1, 2, 3]$;
    106 4. The shelf looks like $[1, \textbf{2}, 3]$ so the answer is $1$;
    107 5. The shelf will look like $[4, 1, 2, 3]$;
    108 6. The shelf looks like $[4, \textbf{1}, 2, 3]$ so the answer is $1$;
    109 7. The shelf will look like $[5, 4, 1, 2, 3]$;
    110 8. The shelf looks like $[5, 4, \textbf{1}, 2, 3]$ so the answer is $2$.
    111 
    112 Let's take a look at the second example and let's consider queries:
    113 
    114 1. The shelf will look like $[100]$;
    115 2. The shelf will look like $[100, 100000]$;
    116 3. The shelf will look like $[100, 100000, 123]$;
    117 4. The shelf will look like $[101, 100, 100000, 123]$;
    118 5. The shelf looks like $[101, 100, 100000, \textbf{123}]$ so the answer is $0$;
    119 6. The shelf will look like $[10, 101, 100, 100000, 123]$;
    120 7. The shelf will look like $[10, 101, 100, 100000, 123, 115]$;
    121 8. The shelf looks like $[10, 101, \textbf{100}, 100000, 123, 115]$ so the answer is $2$;
    122 9. The shelf will look like $[10, 101, 100, 100000, 123, 115, 110]$;
    123 10. The shelf looks like $[10, 101, 100, 100000, 123, \textbf{115}, 110]$ so the answer is $1$.