UVA 1513 树状数组

传送门: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值