G - Balanced Lineup

G - Balanced Lineup

POJ - 3264

思路:水题,线段树的基本操作即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 50010
using namespace std;
int n,q;
struct  nond{
    int l,r,min,max;
}tree[MAXN*4];
void up(int now){
    tree[now].max=max(tree[now*2].max,tree[now*2+1].max);
    tree[now].min=min(tree[now*2].min,tree[now*2+1].min);
    return ;
}
void build(int now,int l,int r){
    tree[now].l=l;tree[now].r=r;
    if(tree[now].l==tree[now].r){
        scanf("%d",&tree[now].max);
        tree[now].min=tree[now].max;
        return ;
    }
    int mid=(tree[now].l+tree[now].r)/2;
    build(now*2,l,mid);
    build(now*2+1,mid+1,r);
    up(now);
}
int querymax(int now,int l,int r){
    if(tree[now].l==l&&tree[now].r==r)
        return  tree[now].max;
    int mid=(tree[now].l+tree[now].r)/2;
    if(r<=mid)    return querymax(now*2,l,r);
    else if(l>mid)    return querymax(now*2+1,l,r);
    else    return max(querymax(now*2,l,mid),querymax(now*2+1,mid+1,r));
}
int querymin(int now,int l,int r){
    if(tree[now].l==l&&tree[now].r==r)
        return tree[now].min;
    int mid=(tree[now].l+tree[now].r)/2;
    if(r<=mid)    return querymin(now*2,l,r);
    else if(l>mid)    return querymin(now*2+1,l,r);
    else return min(querymin(now*2,l,mid),querymin(now*2+1,mid+1,r));
}
int main(){
    scanf("%d%d",&n,&q);
    build(1,1,n);
    for(int i=1;i<=q;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        printf("%d\n",querymax(1,x,y)-querymin(1,x,y));
    }
}

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8468584.html

时间: 2024-08-03 10:43:47

G - Balanced Lineup的相关文章

G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)

G - Balanced Lineup Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3264 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 de

G - Balanced Lineup - poj3264(区间查询)

题意:给你一组值,然后询问某个区间的最大值和最小值得差 分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了 ****************************************************************** #include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;#define Ls

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 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 Joh

BZOJ1636: [Usaco2007 Jan]Balanced Lineup

1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 476  Solved: 345[Submit][Status] 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 deci

poj 3274 -- Gold Balanced Lineup

Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12110   Accepted: 3553 Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared

BZOJ1699: [Usaco2007 Jan]Balanced Lineup排队

1699: [Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 933  Solved: 568[Submit][Status] Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. Jo

[Usaco2007 Jan]Balanced Lineup排队

[Usaco2007 Jan]Balanced Lineup排队 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 2333 Solved: 1424 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q &l

poj 3264 Balanced Lineup RMQ线段树实现

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36613   Accepted: 17141 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 Joh

bzoj 1637: [Usaco2007 Mar]Balanced Lineup

1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000)和种族(0或1). 一直以来 Farmer John 总是喜欢做一些非凡的事,当然这次照相也不例外.他只给一部分牛照相,并且这一组牛的阵容必须是"