两道树状数组

Movie Collection(NWERC 2011)

Problem

Description

Mr. K. I. has a very big movie collection. He has organized his collection in a big stack.Whenever he wants to watch one of the movies, he locates the movie in this stack and removesit carefully, ensuring that the stack doesn’t fall over. After he finishes watching the movie, heplaces it at the top of the stack.Since the stack of movies is so big, he needs to keep track of the position of each movie.It is sufficient to know for each movie how many movies are placed above it, since, with thisinformation, its position in the stack can be calculated. Each movie is identified by a numberprinted on the movie box.Your task is to implement a program which will keep track of the position of each movie.In particular, each time Mr. K. I. removes a movie box from the stack, your program shouldprint the number of movies that were placed above it before it was removed.

Input

On the first line a positive integer: the number of test cases, at most 100. After that per testcase:• one line with two integers n and m (1 ≤ n, m ≤ 100 000): the number of movies in thestack and the number of locate requests.• one line with m integers a1, . . . , am (1 ≤ ai ≤ n) representing the identification numbersof movies that Mr. K. I. wants to watch.For simplicity, assume the initial stack contains the movies with identification numbers 1, 2, . . . , nin increasing order, where the movie box with label 1 is the top-most box.

Output

Per test case:• one line with m integers, where the i-th integer gives the number of movie boxes abovethe box with label ai, immediately before this box is removed from the stack.Note that after each locate request ai, the movie box with label aiis placed at the top of thestack.

Note

感觉是极其经典的树状数组的应用，更加理解了求逆序数对的方法。

Bitris(SEERPC 2012)

传送门: [Gym - 101472B]

Problems

Description

There is a set of 2xN cubes. Each cube has an integer ranging from 1 to N assigned to it, labelingeach of the cube’s sides. Each number is written on exactly two cubes. Cubes are placed one ontop of another, in a pile. If two cubes with the same number stand next to each other, theyannihilate: both cubes disappear, and cubes standing above come down to fill the space.Your task is to disassemble the pile – eliminate all cubes. You are allowedto swap any two neighboring cubes. A swap could be done only after allpossible annihilations are done.For example, if N=4 and cubes are standing as you see on the right thenyou need to make one swap. Cubes with label 3 annihilate immediately;you swap the fourth bottom cube (with label 1) and the fifth bottom cube(with label 4); afterwards, cubes with labels 4 annihilate, followed by cubeswith labels 1 and labels 2. Other option is to swap third and fourth bottomcubes (in this case cubes with labels 1 and 4 annihilate at same time,followed by cubes with label 2), or second and third.If N=3, and cubes are standing like shown on the right, you need toperform 3 swaps. One way to solve is to swap fifth and sixth cubes, thenfourth and fifth; cubes with labels 2 annihilate; then swap second andthird; other cubes annihilate simultaneously.The task is to find the minimal number of swaps, such that all cubes areeliminated.

Input data.

The first line in the input file contains the positive integer N,2<=N<=100000. The second line contains the labels of all cubes, bottom up, split by spaces.

Output data.

The only line in the output should contain one non-negative integer M – the minimalnumber of swaps necessary to destroy all cubes.