Movie collection(2013.09.15)树状数组

I. Movie collection

Time Limit:16000ms
Case Time Limit:16000ms
Memory Limit:262144KB
64-bit integer IO format: %lld Java class name: Main
Font Size: + -

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 integersmandr(1 ≤m,r≤ 100000): the number of movies in the stack and the number of locate requests.
  • one line withrintegersa1,…,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 1,2,…,min increasing order, where the movie box with label 1 is the top-most box.

Output

Per test case:

  • one line withrintegers, where thei-th integer gives the number of movie boxes above the box with labelai, immediately before this box is removed from the stack.

Note that after each locate requestai, the movie box with labelaiis placed at the top of the stack.

Sample in- and output

Input

Output

2
3 3
3 1 1
5 3
4 4 5
2 1 0
3 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


这是个树状数组。。我想了很久也木有想出来怎么处理。。太水了


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 201000
using namespace std;
int p[maxn],a[maxn];
int lowbit(int x)
{
    return x&-x;
}
void update(int x,int i)
{
    while(x<maxn)
    {
        a[x]+=i;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=a[x];
        x-=lowbit(x);
    }
    return ans;
}
int main ()
{
    int t,n,m,i;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        memset(p,0,sizeof(p));
        memset(a,0,sizeof(a));
        for (i=1; i<=n; i++)
        {
            p[i]=n-i+1;//利用p[]数组来记录位置
            update(i,1);
        }
        int pos,s=n;
        while(m--)
        {
            scanf("%d",&pos);
            if (m!=0) printf("%d ",n-sum(p[pos]));//pos代表第几张电影碟片,p[pos]代表着该碟片在树状数组当中的位置
            else printf("%d\n",n-sum(p[pos]));
            update(p[pos],-1);//从p[pos]位置抽出碟片之后,相应的数组需要更新
            p[pos]=++s;//代表着将pos碟片放在了顶上
            update(p[pos],1);//更新数组
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值