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<<1,P,V); else Insert (RT<<1|1,P,V); Pushup (rt,v);} void query (int l,int r,int RT) {if (Node[rt]. MAXV<=MAXV&&NODE[RT]. MINV>=MINV) return; There is no point in searching down, you can exit directly, if (L==NODE[RT].L&&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