Segment Tree (interval max query and point modification)--I. Hate It (HDU 1754)

Source: Internet
Author: User

Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1754

Analysis && solving: Direct set template, you need to be aware of the C input. (t^t,c++ input Direct T Super 3000,c input Instant 800+, non-human)

1. Segment Tree Standard preprocessing:

#define LC (D<<1)
#define RC (d<<1|1)
#define MID (L+r >>1)

2. Achievements:

const int Max = 200005;
int A[max];
struct Tr
{
    int mx;
} TR[MAX*3];
void Push (int root)
{
    tr[root].mx = max (tr[lc].mx, tr[rc].mx);
}
void build (int root, int l, int r)
{
    if (L = = r)//gets to the bottom child node
    {
        tr[root].mx = a[l]; Assigns a value directly from the array
        retur n;
    }
    Build (LC, L, mid);
    Build (RC, mid+1, R);
    Push (root);
}

2. Query Maximum value:

int query (int root,  int L,  int r,  int L,  int R)//l,r indicates the query interval
{
    if (l==l && r = = r)
    {
  return tr[root].mx;
    }

    if (R <= mid)
        return query (LC, L, Mid, L, R);
    else if (L > Mid)
        return query (RC, mid+1, R, L, R);
    else
        return max (query (LC, L, Mid, L, mid), query (RC, mid+1, R, Mid+1, R));
}

3. Point modification:

void update (int root,  int L,  int r,  int loc,  int value)
{
    if (L = = r)
    {
        tr[d]. mx = value;
        return;
    }
    if (Loc <= mid) Update (LC, L, Mid, Loc, k);
    else  update (RC, mid+1, R, loc, K);
    Push (root);
}
AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace St

D

#define LC (D&LT;&LT;1) #define RC (d<<1|1) #define MID (L+r >>1) const int Max = 200005;

int A[max]; struct Tr {int mx;}

TR[MAX*3];

void Push (int d) {tr[d].mx = max (tr[lc].mx, tr[rc].mx);}
        void build (int d, int l, int r) {if (L = = r) {tr[d].mx = a[l];
    return;
    } build (LC, L, mid);
    Build (RC, mid+1, R);
Push (d); } int query (int d, int l, int r, int l, int r) {if (l==l && r = r) {//cout << ":" &
        Lt;<d <<endl;
    return tr[d].mx;
    } if (R <= mid) return query (LC, L, Mid, L, R);
    else if (L > Mid) return query (RC, mid+1, R, L, R);
else return Max (query (LC, L, Mid, L, mid), query (RC, mid+1, R, Mid+1, R)); } void Update (int d, int l, int r, int loc, int k) {if (L = = r) {TR[D].Mx = k;
    return;
    } if (Loc <= mid) Update (LC, L, Mid, Loc, k);
    else update (RC, mid+1, R, loc, K);
Push (d);
    } int main () {int n,m;
        while (~SCANF ("%d%d", &n,&m)) {memset (tr, 0, sizeof (TR));
        for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);
        } build (1, 1, N);
        Char c[2];
            while (m--) {int A, B;
            scanf ("%s%d%d", c,&a,&b);
            if (c[0]== ' U ') update (1,1,N,A,B);
        if (c[0]== ' Q ') printf ("%d\n", Query (1,1,n,a,b));
 }
    }
}

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.