1087 - Diablo
Time Limit: 2 second(s) Memory Limit: 64 MB

All of you must have played the game 'Diablo'. It's an exclusive game to play. In this game the main opponent of you is Diablo. If you kill him the game finishes. But as usual, Diablo is smarter than you.

Diablo has a large number of army. Diablo arranges them in a line in any arbitrary order and everyone is given an integer id. Each time Diablo either adds one army in the end or he calls for the kth army (from left) from the line. Then the army gets out and it attacks you.

Since you are a great magician, you can read Diablo's mind. Now you want to find the id of the armies who are about to attack you.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

The first line of each case is a blank line. The next line contains two integers n (0 ≤ n ≤ 105), denoting the number of the initial army and q (1 ≤ q ≤ 50000) representing the number of queries. The next line contains n space separated integers. The ith integer of this line denotes the id of the ith person. Each of these integers will be positive and fits into a 32 bit signed integer. Each of the next q lines will contain a query, of the form:

a p    (add a person at the end of the line whose id is p)

c k    (call the kth person from the line (from left), k is a positive 32 bit signed integer)

Output

For each case of input, print the case number in a line. Then for all the queries 'c k' you have to print the id of the kth person or 'none' if there is none.

Sample Input

Output for Sample Input

2

5 5

6 5 3 2 1

c 1

c 1

a 20

c 4

c 4

2 1

18811 1991

c 1

Case 1:

6

5

20

none

Case 2:

18811

Notes

Dataset is huge, use faster i/o methods.


PROBLEM SETTER: JANE ALAM JAN
思路:线段数求第k大树,单点更新;
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 #include<map>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 int tree[5000000];
12 int ans[160005];
13 int id[160000];
14 int flag[160000];
15 void build(int l,int r,int k,int u);
16 int ask(int l,int r,int k,int m);
17 void up(int k);
18 void in(int x);
19 int main(void)
20 {
21 int i,j,k;
22 scanf("%d",&k);
23 int s;
24 int n,m;
25 int gg=0;
26 for(s=1; s<=k; s++)
27 {
28 scanf("%d %d",&n,&m);
29 for(i=1; i<=n; i++)
30 {
31 scanf("%d",&ans[i]);
32 }
33 memset(flag,0,sizeof(flag));
34 for(i=n+1; i<=160000; i++)
35 {
36 flag[i]=1;
37 }
38 build(1,160000,0,n);
39 gg=n;
40 int cn=n;
41 printf("Case %d:\n",s);
42 while(m--)
43 {
44 char a[2];
45 int x;
46 scanf("%s %d",a,&x);
47 if(a[0]=='c')
48 {
49 if(gg<x)
50 printf("none\n");
51 else
52 {
53 int cc=ask(1,160000,0,x);
54 printf("%d\n",ans[cc]);
55 gg--;
56 }
57 }
58 else
59 {
60 cn++;
61 flag[cn]=0;
62 ans[cn]=x;
63 gg++;
64 in(cn);
65 }
66 }
67 }
68 return 0;
69 }
70 void build(int l,int r,int k,int u)
71 {
72 if(l==r)
73 {
74 if(r<=u)
75 {
76 id[l]=k;
77 tree[k]=1;
78 }
79 else
80 {
81 id[l]=k;
82 tree[k]=0;
83 }
84 return ;
85 }
86 else
87 {
88 build(l,(l+r)/2,2*k+1,u);
89 build((l+r)/2+1,r,2*k+2,u);
90 tree[k]=tree[2*k+1]+tree[2*k+2];
91 }
92 }
93 int ask(int l,int r,int k,int ans)
94 {
95 if(ans==tree[k])
96 {
97 int c=id[r];
98 if(flag[r]==0)
99 { flag[r]=1;
100 up(c);
101 return r;
102 }
103 else
104 {
105 if(tree[2*k+1]<ans)
106 {
107 return ask((l+r)/2+1,r,2*k+2,ans-tree[2*k+1]);
108 }
109 else if(tree[2*k+1]==ans)
110 {
111 return ask(l,(l+r)/2,2*k+1,ans);
112 }
113 }
114 }
115 else if(ans<tree[k])
116 {
117 if(tree[2*k+1]>=ans)
118 {
119 return ask(l,(l+r)/2,2*k+1,ans);
120 }
121 else if(tree[2*k+1]<ans)
122 {
123 return ask((l+r)/2+1,r,2*k+2,ans-tree[2*k+1]);
124 }
125 }
126 }
127 void up(int k)
128 {
129 tree[k]=0;
130 if(k==0)return ;
131 k=(k-1)/2;
132 while(k>=0)
133 {
134 tree[k]=tree[2*k+1]+tree[2*k+2];
135 if(k==0)
136 return ;
137 k=(k-1)/2;
138 }
139 }
140 void in(int x)
141 {
142 int cc=id[x];
143 tree[cc]=1;
144 if(cc==0)return ;
145 cc=(cc-1)/2;
146 while(cc>=0)
147 {
148 tree[cc]=tree[2*cc+1]+tree[2*cc+2];
149 if(cc==0)
150 return ;
151 cc=(cc-1)/2;
152 }
153 }

1087 - Diablo的更多相关文章

  1. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  2. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  3. 【BZOJ 1087】【SCOI 2005】互不侵犯King

    http://www.lydsy.com/JudgeOnline/problem.php?id=1087 很简单的状压,需要预处理,我两个状态可不可以挨着的预处理出错WA了好几次. 这个位运算预处理好 ...

  4. 轮廓线DP POJ3254 && BZOJ 1087

    补了一发轮廓线DP,发现完全没有必要从右往左设置状态,自然一点: 5 6 7 8 9 1 2 3 4 如此设置轮廓线标号,转移的时候直接把当前j位改成0或者1就行了.注意多记录些信息对简化代码是很有帮 ...

  5. dp 动态规划 hdu 1003 1087

    动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...

  6. BZOJ 1087 题解【状压DP】

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3112  Solved: 1816[Submit][ ...

  7. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  9. hdu 1087 动态规划之最长上升子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1087 Online Judge Online Exercise Online Teaching Online C ...

随机推荐

  1. jquery时间轴tab切换效果实现结合swiper实现滑动显示效果

    需求:根据时间轴进行tab页面内容切换(时间轴需要滑动查看并选择) 实现思路: 结合swiper插件实现滑动显示效果 根据transform: translateX进行左侧切换效果的实现(具体实现cs ...

  2. 同一局域网,远程连接别人的Mysql数据库

    数据库:MySQL 工具: Navicat, 电脑A连接电脑B的数据库, 确保两部电脑都是在同一个局域网,都是连着同一个路由器,或者连接同一个WiFi, 如果不确定是否为同一个局域网,可以打开cmd, ...

  3. 振鹏同学正式学习java的第一天!

    一.今日收获 1.最棒的莫过于运行Java的HelloWorld! 2.在同学的帮助下历经坎坷困苦安装完成了Eclipse软件并设置好环境变量. 3.最最最开始了解了Java的前世今生,编程语言发展的 ...

  4. 对于vue项目更新迭代导致上传至服务器后出现Loading chunk {n} failed和Unexpected token <的解决方式

    相信大家对于vue项目的维护与更新中会遇见很多问题,其中有两种情况最为常见. 一种是Loading chunk {n} failed,这种情况出现的原因是vue页面更新上传至服务器后,由于vue默认打 ...

  5. acute

    In Euclidean geometry, an angle is the figure formed by two rays, called the sides of the angle, sha ...

  6. ceph安装部署

    环境准备 测试环境是4台虚拟机,所有机器都是刚刚安装好系统(minimal),只配置完网卡和主机名的centos7.7,每个osd增加一块磁盘,/dev/sdb ceph-admin ---- adm ...

  7. nodejs-npm模块管理器

    JavaScript 标准参考教程(alpha) 草稿二:Node.js npm模块管理器 GitHub TOP npm模块管理器 来自<JavaScript 标准参考教程(alpha)> ...

  8. 【编程思想】【设计模式】【其他模式】graph_search

    Python版 https://github.com/faif/python-patterns/blob/master/other/graph_search.py #!/usr/bin/env pyt ...

  9. Javaj基础知识runtime error

    遇到的java 运行时错误: NullPointerException空指针  ,简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现在创建图片,调用数组这些操作中,比如图片未经初始 ...

  10. git 使用https方式进行 pull、push代码免密

    由于网络原因我用ssh方法拉取代码每次都提示远程服务连接失败,因此我用了https方式去拉去代码. 这种方式拉取代码每次操作都要输入密码,为了解决这个问题做了一下操作: 在命令行输入 git conf ...