【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 ...
随机推荐
- Scala, Groovy, Clojure, Jython, JRuby and Java ----我们的工作语言
在曾经的一封邮件中,我指出在众多改变中,最明显的一个就是:在java领地上的JVM上使用其它流行的语言的发展变得越来越快.一些老的和新的创建的基于JVM的语言---JRuby 和 Jython ,Ja ...
- C#中将图片转化成base64字符串
厂址:http://www.cnblogs.com/yunfeifei/p/4165351.html 1.在C#中将图片转化成base64字符串: using System; using System ...
- Linux下使用DD命令测试磁盘读写速度
dd是Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换,所以可以用来测试硬盘的读写能力~ 几种常见的DD命令,先看一下区别~ dd bs=6 ...
- mysql 导入导出数据库、数据表的方法
mysql 导入导出数据库.数据表的方法. Linux操作系统中,均在控制台下操作.1,导入数据库:前提:数据库和数据表要存在(已经被创建)(1)将数据表 test_user.sql 导入到test ...
- Atitit.各种 数据类型 ( 树形结构,表形数据 ) 的结构与存储数据库 attilax 总结
Atitit.各种 数据类型 ( 树形结构,表形数据 ) 的结构与存储数据库 attilax 总结 1. 数据结构( 树形结构,表形数据,对象结构 ) 1 2. 编程语言中对应的数据结构 jav ...
- C# FTP操作类可用
public class FtpClient { #region 构造函数 /// <summary> /// 创建FTP工具 /// <para> /// 默认不使用SSL, ...
- 严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.Invoc
错误提示:严重: Dispatcher initialization failed java.lang.RuntimeException: java.lang.reflect.InvocationTa ...
- Storyboard 全解析
XCode 4.3.2 新功能 - Storyboard 最近开始比较有空在玩 XCode 4.3.2,赫然发现它多了个 Storyboard 的东东. Storyboard 这个东西一般来说是在做创 ...
- CentOS6.4下Docker应用环境的部署配置
http://blog.chinaunix.net/uid-619485-id-4973941.html *********************************************** ...
- Phoenix的数据类型和操作符、函数
其实官方文档已经有这些东西了,如下: http://phoenix.apache.org/language/functions.html http://phoenix.apache.org/langu ...