Queue Sequence HDU - 4441
码力不行啊。。。
错误记录:
171行后面对find2的使用错误,原来写的是p=find2(rt,p1),然后再加上一句能过样例但很假的特判
事实上,现在是要寻找最大的j,使得d2[1..j-1]=p1-1
而find2返回的是最大的j,使得d2[1..j]<=p1,因此要这么用
#pragma GCC optimize("Ofast")
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
namespace S
{
const int N=;
int rand1()
{
static int x=;
return x=(48271LL*x+)%;
}
struct Node
{
Node *ch[],*fa;
int sz,d,r;
int d1,d2;ll sum;
}nds[N];
int gsz(Node *o) {return o?o->sz:;}
int gd1(Node *o) {return o?o->d1:;}
int gd2(Node *o) {return o?o->d2:;}
ll gsum(Node *o) {return o?o->sum:;}
void upd(Node *o)
{
o->sz=gsz(o->ch[])+gsz(o->ch[])+;
o->d1=gd1(o->ch[])+gd1(o->ch[])+(o->d>);
o->d2=gd2(o->ch[])+gd2(o->ch[])+(o->d<);
o->sum=ll(gsum(o->ch[]))+gsum(o->ch[])+o->d;
}
int mem;
Node *getnode()
{
Node *t=nds+(mem++);
t->ch[]=t->ch[]=t->fa=;
t->d1=t->d2=t->d=;t->sz=;t->r=rand1();
return t;
}
Node *merge(Node *a,Node *b)
{
if(!a) return b;
if(!b) return a;
if(a->r<b->r)
{
a->ch[]=merge(a->ch[],b);upd(a);
if(a->ch[]) a->ch[]->fa=a;
return a;
}
else
{
b->ch[]=merge(a,b->ch[]);upd(b);
if(b->ch[]) b->ch[]->fa=b;
return b;
}
}
typedef pair<Node*,Node*> pnn;
pnn split(Node *a,int k)
{
if(!a) return pnn(,);
if(!k) return pnn(,a);
int ls=gsz(a->ch[]);pnn y;
if(k<=ls)
{
//if(a->ch[0]) a->ch[0]->fa=0;
y=split(a->ch[],k);
a->ch[]=y.se;
if(a->ch[]) a->ch[]->fa=a;
upd(a);y.se=a;
}
else
{
//if(a->ch[1]) a->ch[1]->fa=0;
y=split(a->ch[],k-ls-);
a->ch[]=y.fi;
if(a->ch[]) a->ch[]->fa=a;
upd(a);y.fi=a;
}
return y;
}
void insert(Node *&a,int p,int x)
{
pnn t1=split(a,p-);Node *t=getnode();
t->d=x;upd(t);
a=merge(merge(t1.fi,t),t1.se);
}
void remove(Node *&a,int p)
{
pnn t1=split(a,p-),t2=split(t1.se,);
a=merge(t1.fi,t2.se);
}
int find1(Node *&a,int k)
{
pnn t1=split(a,k);int ans=t1.fi->d1;
a=merge(t1.fi,t1.se);
return ans;
}
int find2(Node *a,int k)
{
if(!a) return ;
int ls=gd2(a->ch[]);
if(k<ls) return find2(a->ch[],k);
else if(k<ls+(a->d<)) return gsz(a->ch[]);
else return gsz(a->ch[])++find2(a->ch[],k-ls-(a->d<));
}
int find3(Node *&a,int k)
{
pnn t1=split(a,k);int ans=t1.fi->d2;
a=merge(t1.fi,t1.se);
return ans;
}
ll qsum(Node *&a,int l,int r)
{if(l>r) swap(l,r);
pnn t1=split(a,l-),t2=split(t1.se,r-l+);
ll ans=t2.fi->sum;
a=merge(t1.fi,merge(t2.fi,t2.se));
return ans;
}
Node *rt;
Node *find(Node *a,int k)
{
if(!a) return ;
int ls=gsz(a->ch[]);
if(k<=ls) return find(a->ch[],k);
else if(k<=ls+) return a;
else return find(a->ch[],k-ls-);
}
bool gson(Node *a) {return a==a->fa->ch[];}
int findx(Node *a)
{
int ans=gsz(a->ch[])+;
for(;a!=rt;a=a->fa)
if(gson(a))
ans+=gsz(a->fa->ch[])+;
return ans;
}
}
set<int> s;
char tmp[];
int n;
using S::rt;
S::pnn ttt[];
int main()
{
int p,x,i,p1,TT=;
while(scanf("%d",&n)==)
{
S::mem=;
s.clear();rt=;
for(i=;i<=n;i++) s.insert(i);
++TT;
printf("Case #%d:\n",TT);
for(i=;i<=n;i++)
{
scanf("%s",tmp);
if(tmp[]=='i')
{
scanf("%d",&p);p++;
x=*s.begin();s.erase(x);
S::insert(rt,p,x);ttt[x].fi=S::find(rt,p);
//printf("a%d %d\n",p,x);
//printf("%d\n",S::findx(ttt[x].fi));
p1=S::find1(rt,p);p=S::find2(rt,p1-)+;
S::insert(rt,p,-x);ttt[x].se=S::find(rt,p);
//printf("a%d %d\n",p,-x);
}
else if(tmp[]=='r')
{
scanf("%d",&x);
//printf("b%d ",S::findx(ttt[x].fi));
S::remove(rt,S::findx(ttt[x].fi));
//printf("%d\n",S::findx(ttt[x].se));
S::remove(rt,S::findx(ttt[x].se));
s.insert(x);
}
else
{
scanf("%d",&x);
printf("%lld\n",S::qsum(rt,S::findx(ttt[x].fi),S::findx(ttt[x].se)));
}
}
}
return ;
}
Queue Sequence HDU - 4441的更多相关文章
- HDU 4441 Queue Sequence(优先队列+Treap树)(2012 Asia Tianjin Regional Contest)
Problem Description There's a queue obeying the first in first out rule. Each time you can either pu ...
- Q - Play With Sequence HDU - 3971 线段树 重新排序建树
Q - Play With Sequence HDU - 3971 这个题目是一个线段树,比较特别的线段树,就是c询问一定次数之后重新排序建树来优化减低复杂度. 第一次碰到这种题目有点迷. 这个题目写 ...
- HDU 4441 Queue Sequence(splay)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:一个数列,三种操作:(1)插入:找到没在当前数列中的最小的正整数i,将其插在位置p之后,并 ...
- HDU 4441 Queue Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=4441 题意:对于一个序列,每次有三种操作 insert pos 表示在pos插入一个数,这个数是最小的正数 ...
- DNA sequence HDU - 1560
DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- (KMP 模板)Number Sequence -- Hdu -- 1711
http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/Other ...
- (模拟)Arithmetic Sequence -- HDU -- 5400
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5400 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- Number Sequence HDU - 5014
There is a special number sequence which has n+1 integers. For each number in sequence, we have two ...
- AC日记——Number Sequence hdu 1711
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- android keyEvent
http://developer.android.com/reference/android/view/KeyEvent.html
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- Xamarin Android 记事本(三)删改
这篇我就不做太多的说明了,数据操作之前也都举过例子了,这里就直接贴出删除和修改的代码. public override bool OnOptionsItemSelected(IMenuItem ite ...
- kill 挂起 Apache Web Server
[root@hadoop1 ~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8 ...
- mysql 发生系统错误1067
一般是由配置文件错误语法不正确引起的,如my.ini本人在mysql mysql-5.6.29-winx64 配置过程中遇到“发生系统错误1067”主要由于下面两个目录写的格式不正确引起的正确写法如下 ...
- regmap使用介绍【转】
本文转载自:http://blog.csdn.net/hellowxwworld/article/details/10737569 内核3.1引入一套新的API regmap,目的是提取出关于I2C ...
- Centos6.8防火墙设置
# 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service iptables start ...
- Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.st.mapper.UserMapper.userBaseMap
mybatis出现此异常,可能是因为 ***Mapper.xml 文件中存在重名对象,一不小心重复启动了mybatis的逆向工程. 以为会覆盖掉以前生成的,没想到是新生成的和之前生成的重复了 解决:把 ...
- Digit(湘潭大学比赛)
题目链接: 点击打开链接 中文问题目就不解释了. 思路,找到这个数对应的的数字是多少,然后对这个数取对应的位置. 步骤:先打表打出一位数字对应字符串的长度,两位数的,到8,9就差不多了. 先确定给定的 ...
- Eclipse中文乱码问题
最近用TortoiseSVN从androiddex上下载了一个android工程,导入之后发现里面的中文都是乱码,于是在Eclipse里通过Windows-Preferences-Workspace修 ...