Operations on an array
Practice
2.4 (27 votes)
Advanced data structures
C++
Data structures
Fenwick tree
Problem
40% Success 3563 Attempts 30 Points 1s Time Limit 255MB Memory 1024 KB Max Code

You are given an array of \(n\) elements and an integer \(x\). You must perform the following types of operations on the array:

  1. Find the index of the \(k^{th}\) occurrence of \(x\) in the range \(l\) to \(r\) (both inclusive). If there are no indexes that satisfy the condition, then print \(-1\).
  2. Update the value that is present at the given index.

For each query of type 1, print the index of the \(k^{th}\) occurrence of \(x\).

Input format

  • The first line contains two integers \(n\) and \(x\) denoting the number of elements and an integer whose index is required respectively.
  • The second line contains \(n\) integers \(a_i\) denoting the elements of the array
  • The third line contains an integer \(q\) denoting the number of queries
  • Next q lines contain the following type of queries:
    • If type is equal to 1, then \(l\), \(r\), and \(k\) (refer to the first point of the problem statement)
    • If type is equal to 2, then index and value (refer to the second point of the problem statement)

Output format

Print \(q\) integers as the answer to type 1 queries. If there are no indexes that satisfy the condition, then print \(-1\).

Constraints

\(1 ≤ n ≤ 10^6\\ 1 ≤ x ≤ 10^3\\ 1 ≤ a_i ≤ 10^3\\ 1 ≤ q ≤ 10^6\\ 1 ≤ type ≤ 2\\ 1 ≤ l ≤ r ≤ n\\ 1 ≤ k ≤ 10^3\\ 1 ≤ index ≤ n\\ 1 ≤ value ≤ 10^3\)

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:30
7 votes
Tags:
Advanced Data StructuresData StructuresFenwick (Binary Indexed) TreesC++
Points:30
19 votes
Tags:
ApprovedDepth First SearchGraphsMediumReadySegment TreesTrees
Points:30
21 votes
Tags:
DatastructuresFenwick (Binary Indexed) TreesInversion CountFenwick TreeAdvanced Data StructuresData StructuresSegment tree