UVA 1513(树状数组)

来源:互联网 发布:linux中国时区 编辑:程序博客网 时间:2024/05/16 00:56

Movie collection

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 and (1 ≤ m,r ≤ 100 000): the number of movies in the stack and the number of locate requests.
  • one line with integers a1,…,ar (1 ≤ ai ≤ m) representing the identification numbers of movies that Mr. K. I. wants to watch.

For simplicity, assume that the initial stack contains the movies with identification numbers 12,…,m in increasing order, where the movie box with label 1 is the top-most box.

 

Output

Per test case:

  • one line with 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 in- and output

Input

Output

23 33 1 15 34 4 5
2 1 03 0 4

Copyright notice

This problem text is copyright by the NWERC 2011 jury. It is licensed under the Creative Commons Attribution-Share Alike license version 3.0; The complete license text can be found at: http://creativecommons.org/licenses/by-sa/3.0/legalcode


题意:给你n张DVD,按从小到大摆放,最上面是1,最下面是n,给你m个操作,每个操作输出这张DVD上面有多少张DVD,并拿出这张DVD并将它放到最上面。


解题思路:

用树状数组,开两倍n的空间,拿出第一张DVD,把它放在n+1的位置,拿出第二张DVD的时候把它放在n+2的位置。



具体代码如下:


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define sc(a) scanf("%d",&a)#define nsc(a,b) scanf("%d %d",&a,&b)#define pr(a) printf("%d\n",a)#define mem(a,x) memset(a,x,sizeof(a))#define lf(i,l,r) for(int i=l;i<r;i++)const int maxn=1e5+100;int t,m,n,num[maxn],c[maxn*2]; //num数组存储DVD放在第几层int lowbit(int x){    return x&(-x);}void add(int i,int x){    while(i<maxn*2)    {        c[i]+=x;        i+=lowbit(i);    }    return ;}void init(){    mem(c,0);    lf(i,1,m+1)    {        num[i]=m+1-i;        add(num[i],1);    }    return ;}int Sum(int x){    int sum=0;    while(x>0)    {        sum+=c[x];        x-=lowbit(x);    }    return sum;}int main(){    sc(t);    while(t--)    {        nsc(m,n);        init();        int a,count1=m+1;        lf(i,0,n)        {            sc(a);            printf("%d%c",m-Sum(num[a]),i==n-1?'\n':' ');            add(num[a],-1);            num[a]=count1++;            add(num[a],1);        }    }    return 0;}













0 0
原创粉丝点击