【题目链接】

点击打开链接

【算法】

本题也是Splay区间操作的模板题,不过要比BZOJ 3223要稍微复杂一些,做完此题后,我终于对Splay有了更深入的理解,有“拨开云雾见青天”的感觉

本题还是有许多细节的,笔者花了5h才通过了此题

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 100000
const int INF = 2e9;int i,N,M,d,P,x,y,t;
int a[MAXN+10];
string opt;template <typename T> inline void read(T &x) {int f=1; x=0;char c = getchar();for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';x *= f;
}template <typename T> inline void write(T x) {if (x < 0) { putchar('-'); x = -x; }if (x > 9) write(x/10);putchar(x%10+'0');
}template <typename T> inline void writeln(T x) {write(x);puts("");
}struct Splay {int root,total;struct Node {int fa,son[2],size,add,Min,val;bool rev;            } Tree[MAXN*2+10];inline bool get(int x) {return Tree[Tree[x].fa].son[1] == x;}inline void build(int index,int l,int r) {int mid = (l + r) >> 1;Tree[index].size = 1;Tree[index].add = Tree[index].rev = 0;Tree[index].val = Tree[index].Min = a[mid];    if (l == r) return;if (l < mid) {++total;Tree[index].son[0] = total;Tree[total].fa = index;build(total,l,mid-1);Tree[index].size += Tree[Tree[index].son[0]].size;Tree[index].Min = min(Tree[index].Min,Tree[Tree[index].son[0]].Min);}if (mid < r) {++total;Tree[index].son[1] = total;Tree[total].fa = index;build(total,mid+1,r);Tree[index].size += Tree[Tree[index].son[1]].size;Tree[index].Min = min(Tree[index].Min,Tree[Tree[index].son[1]].Min);}}inline void new_node(int index,int x,int f) {Tree[index].rev = 0;Tree[index].size = 1;Tree[index].val = Tree[index].Min = x;Tree[index].add = 0;Tree[index].fa = f;Tree[index].son[0] = Tree[index].son[1] = 0;}inline int query_pos(int x) {int index = root;while (true) {pushdown(index);if (x > Tree[Tree[index].son[0]].size) {x -= Tree[Tree[index].son[0]].size;if (x == 1) return index;--x;index = Tree[index].son[1];} else index = Tree[index].son[0];}        }inline void pushdown(int index) {if (Tree[index].rev) {swap(Tree[index].son[0],Tree[index].son[1]);Tree[Tree[index].son[0]].rev ^= 1;Tree[Tree[index].son[1]].rev ^= 1;Tree[index].rev = 0;}if (Tree[index].add) {Tree[Tree[index].son[0]].val += Tree[index].add;Tree[Tree[index].son[1]].val += Tree[index].add;Tree[Tree[index].son[0]].add += Tree[index].add;Tree[Tree[index].son[1]].add += Tree[index].add;Tree[Tree[index].son[0]].Min += Tree[index].add;Tree[Tree[index].son[1]].Min += Tree[index].add;Tree[index].add = 0;}}inline void update(int index) {Tree[index].size = Tree[Tree[index].son[0]].size + Tree[Tree[index].son[1]].size + 1;Tree[index].Min = Tree[index].val;if (Tree[index].son[0]) Tree[index].Min = min(Tree[index].Min,Tree[Tree[index].son[0]].Min);if (Tree[index].son[1]) Tree[index].Min = min(Tree[index].Min,Tree[Tree[index].son[1]].Min);}inline void splay(int x,int pos) {int f;for (f = Tree[x].fa; (f = Tree[x].fa) != pos; rotate(x)) {if (Tree[f].fa != pos) rotate(get(f) == get(x) ? (f) : (x));}if (!pos) root = x;}inline void rotate(int x) {int f = Tree[x].fa,g = Tree[f].fa,tmpx = get(x),tmpf = get(f);pushdown(f); pushdown(x);if (!f) return;Tree[f].son[tmpx] = Tree[x].son[tmpx^1];if (Tree[x].son[tmpx^1]) Tree[Tree[x].son[tmpx^1]].fa = f;Tree[x].son[tmpx^1] = f;Tree[f].fa = x;Tree[x].fa = g;if (g) Tree[g].son[tmpf] = x;update(f);update(x);}inline void Insert(int p,int val) {int x = query_pos(p),y = query_pos(p+1);splay(x,0); splay(y,root);new_node(++total,val,Tree[root].son[1]);Tree[Tree[root].son[1]].son[0] = total;update(Tree[root].son[1]);update(root);    }    inline void erase(int p) {int x = query_pos(p-1),y = query_pos(p+1);splay(x,0); splay(y,root);Tree[Tree[root].son[1]].son[0] = 0;update(Tree[root].son[1]);update(root);}inline void add(int l,int r,int v) {int x = query_pos(l-1),y = query_pos(r+1);splay(x,0); splay(y,root);Tree[Tree[Tree[root].son[1]].son[0]].val += v;Tree[Tree[Tree[root].son[1]].son[0]].add += v;Tree[Tree[Tree[root].son[1]].son[0]].Min += v;update(Tree[root].son[1]);update(root);}inline void reverse(int l,int r) {int x = query_pos(l-1),y = query_pos(r+1);splay(x,0); splay(y,root);Tree[Tree[Tree[root].son[1]].son[0]].rev ^= 1;}inline int query_min(int l,int r) {int x = query_pos(l-1),y = query_pos(r+1);splay(x,0); splay(y,root);return Tree[Tree[Tree[root].son[1]].son[0]].Min;}inline void revolve(int l,int r,int t) {int x = query_pos(r-t),y = query_pos(r+1);splay(x,0); splay(y,root);int tmp = Tree[Tree[root].son[1]].son[0];Tree[Tree[root].son[1]].son[0] = 0;update(Tree[root].son[1]);update(root);x = query_pos(l-1); y = query_pos(l);splay(x,0); splay(y,root);Tree[Tree[root].son[1]].son[0] = tmp;Tree[tmp].fa = Tree[root].son[1];update(Tree[root].son[1]);update(root);}
} T;int main() {read(N);T.root = T.total = 1;for (i = 2; i <= N + 1; i++) read(a[i]);a[1] = a[N+2] = INF;T.build(1,1,N+2);read(M);while (M--) {cin >> opt;if (opt[0] == 'A') {read(x); read(y); read(d); if (x > y) swap(x,y);T.add(x+1,y+1,d);} else if (opt[0] == 'R' && opt[1] == 'E' && opt[2] == 'V' && opt[3] == 'E'){read(x); read(y);if (x > y) swap(x,y);T.reverse(x+1,y+1);                    } else if (opt[0] == 'R' && opt[1] == 'E' && opt[2] == 'V' && opt[3] == 'O') {read(x); read(y); read(t);if (x > y) swap(x,y);t = (t % (y - x + 1) + y - x + 1) % (y - x + 1); if (!t) continue;T.revolve(x+1,y+1,t);} else if (opt[0] == 'I') {read(x); read(P);T.Insert(x+1,P);} else if (opt[0] == 'D') {read(x); T.erase(x+1);} else if (opt[0] == 'M') {read(x); read(y);if (x > y) swap(x,y);writeln(T.query_min(x+1,y+1));}}return 0;}

