Battle with You-Know-Who

Time limit: 2.0 second
Memory limit: 64 MB
Rooms of the Ministry of Magic are enchanted with a spell which enumerates them automatically. The spell works as follows. The first room created at the Ministry got the number 1. When a new room is created by magic, a number-plate appears at once upon the door. The new number is greater by one than the maximal room number existing at the moment. If a room is not needed anymore, then it is destroyed and all the room numbers that are greater than the number of the destroyed room are lessened by one. Thus, the numeration of the rooms at the Ministry always remains continuous.
Harry Potter found out a list of the numbers of the rooms where Lord Voldemort's Horcruxes are stored (A Horcrux is a magical artifact that provides for the owner's immortality). It seems that now it will be easy for Harry to find the Horcruxes and destroy them. But the task turned out to be more complicated. Because of his mysterious bond with Harry, Voldemort knew at once about Harry's discovery, so he transported himself to the Ministry and started to destroy rooms. This means that numbers of rooms are changing, so when Harry looks at a room's door, he doesn't know which number this door had before. But he knows which numbers were on the doors of the rooms that were destroyed by Voldemort, due to the mentioned bond between them.
Help Harry to defeat Voldemort. You don't have to fight Harry's enemy, but you can help him to determine the true numbers of rooms when he looks at their doors.

Input

The first line contains the number of rooms at the Ministry of Magic N (1 ≤ N ≤ 109) and a numberM (1 ≤ M ≤ 105). Each of the subsequent M lines has the following format:
<letter> <number>
where <letter> is one of the letters 'D' (Destroy) or 'L' (Look at), and <number> is the number on the door of the room which is destroyed or at which Harry looks at the moment. It is guaranteed that not more than 104 rooms will be destroyed during the battle.

Output

The output must contain for each line
L <number>
of the input the true number (which it had before the battle) of the room at which Harry looks. The numbers must be given one in a line.

Sample

input output
20 7
L 5
D 5
L 4
L 5
D 5
L 4
L 5
5
4
6
4
7

分析:参考了两个做法,第一个比较玄,不太好理解,只能写几个证明貌似是对的;

   第二个就是treap树了,参考http://blog.csdn.net/u011686226/article/details/39005875;

   treap树插入询问第K大,询问时二分,询问mid时小于等于mid数有y个,那么mid是第mid-y大的数;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <hash_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={,,-,,,-,,};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,a[maxn],now;
char op[];
int main()
{
int i,j;
a[]=2e9;
scanf("%d%d",&m,&n);
while(n--)
{
scanf("%s%d",op,&k);
int l=,r=now,ans;
while(l<=r)
{
int mid=l+r>>;
if(a[mid]>k)ans=mid,r=mid-;
else l=mid+;
}
if(op[]=='L')printf("%d\n",k+ans);
else
{
for(int i=ans;i<now;i++)a[i]--;
for(int i=now;i>ans;i--)a[i]=a[i-];
a[++now]=2e9;
a[ans]=k;
}
}
//system("Pause");
return ;
}

