UVA

来源:互联网 发布:985软件工程硕士工资 编辑:程序博客网 时间:2024/06/03 22:47

原题:


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 lo cates the movie in this stack and removes it carefully, ensuring
that the stack do esn’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 p osition of each movie. It is
sufficient to know for each movie how many movies are placed ab ove it, since, with this information,
its p osition in the stack can b e calculated. Each movie is identified by a numb er printed on the movie
b ox.
Your task is to implement a program which will keep track of the p osition of each movie. In
particular, each time Mr. K. I. removes a movie b ox from the stack, your program should print the
numb er of movies that were placed ab ove it b efore it was removed.
Input
On the first line a p ositive integer: the numb er of test cases, at most 100. After that p er test case:
one line with two integersn andm (1n, m100000): the number of movies in the stack and
the number of locate requests.
one line withm integersa 1, . . . , a m(1 a in) 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 withm integers, where thei-th integer gives the number of movie boxes above the box
with label a i, immediately before this box is removed from the stack.
Note that after each locate request a i, the movie box with labela iis 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张影碟,每次看过后就放在最上面,查询某次操作前,该影碟在哪个位置。


思路:

      由于要将影片放在前面,将数组空间开成2*n,预留出n个位置,每次操作通过树状数组单点修改该位置-1,在移动后位置+1,查询时sum操作即可。


#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long ll;typedef pair <int,int > P;int m,n,i,j,T;int c[100005*2],pos[100005];int lowbit(int x){    return x&(-x);}void add(int x,int d){    while(x<100005*2)    {        c[x]+=d;        x+=lowbit(x);    }}int sum(int x){    int ans=0;    while(x>0)    {        ans+=c[x];        x-=lowbit(x);    }    return ans;}int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        memset(c,0,sizeof(c));        memset(pos,0,sizeof(pos));        for(int i=1; i<=n; i++)        {            pos[i]=100000+i;            add(pos[i],1);        }        for(int i=0; i<m; i++)        {            int num;            scanf("%d",&num);            int res=sum(pos[num]);            add(pos[num],-1);            pos[num]=100000-i;            add(pos[num],1);            if(i==0)                printf("%d",res-1);            else                printf(" %d",res-1);        }        printf("\n");    }    return 0;}