C. Goodbye Souvenir
time limit per test:6 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
I won't feel lonely, nor will I be sorrowful... not before everything is buried.
A string of n beads is left as the message of leaving. The beads are numbered from 1 to n from left to right, each having a shape numbered by integers between 1 and n
The memory of a shape x in a certain subsegment of beads, is defined to be the difference between the last position and the first position that shape x appears in the segment. The memory of a subsegment is the sum of memories
From time to time, shapes of beads change as well as the memories. Sometimes, the past secreted in subsegments are being recalled, and you are to find the memory
Input
The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 100 000) — the number of beads in the string, and the total number of changes and queries, respectively.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the initial shapes of beads 1, 2, ..., n, respectively.
The following m
- 1 p x (1 ≤p≤n, 1 ≤x≤n), meaning that the shape of thep-th bead is changed intox;
- 2 l r (1 ≤l≤r≤n), denoting a query of memory of the subsegment fromltor, inclusive.
Output
For each query, print one line with an integer — the memory
Examples
Input
7 6 1 2 3 1 3 2 1 2 3 7 2 1 3 1 7 2 1 3 2 2 1 6 2 5 7
Output
5 0 7 1
Input
7 5 1 3 2 1 4 2 3 1 1 4 2 2 3 1 1 7 2 4 5 1 1 7
Output
0 0
Note
The initial string of beads has shapes (1, 2, 3, 1, 3, 2, 1).
Consider the changes and queries in their order:
- 2 3 7: the memory of the subsegment [3, 7] is (7 - 4) + (6 - 6) + (5 - 3) = 5;
- 2 1 3: the memory of the subsegment [1, 3] is (1 - 1) + (2 - 2) + (3 - 3) = 0;
- 1 7 2: the shape of the 7-th bead changes into 2. Beads now have shapes (1, 2, 3, 1, 3, 2, 2)
- 1 3 2: the shape of the 3-rd bead changes into 2. Beads now have shapes (1, 2, 2, 1, 3, 2, 2)
- 2 1 6: the memory of the subsegment [1, 6] is (4 - 1) + (6 - 2) + (5 - 5) = 7;
- 2 5 7: the memory of the subsegment [5, 7] is (7 - 6) + (5 - 5) = 1.
再度加深对CDQ分治的理解。大致题意:给你一个数字序列,然后有两种操作。一是修改,把某个位置的数字改成另外一个数字;二是求一个区间[l,r]中所有同类数字的最大跨度和,即如样例所示。
首先,对于这个同类数字中的最大跨度和,对于每一个数字可以按照套路,分成所有前后相邻两个同类数字的距离和。即1、2、1、3、1、2、3对于数字1的最大跨度,我们可以写成(3-1)+(5-3)=4,也样子相当于只需要维护前后两个相邻的数字即可。然后,我们考虑如何对于每个数字维护这个前后关系,支持删除然后又支持修改,显然平衡树再合适不过。对于每一个数字,我建立一颗平衡树,然后维护前驱和后继,添加和删除。最后对于区间的查询,我们在平衡树外面套一个主席树即可完成区间的求值,对于每一个区间内的数字位置求和即可。
然而,本题的目的并不是考数据结构,真正用树套树的话内存也不够。所以说这里得考虑用CDQ分治大法,对于平衡树,由于只是利用到前驱和后继以及删除,所以STL的set也足够用了。对于一个数字,能够把它与它的前驱的位置差值加入到结果中的条件是它们两个都在所求区间中。根据这一点,我们考虑把这两个东西作为坐标,在二维的平面上操作,对于时间分治。询问[l,r]之间的和也就可以对应到一个矩形的区间中,范围是(l,l)~(r,r)。对于每一个点,我们有三个参数(x,y,val),x表示当前位置,y表示前一个同类数字的位置,val表示两个位置的差值,x、y同时表示在平面中的位置。这样就有了时间和两个平面坐标这三个参数,可以转换为一个三维偏序的问题。具体做法与传统的三维偏序问题类似。对于修改操作则是采用先删除再添加的方式,注意对于ans要使用LL。具体见代码: