Hihocoder #1077: RMQ Problem-segment Tree (segment tree: structure build + update leaves upwards + query + cleverly use father[]+ segment tree array to open 4 times times * "Template")

Source: Internet
Author: User

#1077: RMQ Problem-line tree time limit:10000msSingle Point time limit:1000msMemory Limit:256MBDescribe

Last said: Little hi to small ho out of such a problem: assume the entire shelf from left to right placed N products, and sequentially labeled 1 to N, each small hi gave a section of the interval [L, R], small ho to do is to select the label in this range of all goods weight of the lightest one, And tell little hi the weight of this product. But in the process, the weight of the goods in certain locations may change (for example, the replacement of other kinds of goods) because of the behavior of others.

Little Ho proposed two very simple methods, but none of them could be solved perfectly. So this time, in the face of a bigger data size, what will little ho be good for?

Hint: actually just less than St to calculate some of the interval only

Input

Each test point (input file) has and has only one set of test data.

The 1th behavior of each set of test data is an integer n, meaning as described earlier.

The 2nd behavior of each group of test data n integers, respectively, describes the weight of each commodity, where the I integer represents the weight of the commodity labeled I weight_i.

The 3rd behavior of each set of test data is an integer q, which indicates the sum of the number of times that a small hi has been queried and the number of times the weight of the product has been changed.

Each group of test data n+4~n+q+3 line, each line describes an operation, each line begins with a number belonging to 0 or 1, respectively, indicating that the line describes a query and describe the weight of a product change two cases. For line n+i+3, if the line describes a query, then the next two integers are Li, RI, which represents a range of small hi queries [Li, Ri]; If the line describes a change in the weight of a product, then two integer pi,wi, indicating that the weight of the product with the position number pi is changed to Wi

For 100% of data, meet N<=10^6,q<=10^6, 1<=li<=ri<=n,1<=pi<=n, 0<weight_i, wi<=10^4.

Output

For each set of test data, for each small hi query, according to the order in which they appear in the input, each output line represents the result: the weight of the lightest item in all the items in the interval [Li, Ri].

Sample input
103655 5246 8991 5933 7474 7603 6098 6654 2414 884 60 4 90 2 101 4 70090 5 61 3 79491 3 1227
Sample output
24148847474

The basic problem of line segment tree is the data structure realization of segment tree. Don't talk about algorithms
Code:
#include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include <    String> #include <algorithm>using namespace std;struct node{int ll; int RR; int mm;}    Q[4000010];int fa[1000002];void build_st (int i, int ll, int rr) {q[i].ll=ll;    Q[I].RR=RR;    q[i].mm=210000000;    if (LL = = RR)//{FA[LL] = i;//return;    } build_st (I*2, LL, (LL+RR)/2); Build_st (I*2+1, (LL+RR)/2+1, RR);} void update_st (int ri)//bottom-up update input is the number of the underlying node {if (ri==1) {return;//indicates that the underlying node is the root node modification is complete return}//If not the root node has been Modify until the root node int fi is modified;    FI=RI/2;    q[fi].mm = min (q[fi*2].mm, q[fi*2+1].mm); Update_st (RI/2);}  int min;void query (int i, int ll, int rr) {if (q[i].ll==ll && q[i].rr==rr)//{min= min (min, q[i].mm        );    return;    } i=i<<1;        if (LL&LT;=Q[I].RR) {if (RR&LT;=Q[I].RR) {query (I, LL, RR); } else {query (I, ll, Q[I].RR);    }} i=i+1;        if (rr>=q[i].ll) {if (ll>=q[i].ll) {query (I, LL, RR);        } else {query (I, Q[I].LL, RR);    }}}int Main () {int n, m;    scanf ("%d", &n);    Build_st (1, 1, n);//int i, J;    int DD;        for (I=1; i<=n; i++) {scanf ("%d", &AMP;DD);        Q[FA[I]].MM = DD;    Update_st (Fa[i]);    } scanf ("%d", &m);    int POS, num;    int left, right;        for (j=0; j<m; J + +) {scanf ("%d", &AMP;DD);            if (dd==0)//xun Wen {scanf ("%d%d", &left, &right);            min=210000000;            Query (1, left, right);        printf ("%d\n", Min); } else if (dd==1) {scanf ("%d%d", &pos, &num);//For the cause of the problem, it is certain that the information of a leaf node is modified Q[FA [POS]].            Mm=num;        Update_st (Fa[pos]); }} return 0;}

Hihocoder #1077: RMQ Problem-segment Tree (segment tree: structure build + update leaves upwards + query + cleverly use father[]+ segment tree array to open 4 times times * "Template")

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.