题意:给你n盘歌碟按照(1....n)从上到下放,接着m个询问,每一次拿出x碟,输出x上方有多少碟并将此碟放到开头

直接想其实就是一线段的区间更新,单点求值,但是根据题意我们可以这样想

首先我们倒着存  n--1,接着每次询问时把放碟子放到最后,这样我们要开一个映射数组映射每个碟子在哪个位置

其中我们需要使用树状数组维护每个绝对位置是否有碟子(有些碟子已经放到了后面了),再使用区间求和就好了

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;//开成两倍,因为要向后添加
int mp[Max],bit[Max],tot,tol;//映射位置 被映射的树状数组 总大小
int lowbit(int x)
{
return x&(-x);
}
void Add(int x,int y)
{
while(x<=tol)
{
bit[x]+=y;
x+=lowbit(x);
}
return ;
}
int Sum(int x)
{
int sum=;
while(x)
{
sum+=bit[x];
x-=lowbit(x);
}
return sum;
}
void Init(int n,int m)
{
memset(bit,,sizeof(bit));
tot=n;
tol=n+m;
for(int i=; i<=n; ++i)//倒序存储
{
mp[i]=n-i+;
Add(mp[i],);
}
return;
}
int Solve(int n,int m)
{
ll ans=;
int pos=mp[m];
//printf("\npos=%d\n",pos);
ans=n-Sum(pos);//求后面的
tot++;
Add(pos,-);//更新
Add(tot,);
mp[m]=tot;//映射到开头
return ans;
}
int main()
{
int t,n,m,num;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
Init(n,m);
for(int i=;i<m;++i)
{
scanf("%d",&num);
printf("%d%c",Solve(n,num),i==m-?'\n':' ');
}
}
return ;
}

UVA 1513 Movie collection (树状数组+反向存储)的更多相关文章

  1. UVA 1513 - Movie collection(树状数组)

    UVA 1513 - Movie collection option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...

  2. UVA 11423 - Cache Simulator (树状数组)

    UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...

  3. Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)

    Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...

  4. UVA 1428 - Ping pong(树状数组)

    UVA 1428 - Ping pong 题目链接 题意:给定一些人,从左到右,每一个人有一个技能值,如今要举办比赛,必须满足位置从左往右3个人.而且技能值从小到大或从大到小,问有几种举办形式 思路: ...

  5. uva 12356 Army Buddies 树状数组解法 树状数组求加和恰为k的最小项号 难度:1

    Nlogonia is fighting a ruthless war against the neighboring country of Cubiconia. The Chief General ...

  6. UVA 11525 Permutation (树状数组+YY)

    题意:给你k个数Si,然后给你一个等式   H= ∑  Si ∗ (K − i)!  (i=(1->k)且0 ≤ Si ≤ K − i). 叫你求出第H个全排列 其实这是一个康托展开:X=a[n ...

  7. HDU 4000 Fruit Ninja (树状数组+反向思维)

    题意:给你一串数且每个数都不同,问你(x,y,z)出现 x<z<y 的总次数 首先我们直接想的话不能使用O(n*log2 n)解决,所以可以正难则反 可以求得x<(y,z)的值,减去 ...

  8. UVA - 1471 Defense Lines 树状数组/二分

                                  Defense Lines After the last war devastated your country, you - as the ...

  9. LA 5902 - Movie collection 树状数组(Fenwick树)

    看题传送门 题目大意:XXX喜欢看电影,他有好多好多的影碟,每个影碟都有个独立的编号.开始是从下往上影碟的顺序是n~1,他每次拿出影碟的时候,你需要输出压在该影碟上的有几个.(拿出后其他影碟顺序不变) ...

随机推荐

  1. shell中$0,$?,$!等的特殊用法

    变量说明: $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值)$-使用Set命令设定的Flag一览$*所有参数列表 ...

  2. Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)

    传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...

  3. jQuery下拉菜单插件Tendina.

    插件效果: 下载地址和文档: https://github.com/iprignano/tendina

  4. UICollectionView

    #import "ViewController.h" @interfaceViewController ()<UICollectionViewDataSource,UICol ...

  5. flask-whooshalchemy需要注意的一点

    在学习mega—tutorial时全文搜索模块遇到了问题,那就是使用全文搜索查询出来的数据为空的列表,输出了sql语句后发现where后没有条件,困扰了许久,后来才发现是自己不细心,在进行全文索引时应 ...

  6. C# 自定义Section

    一.在App.config中自定义Section,这个使用了SectionGroup <?xml version="1.0" encoding="utf-8&quo ...

  7. [NHibernate]集合类(Collections)映射

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...

  8. 如何挂载阿里云Linux服务器的“数据盘”(新购买)

    详细操作参考: http://jingyan.baidu.com/article/90808022d2e9a3fd91c80fe9.html 用到的命令行汇总: 1.查看磁盘: fdisk -l 2. ...

  9. php gettext 多语言翻译

    1.在window与linux下的多语言切换有些区别,主要putenv的设置区别. 参考链接:http://www.cnblogs.com/sink_cup/archive/2013/11/20/ub ...

  10. 获取url传参

    function urlparameterforkey(name) { //读取html 数据 ); //待处理的字符串 var patt = new RegExp(name); //要查找的字符串 ...