POJ 3468 A Simple Problem with Integers (Splay树练习)

POJ 3468 A Simple Problem with Integers (Splay树练习)

练习模板题

参考:

大白书

http://blog.csdn.net/crazy_ac/article/details/8034264

http://www.cnblogs.com/kuangbin/archive/2013/04/21/3034081.html

指针:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FD(i, b, a) for(int i = (b); i >= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(a, v) memset(a, v, sizeof(a))
#define PB push_back
#define MP make_pair

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 211111;

#define ll x->ch[0]
#define rr x->ch[1]
#define KT (root->ch[1]->ch[0])

struct Node{
    Node* ch[2];
    int s;
    int val, add;
    LL sum;

    ///比较的是位序
    int cmp(int x)///
    {
        int ss = ch[0]->s;
        if (x == ss + 1) return -1;
        else return x < ss + 1 ? 0 : 1;
    }
    void up()
    {
        s = ch[0]->s + ch[1]->s + 1;
        sum = val + ch[0]->sum + ch[1]->sum;
    }
    void update_down(int c)
    {
        val += c; add += c; sum += 1LL * s * c;
    }
    void down()
    {
        if (add)
        {
            ch[0]->update_down(add);
            ch[1]->update_down(add);
            add = 0;
        }
    }
}N[maxn], *null = &N[0];
int tot;
int a[maxn];
void pre()
{
    tot = 0;
    null->ch[0] = null->ch[1] = null;
    null->val = null->s = null->sum = null->add = 0;
}

///Splay维护一个序列
struct SplayTree{
    Node *root;
    void newnode(Node* &x, int v)///&
    {
        x = &N[++tot];
        ll = rr = null;
        x->val = x->sum = v;
        x->s = 1;
        x->add = 0;
    }
    void init(int n)
    {
        newnode(root, -1);
        newnode(root->ch[1], -1);
        root->ch[1]->up(); root->up();

        build(KT, 1, n);
    }
    void build(Node* &x, int l, int r)///&
    {
        if (l > r) return ;
        int m = (l + r) >> 1;
        newnode(x, a[m]);
        build(ll, l, m - 1);
        build(rr, m + 1, r);
        x->up();
    }
    void rotate(Node* &x, int d)///&
    {
        Node* k = x->ch[d ^ 1]; x->ch[d ^ 1] = k->ch[d]; k->ch[d] = x;
        x->up(); k->up(); x = k;//up
    }
    void splay(Node* &x, int k)///&
    {
        x->down();///pushdown
        int d = x->cmp(k);
        if (d != -1)
        {
            if (d == 1) k -= x->ch[0]->s + 1;
            Node* p = x->ch[d];
            p->down();///pushdown
            int d2 = p->cmp(k);
            if (d2 != -1)
            {
                int k2 = k;
                if (d2 == 1) k2 -= p->ch[0]->s + 1;
                splay(p->ch[d2], k2);///!!!
                if (d == d2) rotate(x, d ^ 1);
                else rotate(x->ch[d], d);
            }
            rotate(x, d ^ 1);
        }
    }
    void update()
    {
        int l, r, c;
        scanf("%d%d%d", &l, &r, &c);
        splay(root, l);
        splay(root->ch[1], r - l + 2);
        KT->update_down(c);
    }
    void query()
    {
        int l, r;
        scanf("%d%d", &l, &r);
        splay(root, l);
        splay(root->ch[1], r - l + 2);
        printf("%lld\n", KT->sum);
    }

}sp;

int main()
{
    int n, m;
    char op[5];
    scanf("%d%d", &n,&m);
    FE(i, 1, n) scanf("%d", &a[i]);
    pre();
    sp.init(n);
    while (m--)
    {
        scanf("%s", op);
        if (op[0] == 'Q') sp.query();
        else sp.update();
    }
    return 0;
}

数组:

#pragma comment(linker, "/STACK:102400000000,102400000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;

//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
typedef long long LL;
typedef vector <int> VI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const int maxn = 211111;

#define ll ch[x][0]
#define rr ch[x][1]
#define KT (ch[ch[rt][1]][0])

int a[maxn];
struct SplayTree{
    int ch[maxn][2];
    int pre[maxn], sz[maxn];
    int val[maxn], add[maxn];
    LL sum[maxn];
    int rt, tot;
    void Rotate(int x, int f)
    {
        int y = pre[x];
        down(y); down(x);///
        ch[y][!f] = ch[x][f];
        pre[ch[x][f]] = y;
        pre[x] = pre[y];
        if (pre[x]) ch[pre[y]][ch[pre[y]][1] == y] = x;
        ch[x][f] = y;
        pre[y] = x;
        up(y);///
    }
    void Splay(int x, int goal)
    {
        down(x);///
        while (pre[x] != goal)
        {
            if (pre[pre[x]] == goal) Rotate(x, ch[pre[x]][0] == x);
            else
            {
                int y = pre[x], z = pre[y];
                int f = ( ch[z][0] == y );
                if (ch[y][f] == x) Rotate(x, !f), Rotate(x, f);
                else Rotate(y, f), Rotate(x, f);
            }
        }
        up(x);///
        if (goal == 0) rt = x;
    }
    void RTO(int k, int goal)
    {
        int x = rt;
        while (sz[ll] + 1 != k)
        {
            if (k < sz[ll] + 1) x = ll;
            else
            {
                k -= (sz[ll] + 1);
                x = rr;
            }
        }
        Splay(x, goal);
    }
    void up(int x)
    {
        sz[x] = 1 + sz[ll] + sz[rr];
        sum[x] = val[x] + sum[ll] + sum[rr];
    }
    void update_down(int x, int c)
    {
        val[x] += c; add[x] += c; sum[x] += 1LL * c * sz[x];
    }
    void down(int x)
    {
        if (add[x])
        {
            update_down(ll, add[x]);
            update_down(rr, add[x]);
            add[x] = 0;
        }
    }
    void newnode(int &x, int c, int f)///&
    {
        x = ++tot;
        ll = rr = 0;
        sz[x] = 1; pre[x] = f;
        val[x] = sum[x] = c;
        add[x] = 0;
    }
    void build(int &x, int l, int r, int f)///&
    {
        if (l > r) return ;
        int m = (l + r) >> 1;
        newnode(x, a[m], f);
        build(ll, l, m - 1, x);
        build(rr, m + 1, r, x);
        up(x);
    }
    void init(int n)
    {
        ch[0][0] = ch[0][1] = pre[0] = sz[0] = 0;
        val[0] = add[0] = sum[0] = 0;
        rt = tot = 0;

        newnode(rt, -1, 0);
        newnode(ch[rt][1], -1, rt);

        build(KT, 1, n, ch[rt][1]);///
        up(ch[rt][1]); up(rt);///
    }
    ///
    void update()
    {
        int l, r, c;
        scanf("%d%d%d", &l, &r, &c);
        RTO(l, 0);
        RTO(r + 2, rt);
        update_down(KT, c);///
    }
    void query()
    {
        int l, r;
        scanf("%d%d", &l, &r);
        RTO(l, 0); RTO(r + 2, rt);
        printf("%lld\n", sum[KT]);
    }
}sp;

int main()
{
    int n, m;
    char op[5];
    scanf("%d%d", &n,&m);
    FE(i, 1, n) scanf("%d", &a[i]);
    sp.init(n);
    while (m--)
    {
        scanf("%s", op);
        if (op[0] == 'Q') sp.query();
        else sp.update();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值