Replace
Practice
5 (7 votes)
Hard
Segment trees
Problem
80% Success 3474 Attempts 50 Points 2s Time Limit 1024MB Memory 1024 KB Max Code

Yet another interesting problem about queries!

You have an array of n integers and q queries on this array. There are two types of queries:

  1. 1 \(a_i\) \(b_i\) \(x_i\) \(y_i\) - change all occurrences of \(x_i\) to \(y_i\) on the segment \([a_i, b_i]\)
  2. 2 \(a_i\) \(b_i\) \(x_i\) - count number of occurrences of \(x_i\) on the segment \([a_i, b_i]\)

This problem will be very popular! So you should solve it before before it became mainstream.

Input format

The first line of input contains two space separated integers n and q (\(1 \leq n, q \leq 5 \cdot 10^5\)) - size of array and number of queries.

The second line of input contains n space separated integers \(a_i\) (\(1 \leq a_i \leq n\)) - the elements of the array before queries.

Then q lines follow. The i-th of them contains parameters of the i-th query.

The i-th line can be:

  • 1 \(a_i\) \(b_i\) \(x_i\) \(y_i\) (\(1 \leq a_i \leq b_i \leq n\), \(1 \leq x_i, y_i \leq n\)) - parameters for first type query or
  • 2 \(a_i\) \(b_i\) \(x_i\) (\(1 \leq a_i \leq b_i \leq n\), \(1 \leq x_i \leq n\)) - parameters for second type query

Output format

For each query of second type print number of occurrences of \(x_i\) on the segment \([a_i, b_i]\).

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
1 votes
Tags:
ApprovedData StructuresHardMatrixSegment Trees
Points:50
2 votes
Tags:
Advanced Data StructuresSegment TreesData Structures
Points:50
3 votes
Tags:
Abstract classAdvanced Data StructuresData StructuresHardSegment Treesmaximum sub array sumoct circuits