POJ 3468 A simple problem with integers (splay interval update, Interval summation)

Source: Internet
Author: User
Topic links

POJ3468 Topic

The template problem of updating the segment tree interval is written once with splay as a template problem for dealing with the interval problem with splay. Analysis Code

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <queue> #include <string> #define LS (rt<<1) #define RS (rt<<1|1) #define B
It (x) (1<< (x)) using namespace Std;
typedef long Long LL;
Const Double Pi=4*atan (1.0);
const int inf=0x3f3f3f3f;
const double eps=1e-6;
const int maxn=100010;
const int MAXM=2*MAXN;
int a[maxn],n;
    struct Splaytree {#define KEY_VALUE (nt[nt[root][1]][0]) int root,tot;
    int NT[MAXN][2],PRE[MAXN],VAL[MAXN],SIZE[MAXN],ADD[MAXN];
    LL SUM[MAXN];
        void Newnode (int &rt,int fa,int v) {Rt=++tot;
        nt[rt][0]=nt[rt][1]=0;
        PRE[RT]=FA;
        Val[rt]=v;
        add[rt]=sum[rt]=0;
    Size[rt]=1;
        } void Update_add (int rt,int v) {if (rt==0) return;
        Val[rt]+=v;
        Add[rt]+=v;
    sum[rt]+= (LL) v*size[rt]; } void Pushup (int rt) {Size[rt]=1+size[nt[rt][0]]+siZE[NT[RT][1]];
    SUM[RT]=SUM[NT[RT][0]]+SUM[NT[RT][1]]+VAL[RT];
            } void Pushdown (int rt) {if (Add[rt]) {Update_add (nt[rt][0],add[rt]);
            Update_add (Nt[rt][1],add[rt]);
        add[rt]=0;
        }} void Build (int &rt,int l,int r,int FA)///Create a fully balanced two-fork tree, the middle sequence traversal is the original sequence {if (l>r) return;
        int mid= (L+R) >>1;
        Newnode (Rt,fa,a[mid]);
        Build (NT[RT][0],L,MID-1,RT);
        Build (NT[RT][1],MID+1,R,RT);
    Pushup (RT);
        } void Init () {for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
        root=tot=0;
        nt[root][0]=nt[root][1]=pre[root]=size[root]=val[root]=sum[root]=add[root]=0;
        Newnode (root,0,-1);
        Newnode (nt[root][1],root,-1); to be able to extract the interval normally, you need to add a-1 Build (key_value,1,n,nt[root][1]) to the first of the sequence.
        Pushup (nt[root][1]);
    Pushup (root); } void Rotate (int x,int kind)//rotation: Kind 0 is L, 1 is right-handed {int y=pre[X];
        Pushdown (y);
        Pushdown (x);
        Nt[y][!kind]=nt[x][kind];
        Pre[nt[x][kind]]=y;
        if (Pre[y]) nt[pre[y]][nt[pre[y]][1]==y]=x;
        Pre[x]=pre[y];
        Nt[x][kind]=y;
        Pre[y]=x;
    Pushup (y);
        } void splay (int x,int goal)///splay Adjustment: Adjust node X to goal below {pushdown (x); while (Pre[x]!=goal) {if (pre[pre[x]]==goal)///single-spin case Rotate (X,nt[pre[x]][0]
            ==X);
                else {int y=pre[x];
                int kind= nt[pre[y]][0]==y;
                    if (nt[y][kind]==x)///Zigzag rotation {Rotate (x,!kind);
                Rotate (X,kind);
                    } else///glyph Rotation {Rotate (y,kind);
                Rotate (X,kind);
        }}} pushup (x);
    if (goal==0) root=x; } int get_kth (int rt,int k)///search for the number of k in the sequence {pushdown (RT);
        int t=size[nt[rt][0]]+1;
        if (k==t) return rt;///If RT is the K number else if (k<t) return get_kth (NT[RT][0],K);
    else return get_kth (NT[RT][1],K-T);
        } void Add (int l,int r,int v) {splay (get_kth (root,l), 0);
        Splay (Get_kth (root,r+2), root);
        Update_add (KEY_VALUE,V);
        Pushup (nt[root][1]);
    Pushup (root);
        } LL Query (int l,int r) {splay (get_kth (root,l), 0);
        Splay (Get_kth (root,r+2), root);
    return Sum[key_value];
}}st;
    int main () {char op[10];
    int q,x,y,z; while (scanf ("%d%d", &n,&q)!=eof) {St.
        Init ();
            while (q--) {scanf ("%s", op);
                if (op[0]== ' C ') {scanf ("%d%d%d", &x,&y,&z); St.
            ADD (x, y, z);
                } if (op[0]== ' Q ') {scanf ("%d%d", &x,&y); printf ("%lLd\n ", St.
            Query (x, y));
}}} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.