POJ3264 Balanced Lineup

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
//	Accepted	4868K	3016MS	C++	2390B	2013-08-09 12:45:26
//  Accepted	5392K	4844MS	G++	2390B	2013-08-09 12:44:44

using namespace std;
const int maxn = 50100;
const int INF = 0x7fffffff;
struct Node {
    int Left, Right;
    int maxval, minxval;
    Node *LeftChild, *RightChild;
    Node() {
        maxval = -1;//保证插入时候更新不会出错。
        minxval = INF;
    }
};
int ql, qr;//全局变量,即每次插入或查询区间。
int Min, Max;//保存要查询区间的最值.
int n, m;
Node *root = NULL;

void Build(Node* cur, int L, int R) {
    cur->Left = L;
    cur->Right = R;
    if(L != R) {
        cur->LeftChild = new Node;
        cur->RightChild = new Node;
        Build(cur->LeftChild, L, (L+R)/2);
        Build(cur->RightChild, (L+R)/2+1, R);
    }
    else {//L == R
        cur->LeftChild = NULL;//没有左右孩子。
        cur->RightChild = NULL;
    }
}
//插入时候每次给ql赋值.
void update(Node* cur, int L, int R, int value) {
    if(L==R && L == ql) { //叶子节点。
        cur->maxval = value;
        cur->minxval = value;
        return;
    }
    Node* LC = cur->LeftChild;
    Node* RC = cur->RightChild;
    int M = (L+R)/2;
    if(ql <= M) update(LC, L, M, value);
    else update(RC, M+1, R, value);
    cur->maxval = max(LC->maxval, RC->maxval);
    cur->minxval = min(LC->minxval, RC->minxval);
}
//区间:[ql, qr], Min, Max 全局变量, 进行初始化。
void query(Node* cur, int L, int R) {
    if(ql <= L && R <= qr) {
        Min = min(Min, cur->minxval);
        Max = max(Max, cur->maxval);
        return;
    }
    Node* LC = cur->LeftChild;
    Node* RC = cur->RightChild;
    int M = (L+R)/2;
    if(ql <= M) query(LC, L, M);
    if(qr > M) query(RC, M+1, R);
    return;
}

void init(){
    int tmp;
    Max = -1;//littl, import;
    Min = 10000000;//big
    Build(root, 1, n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &tmp);
        ql = i;
        update(root, 1, n, tmp);
    }
}

int main()
{
    int start, endx;
    while(scanf("%d%d", &n, &m) != EOF) {
        root = new Node;
        init();
        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &start, &endx);
            ql = start, qr = endx;
            Max = -1, Min = INF;
            query(root, 1, n);
            int res = Max - Min;
            printf("%d\n", res);
        }
    }
    return 0;
}

你可能感兴趣的:(POJ3264 Balanced Lineup)