LA5902&UVA1513 树状数组



题目大意:

将n个物品从上到下编号,放置 每次询问第i个物品上面有几个物品 并且将第i个物品放到最上面。

大体思路

树状数组的应用

将物品重新编号 n~1  c[i]为树状数组的辅助数组,所求前缀和是当前点到底端的距离 每次将取出的物品重新编号 pos[a]=++tot;


#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<cmath>
#include<climits>
#include<cctype>
#include<cstdlib>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=200010;
int c[maxn],pos[maxn];
inline int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int v)
{
    while(x<maxn) {
        c[x]+=v;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int ret=0;
    while(x>0) {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
int n,m,a;
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
        scanf("%d%d",&n,&m);
        memset(c,0,sizeof c);
        for(int i = 1; i <= n; ++i) {
            pos[i]=n-i+1;
            add(i,1);
        }
        int tot=n;
        for(int i = 0; i < m; ++i) {
            scanf("%d",&a);
            if(i) printf(" ");
            printf("%d",n-sum(pos[a]));
            add(pos[a],-1);
            pos[a]=++tot;
            add(pos[a],1);
        }
        printf("\n");
	}

}


题目大意:

将n个物品从上到下编号,放置 每次询问第i个物品上面有几个物品 并且将第i个物品放到最上面。

大体思路

树状数组的应用

将物品重新编号 n~1  c[i]为树状数组的辅助数组,所求前缀和是当前点到底端的距离 每次将取出的物品重新编号 pos[a]=++tot;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值