Tom and Jerry love matrices
Practice
2.9 (17 votes)
Segment trees
Dfs
Advanced data structures
Data structures
Pointer
Math
Problem
92% Success 3920 Attempts 50 Points 3s Time Limit 1024MB Memory 1024 KB Max Code

Tom and Jerry are playing with matrices. Jerry gifts Tom with a matrix of size $$M \times N$$. The matrix element have a unique property that each cell of the matrix has value $$A_{i,j} = X + i + j$$. Since Tom loves challenges, Jerry will give Tom $$Q$$ queries. Each of these queries can be of any of the following three types:

  1. $$R$$ $$C_1$$ $$C_2$$: Delete the cells in the Row $$R$$ starting from column $$C_1$$ and ending at column $$C_2$$ $$(1 \le C_1 \le C_2 \le N, 1 \le R \le M)$$ 
  2. $$C$$ $$R_1$$ $$R_2$$: Delete the cells in column $$C$$ starting from row $$R_1$$ and ending at row $$R_2$$ $$(1 \le R_1 \le R_2 \le M, 1\le C \le N)$$.
  3. $$K$$: Take all the remaining elements in the array and sort the elements by their values. Now, return the $$K^{th}$$ smallest element. If $$K$$ is greater than the number of elements remaining, then print $$-1$$.($$1 \le K \le 10^9$$)

You are required to print the output only in case of the queries of type 3. You must help Tom to solve the tasks and answer the queries given by Jerry.

Input format

  • The first line of input contains four integers, $$M$$, $$N$$, $$X$$, and $$Q$$ ($$1 \le N, M, X \le 10^9, 1 \le Q \le 5*10^5 $$). Here, $$Q$$ is the number of queries and $$M$$ and $$N$$ are the dimensions of the matrix. 
  • The next $$Q$$ lines contain queries of the type mentioned in the question. 
    • 1 A B C for queries of type 1
    • 2 A B C for queries of type 2
    • 3 A for queries of type 3

No two deleted sub rows or subcolumns will intersect, that is the segments queried to be deleted are disjoint.

Output format

For queries of type 3, print a single integer on a new line corresponding to the answer of that query as mentioned in the question.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
10 votes
Tags:
Advanced Data StructuresSegment TreesC++Data Structures
Points:50
2 votes
Tags:
Advanced Data StructuresSegment TreesData Structures
Points:20
76 votes
Tags:
Easy