fhqtreap

其实fhqtreap只有可持久化之后才用新建节点。。。

reverse和splay一样。

//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = ;
int n, root, cnt, m, last;
int size[N], tag[N], a[], rnd[N];
int child[N][];
ll mn[N], add[N], key[N];
void update(int x)
{
size[x] = size[child[x][]] + size[child[x][]] + ;
mn[x] = min(key[x], min(mn[child[x][]], mn[child[x][]]));
}
void pushdown(int x)
{
if(!x) return;
if(add[x])
{
add[child[x][]] += add[x];
add[child[x][]] += add[x];
mn[child[x][]] += add[x];
mn[child[x][]] += add[x];
key[child[x][]] += add[x];
key[child[x][]] += add[x];
add[x] = ;
}
if(tag[x])
{
tag[child[x][]] ^= ;
tag[child[x][]] ^= ;
swap(child[x][], child[x][]);
tag[x] = ;
}
}
int newnode(int x)
{
++cnt;
size[cnt] = ;
rnd[cnt] = rand();
key[cnt] = x;
mn[cnt] = x;
return cnt;
}
int copy(int x)
{
++cnt;
size[cnt] = size[x];
rnd[cnt] = rnd[x];
tag[cnt] = tag[x];
mn[cnt] = mn[x];
add[cnt] = add[x];
child[cnt][] = child[x][];
child[cnt][] = child[x][];
key[cnt] = key[x];
return cnt;
}
int merge(int x, int y)
{
if(!x) return y;
if(!y) return x;
pushdown(x);
pushdown(y);
if(rnd[x] < rnd[y])
{
child[x][] = merge(child[x][], y);
update(x);
return x;
}
else
{
child[y][] = merge(x, child[y][]);
update(y);
return y;
}
}
PII split(int x, int k)
{
if(!x) return make_pair(, );
PII ret;
pushdown(x);
if(k <= size[child[x][]])
{
ret = split(child[x][], k);
child[x][] = ret.second;
ret.second = x;
update(x);
return ret;
}
else
{
ret = split(child[x][], k - size[child[x][]] - );
child[x][] = ret.first;
ret.first = x;
update(x);
return ret;
}
}
void Add(int l, int r, int d)
{
PII t1 = split(root, l - );
PII t2 = split(t1.second, r - l + );
key[t2.first] += d;
add[t2.first] += d;
mn[t2.first] += d;
root = merge(t1.first, merge(t2.first, t2.second));
}
void reverse(int l, int r)
{
int mid = r - l + ;
PII t1 = split(root, l - );
PII t2 = split(t1.second, r - l + );
tag[t2.first] ^= ;
root = merge(t1.first, merge(t2.first, t2.second));
}
void revolve(int l, int r, int t)
{
if(t < ) t = ;
t %= (r - l + );
PII t1 = split(root, l - );
PII t2 = split(t1.second, r - l + );
PII t3 = split(t2.first, r - l + - t);
root = merge(t1.first, merge(merge(t3.second, t3.first), t2.second));
}
void ins(int x, int p)
{
PII t1 = split(root, x);
int a = newnode(p);
root = merge(t1.first, merge(a, t1.second));
}
void del(int x)
{
PII t1 = split(root, x - );
PII t2 = split(t1.second, );
root = merge(t1.first, t2.second);
}
void que(int l, int r)
{
PII t1 = split(root, l - );
PII t2 = split(t1.second, r - l + );
printf("%lld\n", mn[t2.first]);
root = merge(t1.first, merge(t2.first, t2.second));
}
void build(int &x, int l, int r)
{
if(l > r) return;
int mid = (l + r) >> ;
x = newnode(a[mid]);
build(child[x][], l, mid - );
build(child[x][], mid + , r);
update(x);
}
void print(int x)
{
if(!x) return;
pushdown(x);
print(child[x][]);
printf("%d ", key[x]);
print(child[x][]);
}
int main()
{
// freopen("supermemo8.in", "r", stdin);
// freopen("output.txt", "w", stdout);
srand();
scanf("%d", &n);
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]);
mn[] = 10000000000000ll;
build(root, , n);
scanf("%d", &m);
while(m--)
{
char opt[]; int l, r, d, x, t, p; scanf("%s", opt);
if(opt[] == 'A')
{
scanf("%d%d%d", &l, &r, &d);
Add(l, r, d);
}
if(opt[] == 'V' && opt[] == 'E')
{
scanf("%d%d", &l, &r);
reverse(l, r);
}
if(opt[] == 'O')
{
scanf("%d%d%d", &l, &r, &t);
revolve(l, r, t);
}
if(opt[] == 'I')
{
scanf("%d%d", &x, &p);
ins(x, p);
}
if(opt[] == 'D')
{
scanf("%d", &x);
del(x);
}
if(opt[] == 'M')
{
scanf("%d%d", &l, &r);
que(l, r);
}
// print(root);
// puts("");
}
// fclose(stdin); fclose(stdout);
return ;
}

bzoj1895的更多相关文章

  1. 【BZOJ1895】Pku3580 supermemo Splay

    [BZOJ1895]Pku3580 supermemo Description 给出一个初始序列fA1;A2;:::Ang,要求你编写程序支持如下操作: 1. ADDxyD:给子序列fAx:::Ayg ...

  2. [bzoj1895][Pku3580]supermemo_非旋转Treap

    supermemo bzoj-1895 Pku-3580 题目大意:给定一个n个数的序列,需支持:区间加,区间翻转,区间平移,单点插入,单点删除,查询区间最小值. 注释:$1\le n\le 6.1\ ...

  3. BZOJ1895: Pku3580 supermemo

    1895: Pku3580 supermemo Time Limit: 15 Sec  Memory Limit: 64 MBSubmit: 77  Solved: 47[Submit][Status ...

  4. 非旋treap (BZOJ1895)

    记个板子,还是挺好用的. #include <bits/stdc++.h> using namespace std; ]; int rt,n,m,l,r,x,A,B,C,t; struct ...

随机推荐

  1. Python 时间处理---------笔记

    时区处理&格式化 import pytz from datetime import datetime # 设置时区 timezone = pytz.timezone('Asia/Shangha ...

  2. iOS开发中如何实现同步、异步、GET、POST等请求实操演示!

    1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2.异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然 ...

  3. java实例1

    1.满天星星 import java.awt.*; public class xinxin { public static void main(String[] args) { Frame w = n ...

  4. 微服务的一种开源实现方式——dubbo+zookeeper

    转自: http://blog.csdn.NET/zhdd99/article/details/52263609 微服务架构成了当下的技术热点,实现微服务是要付出很大成本的,但也许是因为微服务的优点太 ...

  5. HDU_1068_Girls and Boys_二分图匹配

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. Django - ORM创建基本类

    DBFirst CodeFirst 创建类 1.根据类自动创建数据库表 在app下的models.py中创建表 创建数据库之前,需要在django下的setting.py模块中的INSTALLED_A ...

  7. Modbus 协议解析

  8. js 保留几位小数位数

    定义和用法 toFixed() 方法可把 Number 四舍五入为指定小数位数的数字.

  9. CDOJ 889 Battle for Silver

    Battle for Silver Time Limit: 2999/999MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others) ...

  10. 清北学堂模拟赛d4t1 a

    分析:大模拟,没什么好说的.我在考场上犯了一个超级低级的错误:while (scanf("%s",s + 1)),导致了死循环,血的教训啊,以后要记住了. /* 1.没有发生改变, ...