Line Tree water problem #1077: RMQ problem-line tree

Source: Internet
Author: User

#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm>using namespace std; #define MAXN 1000000 + 10#define Lson L, Mid, Root<<1#define Rson mid+1, R, root&lt ; <1|1#define INF 0x7ffffffint min[maxn<<2], a[maxn];int num;void pushup (int root) {Min[root] = Min (Min[root&lt ; <1], min[root<<1|1]);}        void Bulid (int L, int R, int root) {if (L = = R) {Min[root] = A[++num];    return;    } int mid = (L + R) >> 1;    Bulid (Lson);    Bulid (Rson); Pushup (root);}        void Update (int q, int val, int L, int R, int root) {if (L = = R) {Min[root] = val;    return;    } int mid = (L + R) >> 1;    if (q <= mid) Update (Q, Val, Lson);    else Update (Q, Val, Rson); Pushup (root);}    int Query (int l, int r, int l, int r, int root) {if (l<=l && r>=r) {return min[root];    } int res = INF;    int mid = (L + R) >> 1; if (L <= mid) Res = Min (res, Query (l, R, Lson));    if (R > Mid) res = min (res, Query (l, R, Rson)); return res;}    int main () {int n, q;        while (~SCANF ("%d", &n)) {for (int i=1; i<=n; i++) scanf ("%d", &a[i]);        num = 0;        Bulid (1, N, 1);        scanf ("%d", &q);        int K1, K2, K3;            while (q--) {scanf ("%d%d%d", &k1, &k2, &AMP;K3);            if (K1) Update (K2, K3, 1, N, 1);        else printf ("%d\n", Query (K2, K3, 1, N, 1)); }} return 0;}

Line Tree water problem #1077: RMQ problem-line tree

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: 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.