Balanced Lineup(线段树之区间查找最大最小值)

传送门

线段树的区间查找最大最小值模板.

裸的线段树

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <queue>
#include <vector>
#include <cstdlib>
#include <algorithm>

#define ls u << 1
#define rs u << 1 | 1
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define INF 0x3f3f3f3f

using namespace std;
typedef long long ll;
const int M = 5e4 + 100;
int Max[M << 2],Min[M << 2];

void pushup(int u){
    Max[u] = max(Max[ls],Max[rs]);
    Min[u] = min(Min[ls],Min[rs]);
}

void build(int l,int r,int u){
    if(l == r){
        scanf("%d",&Max[u]);
        Min[u] = Max[u];
    }
    else {
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
        pushup(u);
    }
}

int query(int L,int R,int l,int r,int u,int flag){ //区间查找最大最小值
    int mid = (l + r) >> 1;
    if(L <= l && R >= r){
        return flag ? Max[u] : Min[u];
    }
    int res;
    if(flag){
        res = 0;
        if(L <= mid) res = max(query(L,R,lson,flag),res);
        if(R > mid) res = max(query(L,R,rson,flag),res);
    }
    else {
        res = INF;
        if(L <= mid) res = min(query(L,R,lson,flag),res);
        if(R > mid) res = min(query(L,R,rson,flag),res);
    }
    return res;
}

int main(){
    int n,m;
    cin>>n>>m;
    build(1,n,1);
    while(m--){
        int x,y;
        scanf("%d %d",&x,&y);
        printf("%d\n",query(x,y,1,n,1,1) - query(x,y,1,n,1,0));
    }
    return 0;
}
时间: 2024-12-18 03:25:58

Balanced Lineup(线段树之区间查找最大最小值)的相关文章

【POJ】3264 Balanced Lineup ——线段树 区间最值

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

poj 3264 Balanced Lineup(线段数求区间最大最小值)

链接:http://poj.org/problem?id=3264 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32772   Accepted: 15421 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.

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 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 de

线段树——快速区间查找

线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点.    使用线段树可以快速的查找某一个节点在若干条线段中出现的次数,时间复杂度为O(logN).而未优化的空间复杂度为2N,因此有时需要离散化让空间压缩. #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #

POJ-3264 Balanced Lineup(线段树)

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

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

POJ 3264 Balanced Lineup (线段树)

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

POJ 3264 Balanced Lineup 线段树 第三题

Balanced Lineup 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

nyoj1185 最大最小值 (线段树求区间最大值和最小值)

对于不懂线段树的,先看为这篇文章理解下.点击打开链接 这道题普通方法 ,TLE. 最大最小值 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给出N个整数,执行M次询问. 对于每次询问,首先输入三个整数C.L.R: 如果C等于1,输出第L个数到第R个数之间的最小值: 如果C等于2,输出第L个数到第R个数之间的最大值: 如果C等于3,输出第L个数到第R个数之间的最小值与最大值的和. (包括第L个数和第R个数). 输入 首先输入一个整数T(T≤100),表示有T组数据.