Стайня
Limits: 7 sec., 512 MiB
Як ви вже знаєте, у Зеника з Марічкою є \(n\) жеребців, пронумерованих від \(1\) до \(n\), яких вони тримають у стайні.
Наявність коней у стайні можна задати рядком \(t\) довжини \(n\), що складається із символів
0 і 1. \(t_i=\,\)1, якщо кінь з номером
\(i\) зараз у стайні, та \(t_i=\,\)0, якщо він на
пасовищі.
Задано рядок \(s\), що описує стайню зранку.
Протягом дня Марічка веде коней на пасовище та повертає до стайні.
За одну дію Марічка може:
Відвести зі стайні на пасовище всіх жеребців з номерами від \(1\) до \(i\). Деякі з них можуть уже бути на пасовищі.
Привести з пасовища до стайні всіх жеребців з номерами від \(1\) до \(i\). Деякі з них можуть уже бути в стайні.
Час від часу протягом дня Зеник питає в Марічки, чи є в стайні \(i\)-ий кінь?
Вам задано \(q\) запитів:
1 i 0— Марічка відводить на пасовище коней з номерами від \(1\) до \(i\).1 i 1— Марічка приводить до стайні коней з номерами від \(1\) до \(i\).2 i— Зеник питає в Марічки, чи \(i\)-ий кінь у стайні.
Марічка й так уже втомилася від своєї роботи, а ще Зеник зі своїми питаннями. Допоможіть Марічці відповісти на них.
Input
У першому рядку задано два цілі числа \(n\) та \(q\) — кількість коней у стайні і кількість запитів відповідно.
У другому рядку задано рядок \(s\).
У наступних \(q\) рядках задано запити в описаному вище форматі.
Output
На кожне запитання Зеника виведіть відповідь в окремому рядку —
1, якщо в стайні є кінь, або 0, якщо нема.
Constraints
\(1 \le n \le 5 \cdot 10^5\),
\(1 \le q \le 3 \cdot 10^5\),
рядок \(s\) складається із символів
0 і 1,
Зеник поставить принаймні одне питання Марічці.
Оцінювання складається з таких блоків:
1 бал за приклад з умови,
19 балів: Марічка не приводить коней з пасовища до стайні,
12 балів: \(n \le 1000, q \le 1000\),
30 балів: \(q \le 2 \cdot 10^4\),
38 балів: без додаткових обмежень.
Бали за блок ви отримаєте, тільки якщо ваша програма пройде всі тести з блоку.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 5 8 11011 1 4 1 2 3 1 5 0 2 3 2 5 1 4 1 2 3 2 5 | 1 0 0 1 0 |
Notes
Розглянемо приклад.
Марічка приводить коней з номерами від \(1\) до \(4\) з пасовища до стайні. Рядок стає
11111.Зеник запитує, чи є в стайні кінь з номером \(3\). Відповідь:
1(у стайні є кінь).Марічка відводить коней з номерами від \(1\) до \(5\) на пасовище. Рядок стає
00000.Зеник запитує, чи є в стайні кінь з номером \(3\). Відповідь:
0(у стайні немає коня).Зеник запитує, чи є в стайні кінь з номером \(5\). Відповідь:
0(у стайні немає коня).Марічка приводить коней з номерами від \(1\) до \(4\) з пасовища до стайні. Рядок стає
11110.Зеник запитує, чи є в стайні кінь з номером \(3\). Відповідь:
1(у стайні є конь).Зеник запитує, чи є в стайні кінь з номером \(5\). Відповідь:
0(у стайні немає коня).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Stable - Editorial
Let’s restate the problem. We are given a string \(s\) of 0 and 1. A
query of the first type assigns a given value to the first \(i\) characters, and a query of the second
type asks for the character at position \(i\).
In this problem we maintain the runs of equal values in the string \(s\).
For example, \(s =
\,\)000011100111100000. The first run ends at
position \(4\) and consists of the
character 0. We denote it (\(4\), 0). The second run ends
at position \(7\) and consists of the
character 1. We denote it (\(7\), 1). All runs of \(s\): (\(4\), 0), (\(7\), 1), (\(9\), 0), (\(13\), 1), (\(18\), 0).
If a query assigns 1 to the first \(8\) characters, the string becomes
111111110111100000. The runs of the new string: (\(8\), 1), (\(9\), 0), (\(13\), 1), (\(18\), 0).
As the example shows, a query of the first type removes some runs from the beginning and inserts one run in their place.
Consider a query of the second type. Suppose we need to find the
character at the \(i\)-th position. For
this we need the run that contains this position: the run ending at a
position \(\ge i\) with the smallest
end position. In our example, for \(i =
11\) this is the run (\(13\),
1).
To solve the problem, we keep these runs in order from right to left
(in decreasing order of their end positions). In C++ we can
use vector<pair<int, char>>.
For a query of the first type, we remove some runs from the end
(pop_back) and insert one at the end
(push_back).
To answer a query of the second type, we find the required run with binary search. If you are not familiar with this algorithm, we strongly recommend watching the video lecture on Algotester’s YouTube channel.
Now let’s estimate the complexity of the solution.
Before all queries there are \(O(n)\) runs. Each query of the first type
adds one new run. So the total number of runs added while the algorithm
runs is \(O(n + q)\). And how many run
removals (pop_back) can there be? At first it may seem
that, since there are \(O(n + q)\) runs
in total, each query of the first type can take \(O(n + q)\). However, each run is removed at
most once. So the total number of removals is also \(O(n + q)\).
For each query of the second type we run a binary search in \(O(\log (n + q))\).
Total complexity: \(O(n + q + q \log (n + q)) = O(n + q \log (n + q))\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|