POJ 3468.A Simple Problem with Integers 解题报告

用树状数组和线段树会比较简单,这里用这道题来学习Splay。

第一次写,代码比较丑

/*
       初始化添加一个key值足够大的结点
       保证每个需要的结点都有后继
*/
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;

const int MAXN = 1111111, INF = 0x7fffffff;

struct node {
    //需要的记录信息
    ll  key, val, sum, lazy, Size, Cnt;
    //指向儿子和父亲的指针
    node *ch[2], *pre;
    node() {pre = ch[0] = ch [1] = 0; Size = 1; key = 0;}
    node (ll key) : key (key) {pre = ch[0] = ch[1] = 0; Size = 1, Cnt = 1, lazy = 0;}
    void Csize() {
        Size = Cnt;
        if (ch[0] != NULL) Size += ch[0]->Size;
        if (ch[1] != NULL) Size += ch[1]->Size;
    }
    void Csum() {
        sum = val;
        if (ch[0] != NULL) sum += ch[0]->sum + ch[0]->Size * ch[0]->lazy;
        if (ch[1] != NULL) sum += ch[1]->sum + ch[1]->Size * ch[1]->lazy;
    }
} nil (0), *NIL = &nil;

struct Splay {
    node *root, nod[MAXN];
    int ncnt;//计算key值不同的结点数,已去重
    Splay() {
        ncnt = 0;
        root = & (nod[ncnt++] = node (INF) );
        root->pre = NIL;
        root->val = root->sum = 0;
    }
    void Push_Down (node *x) {
        if (x->lazy != 0) {
            if (x->ch[0] != NULL) x->ch[0]->lazy += x->lazy;
            if (x->ch[1] != NULL) x->ch[1]->lazy += x->lazy;
                     x->val+=x->lazy;
        }
        x->lazy = 0;
    }

    void Update (node *x) {
        x->Csize();
        x->Csum();
    }

    void Rotate (node *x, int sta) { //单旋转操作,0左旋,1右旋
        node *p = x->pre, *g = p->pre;
        Push_Down (p), Push_Down (x);
        p->ch[!sta] = x->ch[sta];
        if (x->ch[sta] != NULL)  x->ch[sta]->pre = p;
        x->pre = g;
        if (g != NIL)
            if (g->ch[0] == p)  g->ch[0] = x;
            else g->ch[1] = x;
        x->ch[sta] = p, p->pre = x, Update (p);
        if (p == root ) root = x;
    }

    void splay (node *x, node *y) { //Splay 操作,表示把结点x,转到根
        for (Push_Down (x) ; x->pre != y;) { //将x的标记往下传
            if (x->pre->pre == y) { //目标结点为父结点
                if (x->pre->ch[0] == x)       Rotate (x, 1);
                else   Rotate (x, 0);
            }
            else {
                node *p = x->pre, *g = p->pre;
                if (g->ch[0] == p)
                    if (p->ch[0] == x)
                        Rotate (p, 1), Rotate (x, 1);//   / 一字型双旋转
                    else
                        Rotate (x, 0), Rotate (x, 1);//  < 之字形双旋转

                else if (p ->ch[1] == x)
                    Rotate (p, 0), Rotate (x, 0);//    \ 一字型旋转
                else
                    Rotate (x, 1), Rotate (x, 0); //   >之字形旋转
            }
        }
        Update (x); //维护x结点
    }
    //找到中序便利的第K个结点,并旋转至结点y的下面。
    void Select (int k, node *y) {
        int tem ;
        node *t ;
        for ( t = root; ; ) {
            Push_Down (t) ; //标记下传
            tem = t->ch[0]->Size ;
            if (k == tem + 1 ) break ; //找到了第k个结点 t
            if (k <= tem)
                t = t->ch[0] ; //第k个结点在左子树
            else
                k -= tem + 1 , t = t->ch[1] ;//在右子树
        }
        splay (t, y);
    }
    bool Search (ll key, node *y) {
        node *t = root;
        for (; t != NULL;) {
            Push_Down (t);
            if (t->key > key && t->ch[0] != NULL) t = t->ch[0];
            else if (t->key < key && t->ch[1] != NULL) t = t->ch[1];
            else
                break;
        }
        splay (t, y);
        return t->key == key;
    }
    void Insert (int key, int val) {
        if (Search (key, NIL) ) root->Cnt++, root->Size++;
        else {
            int d = key > root->key;
            node *t = & (nod[++ncnt] = node (key) );
            Push_Down (root);
            t->val = t->sum = val;
            t->ch[d] = root->ch[d];
            if (root->ch[d] != NULL) root->ch[d]->pre = t;
            t->ch[!d] = root;
            t->pre = root->pre;
            root->pre = t;
            root->ch[d] = NULL;
            Update (root);
            root = t;
        }
        Update (root);
    }
} sp;

