poj-3264-balanced lineup-Single Point update

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=3264

This is a single-point update template problem, it is not explained in detail, hdu enemy soldiers in the problem I have a detailed explanation; Link: http://blog.csdn.net/wlxsq/article/details/46897219

#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue > #include <map> #include <stack> #include <set> #include <vector> #include <algorithm> #define LL Long longusing namespace std;/* This is a template problem for a single point update of a segment tree, but each node has two data, maximum and minimum values; */const int inf=0xffffff0;int minv,maxv; int q,n,a,b;struct node{int l,r,minv,maxv;} node[200010];void pushup (int rt,int v)//Update parent node; {Node[rt]. Minv=min (Node[rt].    MINV,V); NODE[RT]. Maxv=max (Node[rt]. MAXV,V);}    void build (int l,int R,int RT) {int mid= (L+R) >>1;    Node[rt].l=l;    Node[rt].r=r; NODE[RT].    Minv=inf; NODE[RT].    Maxv=-inf;    if (l==r) return;        else{build (l,mid,rt<<1);    Build (mid+1,r,rt<<1|1);    }}void Insert (int rt,int p,int v) {int l=node[rt].l;    int R=NODE[RT].R; if (l==r&&p==l) {Node[rt].        Minv=v; NODE[RT].        Maxv=v;    Return }//node[rt]. Minv=min (V,node[rt].    MINV); NODE[RT]. Maxv=max (V,NODE[RT].    MAXV);    int mid= (L+R) >>1;    if (p<=mid) Insert (RT&LT;&LT;1,P,V);    else Insert (RT&LT;&LT;1|1,P,V); Pushup (rt,v);} void query (int l,int r,int RT) {if (Node[rt]. MAXV&LT;=MAXV&AMP;&AMP;NODE[RT].        MINV&GT;=MINV) return; There is no point in searching down, you can exit directly, if (L==NODE[RT].L&AMP;&AMP;R==NODE[RT].R) {minv=min (Node[rt].        MINV,MINV); Maxv=max (Node[rt].        MAXV,MAXV);    Return    } int mid= (NODE[RT].L+NODE[RT].R) >>1;    if (r<=mid) query (l,r,rt<<1);    else if (l>mid) query (l,r,rt<<1|1);        else{query (l,mid,rt<<1);    Query (mid+1,r,rt<<1|1);        }}int Main () {while (~scanf ("%d%d", &n,&q) {build (1,n,1);            for (int i=1;i<=n;i++) {scanf ("%d", &a);        Insert (1,i,a);            } for (int i=1;i<=q;i++) {scanf ("%d%d", &a,&b);            Minv=inf;            Maxv=-inf;            Query (a,b,1);        printf ("%d\n", MAXV-MINV); }} return 0;} 


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

poj-3264-balanced lineup-Single Point 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.