POJ 3580 SuperMemo
来源:互联网 发布:伺服控制需要编程吗 编辑:程序博客网 时间:2024/06/08 16:55
Splay重口题
题意: 给出一个序列,要求支持以下操作:
Add: 区间[l,r]增加x。
Reverse: 区间[l,r]翻转。
Revolve: 将[l,r]区间的最右一个数移到区间左边x次(这里的次数可能暴大,也可能为负,本题的坑)。
Insert: 在位置pos后面插入一个数x。
Delete: 删除位置pos的数。
Min: 询问区间[l,r]的最小值。
解法: 动态的数据结构,10W的操作数,还要带翻转,so Splay的题。
Reverse: 区间翻转,对于这个操作,由于Splay是一棵中序遍历的二叉树,原本的访问顺序是先左儿子再自己再右儿子,那么现在翻转了之后,就变成先访问右儿子再自己再左儿子,所以只要交换左右子树,然后下推翻转标记即可。
Revolve: 对于这个操作,等效于将最右边的长度为x的段移到最左边,在Splay上的实现,就像盆栽的剪枝移植吧(so called移花接木?- -)。
线段树的lazy思想同样适用于Splay,而Splay更加动态,例如上面的操作Splay全支持,而用线段树只能支持操作1和6。代价就是代码比线段树长。
Splay对区间处理的方式是将待处理区间的左开区间位置旋转到根,把右开区间位置旋转到根的右儿子,那么待处理区间就完整的出现在根的右儿子的左儿子上了(原因:二叉树的中序遍历结构)。
注意在初始化时加入头和尾两个虚拟节点,能够避免很多的讨论。
一个人怒刷3页红版,修改n次,重写一次,总算AC了,真心蛋疼啊。。有没有超越的(⊙o⊙)。
常数写的偏大,然后还是动态内存,好慢。
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int INF = ~0u>>2;struct Node { Node* ch[2]; int val,size,rev,minx,add; int cmp(int x) { if(x == ch[0]->size + 1) return -1; return x < ch[0]->size + 1 ? 0 : 1; }} *nill, *root;void up(Node* o) { if(o == nill) return ; o->size = o->ch[0]->size + o->ch[1]->size + 1; o->minx = min(o->val, min(o->ch[0]->minx, o->ch[1]->minx));}void down(Node* o) { if(o == nill) return ; if(o->add) { for(int d = 0; d < 2; d ++) if(o->ch[d] != nill) { o->ch[d]->add += o->add; o->ch[d]->minx += o->add; o->ch[d]->val += o->add; } o->add = 0; } if(o->rev) { Node* temp = o->ch[0]; o->ch[0] = o->ch[1]; o->ch[1] = temp; o->ch[0]->rev ^= 1; o->ch[1]->rev ^= 1; o->rev = 0; }}void rotate(Node* &o, int d) { if(o == nill) return ; down(o); down(o->ch[d^1]); Node* temp = o; o = o->ch[d^1]; temp->ch[d^1] = o->ch[d]; o->ch[d] = temp; up(o->ch[d]); up(o);}// 将左数第k个节点旋至ovoid splay(Node* &o, int k) { if(o == nill) return ; down(o); int d = o->cmp(k); if(d == 1) k -= o->ch[0]->size + 1; if(d != -1) { Node* p = o->ch[d]; down(p); // 少写这句悲剧一天 int d2 = p->cmp(k); int k2 = (d2 == 0 ? k : k - p->ch[0]->size - 1); if(d2 != -1) { splay(p->ch[d2],k2); if(d == d2) rotate(o,d^1); else rotate(o->ch[d],d); } rotate(o,d^1); }}// 将开区间(l,r)旋到root->ch[1]->ch[0]的位置void RTO(int l, int r) { splay(root,l+1); splay(root->ch[1],r-l);}void New_node(Node* &o, int num) { o = new Node(); o->ch[0] = o->ch[1] = nill; o->size = 1; o->rev = o->add = 0; o->minx = o->val = num;}void Insert(int pos, int x) { RTO(pos,pos+1); New_node(root->ch[1]->ch[0],x); up(root->ch[1]); up(root);}void build(int n) { nill = new Node(); nill->ch[0] = nill->ch[1] = nill; nill->size = 0; nill->val = nill->minx = INF; New_node(root,INF); New_node(root->ch[1],INF); int x; for(int i = 0; i < n; i ++) { scanf("%d", &x); Insert(i,x); }}void Add() { int l, r, x; scanf("%d%d%d", &l, &r, &x); if(l > r) swap(l,r); RTO(l-1,r+1); root->ch[1]->ch[0]->add += x; root->ch[1]->ch[0]->val += x; root->ch[1]->ch[0]->minx += x; up(root->ch[1]); up(root);}void Delete() { int pos; scanf("%d", &pos); if(root->size <= 2) return ; splay(root,pos+1); splay(root->ch[0],pos); Node* temp = root; root = root->ch[0]; root->ch[1] = temp->ch[1]; delete temp; up(root);}void Min() { int l,r; scanf("%d%d", &l, &r); if(l > r) swap(l,r); RTO(l-1,r+1); printf("%d\n", root->ch[1]->ch[0]->minx);}void Reverse() { int l, r; scanf("%d%d", &l, &r); if(l > r) swap(l,r); RTO(l-1,r+1); root->ch[1]->ch[0]->rev ^= 1;}void Revolve() { int l, r, x; scanf("%d%d%d", &l, &r, &x); if(l > r) swap(l,r); x = (x%(r-l+1)+r-l+1)%(r-l+1); if(x == 0) return ; RTO(r-x,r+1); Node* temp = root->ch[1]->ch[0]; root->ch[1]->ch[0] = nill; up(root->ch[1]); up(root); RTO(l-1,l); root->ch[1]->ch[0] = temp; up(root->ch[1]); up(root);}int main() { int n,m; scanf("%d", &n); build(n); scanf("%d", &m); char s[20]; while(m--) { scanf("%s", s); if(s[0] == 'A') Add(); else if(s[0] == 'I') { int pos, x; scanf("%d%d", &pos, &x); Insert(pos,x); } else if(s[0] == 'D') Delete(); else if(s[0] == 'M') Min(); else if(strcmp(s,"REVERSE") == 0) Reverse(); else Revolve(); } return 0;}
- POJ--3580[SuperMemo] Splay_Tree
- poj 3580 SuperMemo
- POJ 3580 SuperMemo
- splay POJ 3580SuperMemo
- POJ 3580 SuperMemo(Splay)
- POJ 3580 SuperMemo(Splay)
- poj 3580 SuperMemo(Splay)
- POJ 3580 SuperMemo
- POJ 3580 SuperMemo(SplayTree)
- poj-3580-SuperMemo-splay
- 【POJ】3580 SuperMemo 【splay】
- POJ 3580 SuperMemo Splay
- poj 3580 SuperMemo
- POJ 3580 SuperMemo [Splay]
- POJ 3580SuperMemo
- POJ 3580 SuperMemo SplayTree
- poj 3580 SuperMemo
- poj 3580 SuperMemo
- hdu——4462(暴力枚举)
- cocos2d-x 函数指针 typedef CCLayer* (*NEWTESTFUNC)() 解释
- poj3254 Corn Fields
- C++学习笔记十三-复制控制
- Linux下Fork与Exec使用
- POJ 3580 SuperMemo
- 在Windows上搭建Android的JAVA开发环境图文教程(Eclipse版本)
- Android Handler、Message完全解析,带你从源码的角度彻底理解
- 多进程引用的动态链接库中的全局变量问题
- halcon读取avi视频
- Linux共享库(动态库)与进程之间共享全局变量可行性分析
- poj 3207 Ikki's Story IV - Panda's Trick
- Exercise2.1 E4(数据结构与程序设计)
- POJ1195_Mobile phones_solution