线段树比大小
Language:
Balanced Lineup
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height. Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group. Input Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive. Output Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 Source #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[50000+5]; int n,q,w,e; int maxx,minn; struct seg { int l,r,ma,mi; }; seg tree[200000+5]; 利用递归形成树; void build(int s,int l,int r) { tree[s].l=l; tree[s].r=r; if(l==r) { tree[s].ma=a[l]; tree[s].mi=a[l]; return; } int m=(l+r)/2; build(s*2,l,m); build(s*2+1,m+1,r); tree[s].ma=max(tree[s*2].ma,tree[s*2+1].ma); tree[s].mi=min(tree[s*2].mi,tree[s*2+1].mi); }
void query(int s,int x,int y) { if(tree[s].l==x&&tree[s].r==y) { minn=min(minn,tree[s].mi); maxx=max(maxx,tree[s].ma); return; } int m=(tree[s].l+tree[s].r)/2; if(y<=m) { query(s*2,x,y); } else if(x>m) { query(s*2+1,x,y); } else { query(s*2,x,m); query(s*2+1,m+1,y); } }
|
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a[50000+5]; int n,q,w,e; int maxx,minn; struct seg { int l,r,ma,mi; }; seg tree[200000+5]; void build(int s,int l,int r) { tree[s].l=l; tree[s].r=r; if(l==r) { tree[s].ma=a[l]; tree[s].mi=a[l]; return; } int m=(l+r)/2; build(s*2,l,m); build(s*2+1,m+1,r); tree[s].ma=max(tree[s*2].ma,tree[s*2+1].ma); tree[s].mi=min(tree[s*2].mi,tree[s*2+1].mi); } void query(int s,int x,int y) { if(tree[s].l==x&&tree[s].r==y) { minn=min(minn,tree[s].mi); maxx=max(maxx,tree[s].ma); return; } int m=(tree[s].l+tree[s].r)/2; if(y<=m) { query(s*2,x,y); } else if(x>m) { query(s*2+1,x,y); } else { query(s*2,x,m); query(s*2+1,m+1,y); } } int main() { scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } build(1,1,n); for(int i=1;i<=q;i++) { scanf("%d%d",&w,&e); minn=1000000+10; maxx=0; query(1,w,e); printf("%d\n",maxx-minn); } return 0; }
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=50000+5; struct seg{ int l,r,ma,mi; }tree[maxn*3+1]; int a[maxn]; int maxx,minn; int n,q,a1,b; void build(int s,int l,int r){ tree[s].l=l; tree[s].r=r; if(l==r){ tree[s].ma=a[l]; tree[s].mi=a[l]; return; }else{ int mid=(tree[s].l+tree[s].r)/2; build(s*2,l,mid); build(s*2+1,mid+1,r); tree[s].ma=max(tree[s*2].ma,tree[s*2+1].ma); tree[s].mi=min(tree[s*2].mi,tree[s*2+1].mi); } } void query(int s,int l,int r){ if(tree[s].l==l&&tree[s].r==r){ maxx=max(maxx,tree[s].ma); minn=min(minn,tree[s].mi); return; }else{ int mid=(tree[s].l+tree[s].r)/2; if(l>mid){ query(s*2+1,l,r); }else if(r<=mid){ query(s*2,l,r); }else{ query(s*2,l,mid); query(s*2+1,mid+1,r); } } } int main(){ scanf("%d%d",&n,&q); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } build(1,1,n); while(q--){ scanf("%d%d",&a1,&b); minn=1000000+1000; maxx=-1; query(1,a1,b); printf("%d\n",maxx-minn); } return 0; }