treap树:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std; const int maxm = ;
int ch[maxm][], r[maxm], val[maxm], sum[maxm], num[maxm], cnt, root; void Node(int &rt, int x){
rt = ++cnt;
ch[rt][] = ch[rt][] = ;
r[rt] = rand();
val[rt] = x;
if(cnt > )
{
sum[rt] = ;
num[rt] = ;
}
else
{
sum[rt] = ;
num[rt] = ;
}
}
void maintain(int rt){ sum[rt] = sum[ch[rt][]]+sum[ch[rt][]]+num[rt];
}
void init()
{
ch[][] = ch[][] = ;
r[] = (1LL<<)-;
val[] = ;
sum[] = ;
cnt = ;
root = ;
Node(root, );
} void rotate(int &rt, int d){
int k = ch[rt][d^]; ch[rt][d^] = ch[k][d]; ch[k][d] = rt;
maintain(rt); maintain(k); rt = k;
} void insert(int &rt, int x){
if(!rt){
Node(rt, x);
return;
}
else{
if(x == val[rt])
num[rt]++;
else
{
int d = x < val[rt] ? : ;
insert(ch[rt][d], x);
if(r[ch[rt][d]] < r[rt]) rotate(rt, d^);
}
}
maintain(rt);
} /*void remove(int &rt, int x){
if(val[rt] == x){
val[rt]--;
if(!val[rt]){
if(!ch[rt][0] && !ch[rt][1])
{
rt = 0;
return;
}
else{
int d = r[ch[rt][0]] > r[ch[rt][1]] ? 1 : 0;
rotate(rt, d);
remove(ch[rt][d], x);
}
else{ }
}
}
else
remove(ch[rt][x>val[rt]], x);
maintain(rt);
}*/ int kth(int rt, int k){
if(rt == )
return ;
if(val[rt] <= k)
return sum[ch[rt][]]+num[rt]+kth(ch[rt][], k);
return kth(ch[rt][], k);
} int main()
{
int n, m;
while(scanf("%d %d", &n, &m) != EOF)
{
init();
while(m--)
{
char s[];
int x;
scanf("%s %d", s, &x);
int l = , r = n, ans;
while(l<=r)
{
int mid=l+r>>,y=kth(root,mid);
if(x<=mid-y)ans=mid,r=mid-;
else l=mid+;
}
if(s[] == 'L')
{
printf("%d\n", ans);
}
else
{
insert(root, ans);
}
}
}
//system("pause");
return ;
}

ural1439 Battle with You-Know-Who的更多相关文章

  1. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  2. Codeforces 738D. Sea Battle 模拟

    D. Sea Battle time limit per test: 1 second memory limit per test :256 megabytes input: standard inp ...

  3. Codeforces #380 div2 D(729D) Sea Battle

    D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

  5. get a new level 25 battle pet in about an hour

    If you have 2 level 25 pets and any level 1 pet, obviously start with him in your lineup. Defeat all ...

  6. 需求文档2_The Battle of Polytopia

    需求文档 ------------------------------------- 1. 游戏详细分析 The Battle of Polytopia简要介绍 探索型.策略型的对战塔防游戏,回合制. ...

  7. Codeforces 567D One-Dimensional Battle Ships

    传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...

  8. Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞

    D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

  9. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

随机推荐

  1. 【转】mysql函数

    MySQL函数 MySQL数据库提供了很多函数包括: 数学函数: 字符串函数: 日期和时间函数: 条件判断函数: 系统信息函数: 加密函数: 格式化函数: 一.数学函数 数学函数主要用于处理数字,包括 ...

  2. 17 个 tar 命令实用示例【转】

    Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...

  3. redis学习一

    一.简介: 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.MongoDB ...

  4. js中对style中的多个属性进行设值

    js中对style中的多个属性进行设值: 看一下案例自然就明白: document.getElementById("my_wz1").style.cssText="bac ...

  5. C# Socket的TCP通讯

    Socket的TCP通讯 一. socket的通讯原理 服务器端的步骤如下. (1)建立服务器端的Socket,开始侦听整个网络中的连接请求. (2)当检测到来自客户端的连接请求时,向客户端发送收到连 ...

  6. MySQ安装

    1.去官网下载安装包 .http://www.mysql.com/downloads/ 2.安装过程中会出现下面的提示:记得保存你的MySQL的初始的默认密码.如果没有注意,那么恢复起来有点麻烦,后续 ...

  7. 闭包 -> map / floatMap / filter / reduce 浅析

    原创: 转载请注明出处 闭包是自包含的函数代码块,可以在代码中被传递和使用 闭包可以捕获和存储其所在上下文中任意常量和变量的引用.这就是所谓的闭合并包裹着这些常量和变量,俗称闭包.Swift 会为您管 ...

  8. required 引发的小小思考

    原创:转载请注明出处 首先,因为遇到问题如下: class MainTabBar: UITabBar { override init(frame: CGRect) { super.init(frame ...

  9. Lucene 简单手记http://www.cnblogs.com/hoojo/archive/2012/09/05/2671678.html

    什么是全文检索与全文检索系统? 全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查 ...

  10. UVALive 2522 Chocolate(概率DP)

    思路:定义DP方程dp[i][j]标记选到第i个巧克力的时候,桌面上还剩下j个巧克力,状态转移有两个方向,dp[i-1][j-1],dp[i-1]lj+1],分别表示桌面上多了一个和消了一个,乘上需要 ...