题意:有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. Linux centosVMware LAMP php-fpm的pool、php-fpm慢执行日志、open_basedir、php-fpm进程管理

    一.php-fpm的pool vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加 include = etc/php-fpm.d/*.conf mkd ...

  2. [ 剑指Offer ] Week2 学习笔记

    打印从1到最大的n位数 题解:由于未知n的大小,需要考虑大数问题.在这样的情况下,逐位地将字符串转换为数字输出,不会有溢出的可能.使用全排列的方式列出所有数字,省去了需要考虑进位的可能. 初始化数组, ...

  3. Day11 - N - Game HDU - 3389

    题目链接 题意是说有1到n个标号的盒子,选择一个非空的盒子A,B是否空无所谓,满足(A+B)%2=1,(A+B)%3=0,A>B 解上面的同余方程组,最小解为3,循环为2*3=6,那我们可以把前 ...

  4. UniGUI之Login窗口(10)

    在UniGUI的CHM帮助里读到的. 一定要新建一个其他空白的工程,然后再添加LoginForm LoginForm 是另一种特殊的表单类型, 仅用于登录目的. 此操作将创建一个与常规窗体外观相同的空 ...

  5. Python之第一次自夸

    有一个好玩的代码 import win32com.client g = win32com.client.Dispatch("SAPI.SPVOICE") g.Speak(" ...

  6. PaperReading20200221

    CanChen ggchen@mail.ustc.edu.cn Busy... Human-level concept learning through probabilistic program i ...

  7. Java 虚拟机程序监控工具软件

    jdk自带的查看工具(jdk安装目录/bin) .Java VisualVM .jconsole

  8. 一、iBatis进行分页查询

    1.ibatis理解: iBatis属于半自动化的ORM框架,我们需要编写SQL语句,由iBatis进行数据库访问,返回结果.而iBatis可以为我们做的更多,比如对查询参数集合.结果.分页查询.事务 ...

  9. canvas象棋 画图

    今天写了一个canvas画图的象棋 .js基础不行,只画了个图,以后补充... <!DOCTYPE html> <html lang="en"> <h ...

  10. Vue - 路由守卫使用

    import Vue from 'vue' import VueRouter from 'vue-router' import Home from '../views/Home.vue' Vue.us ...