poj 3264 Balanced Lineup
2013-08-14 16:31
246 查看
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
Sample Output
Source
第二个线段树的题
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 28851 | Accepted: 13593 | |
Case Time Limit: 2000MS |
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<stdio.h> #define INF 0x3f3f3f3f int mmax,mmin; int Max(int a,int b) { return a>b?a:b; } int Min(int a,int b) { return a<b?a:b; } struct node { int l; int r; int max; int min; }p[800005]; void build_T(int t,int l,int r) { p[t].l=l; p[t].r=r; p[t].max=0; p[t].min=0; if(r==l) return; int temp=t<<1; int mid=(l+r)>>1; build_T(temp,l,mid); build_T(temp+1,mid+1,r); } void insert(int t,int l,int val) { if(p[t].l==p[t].r) { p[t].max=val; p[t].min=val; return ; } int temp=t<<1; int mid=(p[t].l+p[t].r)>>1; if(l<=mid) insert(temp,l,val); else insert(temp+1,l,val); p[t].max=Max(p[temp].max,p[temp+1].max); p[t].min=Min(p[temp].min,p[temp+1].min); } void query(int t,int l,int r) { if(p[t].r==r&&p[t].l==l) { mmax=Max(mmax,p[t].max); mmin=Min(mmin,p[t].min); return; } int temp=t<<1; int mid=(p[t].l+p[t].r)>>1; if(r<=mid) query(temp,l,r); else if(l>mid) query(temp+1,l,r); else { query(temp,l,mid); query(temp+1,mid+1,r); } } int main() { int i,m,x,y,n; //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d %d",&n,&m)!=EOF) { build_T(1,1,n); for(i=1;i<=n;i++) { scanf("%d",&x); insert(1,i,x); } while(m--) { scanf("%d %d",&x,&y); mmax=-INF; mmin=INF; query(1,x,y); printf("%d\n",mmax-mmin); } } return 0; }
相关文章推荐
- poj 3264 Balanced Lineup(基础线段树)
- POJ—3264(Balanced Lineup)
- poj 3264 Balanced Lineup
- poj 3264 Balanced Lineup
- POJ 3264 Balanced Lineup
- POJ 3264 Balanced Lineup (RMQ线段树)
- poj 3264 -Balanced Lineup (RMQ与线段树两种做法)
- POJ 3264 Balanced Lineup 线段树入门(点的查找)
- poj 3264 Balanced Lineup
- POJ 3264 Balanced Lineup(简单的RMQ)
- POJ3264 Balanced Lineup 线段树基础
- POJ 3264: Balanced Lineup
- poj_3264_Balanced Lineup(线段树)
- POJ 3264 Balanced Lineup (区间最值 RMQ模板/线段树)
- POJ 3264 Balanced Lineup (RMQ)
- POJ 3264 Balanced Lineup (RMQ)
- (简单) POJ 3264 Balanced Lineup,RMQ。
- poj_3264 Balanced Lineup 线段树+点修改/RMQ
- POJ 3264 Balanced Lineup
- POJ 3264 Balanced Lineup【简单线段树,区间最值,无更新】