【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 ...
随机推荐
- 3dmax fx shader, vertex color
美术那边需要一个能在3dmax里用的支持diffuse纹理和顶点色的additive shader(不带光照). 以前没搞过这个,于是从3dmax自带的vertexcolor.fx,DiffuseBu ...
- atitit.手动配置列表文件的选择and 数据的层次结构 attilax总结最佳实践--yaml
atitit.手动配置列表文件的选择and 数据的层次结构 attilax总结最佳实践--yaml 1. yaml是个好的选择.. 1 2. 数据的层次结构--结构:hash,list,和block ...
- 深入浅出Spring(一)Spring概述
现在很多的企业级项目中基本上都会用到了Spring框架,那么为什么会出现Spring,Spring是什么?这次的博文我主要为大家简单介绍一下Spring. Java EE优缺点 我们都知道在2003年 ...
- linux命令之fuser
Usage: fuser [ -a | -s | -c ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME... [ - ] [ -n SPACE ] [ -SIGN ...
- python3+spark2.1+kafka0.8+sparkStreaming
python代码: import time from pyspark import SparkContext from pyspark.streaming import StreamingContex ...
- 将多个文件夹内的txt合并
import os import re def text_create(name): """ 创建txt文件夹 """ desktop_pa ...
- 广义线性模型 - Andrew Ng机器学习公开课笔记1.6
在分类问题中我们如果: 他们都是广义线性模型中的一个样例,在理解广义线性模型之前须要先理解指数分布族. 指数分布族(The Exponential Family) 假设一个分布能够用例如以下公式表达, ...
- Unix系统编程()信号:概念和概述
这篇将一口气学完信号的基本概念,但是有很多的细节,所以篇幅较长,请做好心理准备. (他大爷的,一口气没有学完,太懒了) 有以下主题: 各种不同信号及其用途 内核可能为进程产生信号的环境,以及某一进程向 ...
- Linux下HTTP Server
想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...
- maven编译插件版本配置案例
<!-- Build Settings 构建设置 --> <build> <finalName>${project.artifactId}</finalNam ...