bzoj 1251/tyvj p2195 序列终结者

原题链接:http://www.tyvj.cn/p/2195

简单的用伸展树求区间最值,区间翻转。

不想吐槽了,wa快吐的心都有了。

结果发现,对于操作1. 将[L,R]这个区间内的所有数加上V。 这个V可能为0。

在这点上简直欲哭无泪了/(ㄒoㄒ)/~~。。。

好吧,看代码吧:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define Max_N 50010
#define _max(a,b) ((a)>(b)?(a):(b))
#define _size(T) ((T)==null ? 0 : (T)->size)
typedef struct _spt{
    int val, max, rev, size, delta;
    struct _spt *pre, *ch[2];
}splay;
int sz = 0;
splay *root, *null, stack[Max_N];
void push_up(splay *x){
    if (x == null) return;
    x->size = _size(x->ch[0]) + _size(x->ch[1]) + 1;
    x->max = _max(x->val, _max(x->ch[0]->max, x->ch[1]->max));
}
void update(splay *x){
    splay *t = null;
    if (x == null) return;
    x->rev ^= 1;
    t = x->ch[0];
    x->ch[0] = x->ch[1];
    x->ch[1] = t;
}
void push_down(splay *x){
    int t = 0;
    if (x== null) return;
    if (x->delta != 0){
        t = x->delta;
        if (x->ch[0] != null){
            x->ch[0]->delta += t;
            x->ch[0]->max += t;
            x->ch[0]->val += t;
        }
        if (x->ch[1] != null){
            x->ch[1]->delta += t;
            x->ch[1]->max += t;
            x->ch[1]->val += t;
        }
        x->delta = 0;
    }
    if (x->rev != 0){
        x->rev ^= 1;
        update(x->ch[0]);
        update(x->ch[1]);
    }
}
splay *_calloc(int val){
    splay *p = &stack[sz++];
    p->delta = p->rev = 0;
    p->size = 1, p->max = p->val = val;
    p->pre = p->ch[0] = p->ch[1] = null;
    return p;
}
void rotate(splay *x, int c){
    splay *y = x->pre;
    push_down(y), push_down(x);
    y->ch[!c] = x->ch[c];
    if (x->ch[c] != null) x->ch[c]->pre = y;
    x->pre = y->pre;
    if (y->pre != null) y->pre->ch[y->pre->ch[0] != y] = x;
    x->ch[c] = y;
    y->pre = x;
    push_up(y);
    if (y == root) root = x;
}
void splay_splay(splay *x, splay *f){
    if (x == root) return;
    for (; x->pre != f; push_down(x)){
        if (x->pre->pre == f){
            rotate(x, x->pre->ch[0] == x);
        } else {
            splay *y = x->pre, *z = y->pre;
            if (z->ch[0] == y){
                if (y->ch[0] == x) rotate(y, 1), rotate(x, 1);
                else rotate(x, 0), rotate(x, 1);
            } else {
                if (y->ch[1] == x) rotate(y, 0), rotate(x, 0);
                else rotate(x, 1), rotate(x, 0);
            }
        }
    }
    push_up(x);
}
splay *built(int l, int r){
    splay *p = null;
    if (l > r) return null;
    int mid = (l + r) >> 1;
    p = _calloc(0);
    p->ch[0] = built(l, mid - 1);
    if (p->ch[0] != null) p->ch[0]->pre = p;
    p->ch[1] = built(mid + 1, r);
    if (p->ch[1] != null) p->ch[1]->pre = p;
    push_up(p);
    return p;
}
void initialize(int l, int r){
    null = _calloc(-INF);
    null->size = 0;
    root = _calloc(-INF);
    root->ch[1] = _calloc(-INF);
    root->ch[1]->pre = root;
    splay *x = built(l, r);
    root->ch[1]->ch[0] = x;
    x->pre = root->ch[1];
    push_up(root->ch[1]);
    push_up(root);
}
splay *select(splay *x, int k){
    int t = 0;
    splay *ret = x;
    for (;;){
        push_down(ret);
        t = _size(ret->ch[0]);
        if (k == t) break;
        else if (k < t) ret = ret->ch[0];
        else k -= t + 1, ret = ret->ch[1];
    }
    return ret;
}
splay *get_range(int l, int r){
    splay_splay(select(root, l - 1), null);
    splay_splay(select(root, r + 1), root);
    return root->ch[1]->ch[0];
}
void change(int l, int r, int c){
    splay *x = get_range(l, r);
    if (c != INF){
        x->delta += c;
        x->val += c;
        x->max += c;
    } else {
        update(x);
    }
}
void query(int l, int r){
    splay *x = get_range(l, r);
    printf("%d\n", x->max);
}
int main(){
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int a, b, c, d, N, M;
    while (~scanf("%d %d", &N, &M)){
        sz = 0;
        initialize(1, N);
        while (M--){
            scanf("%d", &a);
            if (1 == a) scanf("%d %d %d", &b, &c, &d), change(b, c, d);
            else if (2 == a) scanf("%d %d", &b, &c), change(b, c, INF);
            else scanf("%d %d", &b, &c), query(b, c);
        }
    }
    return 0;
}
时间: 2024-08-24 11:03:37

