poj 3264Balanced Lineup

poj 3264Balanced Lineup

题意:求 一段 区间 的 最大值和最小值 的差值

题解:线段树

碎碎念:某种意义上说,第一道自己手写的线段树,总之蛮好~

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;
const int MAXN = 50000+10;
const int INF =  1000000;
int arr[MAXN];
int segx[MAXN<<2];
int segi[MAXN<<2];
void build(int l,int r,int x){
    if(l==r){
        segx[x] = arr[l];
        segi[x] = arr[l];
        return;
    }
    int mid = (l+r)>>1;
    build(l,mid,x<<1);
    build(mid+1,r,x<<1|1);
    segx[x]= max(segx[x<<1],segx[x<<1|1]);
    segi[x]= min(segi[x<<1],segi[x<<1|1]);
    return;
}
int mi,mx;
void quercy(int l,int r,int x,int L,int R){
    if(l>=L && r<=R){
        mi = min(mi,segi[x]);
        mx = max(mx,segx[x]);
        return;
    }
    int mid = (l+r)>>1;
    if(mid>=L){
        quercy(l,mid,x<<1,L,R);
    }
    if(mid<R){
        quercy(mid+1,r,x<<1|1,L,R);
    }
}
int main(){
//    freopen("input.txt","r",stdin);
    int n,q;
    while(~scanf("%d%d",&n,&q)){
        for(int i = 1;i<=n;i++){
            scanf("%d",&arr[i]);
        }
        build(1,n,1);
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            mi = INF;
            mx = 1;
            quercy(1,n,1,l,r);
            printf("%d\n",mx-mi);
        }
    }
    return 0;
}

题解2:平方分割

分桶法:把一排物品或平面分成桶,每个桶维护自己的信息

把一段区间 维护成 一个桶,然后求其最值

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;
const int MAXN = 50000+10;
const int INF =  1000000;
int arr[MAXN];
const int sz = 200;
int bucket[260][sz];
int MX[260];
int MI[260];
int main(){
//    freopen("input.txt","r",stdin);
    int n,q;
    while(~scanf("%d%d",&n,&q)){
        fill(MI,MI+260,INF);
        fill(MX,MX+260,1);
        for(int i = 1;i<=n;i++){
            int x = i/sz;
            int y = i%sz;
            scanf("%d",&bucket[x][y]);
            MI[x] = min(MI[x],bucket[x][y]);
            MX[x] = max(MX[x],bucket[x][y]);
        }
        bucket[0][0] = bucket[0][1];
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            int mi = INF;
            int mx = 1;
            int x1 = l/sz;
            int y1= l%sz;
            int x2 = r/sz;
            int y2 = r%sz;
            if(x1==x2){
                for(int i = y1;i<=y2;i++){
                    mi = min(bucket[x1][i],mi);
                    mx = max(bucket[x1][i],mx);
                }
                printf("%d\n",mx-mi);
                continue;
            }
            for(int i = y1;i < sz;i++){
                mi = min(bucket[x1][i],mi);
                mx = max(bucket[x1][i],mx);
            }
            for(int i = 0;i<=y2;i++){
                mi = min(bucket[x2][i],mi);
                mx = max(bucket[x2][i],mx);
            }
            for(int i=x1+1;i<x2;i++){
                mi = min(MI[i],mi);
                mx = max(MX[i],mx);
            }
            printf("%d\n",mx-mi);
        }
    }
    return 0;
}
时间: 2024-10-15 12:35:41

poj 3264Balanced Lineup的相关文章

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

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 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 Balanced Lineup(RMQ_ST)

题目链接:http://poj.org/problem?id=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 decides to organize a game of Ultimate Frisbee with some of the cows. To keep things si

POJ 3264-Balanced Lineup-RMQ问题

裸RMQ问题 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 5 using namespace std; 6 7 const int MAXN = 50010; 8 9 int min_dp[MAXN][20],max_dp[MAXN][20]; 10 int min_mm[MAXN],max_mm[MAXN]; 11 int b[MAXN]; 12 int N,Q; 13 14 vo

基于DP+位运算的RMQ算法

来源:http://blog.csdn.net/y990041769/article/details/38405063 RMQ算法,是一个快速求区间最值的离线算法,预处理时间复杂度O(n*log(n)),查询O(1),所以是一个很快速的算法,当然这个问题用线段树同样能够解决. 问题:给出n个数ai,让你快速查询某个区间的的最值. 算法分类:DP+位运算 算法分析:这个算法就是基于DP和位运算符,我们用dp[i ][j]表示从第 i 位开始,到第 i + 2^j -1 位的最大值或者最小值. 那么

poj 3264 Balanced Lineup

题目链接:http://poj.org/problem?id=3264 题目大意:就是给你一串数,问你最大数和最小数的差值....... 思路:最基本的线段树,只需要建树和查询,修改都省啦,但是查询要写两个,一个查询最大值,一个查询最小值......然后就能AC掉.....但是话说poj把它分类到RMQ中.... code: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream

poj 3264 Balanced Lineup 区间极值RMQ

题目链接:http://poj.org/problem?id=3264 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 wil

[POJ] 3264 Balanced Lineup [ST算法]

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

POJ 3264:Balanced Lineup Rmq模板

Balanced Lineup 题目链接: http://poj.org/problem?id=3264 题意: 求区间最大值和最小值的差 题解: Rmq模板题 代码 #include<stdio.h> #include<math.h> const int N=5e4+1; int dpmax[N][17]; int dpmin[N][17]; int mmax(int x,int y) { return x>y?x:y; } int mmin(int x,int y) {