zoukankan      html  css  js  c++  java
  • kuangbin专题七 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.. 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



    线段树维护最大最小,不涉及更改,只用pushup query就可以了


     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 #include <string.h>
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <algorithm>
    12 #include <sstream>
    13 #include <stack>
    14 using namespace std;
    15 #define FO freopen("in.txt","r",stdin);
    16 #define rep(i,a,n) for (int i=a;i<n;i++)
    17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
    18 #define pb push_back
    19 #define mp make_pair
    20 #define all(x) (x).begin(),(x).end()
    21 #define fi first
    22 #define se second
    23 #define SZ(x) ((int)(x).size())
    24 #define debug(x) cout << "&&" << x << "&&" << endl;
    25 #define lowbit(x) (x&-x)
    26 #define mem(a,b) memset(a, b, sizeof(a));
    27 typedef vector<int> VI;
    28 typedef long long ll;
    29 typedef pair<int,int> PII;
    30 const ll mod=1000000007;
    31 const int inf = 0x3f3f3f3f;
    32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
    33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
    34 //head
    35 
    36 const int maxn=200010;
    37 int minn[maxn<<2],maxx[maxn<<2],n,q,a[maxn],maxpos,minpos;
    38 
    39 void pushup(int rt) {
    40     minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
    41     maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);
    42 }
    43 
    44 void build(int rt,int L,int R){
    45     minn[rt]=0;
    46     maxx[rt]=0;
    47     if(L==R) {
    48         scanf("%d",&a[rt]);
    49         minn[rt]=maxx[rt]=a[rt];
    50         return;
    51     }
    52     int mid=(L+R)>>1;
    53     build(rt<<1,L,mid);
    54     build(rt<<1|1,mid+1,R);
    55     pushup(rt);
    56 }
    57 
    58 void query(int rt,int L,int R,int l,int r) {
    59     if(L>=l&&R<=r) {
    60         minpos=min(minpos,minn[rt]);
    61         maxpos=max(maxpos,maxx[rt]);
    62         return;
    63     }
    64     int mid=(L+R)>>1;
    65     if(l<=mid) query(rt<<1,L,mid,l,r);
    66     if(r>mid) query(rt<<1|1,mid+1,R,l,r);
    67 }
    68 
    69 int main() {
    70     while(~scanf("%d%d",&n,&q)) {
    71         build(1,1,n);
    72         int l,r;
    73         while(q--) {
    74             maxpos=-1,minpos=inf;
    75             scanf("%d%d",&l,&r);
    76             query(1,1,n,l,r);
    77             printf("%d
    ",l==r?0:maxpos-minpos);
    78         }
    79     }
    80 }


  • 相关阅读:
    intel cpu 架构
    nginx-nagios-pnp4nagios-nrpe安装配置过程
    iostat 命令detail
    nagios环境
    编译安装PHP 5.3.13
    nagios支持PNP图形显示
    nagios配置安装
    在xp下禁用media sense
    linux awk常用命令【转载】
    sed命令解析[转载]
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9865238.html
Copyright © 2011-2022 走看看