bzoj 1251/tyvj p2195 序列终结者的相关文章

伸展树复习 (bzoj 1251 序列终结者)

本来要看LCT的,确发现自己弱得连splay都忘记了,复习一发,顺便重写一发 关键点: 1. 伸展树为左小右大的二叉树,所以旋转操作不会影响树的性质 2. 区间操作为: int u = select(L - 1), v = select(R + 1); splay(u, 0); splay(v, u); //通过旋转操作把询问的区间聚集到根的右子树的左子树下 因为伸展树为左小右大的二叉树,旋转操作后的所以对于闭区间[L, R]之间的所有元素都聚集在根的右子树的左子树下 因为闭区间[L, R],

【BZOJ】1251: 序列终结者(splay)

http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint... #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #inc

BZOJ 1251: 序列终结者( splay )

先orz一下clj...我的splay跟着他写的... 这道题很普通的splay我调了这么久 T T , 就是因为 null 的值初始化为0 , 结果就挂了... -------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream>

bzoj 1251: 序列终结者 2011-12-20

1251: 序列终结者Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 650  Solved: 277[Submit][Status][Discuss]Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可

【BZOJ】【1251】序列终结者

Splay 还是splay序列维护,这题我WA了的原因是:在Push_up的时候,当前子树的max我是直接取的L.R和v[x]的最大值,但是如果没有左/右儿子,默认是会访问0号结点的mx值,而这个值没有初始化成-INF,所以就会导致所有负max值全部变为0…… 1 /************************************************************** 2 Problem: 1251 3 User: Tunix 4 Language: C++ 5 Resul

BZOJ 1251 序列终结者(Splay)

题目大意 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思.这道题目 就叫序列终结者吧.[问题描述] 给定一个长度为N的序列,每个序列的元素是一个整数(废话).要支持以下三种操作: 1. 将 [L, R] 这个区间内的所有数加上 V. 2. 将 [

BZOJ1251&#183;序列终结者

好像不能附传送门了..这是道sb权限题.. 1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MB Description 网 上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思.这道题目 就叫序列终

[BZOJ1251]序列终结者

试题描述 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量--这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个"库"可以依靠,没有什么其他的意思.这道题目 就叫序列终结者吧. [问题描述] 给定一个长度为N的序列,每个序列的元素是一个整数(废话).要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V. 2.

【bzoj1251】序列终结者 splay

序列终结者 Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思.这道题目 就叫序列终结者吧. [问题描述] 给定一个长度为N的序列,每个序列的元素是一个整数(废话).要支持以下三种操作: 1. 将[L,R]这个区间内的所有数