Balanced Lineup (POJ-3264) (segment tree)

Source: Internet
Author: User

A very basic line of the tree problem, there is a place to card me for a long time, I am the following code for the interval is left closed to open, so if the desired interval includes the interval endpoint, you need to be in the right interval +1

Segment tree is an efficient data structure, which is characterized by finding the minimum and maximum values in an interval. The data structure feels like a template, but the thought of it should be more worthy of our study, but now there are not a few people can calm down to study the principle of it.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < queue> #include <vector> #include <map>using namespace std;const int INF = 1000000+10;const int max_n =    50000+100;int n,m,a,b,q,dat[max_n*2-1],dat2[max_n*2-1];void init (int n_) {n = 1;    while (N<n_) n *= 2; for (int i=0;i<2*n-1;i++) dat[i] = INF;}    void Update (int k,int a) {k + = n-1;    Dat[k] = A;        while (k>0) {k= (k-1)/2;    Dat[k] = min (dat[k*2+1],dat[k*2+2]); }}void init2 (int n_) {for (int i=0;i<2*n-1;i++) dat2[i] =-100;}    void Update2 (int k,int a) {k + = n-1;    Dat2[k] = A;        while (k>0) {k = (k-1)/2;    Dat2[k] = max (dat2[k*2+1],dat2[k*2+2]); }}int query (int a,int b,int k,int l,int R) {if (r<=a| |    B&LT;=L) return INF;    if (a<=l&&r<=b) return dat[k];        else {int VL = query (A,b,k*2+1,l, (l+r)/2);        int VR = query (a,b,k*2+2, (l+r)/2,r);    return min (VL,VR); }}int Query2 (int A,int b,int K,int l,int R) {if (r<=a| |    B&LT;=L) return-1;    if (a<=l&&r<=b) return dat2[k];        else {int VL = Query2 (A,b,k*2+1,l, (l+r)/2);        int VR = Query2 (a,b,k*2+2, (l+r)/2,r);    Return Max (VL,VR);    }}int Main () {scanf ("%d%d", &m,&q);    Init (m);    Init2 (m);        for (int i=1;i<=m;i++) {scanf ("%d", &a);        Update (I,A);    Update2 (I,a);    } int maxn = 1000000000;        while (q--) {scanf ("%d%d", &a,&b);        int v = query (a,b+1,0,0,n);        int u = query2 (a,b+1,0,0,n);    printf ("%d\n", u-v); } return 0;}


Balanced Lineup (POJ-3264) (segment 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: 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.