您的位置:首页 > 其它

UVa 1513 - Movie collection 解题报告(线段树)

2014-03-02 20:25 411 查看
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 removes it carefully, ensuring that the
stack doesn't fall over. After he finishes watching the movie, he places 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 this information, its position in the
stack can be calculated. Each movie is identified by a number printed 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 should print 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 test case:

one line with two integers n and m (1

n, m

100000):
the number of movies in the stack and the number of locate requests.
one line with m integers a1,..., am (1

ai

n) representing
the identification numbers of movies that Mr. K. I. wants to watch.

For simplicity, assume the initial stack contains the movies with identification numbers 1, 2,..., n in 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 above the 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 ai is placed at the top of the stack.

Sample Input 

2
3 3
3 1 1
5 3
4 4 5


Sample Output 

2 1 0
3 0 4

 
    解题报告:假设n = 5。开始状态时,书的排列如下:
    5 4 3 2 1
    右边为顶端。现在我用一个pos数组记录每本书的当前位置:pos[4] = 2。现在一共5本书。
    如果现在我要取编号为4的书,那么编号为4的书上方一共有n - pos[4]本书。
    假设我们不是将4拿到栈顶,而是放一本新的编号为4的书,栈的状态就是这样。
    5 4 3 2 1 4'
    一共6本书,pos[4]的位置更新为6。那么编号为2的书上方的书仍然可以使用n'-pos[2]计算(n' = n+1)。
    而不能直接计算的是编号为4之前的书。因为旧的编号为4的书不计入计算了。所以我们使用线段树对每次取得书下方的区间进行加上-1的操作,查询上方多少书时减去不计入计算的书的数量。使用懒惰标记。代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 200020;
int add[maxn << 2];
int pos[maxn];

#define lson l,m,pos<<1
#define rson m+1,r,pos<<1|1
#define defm int m = (l+r)>>1;

void updateSon(int pos)
{
if (add[pos] == 0) return;
add[pos << 1] += add[pos];
add[pos << 1 | 1] += add[pos];
add[pos] = 0;
}

void update(int L, int R, int val, int l, int r, int pos)
{
if (L <= l && r <= R)
{
add[pos] += val;
return;
}

updateSon(pos);
defm;
if (L <= m)
update(L, R, val, lson);
if (m < R)
update(L, R, val, rson);
}

int query(int L, int R, int l, int r, int pos)
{
if (L <= l && r <= R)
{
return add[pos];
}

updateSon(pos);
defm;
return (L <= m ? query(L, R, lson) : 0) + (m < R ? query(L, R, rson) : 0);
}

void work()
{
memset(add, 0, sizeof(add));

int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
pos[i] = n - i + 1;

int count = n;
int len = n + m;

while (m--)
{
int book;
scanf("%d", &book);
count++;

int val = query(pos[book], pos[book], 1, len, 1);
printf("%d", count - pos[book] - 1 + val);
printf(m ? " " : "\n");
update(1, pos[book], -1, 1, len, 1);
pos[book] = count;
}
}

int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
work();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树