【POJ 3580】SuperMemo Splay
题意
给定$n$个数,$m$个询问,每次在$[L,R]$区间加上一个数,或者反转一个区间$[L,R]$,或者循环右移区间$[L,R]$共$T$次,或者在第$x$个数后插入一个数$p$,或者删除第$x$个数,或者查询区间$L,R$的最小值
Splay模板题
对于区间操作,利用splay提取区间后打懒标记更新,单点操作看作是对$[L,L+1]$区间的操作
时间复杂度$O(m \log n)$
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define inf 0x7f7f7f7f
using namespace std;
const int N = 800005;
int n, m, a[N];
int ch[N][2], fa[N], key[N], rev[N], sz[N], Min[N], add[N];
int root, tot;
inline int get(int x) {return ch[fa[x]][1] == x;} // left is 0, right is 1
inline void pushup(int x) {
sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
Min[x] = key[x];
if(ch[x][0]) Min[x] = min(Min[x], Min[ch[x][0]]);
if(ch[x][1]) Min[x] = min(Min[x], Min[ch[x][1]]);
}
inline void update_add(int x,int add_) {key[x] += add_; add[x] += add_; Min[x] += add_;}
inline void pushdown(int x) {
if(!x) return;
if(add[x]) {
if(ch[x][0]) {update_add(ch[x][0], add[x]);}
if(ch[x][1]) {update_add(ch[x][1], add[x]);}
add[x] = 0;
}
if(rev[x]) {
rev[x] = 0;
swap(ch[x][0], ch[x][1]);
rev[ch[x][0]] ^= 1; rev[ch[x][1]] ^= 1;
}
}
inline void rotate(int x) {
int f = fa[x], ff = fa[f], which = get(x);
pushdown(f); pushdown(x);
ch[f][which] = ch[x][which ^ 1];
fa[ch[f][which]] = f;
ch[x][which ^ 1] = f;
fa[f] = x; fa[x] = ff;
if(ff) ch[ff][ch[ff][1] == f] = x;
pushup(f); pushup(x);
}
inline void splay(int x, int target) {
while(fa[x] != target) {
if(fa[fa[x]] != target) {
rotate((get(x) == get(fa[x])) ? fa[x] : x);
}
rotate(x);
}
if(!target) root = x;
}
inline int newnode(int v, int f) {
int x = ++tot;
ch[x][0] = ch[x][1] = 0; fa[x] = f;
key[x] = Min[x] = v; sz[x] = 1; add[x] = rev[x] = 0;
return x;
}
int build(int l, int r, int f) {
if(l > r) return 0;
int mid = (l + r) / 2;
int x = newnode(a[mid], f);
ch[x][0] = build(l, mid - 1, x);
ch[x][1] = build(mid + 1, r, x);
pushup(x);
return x;
}
inline void init() {tot = 0; root = build(0, n + 1, 0);}
inline int get_kth(int x, int k) {
if(!x) return 0;
while(1) {
pushdown(x);
if(k == sz[ch[x][0]] + 1) break;
if(k > sz[ch[x][0]] + 1) {
k -= sz[ch[x][0]] + 1; x = ch[x][1];
}else x = ch[x][0];
}
return x;
}
char opt[20]; int L, R, d;
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
a[0] = -inf; a[n + 1] = inf;
init();
scanf("%d", &m);
for(int i = 1; i <= m; ++i) {
scanf("%s", opt);
if(!strcmp(opt, "ADD")) {
scanf("%d%d%d", &L, &R, &d);
splay(get_kth(root, L), 0); splay(get_kth(root, R + 2), root);
update_add(ch[ch[root][1]][0], d);
pushup(ch[root][1]); pushup(root);
}else if(!strcmp(opt, "REVERSE")) {
scanf("%d%d", &L, &R);
splay(get_kth(root, L), 0); splay(get_kth(root, R + 2), root);
rev[ch[ch[root][1]][0]] ^= 1;
pushup(ch[root][1]); pushup(root);
}else if(!strcmp(opt, "REVOLVE")) {
scanf("%d%d%d", &L, &R, &d);
d = (d % (R - L + 1) + (R - L + 1)) % (R - L + 1);
if(!d) continue;
splay(get_kth(root, R - d + 1), 0); splay(get_kth(root, R + 2), root);
int tmp = ch[ch[root][1]][0];
ch[ch[root][1]][0] = 0;
pushup(ch[root][1]); pushup(root);
splay(get_kth(root, L), 0); splay(get_kth(root, L + 1), root);
ch[ch[root][1]][0] = tmp; fa[tmp] = ch[root][1];
pushup(ch[root][1]); pushup(root);
}else if(!strcmp(opt, "INSERT")) {
scanf("%d%d", &L, &R);
splay(get_kth(root, L + 1), 0); splay(get_kth(root, L + 2), root);
ch[ch[root][1]][0] = newnode(R, ch[root][1]);
pushup(ch[root][1]); pushup(root);
}else if(!strcmp(opt, "DELETE")) {
scanf("%d%d", &L);
splay(get_kth(root, L), 0); splay(get_kth(root, L + 2), root);
ch[ch[root][1]][0] = 0;
pushup(ch[root][1]); pushup(root);
}else if(!strcmp(opt, "MIN")) {
scanf("%d%d", &L, &R);
splay(get_kth(root, L), 0); splay(get_kth(root, R + 2), root);
printf("%d\n", Min[ch[ch[root][1]][0]]);
}
}
return 0;
}
【POJ 3580】SuperMemo Splay的更多相关文章
- 【POJ 3580】 SuperMemo
[题目链接] 点击打开链接 [算法] 本题也是Splay区间操作的模板题,不过要比BZOJ 3223要稍微复杂一些,做完此题后,我终于对Splay有了更深入的理解,有“拨开云雾见青天”的感觉 本题还是 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- npm安装package.json
npm安装package.json时,直接转到当前项目目录下,用命令npm install 或npm install --save-dev安装即可,自动将package.json中的模块安装到node ...
- nginx源代码分析--监听套接字的创建 套接字的监听 HTTP请求创建连接
作为一个webserver,那么肯定是有监听套接字的,这个监听套接字是用于接收HTTP请求的,这个监听套接字的创建是依据配置文件的内容来创建的,在nginx.conf文件里有多少个地址就须要创建多少个 ...
- Node.js学习笔记(6)——使用Express创建一个工程
前提是搭建好了环境,node,npm,express:(推荐全局安装) 开始用express创建一个基础工程: express –t ejs microblog 进入文件夹之后 npm-install ...
- Zabbix-20160817-高危SQL注入漏洞
漏洞概述: zabbix是一个开源的企业级性能监控解决方案.近日,zabbix的jsrpc的profileIdx2参数存在insert方式的SQL注入漏洞,攻击者无需授权登陆即可登陆zabbix管理系 ...
- windows下如何快速优雅的使用python的科学计算库?
Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy.pandas和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些 ...
- 大数据:Hive常用参数调优
1.limit限制调整 一般情况下,Limit语句还是需要执行整个查询语句,然后再返回部分结果. 有一个配置属性可以开启,避免这种情况---对数据源进行抽样 hive.limit.optimize.e ...
- Micro Python:运行在微控制器上的Python
Micro Python运行在微控制器上的Python.遵守MIT协议.由剑桥大学的理论物理学家乔治·达明设计.和Arduino类似,但Micro Python更强大. Micro Python的软件 ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- CentOS系统环境下安装MongoDB
(1)进入MongoDB下载中心:http://www.mongodb.org/downloads We recommend using these binary distributions (官方推 ...
- memcached系列
memcached系列:http://blog.csdn.net/xingxing513234072/article/category/2462865