ural1439 Battle with You-Know-Who
Battle with You-Know-Who
Memory limit: 64 MB
Input
<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
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 |
5 |
分析:参考了两个做法,第一个比较玄,不太好理解,只能写几个证明貌似是对的;
第二个就是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的更多相关文章
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces 738D. Sea Battle 模拟
D. Sea Battle time limit per test: 1 second memory limit per test :256 megabytes input: standard inp ...
- 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 ...
- 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 ...
- 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 ...
- 需求文档2_The Battle of Polytopia
需求文档 ------------------------------------- 1. 游戏详细分析 The Battle of Polytopia简要介绍 探索型.策略型的对战塔防游戏,回合制. ...
- Codeforces 567D One-Dimensional Battle Ships
传送门 D. One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes ...
- 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 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
随机推荐
- php的memcache和memcached扩展区别【转载】
老生长谈的问题了.我这里就整理一下. memcache的文档在:http://pecl.php.net/package/memcache memcached的文档在:http://pecl.php.n ...
- 关于Select * 与Select 字段名 的问题!
[转]http://blog.csdn.net/tongyu2009/article/details/8252418 1.SELECT * 语句取出表中的所有字段,不论该字段的数据对调用的应用程序是否 ...
- Windows下搭建PHP开发环境【总结】
一.准备工作-下载所需软件 Apache 进入apache服务器官网http://httpd.apache.org/ ,下面是下载的教程:http://jingyan.baidu.com/articl ...
- mongodb状态
基本信息 spock:PRIMARY>db.serverStatus() { "host" :"h6.corp.yongche.org", //主机名 & ...
- isr
Kafaka动态维护了一个同步状态的副本的集合(a set of in-sync replicas),简称ISR
- pkgmgmt: Comparison between different Linux Systems..
found this page.. already done by precedents.. installation: aptitude install apt-get install yum in ...
- PHP缓存技术的多种方法小结
这里所说的数据缓存是指数据库查询PHP缓存机制,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接从缓 ...
- Android抽屉(SlidingDrawer --类似android通知栏下拉效果)
Android抽屉(SlidingDrawer)的实现发 - 红黑联盟http://www.2cto.com/kf/201301/182507.html 可动态布局的Android抽屉之基础http: ...
- CDockablePane使用总结
基于 http://blog.csdn.net/kikaylee/article/details/8936953 CDockablePane的基本布局和用法 新建一个SDI工程,在CMainFrame ...
- POJ 1236 Network of Schools (tarjan算法+缩点)
思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...