题意:有n个影碟,标号为1~n,位置为0~n-1,每次取出一个影碟看完后,将其放在最前面(标号为0处),问每个影碟取出前,其位置之前有多少个影碟。

分析:

1、数组大小开为100000*2,后100000个位置放初始的n个影碟,每次看完一个影碟后,依次向前放在前100000个位置中。

2、将放置影碟处标记为1---add(x, 1),当影碟取出时,再add(x,-1)。

3、通过sum(x)-1可知当前位置之前有多少个影碟。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1000;
const double pi = acos(-1.0);
const int MAXN = 200000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int n, m;
int pos[MAXN];
int sum(int x){
int ans = 0;
for(int i = x; i >= 1; i -= lowbit(i)){
ans += a[i];
}
return ans;
}
void add(int x, int value){
for(int i = x; i < MAXN; i += lowbit(i)){
a[i] += value;
}
}
vector<int> ans;
int main(){
int T;
scanf("%d", &T);
while(T--){
ans.clear();
memset(a, 0, sizeof a);
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i){
pos[i] = 100000 + i;
add(pos[i], 1);
}
int cnt = 100000;
while(m--){
int k;
scanf("%d", &k);
ans.push_back(sum(pos[k]) - 1);
add(pos[k], -1);
pos[k] = cnt--;
add(pos[k], 1);
}
int len = ans.size();
for(int i = 0; i < len; ++i){
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}

  

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. UVA - 1471 Defense Lines 树状数组/二分

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

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

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

  9. UVA 1513 Movie collection (树状数组+反向存储)

    题意:给你n盘歌碟按照(1....n)从上到下放,接着m个询问,每一次拿出x碟,输出x上方有多少碟并将此碟放到开头 直接想其实就是一线段的区间更新,单点求值,但是根据题意我们可以这样想 首先我们倒着存 ...

  10. UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

随机推荐

  1. How to add VTL tapes on DD6300 & label them into "Oracle_VTL" pool

    Dear all , This is  liulei ,   I was back so , How to add VTL tapes on DD6300 & label them into ...

  2. 「国家集训队」Crash的数字表格

    题目描述 求(对 \(20101009\) 取模,\(n,m\le10^7\) ) \[\sum_{i=1}^n\sum_{j=1}^m\operatorname{lcm}(i,j)\] 大体思路 推 ...

  3. Python 基础之集合相关操作与函数和字典相关函数

    一:集合相关操作与相关函数 1.集合相关操作(交叉并补) (1)intersection() 交集 set1 = {"one","two","thre ...

  4. esxi虚拟机无法开机,提示“没有更多空间可供虚拟磁盘server-000001.vmdk使用。也许通过释放相关卷上的磁盘空间并单击 重试 继续此会话,单击 取消 可终止此会话”

    背景:esxi安装在32G的U盘上,硬盘总大小:1.64T,虚拟机A占用:600GB,虚拟机B占用:900GB.所以还有剩余不到200G左右. 原因是宿主机硬盘空间不足.通过图中可以看出空间已经只剩2 ...

  5. 微信小程序 画布arc截取圆形图片

    画布提供了一种可以创建圆的方法 arc(x, y, r, s, e, counterclockwise) x,y:圆心 r:圆的半径 s:起始弧度 (0) e:终止弧度 (1.5 * Math.PI) ...

  6. 【AOP】在Aspect中无法获取实现类方法的注解

    问题 MethodSignature methodSignature = (MethodSignature) (joinPoint.getSignature()); Method method = m ...

  7. 使用anaconda 3安装tensorflow 1.15.0 (win10环境)

    0.写在前面 ​ 之前其实安装过一次tensorflow,但是由于电脑中毒,重装了系统,把所有的环境全部删除了.之前在博客里转发了一篇别人在win10安装tensorflow的教程,但是版本比较旧了, ...

  8. 「CF1C Ancient Berland Circus」

    CF第一场比赛的最后一题居然是计算几何. 这道题的考点也是比较多,所以来写一篇题解. 前置芝士 平面直角坐标系中两点距离公式:\(l=\sqrt{(X_1-X_2)^2+(Y_1-Y_2)^2}\) ...

  9. IAR 设置问题

    IAR 设置问题 1.worksplace 的设置问题:edit configuration 这样,每个工程都可以有一个workplace,方便切换. 2.文件夹包含问题 3.包含.dat文件问题 . ...

  10. redis-String字符串

    存储类型:可以用来存储字符串.整数.浮点数. 常用命令: 1. SET key value [EX seconds] [PX milliseconds] [NX|XX] 1.1 EX seconds ...