Trivial intervals (line tree interval update + tricks!) )

Source: Internet
Author: User
Tags fread min stdin
a trivial intervalTime limit: 4 Sec Memory limit:
Submissions: 131 Resolution: 26
Submitted State [Discussion Version] Title DescriptionGiven an integer sequence of length n A[1..N], there are three kinds of operations:
1 L R x: Add x for each number of [L, R] intervals
2 L R: Change each a[i [L, R] interval to an integral part of sqrt (A[i])
3 L R: For all numbers of [L, R] intervals
where L and R and X represent an integer
inputThe first line is a T, which represents the number of data groups.
For each set of data
LINE1: Two number n m, representing integer sequence length and operand
Line2:n number, indicating A[1..N]
Line3 ... Line3+m-1: One query per line, for the third type of inquiry, please output the answer.
For each type of inquiry, the number of the operation is given first, and then the corresponding operation is given, and the number is corresponding to the title description.
Data contract:
1<=t<=5
N,m <= 100000
1<= A[i], x<=100000
OutputFor the third type of inquiry, output the answer. Each answer takes up one line. Sample Input
1
5 5
1 2 3 4 5
1 3 5 2
2 1 4
3 2 4
2 3 5
3 1 5
Sample Output
5
6
Tips

Source

Submitted State
Segment tree, and then record the maximum minimum value of the current interval, if it is the same, then update it directly to the interval update.

#include <bits/stdc++.h> #define LL long Long const int maxn=1e5+10;
const int N=2E5+10;
using namespace Std;
#define LS rt<<1 #define RS rt<<1|1 int N,M,K,T,A[MAXN];
ll tag[maxn<<2],ma[maxn<<2],mi[maxn<<2],sum[maxn<<2];
const INT buffersize=1<<16;
Char Buffer[buffersize],*head,*tail;
        inline char Getchar () {if (head==tail) {int l=fread (Buffer,1,buffersize,stdin);
    Tail= (Head=buffer) +l;
} return *head++;
    } inline int read () {int X=0,f=1;char c=getchar (); for (;!
    IsDigit (c); C=getchar ()) if (c== '-') f=-1;
    for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 ';
return x*f;
    } void Pup (int l,int r,int RT) {int mid=l+r>>1;
    SUM[RT]=SUM[LS]+SUM[RS];
    Ma[rt]=max (Ma[ls],ma[rs]);
    Mi[rt]=min (Mi[ls],mi[rs]);
tag[rt]=0;
    } void PDW (int l,int r,int RT) {int mid=l+r>>1;
    sum[ls]+=tag[rt]* (mid-l+1);
    MA[LS]+=TAG[RT];
    MI[LS]+=TAG[RT];
    TAG[LS]+=TAG[RT]; Sum[rs]+=tag[rt]* (R-MID);
    MA[RS]+=TAG[RT];
    MI[RS]+=TAG[RT];
    TAG[RS]+=TAG[RT];
tag[rt]=0;
        } void Build (int l,int R,int RT) {if (l==r) {tag[rt]=0;
        MA[RT]=MI[RT]=SUM[RT]=A[L];
    Return
    } int mid=l+r>>1;
    Build (L,mid,ls);
    Build (MID+1,R,RS);
Pup (L,R,RT);
        } void upd (int l,int r,ll v,int l,int r,int RT) {if (l<=l&&r<=r) {sum[rt]+=v* (r-l+1);
        Ma[rt]+=v;
        Mi[rt]+=v;
        Tag[rt]+=v;
    Return
    } int mid=l+r>>1;
    if (Tag[rt]) PDW (L,R,RT);
    if (l<=mid) upd (L,R,V,L,MID,LS);
    if (r>mid) upd (L,R,V,MID+1,R,RS);
Pup (L,R,RT); 
            } void qsqrt (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) {if (Ma[rt]==mi[rt]) {
            TAG[RT]-=MA[RT];
            MA[RT]=SQRT (Ma[rt]);
            TAG[RT]+=MA[RT];
            MI[RT]=MA[RT];
            sum[rt]= (r-l+1) *ma[rt];
        Return
} else if (ma[rt]==mi[rt]+1) {            if (ll) sqrt (ma[rt]) = = (ll) sqrt (Mi[rt]) +1) {TAG[RT]-=MA[RT];
                sum[rt]-= (r-l+1) * (ma[rt]-(LL) sqrt (Ma[rt]));
                MA[RT]=SQRT (Ma[rt]);
                TAG[RT]+=MA[RT];
                Mi[rt]=ma[rt]-1;
            Return
    }}} int mid=l+r>>1;
    if (Tag[rt]) PDW (L,R,RT);
    if (l<=mid) qsqrt (L,R,L,MID,LS);
    if (r>mid) qsqrt (L,R,MID+1,R,RS);
