链接

操作不少,不过都是一些基本的操作,增删,旋转,逆转,询问最小。

注意一点:T<0时 让t=0;

旋转的时候,是顺时针旋转,数据范围在int内。

刚开始旋转转错方向了。。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 200010
#define LL long long
#define INF 0xfffffff
#define key_value ch[ch[root][1]][0]
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
using namespace std;
int a[N];
struct splay_tree
{
int pre[N],size[N];
int ch[N][];
int root,tot;
int lz2[N];
LL s[N],lz1[N],key[N];
// void dfs(int x)
// {
// if(x)
// {
// dfs(ch[x][0]);
// printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d lz1 = %d lz2 = %d min = %d\n",
// x,ch[x][0],ch[x][1],pre[x],size[x],key[x],lz1[x],lz2[x],s[x]);
// dfs(ch[x][1]);
// }
// }
// void debug()
// {
// printf("root:%d\n",root);
// dfs(root);
// }
//以上用于debug*/
void newnode(int &x,int v,int fa)//新建一结点
{
x = ++tot;
ch[x][]=ch[x][] = ;
pre[x] = fa;
lz1[x] = lz2[x] = ;
size[x] = ;
s[x] = v;
key[x] = v;
}
void pushdown(int w)
{
int l = ch[w][],r = ch[w][];
if(lz1[w])
{
lz1[l] += lz1[w];
lz1[r] += lz1[w];
s[l]+=lz1[w];
s[r]+=lz1[w];
key[l]+=lz1[w];
key[r]+=lz1[w];
lz1[w] = ;
}
if(lz2[w])
{
lz2[l]^=lz2[w];
lz2[r]^=lz2[w];
swap(ch[w][],ch[w][]);
lz2[w] = ;
}
}
void pushup(int w)
{
size[w] = size[ch[w][]]+size[ch[w][]]+;
s[w] = key[w];
if(ch[w][])
s[w] = min(s[w],s[ch[w][]]);
if(ch[w][])
s[w] = min(s[w],s[ch[w][]]);
}
void rotate(int r,int kind)
{
int y = pre[r];
pushdown(y);
pushdown(r);
ch[y][!kind] = ch[r][kind];
pre[ch[r][kind]] = y;
if(pre[y])
{
ch[pre[y]][ch[pre[y]][]==y] = r;
}
pre[r] = pre[y];
ch[r][kind] = y;
pre[y] = r;
pushup(y);
pushup(r);
}
void splay(int r,int goal)
{
pushdown(r);
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
rotate(r,ch[pre[r]][]==r);
}
else
{
int y = pre[r];
int kind = (ch[pre[y]][]==y);
if(ch[y][kind]==r)
{
rotate(r,!kind);
rotate(r,kind);
}
else
{
rotate(y,kind);
rotate(r,kind);
}
}
}
pushup(r);
if(goal==) root = r;
}
int get_k(int k)
{
int r = root;
pushdown(r);
while(size[ch[r][]]+!=k)
{
if(size[ch[r][]]>=k)
r = ch[r][];
else
{
k-=(size[ch[r][]]+);
r = ch[r][];
}
pushdown(r);
}
return r;
}
void add(int l,int r,int k)
{
splay(get_k(l),);
splay(get_k(r+),root);
lz1[key_value]+=k;
s[key_value]+=k;
key[key_value]+=k;
pushup(ch[root][]);
pushup(root);
}
LL query(int l,int r)
{
splay(get_k(l),);
splay(get_k(r+),root);
return s[key_value];
}
void reverse(int l,int r)
{
splay(get_k(l),);
splay(get_k(r+),root);
lz2[key_value]^=;
pushup(ch[root][]);
pushup(root);
}
void revolve(int l,int r,int k)
{
splay(get_k(r-k+),);
splay(get_k(r+),root);
int nod = key_value;
key_value = ;
pushup(ch[root][]);
pushup(root); splay(get_k(l),);
splay(get_k(l+),root);
key_value = nod;
pre[nod] = ch[root][];
pushup(ch[root][]);
pushup(root);
}
void insert(int k,int p)
{
splay(get_k(k),);
splay(get_k(k+),root);
newnode(key_value,p,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
void updelete(int k)
{
splay(get_k(k),);
splay(get_k(k+),root);
key_value = ;
pushup(ch[root][]);
pushup(root);
}
void build(int &x,int l,int r,int fa)
{
int m = (l+r)>>;
if(l>r) return ;
newnode(x,a[m],fa);
build(ch[x][],l,m-,x);
build(ch[x][],m+,r,x);
pushup(x);
}
void init(int o)
{
int i;
for(i = ; i <= o ; i++)
scanf("%d",&a[i]);
size[] = ch[][] = ch[][] = key[] = lz1[] = lz2[] = s[] = ;
root = tot = ;
newnode(root,,);
newnode(ch[root][],,root);
build(ch[ch[root][]][],,o,ch[root][]);
size[root] = ;
pushup(ch[root][]);
pushup(root);
}
// void work()
// {
// for(int i = 1; i <= size[root] ;i++)
// cout<<key[get_k(i)]<<" ";
// puts("");
// }
} SP;
int main()
{
int n,q,k,x,y;
char sq[];
while(scanf("%d",&n)!=EOF)
{
SP.init(n);
scanf("%d",&q);
while(q--)
{
scanf("%s",sq);
if(sq[]=='A')
{
scanf("%d%d%d",&x,&y,&k);
SP.add(x,y,k);
}
else if(strcmp(sq,"REVERSE")==)
{
scanf("%d%d",&x,&y);
SP.reverse(x,y);
}
else if(strcmp(sq,"REVOLVE")==)
{
scanf("%d%d%d",&x,&y,&k);
if(k<=) continue;
k = k%(y-x+);
SP.revolve(x,y,k);
}
else if(sq[]=='I')
{
scanf("%d%d",&k,&x);
SP.insert(k+,x);
}
else if(sq[]=='D')
{
scanf("%d",&k);
SP.updelete(k);
}
else if(sq[]=='M')
{
scanf("%d%d",&x,&y);
printf("%d\n",SP.query(x,y));
}
//SP.work();
}
}
return ;
}

po3580SuperMemo(splay)的更多相关文章

  1. 伸展树(Splay树)的简要操作

    伸展树(splay树),是二叉排序树的一种.[两个月之前写过,今天突然想写个博客...] 伸展树和一般的二叉排序树不同的是,在每次执行完插入.查询.删除等操作后,都会自动平衡这棵树.(说是自动,也就是 ...

  2. 【BZOJ3506】排序机械臂(Splay)

    [BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...

  3. 【BZOJ1500】【NOI2005】维修数列(Splay)

    [BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...

  4. 【CF809D】Hitchhiking in the Baltic States(Splay,动态规划)

    [CF809D]Hitchhiking in the Baltic States(Splay,动态规划) 题面 CF 洛谷 题解 朴素\(dp\):设\(f[i][j]\)表示当前考虑到第\(i\)个 ...

  5. 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)

    [BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题

  6. 【BZOJ1056】[HAOI2008]排名系统(Splay)

    [BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...

  7. 伸展树(Splay Tree)进阶 - 从原理到实现

    目录 1 简介 2 基础操作 2.1 旋转 2.2 伸展操作 3 常规操作 3.1 插入操作 3.2 删除操作 3.3 查找操作 3.4 查找某数的排名.查找某排名的数 3.4.1 查找某数的排名 3 ...

  8. 【BZOJ1014】火星人(Splay,哈希)

    [BZOJ1014]火星人(Splay,哈希) 题面 BZOJ 题解 要动态维护这个串,一脸的平衡树. 那么用\(Splay\)维护这个哈希值就好了. 每次计算答案的时候二分+Splay计算区间哈希值 ...

  9. 【BZOJ2329】括号修复(Splay)

    [BZOJ2329]括号修复(Splay) 题面 BZOJ 洛谷 题解 本来想着用线段树来写 但是有一个区间翻转 所以不能用线段树了,就只能用平衡树 然后直接\(Splay\)就好了 注意一下几个标记 ...

随机推荐

  1. html5--3.22 综合实例03

    html5--3.22 综合实例03 学习要点 通过一个综合实例来回顾学过的内容 这一章的内容比较多,不必强求一下子全记住,多做一些练习,用得多了自然就可以记住了 可以自己找一些实例练练手,比如各网站 ...

  2. hdu-5776 sum(同余)

    题目链接: sum Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others) Pro ...

  3. 组合数学中的常见定理&组合数的计算&取模

    组合数的性质: C(n,m)=C(n,n-m); C(n,m)=n!/(m!(n-m)!); 组合数的递推公式: C(n,m)=  C(n-1,m-1)+C(n-1,m); 组合数一般数值较大,题目会 ...

  4. BZOJ_3786_星系探索_splay维护出栈入栈序

    BZOJ_3786_星系探索_splay维护出栈入栈序 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为 ...

  5. Redis GEO 特性在 LBS 中的应用总结

    什么是LBS LBS(Location Based Service),基于位置的服务. Redis和GEO Redis 是最热门的 nosql 数据库之一,它的最大特点就是快.所以在 LBS 这种需要 ...

  6. D - Opponents

    Description Arya has n opponents in the school. Each day he will fight with all opponents who are pr ...

  7. Asset Catalog Help (四)---Adding an iOS App Icon Set or Launch Image Set

    Adding an iOS App Icon Set or Launch Image Set Organize different resolutions of your app icons and ...

  8. c++中虚函数与纯虚函数的区别(转)

    首先:强调一个概念定义一个函数为虚函数,不代表函数为不被实现的函数.定义他为虚函数是为了允许用基类的指针来调用子类的这个函数.定义一个函数为纯虚函数,才代表函数没有被实现.定义纯虚函数是为了实现一个接 ...

  9. PaddlePaddle分布式训练及CTR预估模型应用

    前言:我在github上创建了一个新的repo:PaddleAI, 准备用Paddle做的一系列有趣又实用的案例,所有的案例都会上传数据代码和预训练模型,下载后可以在30s内上手,跑demo出结果,让 ...

  10. 洛谷 - P2551 - 华夏60战斗机 - 简单dp

    https://www.luogu.org/problemnew/show/P2551 首先这道题没有给Hm的最大值,很坑,只能随便开一个100没想到还过了. 观察题目,发现虽然高度可以变化,但是速度 ...