传送门:UVA 1513
题意
给m个movie, 编号一到m, 1在上, 每次把要看的movie看完后放在最上面, 查询每次要看之前上面有几个movie
题解
每次更新把上个位置去掉, 新的位置可以通过扩展数组实现, 因为r <= 100000干脆数组开两倍, 最上面的movie更新放在后面, 这样每次更新前查询就可以了
AC code:
/*
adrui's submission
Language : C++
Result : Accepted
Favorite : Dragon Balls
Love : yy
Motto : Choose & Quit
Standing in the Hall of Fame
*/
#include<iostream>
#include<cstring>
using namespace std;
const int maxn(100005);
int m, r, c[maxn << 1], cnt[maxn];
#define debug 0
#define lowbit(x) (x & (-x))
#define M(a, b) memset(a, b, sizeof(a))
void add(int x, int v) {
while (x <= maxn << 1) { //这里要是maxn 因为没有指定m, r关系
c[x] += v;
x += lowbit(x);
}
}
int getSum(int x) {
int res = 0;
while (x) {
res += c[x];
x -= lowbit(x);
}
return res;
}
int main() {
#if debug
freopen("in.txt", "r", stdin);
#endif //debug
cin.tie(0);
cin.sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
cin >> m >> r;
M(c, 0);
for (int i = 1; i <= m; ++i) {
cnt[i] = m + 1 - i;
add(cnt[i], 1);
}
int tot = m + 1, a;
while (r--) {
cin >> a;
cout << m - getSum(cnt[a]) << (r ? " " : "\n");//查询
add(cnt[a], -1); //消去上一个位置
cnt[a] = tot++; //更新后位置移到后面
add(cnt[a], 1); //更新新位置
}
}
return 0;
}