码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3580 SuperMemo (Splay)

时间:2017-10-03 14:33:09      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:ons   debug   ras   class   root   link   val   namespace   序列   

题意:给出一个数字序列,有6种操作:

 ADD x y d: 第x个数到第y个数加d 。

 REVERSE x y : 将区间[x,y]中的数翻转 。 

 REVOLVE x y t :将区间[x,y]向右循环旋转 t 次,如1 2 3 4 5 旋转2次后就变成4 5 1 2 3 。

 INSERT x p :在第x个数后面插入p 。 

 DELETE x :删除第x个数 。

 MIN x y : 查询区间[x,y]中的最小值 。

析:直接用伸展树维护就好,注意在维护第三个操作时,如果 t < 0,直接忽略就好,再就是第三个操作可以看成是先剪切,再插入的操作,不要一个一个的,要直接直接剪切一段区间。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 1000;
const int maxm = 100 + 10;
const ULL mod = 10007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
#define Key_value ch[ch[root][1]][0]
int pre[maxn], ch[maxn][2], key[maxn], sz[maxn];
int root, tot1;
int minv[maxn], rev[maxn], addv[maxn];
int s[maxn], tot2, a[maxn];

void NewNode(int &rt, int fa, int x){
  if(tot2)  rt = s[tot2--];
  else  rt = ++tot1;
  pre[rt] = fa;
  key[rt] = minv[rt] = x;
  ch[rt][0] = ch[rt][1] = 0;
  rev[rt] = addv[rt] = 0;
  sz[rt] = 1;
}

void push_up(int rt){
  int l = ch[rt][0], r = ch[rt][1];
  sz[rt] = sz[l] + sz[r] + 1;
  minv[rt] = min(minv[l], min(minv[r], key[rt]));
}

void update_rev(int rt){
  if(!rt)  return ;
  swap(ch[rt][0], ch[rt][1]);
  rev[rt] ^= 1;
}

void update_addv(int rt, int val){
  if(!rt)  return ;
  key[rt] += val;
  minv[rt] += val;
  addv[rt] += val;
}

void push_down(int rt){
  if(addv[rt]){
    update_addv(ch[rt][0], addv[rt]);
    update_addv(ch[rt][1], addv[rt]);
    addv[rt] = 0;
  }
  if(rev[rt]){
    update_rev(ch[rt][0]);
    update_rev(ch[rt][1]);
    rev[rt] = 0;
  }
}

void Build(int &rt, int l, int r, int fa){
  if(l > r)  return ;
  int m = l+r >> 1;
  NewNode(rt, fa, a[m]);
  Build(ch[rt][0], l, m-1, rt);
  Build(ch[rt][1], m+1, r, rt);
  push_up(rt);
}

void Init(){
  tot1 = root = tot2 = 0;
  ch[root][0] = ch[root][1] = sz[root] = pre[root] = 0;
  addv[root] = rev[root] = key[root] = 0;
  minv[root] = INF;
  NewNode(root, 0, -1);
  NewNode(ch[root][1], root, -1);
  for(int i = 0; i < n; ++i)  scanf("%d", a+i);
  Build(Key_value, 0, n-1, ch[root][1]);
  push_up(ch[root][1]);
  push_up(root);
}

int Get_kth(int rt, int k){
  push_down(rt);
  int t = sz[ch[rt][0]] + 1;
  if(t == k)  return rt;
  if(t > k)  return Get_kth(ch[rt][0], k);
  return Get_kth(ch[rt][1], k-t);
}

void Rotate(int x, int k){
  int y = pre[x];
  push_down(y);
  push_down(x);
  ch[y][!k] = ch[x][k];
  pre[ch[x][k]] = y;
  if(pre[y])  ch[pre[y]][ch[pre[y]][1]==y] = x;
  pre[x] = pre[y];
  ch[x][k] = y;
  pre[y] = x;
  push_up(y);
}

void Splay(int rt, int goal){
  push_down(rt);
  while(pre[rt] != goal){
    if(pre[pre[rt]] == goal){
      push_down(pre[rt]);
      push_down(rt);
      Rotate(rt, ch[pre[rt]][0] == rt);
      continue;
    }
    push_down(pre[pre[rt]]);
    push_down(pre[rt]);
    push_down(rt);
    int y = pre[rt];
    int k = ch[pre[y]][0] == y;
    if(ch[y][k] == rt){
      Rotate(rt, !k);
      Rotate(rt, k);
    }
    else{
      Rotate(y, k);
      Rotate(rt, k);
    }
  }
  push_up(rt);
  if(goal == 0)  root = rt;
}

void Insert(int pos, int tot){
  for(int i = 0; i < tot; ++i)  scanf("%d", a+i);
  Splay(Get_kth(root, pos+1), 0);
  Splay(Get_kth(root, pos+2), root);
  Build(Key_value, 0, tot-1, ch[root][1]);
  push_up(ch[root][1]);
  push_up(root);
}

void Erase(int rt){
  if(!rt)  return ;
  s[++tot2] = rt;
  Erase(ch[rt][0]);
  Erase(ch[rt][1]);
}

void Delete(int pos, int tot){
  Splay(Get_kth(root, pos), 0);
  Splay(Get_kth(root, pos+tot+1), root);
  Erase(Key_value);
  pre[Key_value] = 0;
  Key_value = 0;
  push_up(ch[root][1]);
  push_up(root);
}

void Reverse(int pos, int tot){
  Splay(Get_kth(root, pos), 0);
  Splay(Get_kth(root, pos+tot+1), root);
  update_rev(Key_value);
  push_up(ch[root][1]);
  push_up(root);
}

int query(int pos, int tot){
  Splay(Get_kth(root, pos), 0);
  Splay(Get_kth(root, pos+tot+1), root);
  return minv[Key_value];
}

void Make_addv(int pos, int tot, int c){
  Splay(Get_kth(root, pos), 0);
  Splay(Get_kth(root, pos+tot+1), root);
  update_addv(Key_value, c);
  push_up(ch[root][1]);
  push_up(root);
}


void Revolve(int x, int y, int t){
  int tot = y - x + 1;
  if(t < 0)  return ;
  t = t % tot;
  if(t == 0)  return ;
  int pos = y - t + 1;
  Splay(Get_kth(root, pos), 0);
  Splay(Get_kth(root, pos+1+t), root);
  int newroot = Key_value;
  Key_value = 0;
  push_up(ch[root][1]);
  push_up(root);
  Splay(Get_kth(root, x), 0);
  Splay(Get_kth(root, x+1), root);
  Key_value = newroot;
  pre[newroot] = ch[root][1];
  push_up(ch[root][1]);
  push_up(root);
}

int main(){
  scanf("%d", &n);
  ms(minv, INF);
  Init();
  scanf("%d", &m);
  char op[10];
  int x, y, t;
  while(m--){
    scanf("%s %d", op, &x);
    if(op[0] == ‘A‘){
      scanf("%d %d", &y, &t);
      Make_addv(x, y - x + 1, t);
    }
    else if(op[0] == ‘D‘)  Delete(x, 1);
    else if(op[0] == ‘I‘)  Insert(x, 1);
    else if(op[0] == ‘M‘){
      scanf("%d", &y);
      printf("%d\n", query(x, y - x + 1));
    }
    else if(op[3] == ‘E‘){
      scanf("%d", &y);
      Reverse(x, y - x + 1);
    }
    else {
      scanf("%d %d", &y, &t);
      Revolve(x, y, t);
    }
  }
  return 0;
}

  

POJ 3580 SuperMemo (Splay)

标签:ons   debug   ras   class   root   link   val   namespace   序列   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7623691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!