Pup (L,R,RT);
    } ll Gao (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) return SUM[RT];
    int mid=l+r>>1;
    if (Tag[rt]) PDW (L,R,RT);
    ll ret=0;
    if (l<=mid) Ret+=gao (L,R,L,MID,LS);
    if (r>mid) Ret+=gao (L,R,MID+1,R,RS);
return ret;
    } int main () {t=read ();
        while (t--) {n=read (); M=read ();
        for (int i=1;i<=n;i++) a[i]=read ();
        Build (1,n,1);
            while (m--) {int b,c,d,e;
            B=read (), C=read (), D=read ();
     if (b==1)       {E=read ();
            UPD (c,d,e,1,n,1);
            } else if (b==2) {qsqrt (c,d,1,n,1);
        } else printf ("%lld\n", Gao (c,d,1,n,1));
}} return 0; }

PS Time-Out code:
#include <bits/stdc++.h> using namespace std;
const INT buffersize=1<<16;
Char Buffer[buffersize],*head,*tail;
        inline char Getchar () {if (head==tail) {int l=fread (Buffer,1,buffersize,stdin);
    Tail= (Head=buffer) +l;
} return *head++;
    } inline int read () {int X=0,f=1;char c=getchar (); for (;!
    IsDigit (c); C=getchar ()) if (c== '-') f=-1;
    for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 ';
return x*f; } #define LL Long long #define L (root) (root << 1) #define R (Root) ((root) << 1) | 1) const INT MAXN = 1
e5 + 5;

int NUMBERS[MAXN];

