zoukankan      html  css  js  c++  java
  • POJ3264 Balanced Lineup 【线段树】+【单点更新】

    Balanced Lineup
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 32778   Accepted: 15425
    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 ≤ 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

    2014-9-4 12:07:18更新:

    #include <stdio.h>
    #include <algorithm>
    #define inf 0x7fffffff
    #define maxn 50002
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    using namespace std;
    
    struct Node{
    	int maxv, minv;
    } tree[maxn << 2];
    int arr[maxn], minv, maxv;
    
    void pushUp(int rt){
    	tree[rt].maxv = max(tree[rt << 1].maxv, tree[rt << 1 | 1].maxv);
    	tree[rt].minv = min(tree[rt << 1].minv, tree[rt << 1 | 1].minv);
    }
    
    void build(int l, int r, int rt)
    {
    	if(l == r){
    		tree[rt].maxv = tree[rt].minv = arr[l];
    		return;
    	}
    	int mid = (l + r) >> 1;
    	build(lson); build(rson);
    	pushUp(rt);
    }
    
    void query(int left, int right, int l, int r, int rt)
    {
    	if(left == l && right == r){
    		maxv = max(maxv, tree[rt].maxv);
    		minv = min(minv, tree[rt].minv);
    		return;
    	}
    	int mid = (l + r) >> 1;
    	if(right <= mid) return query(left, right, lson);
    	else if(left > mid) return query(left, right, rson);
    	query(left, mid, lson); query(mid + 1, right, rson);
    }
    
    int main()
    {
    	int n, m, i, a, b;
    	while(scanf("%d%d", &n, &m) == 2){
    		for(i = 1; i <= n; ++i)
    			scanf("%d", &arr[i]);
    		build(1, n, 1);
    		while(m--){
    			scanf("%d%d", &a, &b);
    			minv = inf; maxv = 0;
    			query(a, b, 1, n, 1);
    			printf("%d
    ", maxv - minv);
    		}
    	}
    	return 0;
    }


    #include <stdio.h>
    #define maxn 200002
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    
    struct Node{
    	int min, max;
    } tree[maxn << 2];
    int maxAns, minAns;
    
    int maxVal(int a, int b)
    {
    	return a > b ?

    a : b; } int minVal(int a, int b) { return a < b ? a : b; } void build(int l, int r, int rt) { if(l == r){ scanf("%d", &tree[rt].min); tree[rt].max = tree[rt].min; return; } int mid = (l + r) >> 1; build(lson); build(rson); tree[rt].max = maxVal(tree[rt << 1].max, tree[rt << 1 | 1].max); tree[rt].min = minVal(tree[rt << 1].min, tree[rt << 1 | 1].min); } void query(int left, int right, int l, int r, int rt) { if(left == l && right == r){ if(tree[rt].max > maxAns) maxAns = tree[rt].max; if(minAns > tree[rt].min) minAns = tree[rt].min; return; } int mid = (l + r) >> 1; if(right <= mid) query(left, right, lson); else if(left > mid) query(left, right, rson); else{ query(left, mid, lson); query(mid + 1, right, rson); } } int main() { int n, q, i, a, b; scanf("%d%d", &n, &q); build(1, n, 1); while(q--){ scanf("%d%d", &a, &b); maxAns = 1; minAns = 1000000; query(a, b, 1, n, 1); printf("%d ", maxAns - minAns); } return 0; }



  • 相关阅读:
    P1197 [JSOI2008]星球大战[并查集+图论]
    P1955 [NOI2015]程序自动分析[离散化+并查集]
    取模运算律[简单数学]
    P1462 通往奥格瑞玛的道路[最短路+二分+堆优化]
    P1330 封锁阳光大学[搜索+染色]
    P1168 中位数[堆 优先队列]
    P2661 信息传递[最小环+边带权并查集]
    P1080 【NOIP 2012】 国王游戏[贪心+高精度]
    P2085 最小函数值[优先队列]
    【转】priority_queue的用法
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6752973.html
Copyright © 2011-2022 走看看