题意:给你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. java集合学生管理系统

    //student.java package com.sran.www; import java.util.Arrays; import java.util.Scanner; public class ...

  2. Logistic Regression - Formula Deduction

    Sigmoid Function \[ \sigma(z)=\frac{1}{1+e^{(-z)}} \] feature: axial symmetry: \[ \sigma(z)+ \sigma( ...

  3. jquery生成元素注册事件无效,及事件委托的使用

    在页面加载完成之后,我们在页面操作用js生成html代码到页面,动态的添加元素带页面上 但是,这里可能很多人就必须碰到的一个问题就出现了,当你之后动态添加了元素到页面上,发现这个元素的绑定事件无效,如 ...

  4. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  5. ng-show与ng-if区别

    <p>ng-show and ng-if : </p> <div ng-show="isShow">ng-show是否显示</div> ...

  6. c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出

    c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...

  7. Python Day1

    一.安装python windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装到C盘下 3.配置环境变量 右键计算机属性---高级系统设置 ...

  8. Finalize()、Dispose()、SafeHandle、GC

    Finalize https://msdn.microsoft.com/en-us/library/system.object.finalize%28v=vs.110%29.aspx https:// ...

  9. CSS line-height与vertical-align:baseline

    一.当line-height与vertical-align相遇,会发生很多匪夷所思的现象 首先,请看如下代码: <!DOCTYPE html> <html> <head& ...

  10. 【荐】PHP上传文件大小限制大全

    在php.ini里面可以设置最大上传文件大小 解决方法: 第一: 在php.ini里面查看如下行: upload_max_filesize = 8M post_max_size = 10M memor ...