LL DELAY[MAXN * 4], SUM[MAXN * 4], MX[MAXN * 4], MN[MAXN * 4];
    struct Node {int left, right;
    LL delay;
    LL sum;
    LL MX, MN;
    int mid () {return left + ((right-left) >> 1);

}} TREE[MAXN * 4];
    void pushup (int root) {tree[root].sum = Tree[l (root)].sum + tree[r (root)].sum;
    tree[root].mx = Max (tree[l (Root)].mx, tree[r (root)].mx); tree[root].mn = min(Tree[l (Root)].mn, tree[r (root)].mn);
Tree[root].delay = 0;
        } void pushdown (int root, int l, int r) {LL mid = (R + L) >> 1;
        Tree[l (root)].delay + = Tree[root].delay;
        Tree[r (root)].delay + = Tree[root].delay;
        Tree[l (root)].sum + = Tree[root].delay * (mid-l + 1);
        Tree[r (root)].sum + = Tree[root].delay * (r-mid);
        Tree[l (root)].mx + = Tree[root].delay;
        Tree[r (root)].mx + = Tree[root].delay;
        Tree[l (root)].mn + = Tree[root].delay;
        Tree[r (root)].mn + = Tree[root].delay;

Tree[root].delay = 0;
    } void build (int root, int left, int.) {tree[root].left = left;
    Tree[root].right = right;
        if (left = = right) {tree[root].delay = 0;
        Tree[root].sum = Numbers[left];
        tree[root].mx = Numbers[left];
        tree[root].mn = Numbers[left];
    Return
    } int mid = Tree[root].mid ();
    Build (L (root), left, mid);
    Build (R (root), Mid + 1, right);
Pushup (root); } LL query (int root, int left, int. right) {if (Tree[root].left = = Left && Tree[root].right = = right) {R
    Eturn tree[root].sum;
    } if (Tree[root].delay) pushdown (Root, Tree[root].left, tree[root].right);
    int mid = Tree[root].mid ();
    if (right <= mid) {return query (L (root), left, right);
    } else if (Left > mid) {return query (R (root), left, right);
    } else {return query (root), left, mid) + query (root), Mid + 1, right); }} void update (int root, int left, int. right, LL Add) {if (Tree[root].left = = Left && Tree[root].right = = R
        ight) {tree[root].delay + = add;
        Tree[root].sum + = Add * (Right-left + 1);
        Tree[root].mx + = add;
        Tree[root].mn + = add;
    Return
    } if (Tree[root].delay) pushdown (Root, Tree[root].left, tree[root].right);
    int mid = Tree[root].mid ();
    if (right <= mid) {Update (L (root), left, right, add); } else if (left > mID) {Update (root), left, right, add);
        } else {update (L (root), left, Mid, add);
    Update (R (root), Mid + 1, right, add);
} pushup (root);
        } void sq (int root, int left, int. right) {if (Tree[root].left = = Left && Tree[root].right = = right) {
        LL mx = sqrt (tree[root].mx);
        LL mn = sqrt (tree[root].mn);
            if (tree[root].mx = = tree[root].mn) {tree[root].delay-= (TREE[ROOT].MX-MX);
            tree[root].sum = mx * (right-left + 1);
            tree[root].mx = mx;
            Tree[root].mn = MN;
Return }} else if ((tree[root].mx = = tree[root].mn + 1) && (mx = = mn + 1)) {tree[root].de
            Lay-= (TREE[ROOT].MX-MX);
            Tree[root].sum-= (tree[root].mx-mx) * (Right-left + 1);
            tree[root].mx = mx;
        Tree[root].mn = MN;
    }} if (Tree[root].delay) pushdown (Root, Tree[root].left, tree[root].right); int mid = Tree[Root].mid ();
    if (right <= mid) {sq (L (root), left, right);
    } else if (Left > mid) {sq (root), left, right);
        } else {sq (L (root), left, mid);
    Sq (R (root), Mid + 1, right);
} pushup (root);
    } int main () {int t;
    int n, m;
    int i;

int op, L, R, X;
    scanf ("%d", &t);
    t = read ();
        while (t--) {//scanf ("%d%d", &n, &m);
        n = Read (), M = Read ();
            for (i = 1; I <= n; ++i) {//scanf ("%d", &numbers[i]);
        Numbers[i] = read ();
        } build (1, 1, N);
            for (i = 0; i < m; ++i) {//scanf ("%d", &op);
            OP = read ();
                if (op = = 1) {//scanf ("%d%d%d", &l, &r, &x);
                L = Read (), r = Read (), x = Read ();
                Update (1, L, r, X);
            printf ("Debug op = 1\n"); } else if (op = = 2) {//scanf ("%d%d", &l, &r);
                L = Read (), r = Read ();
                Sq (1, L, R);
            printf ("Debug op = 2\n");
                } else {//scanf ("%d%d", &l, &r);
                L = Read (), r = Read ();
                printf ("%lld\n", Query (1, L, R));
            printf ("Debug op = 3\n");
}}} return 0;
    } int main2 () {int t;
    int n, m;
    int i;

    int op, L, R, X;
scanf ("%d", &t);
    t = read ();
        while (t--) {scanf ("%d%d", &n, &m);//n = Read (), M = Read ();
        for (i = 1; I <= n; ++i) {scanf ("%d", &numbers[i]);//numbers[i] = read ();
        } build (1, 1, N);
            for (i = 0; i < m; ++i) {scanf ("%d", &op);//op = read (); if (op = = 1) {scanf ("%d%d%d", &l, &r, &x);//L = Read (), r = Read (), x = Read
                ();
     Update (1, L, r, X);           printf ("Debug op = 1\n");
                } else if (op = = 2) {scanf ("%d%d", &l, &r);//L = Read (), r = Read ();
                Sq (1, L, R);
            printf ("Debug op = 2\n");
                } else {scanf ("%d%d", &l, &r);//L = Read (), r = Read ();
                printf ("%lld\n", Query (1, L, R));
            printf ("Debug op = 3\n");
}}} 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.