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;
}
#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;
}
相关文章推荐
- UvaLive-5902-Movie collection
- UVALive - 5902 Movie collection
- uvalive 5902(BIT)
- UVALive 5902 Movie collection(树状数组)
- UVALive 5902 Movie collection
- UVa 1513 / UVALive 5902 Movie collection (树状数组)
- UVa 1513 / UVALive 5902 Movie collection (树状数组)
- UVALive - 5902 Movie collection 二叉索引树
- UVALive 5902 Movie collection
- UVALive 6173 Casting
- UVALive 3958 Weird Numbers (负进制数)
- POJ1040 HDU1456 UVA301 UVALive5516 Transportation题解
- UVALive 3713 Astronauts (2-SAT,变形)
- UVALive 7279 Sheldon Numbers(位运算、暴力)
- UVa 10716 - Evil Straw Warts Live
- 指南 第三章 例题5 UVALive 3644 X-Plosives(并查集)
- UVALive 6887 Book Club
- UVALive 4327 Parade(hdu 2490 Parade)
- UVALive 4048 Fund Management(状压DP)
- UVaLive 3902 Network (无根树转有根树,贪心)