转载于:https://www.cnblogs.com/evenbao/p/9196411.html

【POJ 3580】 SuperMemo相关推荐

  1. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  2. BZOJ 2287 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. &q ...

  3. 【POJ 3026】Borg Maze

    [POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...

  4. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  5. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...

  6. 2287. 【POJ Challenge】消失之物(数组递推\分治优化背包)

    2287. [POJ Challenge]消失之物 这题的思想和P4564 [CTSC2018]假面优化的思想一样,应该反过来说,假面那个题应该是借鉴这题的思路. 显然不能枚举每个物品消失O(n)O( ...

  7. bzoj2287【POJ Challenge】消失之物 缺一01背包

    bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...

  8. 【POJ - 1364】King(差分约束判无解)

    题干: Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ` ...

  9. *【POJ - 2796】 Feel Good (前缀和优化+单调栈维护)

    题干: Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12409   Accepted: 3484 Ca ...

  10. 【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)

    题干: Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished play ...

最新文章

  1. 搭建Mantis 缺陷管理系统(转)
  2. xilinxaxi ethernet 硬件时间戳告警
  3. python四个带 key 参数的函数(max、min、map、filter)
  4. ***必须要掌握的计算机知识
  5. Fabric--CA 应用与配置
  6. intellij 常用设置
  7. leetcode 1738. 找出第 K 大的异或坐标值
  8. 项目案例:qq数据库管理_2小时元项目:项目管理您的数据科学学习
  9. 【数据结构与算法】广度优先遍历(BFS) 深度优先遍历(DFS)
  10. 如何在本机安装mysql_机器人之如何在本机安装MySQL,并配置电脑为数据库服务器...
  11. bzoj2655 calc
  12. 大数据工程师简历_大数据毕业生简历该怎么写?
  13. 2017AAAI召开 百度王海峰揭秘百度自然语音处理技术
  14. 从祖师级到新生代,48位开发者的「武功秘籍」
  15. 【已解决】Android Studio下,gradle project sync failed 错误
  16. 解决报错:OSError: Failed to open file b‘D:\\\xe5\xad\xa6\xe4\xb9\xa0\\scipy-_7cm39vc‘(图文并茂版详细版!!)
  17. clear:both的认知
  18. uni-app的生命周期说明及平台差异性说明
  19. uniapp离线打包apk安装在android12上无法安装
  20. 阿拉伯数字转换为中文大写数字

热门文章

  1. 计算机显示器闪烁,电脑显示器闪屏怎么办 显示器闪屏原因【解决方法】
  2. Fiori 动态磁贴示例
  3. 团队组成五个基本要素_团队管理的五个基本要素
  4. java多线程简单模拟12306抢票
  5. 制作OpenStack xpsp3镜像
  6. 为什么docker的端口映射需要开启ip转发功能?
  7. 语音识别之Fbank特征提取工具的比较(kaldi、python_speech_features、torchaudio)
  8. ERROR Error: [copy-webpack-plugin] patterns must be an array
  9. 散文:dflow 是如何实现slice的
  10. Hulu校招补录已开始,1年内工作经验也可投!