UVA 1513 - Movie collection

option=com_onlinejudge&Itemid=8&page=show_problem&category=502&problem=4259&mosmsg=Submission+received+with+ID+13965699" target="_blank" style="">题目链接

题意:有一些光盘,一開始是n-1叠上去的(1最顶),如今每次抽出一张,要问这一张上面有多少张,然后把抽出来这张放到最顶,输出每次询问

思路:树状数组,数组维护每一个位置以下有多少张光碟,这样每次拿出一张,就在对应位置-1就能够了,然后在最顶位置上多一个位置+1

代码:

#include <cstdio>
#include <cstring> #define lowbit(x) (x&(-x))
const int N = 200005;
int t, n, m, arr[N], bit[N]; void add(int x, int v) {
while (x < N) {
bit[x] += v;
x += lowbit(x);
}
} int query(int x) {
int ans = 0;
while (x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
memset(bit, 0, sizeof(bit));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
arr[i] = n + 1 - i;
add(i, 1);
}
int v;
for (int i = 1; i <= m; i++) {
scanf("%d", &v);
int under = query(arr[v]);
printf("%d%c", n - under, i == m ? '\n' : ' ');
add(arr[v], -1);
arr[v] = n + i;
add(n + i, 1);
}
}
return 0;
}

UVA 1513 - Movie collection(树状数组)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 移动开发中的Scheme跳转说明——Allowing OtherApps to Start Your Activity

    Allowing OtherApps to Start Your Activity 为了开发更多人使用的App,我们总希望我们的App能够提供一种接口被其他App调用.如我们常见的 大众点评  与  ...

  2. mysql dump 参数

    mysql dump 参数: -R, --routines Dump stored routines (functions and procedures). 备份 函数和存储过程: -E, --eve ...

  3. UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)

    继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...

  4. C++智能指针--weak_ptr

    weak_ptr是对对象的一种弱引用,它不会添加对象的引用计数.weak_ptr和shared_ptr之间能够相互转换.shared_ptr能够直接赋值给week_ptr,week_ptr可通过调用l ...

  5. pwd的实现

    #include <string.h> #include <stdlib.h> #include <dirent.h> #include <sys/types ...

  6. hdu 4039 The Social Network

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4039 题目分类:字符串+bfs 题意:给一个人际关系图,根据关系图,给一个人推荐一个人认识 题目分析: ...

  7. Android应用中Back键的监听及处理

    MainActivity如下: package cn.testnbackpressed; import android.os.Bundle; import android.view.KeyEvent; ...

  8. 浅谈Jquery的使用上篇

    一. 1.Jquery是什么?有什么特性? jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取.HTML 元素操作. CSS 操作 .HTML 事 ...

  9. A Game of Thrones(17) - Bran

    It seemed as though he had been falling for years. Fly, a voice whispered in the darkness, but Bran ...

  10. Fluentd: Open Source Log Management

    Fluentd: Open Source Log Management "Fluentd" is an open-source tool to collect events and ...