Replace

5

7 votes
Hard, Segment Trees
Problem

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 ai bi xi yi - change all occurrences of xi to yi on the segment [ai,bi]
  2. 2 ai bi xi - count number of occurrences of xi on the segment [ai,bi]

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 (1n,q5105) - size of array and number of queries.

The second line of input contains n space separated integers ai (1ain) - 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 ai bi xi yi (1aibin, 1xi,yin) - parameters for first type query or
  • 2 ai bi xi (1aibin, 1xin) - parameters for second type query

Output format

For each query of second type print number of occurrences of xi on the segment [ai,bi].

Time Limit: 2
Memory Limit: 1024
Source Limit:
Explanation

Initially array is [3,2,1,2].

First query: there are 2 occurrences of 2 on segment [2,4].

Second query changes array to [3,2,2,2].

Third query: there are 3 occurrences of 2 on segment [2,4].

Editor Image

?