XTU1238 Segment tree (segment trees/interval max update)

Source: Internet
Author: User

Test instructions has four operations on an array

    • 1: Add all values in the interval [L, R] to C
    • 2: Change all values in the interval [L, R] to C
    • 3: Change all values in the interval [L, R] to C
    • 4: Minimum and maximum values in the output interval [L, R]

Minimum and maximum values for each operation 4 output

Base line tree in Xiangtan card for a long time not written out
Segment Tree Maintenance Three value interval maximum MAXV, interval minimum value MINV, interval increment value add
Operation 1 is an easy to implement Operation 2 and 3:1 to be more complicated. You want to operate on each value separately so it's definitely going to be T, after all, it doesn't make sense to use a segment tree. The MAXV and MINV of all the maximum interval nodes contained in the corresponding interval at each update are identified.

    • Operation 2:maxv = min (MAXV, c), Minv = min (MINV, c)
    • Operation 3:MAXV = max (MAXV, c), MINV = max (MINV, C)

Then you know the MAXV,MINV of the parent interval, and then through the Pashdown function, you can update the sub-interval as needed because all the values of the sub-interval are between the MINV and MAXV of the parent interval, then look at the code.

#include <bits/stdc++.h>#define LC P<<1, S, Mid#define RC p<<1|1, mid+1, E#define MID ((s+e) >>1)using namespace STD;Const intN =200005;intMaxv[n *4], Minv[n *4], Add[n *4];voidPushup (intp) {Maxv[p] = max (maxv[p <<1], Maxv[p <<1|1]); Minv[p] = min (minv[p <<1], Minv[p <<1|1]);}voidPushdown (intP) {intL = P <<1;intr = P <<1|1;if(Add[p])        {Add[l] + = add[p];        Add[r] + = add[p];        Minv[l] + = add[p];        Minv[r] + = add[p];        Maxv[l] + = add[p];        Maxv[r] + = add[p]; ADD[P] =0; }all values of the//sub-interval must be between the MINV~MAXV of the parent intervalMINV[L] = max (Minv[l], minv[p]);    Minv[l] = min (Minv[l], maxv[p]);    MAXV[L] = max (Maxv[l], minv[p]);    Maxv[l] = min (Maxv[l], maxv[p]);    Minv[r] = max (Minv[r], minv[p]);    Minv[r] = min (Minv[r], maxv[p]);    Maxv[r] = max (Maxv[r], minv[p]); Maxv[r] = min (Maxv[r], maxv[p]);}voidBuildintPintSintE) {if(s = = e) {scanf("%d", &maxv[p]); MINV[P] = maxv[p];return;    } build (LC);    Build (RC); Pushup (P);}voidUpdateintPintSintEintLintRintVintOP) {if(S >= l && e <= R) {if(OP = =1)//Add all values of [l,r] interval to v{Add[p] + = V;            Minv[p] + = V;        Maxv[p] + = V; }Else if(OP = =2)//change all values greater than V in the [l,r] interval to v{Maxv[p] = min (maxv[p], V);        Minv[p] = min (minv[p], V); }Else  //op = 3 changes all values less than V in the [l,r] interval to v{Maxv[p] = max (V, Maxv[p]);        MINV[P] = max (V, Minv[p]); }return; } pushdown (P);if(l <= mid) Update (LC, L, R, V, op);if(R > Mid) Update (RC, L, R, V, op); Pushup (P);}intQueryintPintSintEintLintRintOP) {if(s = = L && E = = r)//op = 0 o'clock Query Minimum op = 1 o'clock Max Query Value        returnOp?    MAXV[P]: minv[p]; Pushdown (P);if(R <= Mid)returnQuery (LC, L, r, op);if(L > Mid)returnQuery (RC, L, r, op);if(OP)returnMax (Query (LC, L, Mid, op), query (RC, Mid +1, r, op));returnMin (Query (LC, L, Mid, op), query (RC, Mid +1, r, op));}intMain () {intT, N, Q, L, R, OP, c, Ax, in;scanf("%d", &t); while(t--) {memset(Add,0,sizeof(add));scanf("%d%d", &n, &q); Build1,1, n); while(q--) {scanf("%d%d%d", &op, &l, &r);if(OP = =4) {ax = query (1,1, N, L, R,1); in = Query (1,1, N, L, R,0);printf("%d%d\n", in, Ax); }Else{scanf("%d", &c); Update1,1, N, L, R, c, op); }        }    }return 0;}

Segment Tree

Problem Description:

A contest isn't integrity without problems about data structure.
There is an array a[1],a[2],..., a[n]. and q Questions of the following 4 types:? 1 L r c-update A[k] with A[k]+c for all l≤k≤r
? 2 L R c-update a[k] with min{a[k],c} for all l≤k≤r;
? 3 L R c-update a[k] with max{a[k],c} for all l≤k≤r;
? 4 L R-ask for min{a[k]:l≤k≤r} and Max{a[k]:l≤k≤r}.

Input

The first line contains an integer T (no more than 5) which represents the number of test cases.

For each test case, the first line contains 2 integers n,q (1≤n,q≤200000).

The second line contains n integers a1,a2,..., an which indicates the initial values of the array (|ai|≤).

Each of the following Q lines contains an integer t which denotes the type of i-th question. If t=1,2,3, 3 integers l,r,c follows. If t=4, 2 integers l,r follows. (1≤ti≤4,1≤li≤ri≤n)

If T=1, |ci|≤2000;

If t=2,3, |ci|≤10^9.

Output

For each question of type 4, output of integers denote the minimum and the maximum.

Sample Input

1
1 1
1
4 1 1

Sample Output

1 1

Source
XTU Onlinejudge

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

XTU1238 Segment Tree (segment trees/interval max update)

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.