#FT0000052. Tập hợp với truy vấn phần tử trước và sau (Predecessor Problem)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tập hợp với truy vấn phần tử trước và sau (Predecessor Problem)
Predecessor Problem
Source: Library Checker
Version: Phuoc Hung OJ Extended
Problem Statement
A set contains distinct integers between and . Its initial content is encoded by a binary string of length : value is initially present exactly when the zero-indexed character is 1.
Process operations in order: 0 k inserts if absent; 1 k erases if present; 2 k prints 1 if is present or 0 otherwise; 3 k prints the smallest present value at least ; and 4 k prints the greatest present value at most . For either predecessor/successor operation, print -1 when no suitable value exists.
Input
- Line 1: integers .
- Line 2: a binary string of exactly characters describing the initial set.
- Next lines: each line contains operation code and key .
Output
Print one integer for each type 2, 3, or 4 query, in the order they occur.
Subtasks
Subtask 1 (20 points):
- ; ; has exactly binary digits; and in every query.
Subtask 2 (30 points):
- ; ; has exactly binary digits; and in every query.
Subtask 3 (50 points):
- ; ; has exactly binary digits; and in every query.
Examples
Examples 1
Input
6 9
010101
3 3
4 3
4 0
0 4
1 3
2 4
2 3
3 3
4 3
Output
3
3
-1
1
0
4
1
Explanation
From 010101, the set initially is . The successor and predecessor of both equal , whereas no element is at most . Insert and erase , leaving . Membership tests for and yield . The successor of is and its predecessor is .
Examples 2
Input
3 6
000
3 0
0 2
4 1
2 2
1 2
2 2
Output
-1
-1
1
0
Explanation
The set starts empty, so no successor of exists. After inserting , the set is , whose predecessor of is absent. Membership of is 1; after erasing it, the membership becomes 0.