//
// main.cpp
// uva 1513 - Movie collection--树状数组--预留前n个位置
//
// Created by XD on 15/9/10.
// Copyright (c) 2015年 XD. All rights reserved.
//
/*
树状数组
做法就是预留前m个位置给后面的要看的的video,因为他们要移到顶端,所以我们就可以使用header表示现在最前端的在哪个位置
使用flag[i]表示标号为i的位置在哪 。对于一个询问,就可以将时间复杂度降到logn级别。
*/
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
#define ll long long
using namespace std ;
const int maxn = 100000 *2+10 ;
int flag[101010] ;
int c[maxn] ;
int head ;
int mn ;
int lowerbit(int x)
{
return x&(-x) ;
}
void update(int x, int v)
{
while (x <= maxn){
c[x] += v ; x += lowerbit(x) ;
}
}
int sum(int x)
{
int ret =0 ;
while (x > 0) {
ret += c[x] ; x -= lowerbit(x) ;
}
return ret ;
}
int main(int argc, const char * argv[]) {
int n ,T,m,pos;
scanf("%d" , &T) ;
while (T--) {
memset(c, 0, sizeof(c)) ;
scanf("%d%d" , &n , &m) ;
for (int i = 1; i <= n ; i++) {
flag[i] = i + m + 5 ;
update( i+ m + 5, 1) ;
}
head = m + 1 ;
for (int i = 0 ; i < m-1; i++) {
scanf("%d" , &pos) ;
printf("%d " , sum(flag[pos])-1) ;
update(flag[pos], -1) ;
flag[pos] = head-- ;
update(flag[pos], 1) ;
}
scanf("%d" , &pos) ;
printf("%d\n" , sum(flag[pos])-1) ;
}
return 0;
}
uva 1513 - Movie collection--树状数组--预留前n个位置
最新推荐文章于 2017-08-18 20:45:38 发布