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

HYSBZ - 1500 (splay)

时间:2017-04-28 17:12:41      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:维护   key   oid   deb   ras   wap   stdin   names   log   

题意:略。

析:利用spaly直接维护就好。

代码如下:

#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>
#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 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 LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e5 + 10;
const int mod = 1e9 + 7;
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 sum[maxn], rev[maxn], setv[maxn];
int lx[maxn], rx[maxn], mx[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] = sum[rt] = x;
  ch[rt][0] = ch[rt][1] = 0;
  lx[rt] = rx[rt] = mx[rt] = x;
  rev[rt] = setv[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;
  sum[rt] = sum[l] + sum[r] + key[rt];
  lx[rt] = max(lx[l], sum[l]+key[rt]+max(0, lx[r]));
  rx[rt] = max(rx[r], sum[r]+key[rt]+max(0, rx[l]));
  mx[rt] = max(0, rx[l]) + key[rt] + max(0, lx[r]);
  mx[rt] = max(mx[rt], max(mx[l], mx[r]));
}

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

void update_setv(int rt, int val){
  if(!rt)  return ;
  key[rt] = val;
  sum[rt] = val * sz[rt];
  lx[rt] = rx[rt] = mx[rt] = max(val, sum[rt]);
  setv[rt] = 1;
}

void push_down(int rt){
  if(setv[rt]){
    update_setv(ch[rt][0], key[rt]);
    update_setv(ch[rt][1], key[rt]);
    setv[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;
  setv[root] = rev[root] = sum[root] = key[root] = 0;
  lx[root] = rx[root] = mx[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 sum[Key_value];
}

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

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

int main(){
  while(scanf("%d %d", &n, &m) == 2){
    Init();
    char op[20];
    while(m--){
      scanf("%s", op);
      int x, y;
      if(op[0] == ‘I‘){
        scanf("%d %d", &x, &y);
        Insert(x, y);
      }
      else if(op[0] == ‘D‘){
        scanf("%d %d", &x, &y);
        Delete(x, y);
      }
      else if(op[0] == ‘R‘){
        scanf("%d %d", &x, &y);
        Reverse(x, y);
      }
      else if(op[0] == ‘G‘){
        scanf("%d %d", &x, &y);
        printf("%d\n", query(x, y));
      }
      else if(op[2] == ‘K‘){
        int z;
        scanf("%d %d %d", &x, &y, &z);
        Make_setv(x, y, z);
      }
      else{
        printf("%d\n", Get_MaxSum(1, sz[root]-2));
      }
    }
  }
  return 0;
}

  

HYSBZ - 1500 (splay)

标签:维护   key   oid   deb   ras   wap   stdin   names   log   

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

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