POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 53721   Accepted: 25244
Case Time Limit: 2000MS

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 ≤ ABN), 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

 

题意就是在一堆数里找一定范围里的最大值和最小值,计算差值。

因为知道这个题肯定不能瞎写就写对,所以还很认真的写了一个代码,真是智障,不管怎么写,反正T了。

线段树,不会用,瞎写。。。

线段树大法好,可惜智障,改了12遍(此刻内心¥…&%……*(*&),出现各种问题嘛,存最小值要再有东西存人家啊。。。

最后实在改不出来了,求助了大佬,最后问题还是出在最小值问题上,要求最小值,一开始比较的ans就要比数组里的最大数要大啊。。。要审清题和题意。。。

线段树!!!

 

代码(垃圾写的乱七八糟):

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
#define N 300000
struct node{
    int left,right,maxx,minn;
}tree[N*4];
int n,m,minn,maxx;
void build(int l,int r,int pos){
    tree[pos].left=l;
    tree[pos].right=r;
    tree[pos].maxx=0;
    tree[pos].minn=1e9;
    if(tree[pos].left==tree[pos].right)return;
    int mid=(l+r)>>1;
    build(l,mid,pos*2);
    build(mid+1,r,pos*2+1);
}
void update(int x,int y,int pos){
    if(tree[pos].left==x&&tree[pos].right==x){
        tree[pos].maxx=y;
        tree[pos].minn=y;
        return;
    }
    int mid=(tree[pos].left+tree[pos].right)>>1;
    if(x>mid)
        update(x,y,pos*2+1);
    else
        update(x,y,pos*2);
  tree[pos].maxx=max(tree[pos*2].maxx,tree[pos*2+1].maxx);
  tree[pos].minn=min(tree[pos*2].minn,tree[pos*2+1].minn);
}
void query(int x,int y,int pos){
    if(tree[pos].left==x&&tree[pos].right==y){
        maxx=max(maxx,tree[pos].maxx);
        minn=min(minn,tree[pos].minn);
        return ;
    }
    int mid=(tree[pos].left+tree[pos].right)>>1;
    if(y<=mid)query(x,y,pos*2);
    else if(x>mid)query(x,y,pos*2+1);
    else{
        query(x,mid,pos*2);
        query(mid+1,y,pos*2+1);
    }
}
int main(){
    int m,n;
    int ans1,ans2;
    while(~scanf("%d%d",&n,&m)){
        getchar();
        build(1,n,1);
        int x;
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            update(i,x,1);
        }
        int a,b;
        while(m--){
            scanf("%d%d",&a,&b);
                maxx=0;
                minn=1e9;
                query(a,b,1);
                printf("%d\n",maxx-minn);
        }
    }
    return 0;
}

 

(╯°Д°)╯︵┻━┻

 

posted @ 2017-07-07 23:43  ZERO-  阅读(370)  评论(8编辑  收藏  举报