ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1001 [USACO 2007 Jan S]Balanced Lineup 线段树-最大最小值

2022-08-07 18:33:00  阅读:147  来源: 互联网

标签:int cin USACO height Jan tr1 2007 cows tr2


 链接:https://ac.nowcoder.com/acm/contest/26896/1001
来源:牛客网

题目描述

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 100,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 ≤ 30) 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.

输入描述:

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.

输出描述:

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.
示例1

输入

复制
6 3
1
7
3
4
2
5
1 5
4 6
2 2

输出

复制
6
3
0

分析

易错点:这种两个小于号的写法,是所求区间不变,查询区间挨个变小,比较的时候也要用查询区间和所求区间比较,不然很容易出bug。。

//-------------------------代码----------------------------

//#define int ll
const int N = 1e5+10;
int n,m;
int a[N];

int tr1[4*N];
int tr2[4*N];

void init(int p,int l,int r) {
    if(l == r)  {
        tr1[p] = a[l];
        tr2[p] = a[l];
        rt;
    }
    int mid = l + r >>1;
    init(2 * p,l,mid);
    init(2 * p + 1,mid + 1,r);
    tr1[p] = max(tr1[p * 2],tr1[p * 2 + 1]); 
    tr2[p] = min(tr2[p * 2],tr2[p * 2 + 1]);
    
}

int calc(int p,int x,int y,int l,int r,bool q) {
    if(x <= l && r <= y ) {
        if(q == 1)return tr1[p];
        else return tr2[p];
    } 
    int mid = l + r >> 1;
    int mx = 0;
    int mn = 1e9;
    if(x <= mid) {
        if(q)mx = max(mx , calc(p << 1, x, y, l, mid,1));
        if(!q)mn = min(mn , calc(p << 1, x, y, l, mid,0));
    }
    if(y > mid ) {
        if(q)mx = max(mx , calc(p << 1 | 1, x, y, mid + 1, r,1));
        if(!q)mn = min(mn , calc(p << 1 | 1, x, y, mid + 1, r,0));
    }
    if(q == 1) return mx;
    else return mn;
}


void solve()
{
//    cin>>n>>m;
    cin>>n>>m;
    fo(i,1,n) {
        cin>>a[i];
    }
    init(1,1,n);
    fo(i,1,m) {
        int l,r;
        cin>>l>>r;
        cout<<calc(1,l,r,1,n,1) - calc(1,l,r,1,n,0)<<endl;
    }
}
void main_init() {}
signed main(){
    AC();clapping();TLE;
    cout<<fixed<<setprecision(12);
    main_init();
//  while(cin>>n,n)
//  while(cin>>n>>m,n,m)
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

标签:int,cin,USACO,height,Jan,tr1,2007,cows,tr2
来源: https://www.cnblogs.com/er007/p/16559578.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有