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
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
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6 3 0
报告:
题目大意: 有一排高度不一的牛(...),现在给出它们的高度,给几个区间求区间内的最高的牛和最矮的牛的“身高差”。
题解:
自己写的时候是用结构体记录各个区间内的最大值和最小值,然后返回查询的区间所求的最大值和最小值的差。样例过了,但是其他的却WA了。。表示很奇怪,现在写博客才想起来我这一点还没想明白,就直接去看题解了........那先把这个只过得了样例的程序传了吧。
.......
结果去调试发现连样例都没有过,于是分析了一下,这样写的话,如果查询区间在以分的区间两端,就只会直接返回一段的差值了。代码如下:
1 #include<iostream> 2 #include<cstdio> 3 #define l(u) (u<<1) 4 #define r(u) (u<<1|1) 5 using namespace std; 6 int n,q,highest,lowest; 7 int cow[50005]; 8 struct pp{ 9 int l,r,mins,maxs; 10 }; 11 pp node[200005]; 12 int min(int a,int b) 13 { 14 return a<b?a:b; 15 } 16 int max(int a,int b) 17 { 18 return a>b?a:b; 19 } 20 void build(int u,int left,int right) 21 { 22 node[u].l=left;node[u].r=right; 23 if (node[u].l==node[u].r) 24 { 25 node[u].mins=cow[left]; 26 node[u].maxs=cow[left]; 27 return ; 28 } 29 int mid=(left+right)>>1; 30 build(l(u),left,mid); 31 build(r(u),mid+1,right); 32 node[u].mins=min(node[l(u)].mins,node[r(u)].mins); 33 node[u].maxs=max(node[l(u)].maxs,node[r(u)].maxs); 34 } 35 int query(int u,int left,int right) 36 { 37 if (node[u].l==left&&node[u].r==right) 38 { 39 return node[u].maxs-node[u].mins; 40 } 41 int mid=(node[u].l+node[u].r)>>1; 42 if (right<=mid) query(l(u),left,right); 43 else if (left>mid) query(r(u),left,right); 44 else 45 { 46 query(l(u),left,mid); 47 query(r(u),mid+1,right); 48 } 49 } 50 int main() 51 { 52 freopen("balance.in","r",stdin); 53 cin>>n>>q; 54 for(int i=1;i<=n;i++) 55 scanf("%d",&cow[i]); 56 build(1,1,n); 57 for(int i=1;i<=q;i++) 58 { 59 int a,b; 60 //highest=0;//*** 61 //lowest=1234567;//*** 62 scanf("%d%d",&a,&b); 63 printf("%d\n",query(1,a,b)); 64 } 65 return 0; 66 }
正确的代码中是用 high 和low 来记录比较最大值和最小值并且只是 void 并不 返回值,这样一来就可以得到正确的答案了。
代码如下:
1 #include<iostream> 2 #include<cstdio> 3 #define l(u) (u<<1) 4 #define r(u) (u<<1|1) 5 using namespace std; 6 int n,q,highest,lowest; 7 int cow[50005]; 8 struct pp{ 9 int l,r,mins,maxs; 10 }; 11 pp node[200005]; 12 int min(int a,int b) 13 { 14 return a<b?a:b; 15 } 16 int max(int a,int b) 17 { 18 return a>b?a:b; 19 } 20 void build(int u,int left,int right) 21 { 22 node[u].l=left;node[u].r=right; 23 if (node[u].l==node[u].r) 24 { 25 node[u].mins=cow[left]; 26 node[u].maxs=cow[left]; 27 return ; 28 } 29 int mid=(left+right)>>1; 30 build(l(u),left,mid); 31 build(r(u),mid+1,right); 32 node[u].mins=min(node[l(u)].mins,node[r(u)].mins); 33 node[u].maxs=max(node[l(u)].maxs,node[r(u)].maxs); 34 } 35 void query(int u,int left,int right) 36 { 37 if (node[u].l==left&&node[u].r==right) 38 { 39 lowest=min(lowest,node[u].mins);//*** 40 highest=max(highest,node[u].maxs);///*** 41 return ; 42 } 43 int mid=(node[u].l+node[u].r)>>1; 44 if (right<=mid) query(l(u),left,right); 45 else if (left>mid) query(r(u),left,right); 46 else 47 { 48 query(l(u),left,mid); 49 query(r(u),mid+1,right); 50 } 51 } 52 int main() 53 { 54 //freopen("balance.in","r",stdin); 55 cin>>n>>q; 56 for(int i=1;i<=n;i++) 57 scanf("%d",&cow[i]); 58 build(1,1,n); 59 for(int i=1;i<=q;i++) 60 { 61 int a,b; 62 highest=0;//*** 63 lowest=1234567;//*** 64 scanf("%d%d",&a,&b); 65 query(1,a,b); 66 printf("%d\n",highest-lowest); 67 } 68 return 0; 69 }
注意事项:
1、定初值。