【BZOJ1895】Pku3580 supermemo Splay
【BZOJ1895】Pku3580 supermemo
Description
Input
Output
Sample Input
1
2
3
4
5
2
ADD 2 4 1
MIN 4 5
Sample Output
HINT
输入、输出以及中间运算结果均不会超过32位整数。
对于30%的数据,n;m 6 1000;
对于100%的数据,n;m 6 100000。
题解:裸的Splay不解释
个人比较懒,对于区间平移操作直接改为翻转3次,结果因为没取模而狂TLE不止,改完后常数大得惊人。。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int tot,root,n,m;
struct node
{
int rev,tag,ch[2],fa,v,siz,sm;
}s[600010];
char str[10];
int rd()
{
int ret=0; char gc=getchar();
while(gc<'0'||gc>'9') gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret;
}
void pushup(int x)
{
s[x].siz=s[s[x].ch[0]].siz+s[s[x].ch[1]].siz+1;
s[x].sm=min(min(s[x].v,s[s[x].ch[0]].sm),s[s[x].ch[1]].sm);
}
void pushdown(int x)
{
if(s[x].ch[0]) s[s[x].ch[0]].v+=s[x].tag,s[s[x].ch[0]].sm+=s[x].tag,s[s[x].ch[0]].tag+=s[x].tag;
if(s[x].ch[1]) s[s[x].ch[1]].v+=s[x].tag,s[s[x].ch[1]].sm+=s[x].tag,s[s[x].ch[1]].tag+=s[x].tag;
s[x].tag=0;
if(s[x].rev)
{
swap(s[x].ch[0],s[x].ch[1]);
if(s[x].ch[0]) s[s[x].ch[0]].rev^=1;
if(s[x].ch[1]) s[s[x].ch[1]].rev^=1;
s[x].rev=0;
}
}
void build(int l,int r,int last)
{
if(l>r) return ;
int mid=l+r>>1;
s[mid].fa=last,s[last].ch[mid>last]=mid;
build(l,mid-1,mid),build(mid+1,r,mid);
pushup(mid);
}
void rotate(int x,int &k)
{
int y=s[x].fa,z=s[y].fa,d=(x==s[y].ch[1]);
if(z) s[z].ch[y==s[z].ch[1]]=x;
if(y==k) k=x;
s[x].fa=z,s[y].fa=x,s[y].ch[d]=s[x].ch[d^1];
if(s[x].ch[d^1]) s[s[x].ch[d^1]].fa=y;
s[x].ch[d^1]=y;
pushup(y),pushup(x);
}
int find(int x,int y)
{
pushdown(x);
if(y<=s[s[x].ch[0]].siz) return find(s[x].ch[0],y);
if(y==s[s[x].ch[0]].siz+1) return x;
return find(s[x].ch[1],y-s[s[x].ch[0]].siz-1);
}
void splay(int x,int &k)
{
while(x!=k)
{
int y=s[x].fa,z=s[y].fa;
if(y!=k)
{
if((x==s[y].ch[0])^(y==s[z].ch[0])) rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
int main()
{
n=rd();
s[0].sm=1<<30;
int i,a,b,c;
for(i=1;i<=n;i++) scanf("%d",&s[i+1].v);
tot=n+2,root=(tot+1)/2;
build(1,root-1,root),build(root+1,n+2,root);
pushup(root);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%s",str);
if(str[0]=='A')
{
a=rd()+1,b=rd()+1,c=rd();
splay(find(root,a-1),root),splay(find(root,b+1),s[root].ch[1]);
s[s[s[root].ch[1]].ch[0]].tag+=c;
s[s[s[root].ch[1]].ch[0]].v+=c;
s[s[s[root].ch[1]].ch[0]].sm+=c;
pushup(s[root].ch[1]),pushup(root);
}
if(str[0]=='R'&&str[3]=='E')
{
a=rd()+1,b=rd()+1;
splay(find(root,a-1),root),splay(find(root,b+1),s[root].ch[1]);
s[s[s[root].ch[1]].ch[0]].rev^=1;
}
if(str[0]=='R'&&str[3]=='O')
{
a=rd()+1,b=rd()+1,c=rd()%(b-a+1);
if(c==0) continue;
splay(find(root,a-1),root),splay(find(root,b+1),s[root].ch[1]);
s[s[s[root].ch[1]].ch[0]].rev^=1;
splay(find(root,a-1),root),splay(find(root,a+c),s[root].ch[1]);
s[s[s[root].ch[1]].ch[0]].rev^=1;
splay(find(root,a+c-1),root),splay(find(root,b+1),s[root].ch[1]);
s[s[s[root].ch[1]].ch[0]].rev^=1;
}
if(str[0]=='I')
{
a=rd()+1,b=rd();
splay(find(root,a),root),splay(find(root,a+1),s[root].ch[1]);
s[s[root].ch[1]].ch[0]=++tot;
s[tot].v=s[tot].sm=b,s[tot].siz=1,s[tot].fa=s[root].ch[1];
pushup(s[root].ch[1]),pushup(root);
}
if(str[0]=='D')
{
a=rd()+1;
splay(find(root,a-1),root);
splay(find(root,a+1),s[root].ch[1]);
s[s[root].ch[1]].ch[0]=0;
pushup(s[root].ch[1]),pushup(root);
}
if(str[0]=='M')
{
a=rd()+1,b=rd()+1;
splay(find(root,a-1),root),splay(find(root,b+1),s[root].ch[1]);
printf("%d\n",s[s[s[root].ch[1]].ch[0]].sm);
}
}
return 0;
}
【BZOJ1895】Pku3580 supermemo Splay的更多相关文章
- 【BZOJ1014】火星人(Splay,哈希)
[BZOJ1014]火星人(Splay,哈希) 题面 BZOJ 题解 要动态维护这个串,一脸的平衡树. 那么用\(Splay\)维护这个哈希值就好了. 每次计算答案的时候二分+Splay计算区间哈希值 ...
- 【NOIP2017】列队(Splay)
[NOIP2017]列队(Splay) 题面 洛谷 题解 其实好简单啊... 对于每一行维护一棵\(Splay\) 对于最后一列维护一棵\(Splay\) \(Splay\)上一个节点表示一段区间 每 ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- 【BZOJ1251】序列终结者 Splay
一道模板题,一直没发现自己的快速读入读不了负数,我竟然能活到现在真是万幸. #include <iostream> #include <cstdio> #define inf ...
- PKU-3580 SuperMemo(Splay模板题)
SuperMemo 题目链接 Your friend, Jackson is invited to a TV show called SuperMemo in which the participan ...
- 【BZOJ-3786】星系探索 Splay + DFS序
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 647 Solved: 212[Submit][Status][Discuss] ...
- 【BZOJ-1014】火星人prefix Splay + 二分 + Hash
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5852 Solved: 1871[Submit] ...
- 【BZOJ-1500】维修数列 Splay
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 11047 Solved: 3460[Submit][Statu ...
- 【BZOJ-2809】dispatching派遣 Splay + 启发式合并
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2334 Solved: 1192[Submi ...
随机推荐
- 如果你写PHP, 请多注意自己是否有良好的习惯
如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. $row['id'] 的速度是$row[id]的7倍. echo 比 print 快,并且使用echo的多重参数 ...
- ubuntu apt-get方式安装与卸载
在ubuntu终端中安装软件: 安装软件 apt-get install softname1 softname2 softname3……卸载软件 apt-get remove softname1 so ...
- windows 添加打印机
控制面板---->硬件和声音---->设备和打印机--->点击添加打印机 最后安驱动(选择通用) OK!
- NGINX + LUA实现复杂的控制
安装lua_nginx_module 模块 lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty Centos和debian的安装就简单了.. 这里说下freebs ...
- 检查是否是移动端(Mobile)、ipad、iphone、微信、QQ等
<script type="text/javascript"> //判断访问终端 var browser={ versions:function(){ var u = ...
- js to json字符串
var last=obj.toJSONString(); //将JSON对象转化为JSON字符 或者 var last=JSON.stringify(obj); //将JSON对象转化为JSON字符
- C# TextBox常用方法总结
我们在使用C# TextBox进行开发操作的时候经常会碰到C# TextBox的使用,那么C# TextBox的使用有没有一些常用的技巧呢?如C# TextBox换行的处理,其实就是一些常用的操作,那 ...
- Caliburn Micro框架快速上手(WP)
一.使用nuget添加起始工程 二.修改App.xaml文件和App.xaml.cs文件 AppBootstrapper介绍: AppBootstrapper根据中文的直译可以 ...
- [uboot]uboot如何引导系统
转自:http://bbs.elecfans.com/jishu_455028_1_1.html 如6410的bootcmd和bootargs默认存在于uboot1.1.6/include/confi ...
- C++中类所占的存储空间
#include <iostream> using namespace std; class A { int m_a; int get() { return m_a; } virtual ...