ll  n, m, x;
int main() {
    scanf ("%lld %lld", &n, &m);
    sp.Insert (0, 0);
    sp.Insert (n + 1, 0);
    for (int i = 1; i <= n; i++) {
        scanf ("%lld", &x);
        sp.Insert (i, x);
    }
    char cmd;
    int l, r;
    for (int i = 1; i <= m; i++) {
        scanf ("\n%c %d %d", &cmd, &l, &r);
        sp.Search (l - 1, NIL);
        sp.Search (r + 1, sp.root);
        if (cmd == ‘Q‘) {
            node *t = sp.root->ch[1]->ch[0];
            ll ans = t->sum + t->Size * t->lazy;
            printf ("%lld\n", ans);
        }
        if (cmd == ‘C‘) {
            ll c;
            scanf ("%lld", &c);
            sp.root->ch[1]->ch[0]->lazy += c;
        }
    }
    return 0;
}

时间: 2024-12-22 03:22:00

POJ 3468.A Simple Problem with Integers 解题报告的相关文章

POJ 3468 A Simple Problem with Integers(线段树)

题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 56005   Accepted: 16903 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with

poj 3468 A Simple Problem with Integers 【线段树-成段更新】

题目:poj 3468 A Simple Problem with Integers 题意:给出n个数,两种操作 1:l -- r 上的所有值加一个值val 2:求l---r 区间上的和 分析:线段树成段更新,成段求和 树中的每个点设两个变量sum 和 num ,分别保存区间 l--r 的和 和l---r 每个值要加的值 对于更新操作:对于要更新到的区间上面的区间,直接进行操作 加上 (r - l +1)* val .下面的区间标记num += val 对于求和操作,每次进行延迟更新,把num值

POJ 3468 A Simple Problem with Integers(线段树区间更新)

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

POJ 3468 A Simple Problem with Integers 【树状数组】

题目链接:http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b,要求输出v[a]到v[b]之间的和,另一种给出三个数a,b,c,让v[a]到v[b]之间的数全都加上c. 完全是树状数组能够实现的功能,但是如果就这样单纯的套用模板,做第二种操作是更新每个值,这样的操作就有可能超时. 换一种思路,既然第二种操作是给某区间上的所有数加上相同的值,那么应该是能够简化的才对. 假设数组sum[i]为原数组从v[1]到v[i]的和,数

POJ 3468 A Simple Problem with Integers(详细题解)

这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的. 此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的 具体思路 在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新Sum(加上本次增量),再将增量往下传. 这样更新的复杂度就是O(log(n))在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc代表的所有增量累加到Sum上后将Inc清0,接下来再往下查询. Inc往下带的过程也是区

poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   Accepted: 17753 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

线段树(成段更新) POJ 3468 A Simple Problem with Integers

题目传送门 1 /* 2 线段树-成段更新:裸题,成段增减,区间求和 3 注意:开long long:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 using namespace std; 11 12 #define lson l, mid, rt <<

POJ - 3468 A Simple Problem with Integers (区间求和)

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000."Q a b" means querying the sum of Aa, Aa+1, ... , Ab. 分析:自己写的有点麻烦了,写的时候手残+脑残,改了好久. val+lazy*(r-l+1)表示和,如果lazy==0表示当前区间加的值不统一. 1 #include <iostream