PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000052. Tập hợp với truy vấn phần tử trước và sau (Predecessor Problem)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

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 SS contains distinct integers between 00 and N−1N-1. Its initial content is encoded by a binary string TT of length NN: value ii is initially present exactly when the zero-indexed character TiT_i is 1.

Process QQ operations in order: 0 k inserts kk if absent; 1 k erases kk if present; 2 k prints 1 if kk is present or 0 otherwise; 3 k prints the smallest present value at least kk; and 4 k prints the greatest present value at most kk. For either predecessor/successor operation, print -1 when no suitable value exists.

Input

  • Line 1: integers N,QN,Q.
  • Line 2: a binary string TT of exactly NN characters describing the initial set.
  • Next QQ lines: each line contains operation code cc and key kk.

Output

Print one integer for each type 2, 3, or 4 query, in the order they occur.

Subtasks

Subtask 1 (20 points):

  • 1≤N≤1001\le N\le 100; 1≤Q≤1001\le Q\le 100; TT has exactly NN binary digits; 0≤c≤40\le c\le4 and 0≤k<N0\le k<N in every query.

Subtask 2 (30 points):

  • 1≤N≤200001\le N\le 20000; 1≤Q≤200001\le Q\le 20000; TT has exactly NN binary digits; 0≤c≤40\le c\le4 and 0≤k<N0\le k<N in every query.

Subtask 3 (50 points):

  • 1≤N≤100000001\le N\le 10000000; 1≤Q≤10000001\le Q\le 1000000; TT has exactly NN binary digits; 0≤c≤40\le c\le4 and 0≤k<N0\le k<N 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 {1,3,5}\{1,3,5\}. The successor and predecessor of 33 both equal 33, whereas no element is at most 00. Insert 44 and erase 33, leaving {1,4,5}\{1,4,5\}. Membership tests for 44 and 33 yield 1,01,0. The successor of 33 is 44 and its predecessor is 11.

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 00 exists. After inserting 22, the set is {2}\{2\}, whose predecessor of 11 is absent. Membership of 22 is 1; after erasing it, the membership becomes 0.