poj2892
题解:
答案=后缀-前缀-1
如果被轰了,那么就时0
在一开始加入0,n+1,保证有前缀后缀
代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
char s[];
int n,m,la[N],ppp,pre[N],in[N],x,size[N],c[N][],root,tot,data[N];
void rot(int x)
{
int y=pre[x],k=(c[y][]==x);
size[y]=size[c[y][k]]+size[c[x][k]]+;
size[x]=size[c[x][!k]]+size[y]+;
c[y][!k]=c[x][k];
pre[c[y][!k]]=y;
pre[x]=pre[y];
if(pre[y])c[pre[y]][c[pre[y]][]==y]=x;
c[x][k]=y;pre[y]=x;
}
void splay(int x,int g)
{
for(int y=pre[x];y!=g;rot(x),y=pre[x])
if(pre[y]!=g)rot((x==c[y][])==(y==c[pre[y]][])?y:x);
if(g==)root=x;
}
void insert(int x)
{
int y=root;
while(c[y][x>data[y]]) y=c[y][x>data[y]];
data[++tot]=x;
c[tot][]=c[tot][]=;
pre[tot]=y;
if(y)c[y][x>data[y]]=tot;
splay(tot,);
}
void del(int x)
{
int y=root;
while(data[y]!=x) y=c[y][x>data[y]];
splay(y,);
y=c[root][];
bool b;
if(!y) b=,y=c[root][];else b=;
while(c[y][b]) y=c[y][b];
splay(y,root);
c[y][b]=c[root][b];pre[c[root][b]]=y;pre[y]=;root=y;
size[y]=size[c[y][!b]]+size[c[y][b]];
}
int findpre(int x)
{
int ans;
for(int y=root;y;)
if(data[y]<x)ans=data[y],y=c[y][];
else y=c[y][];
return ans;
}
int findnxt(int x)
{
int ans;
for(int y=root;y;)
if(data[y]<=x)y=c[y][];
else ans=data[y],y=c[y][];
return ans;
}
int read()
{
int x=;char c;
for (;c<''||c>'';c=getchar());
for (;c>=''&&c<='';c=getchar())x=x*+c-;
return x;
}
void write(int x)
{
if (x>=)write(x/);
putchar(x%+);
}
int main()
{
n=read();m=read();
insert();insert(n+);
while (m--)
{
scanf("%s",&s);
if (s[]=='D')
{
x=read();
insert(x);
in[x]=;
la[++ppp]=x;
}
if (s[]=='Q')
{
x=read();
if (in[x])
{
puts("");
continue;
}
int l=findpre(x),r=findnxt(x);
write(r-l-);puts("");
}
if (s[]=='R'&&ppp)in[la[ppp]]=,del(la[ppp--]);
}
}
poj2892的更多相关文章
- [POJ2892]Tunnel Warfare
[POJ2892]Tunnel Warfare 试题描述 During the War of Resistance Against Japan, tunnel warfare was carried ...
- ACM训练计划step 2 [非原创]
(Step2-500题)POJ训练计划+SGU 经过Step1-500题训练,接下来可以开始Step2-500题,包括POJ训练计划的298题和SGU前两章200题.需要1-1年半时间继续提高解决问题 ...
- ACM学习大纲
1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...
- ACM训练大纲
1. 算法总结及推荐题目 1.1 C++ STL • STL容器: set, map, vector, priority_queue, queue, stack, deque, bitset• STL ...
- ACM学习大纲(转)
1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...
- POJ训练计划
POJ训练计划 Step1-500题 UVaOJ+算法竞赛入门经典+挑战编程+USACO 请见:http://acm.sdut.edu.cn/bbs/read.php?tid=5321 一.POJ训练 ...
随机推荐
- soapUi在调用过程中日期参数
中间加个T 2012-11-05T16:38:30 相关描述:
- Delphi XE5 for Android (三)
在VCL下,常用的询问对话框包括 procedure TfrmMainVCL.btnAppMessageboxClick(Sender: TObject); begin if Applicatio ...
- C语言通过枚举网卡,API接口可查看man 7 netdevice--获取接口IP地址
/*代码来自StackOverflow: http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-c ...
- linq——group by
多列排序&&聚合函数 var result = from i in (from uh in db.UserHistories ...
- ClickOnce部署winform
1.完成winform程序. 示例: 2.项目->属性->发布. 有关ftp在iis中的配置,请参考:IIS中添加ftp站点 :url也需在iis中配置.flashfxp中显示ftp成功 ...
- UVa 11212 编辑书稿(dfs+IDA*)
https://vjudge.net/problem/UVA-11212 题意:给出n个自然段组成的文章,将他们排列成1,2...,n.每次只能剪切一段连续的自然段,粘贴时按照顺序粘贴. 思路:状态空 ...
- 关于C++中的友元函数的总结
1.友元函数的简单介绍 1.1为什么要使用友元函数 在实现类之间数据共享时,减少系统开销,提高效率.如果类A中的函数要访问类B中的成员(例如:智能指针类的实现),那么类A中该函数要是类B的友元函数.具 ...
- linux awk命令详解--转载
awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 awk处理过程: 依次对每一行进行处理,然后输出 awk命令形式: awk [-F ...
- Java中的hashcode方法
一.hashCode方法的作用 对于包含容器类型的程序设计语言来说,基本上都会涉及到hashCode.在Java中也一样,hashCode方法的主要作用是为了配合基于散列的集合一起正常运行,这样的散列 ...
- preg_match
$baseUrl = request()->baseUrl(); if (!preg_match('/admin.php/',$baseUrl)) { $this->redirect('/ ...