您的位置:首页 > 其它

UVALive 5902 Movie collection

2017-01-24 15:51 405 查看
把stack倒着看,把一个元素放到最顶上即把这个元素放到最低部

#include<bits/stdc++.h>
using namespace std;

const int MAXN = 2e5+5;
int c[MAXN], a[MAXN];

int lowbit(int x) { return x&-x; }

void add(int x, int add) {
while(x < MAXN) {
c[x] += add;
x += lowbit(x);
}
}

int sum(int x) {
int ret = 0;
while(x > 0) {
ret += c[x];
x -= lowbit(x);
}
return ret;
}

int main() {
int T;
cin >> T;
while(T--) {
int n, m; scanf("%d%d", &n, &m);
memset(c, 0, sizeof(c));
for(int i = 1; i <= n; i++) add(i, 1), a[i] = n-i+1;
int cur = n;
while(m--) {
int t;
scanf("%d", &t);
printf("%d", sum(MAXN-1)-sum(a[t]));
printf("%c", m?' ':'\n');
add(a[t], -1);
a[t] = ++cur;
add(a[t], 1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: