zoukankan      html  css  js  c++  java
  • poj3264 balanced lineup【线段树】

    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.. NQ+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

    两棵树分别保存区间最大最小值即可

    
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define inf 1e18
    using namespace std;
    
    int q, n;
    const int maxn = 50005;
    int cow[maxn], talltree[maxn << 2], shorttree[maxn<<2];
    
    void tall_pushup(int rt)//更新
    {
        talltree[rt] = max(talltree[rt << 1], talltree[rt << 1 | 1]);
    }
    
    void short_pushup(int rt)
    {
        shorttree[rt] = min(shorttree[rt<<1], shorttree[rt<<1|1]);
    }
    
    void tall_build(int l, int r, int rt)
    {
        if(l == r){
            talltree[rt] = cow[l];
            return;
        }
        int m = (l + r) >> 1;
        tall_build(l, m, rt << 1);
        tall_build(m + 1, r, rt << 1 | 1);
        tall_pushup(rt);
    }
    
    void short_build(int l, int r, int rt)
    {
        if(l == r){
            shorttree[rt] = cow[l];
            return;
        }
        int m = (l + r) >> 1;
        short_build(l, m, rt << 1);
        short_build(m + 1, r, rt << 1 | 1);
        short_pushup(rt);
    }
    
    /*void pushdown(int rt, int ln, int rn)
    {
        if(lazy[rt]){
            lazy[rt << 1] += lazy[rt];
            lazy[rt << 1 | 1] += lazy[rt];
            tree[rt << 1] += lazy[rt] * ln;
            tree[rt << 1 | 1] += lazy[rt] * rn;
            lazy[rt] = 0;
        }
    }
    
    void update(int L, int C, int l, int r, int rt)
    {
        if(l == r){
            tree[rt] += C;
            return;
        }
        int m = (l + r) >>1;
        if(L <= m) update(L, C, l, m, rt << 1);
        else update(L, C, m + 1, r, rt << 1 | 1);
        pushup(rt);
    }
    
    void update(int L, int R, int C, int l, int r, int rt)
    {
        if(L <= l && r <= R){//本区间完全在操作区间内
            tree[rt] += C * (r - l + 1);
            lazy[rt] += C;
            return;
        }
        int m = (l + r) >> 1;
        pushdown(rt, m - l + 1, r - m);
        if(L <= m) update(L, R, C, l, m, rt << 1);
        if(R > m) update(L, R, C, m + 1, r, rt << 1 | 1);
        pushup(rt);
    }*/
    
    int tall_query(int L, int R, int l, int r, int rt)
    {
        if(L <= l && r <= R){
            return talltree[rt];
        }
        int m = (l + r) >> 1;
        //pushdown(rt, m - l + 1, r - m);
    
        int ans = 0;
        if(L <= m) ans = max(ans, tall_query(L, R, l, m, rt << 1));
        if(R > m) ans = max(ans, tall_query(L, R, m + 1, r, rt << 1 | 1));
        return ans;
    }
    
    int short_query(int L, int R, int l, int r, int rt)
    {
        if(L <= l && r <= R){
            return shorttree[rt];
        }
        int m = (l + r) >> 1;
        //pushdown(rt, m - l + 1, r - m);
    
        int ans = inf;
        if(L <= m) ans = min(ans, short_query(L, R, l, m, rt << 1));
        if(R > m) ans = min(ans, short_query(L, R, m + 1, r, rt << 1 | 1));
        return ans;
    }
    
    
    int main()
    {
        while(scanf("%d%d", &n, &q) != EOF){
            for(int i = 1; i <= n; i++){
                scanf("%d", &cow[i]);
            }
            tall_build(1, n, 1);
            short_build(1, n, 1);
    
            while(q--){
                int a, b;
                scanf("%d%d", &a, &b);
                cout<<tall_query(a, b, 1, n, 1) - short_query(a, b, 1, n, 1)<<endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    c++ new 堆 栈
    c++ int 负数 补码 隐式类型转换
    python json 序列化
    %pylab ipython 中文
    matplotlib中什么是后端
    IPython 4.0发布:Jupyter和IPython分离后的首个版本
    ipython
    python 类
    python 高级特性
    windows网络模型
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643396.html
Copyright © 2011-2022 走看看