zoukankan      html  css  js  c++  java
  • poj 3264 , 线段树

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

    Source

     
    线段树和基础的RMQ,最基本的吧
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<set>
    #include<queue>
    #include<string>
    #include<cmath>
    #include<fstream>
    #include<iomanip>
    
    using namespace std;
    
    #define LL long long
    #define MAXN 55555
    #define lson rt<<1, l, m
    #define rson rt<<1|1, m, r
    int MAX[MAXN<<2], MIN[MAXN<<2];
    int n, q;
    
    void push_up(int rt){
        MAX[rt] = max(MAX[rt<<1], MAX[rt<<1|1]);
        MIN[rt] = min(MIN[rt<<1], MIN[rt<<1|1]);
    }
    
    void build(int rt, int l, int r){
        if(l+1 == r){
            scanf(" %d", MIN+rt);   MAX[rt] = MIN[rt];
            return;
        }
        int m = l + r >> 1;
        build(lson);
        build(rson);
        push_up(rt);
    }
    
    int tmax, tmin;
    void query(int rt, int l, int r, int cl, int cr){
        if(cl<=l && cr>=r){
            tmax = max(tmax, MAX[rt]);
            tmin = min(tmin, MIN[rt]);
            return;
        }
    
        int m = l + r >> 1;
        if(cl < m) query(lson, cl, cr);
        if(cr > m) query(rson, cl, cr);
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        while(scanf(" %d %d", &n, &q)==2){
            build(1, 1, 1+n);
            while(q--){
                int a, b;
                scanf(" %d %d", &a, &b);
                tmax = 0;   tmin = 11000000;
                query(1, 1, 1+n, a, b+1);
                printf("%d
    ", tmax-tmin);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Django 之Ajax&Json&CORS&同源策略&Jsonp用法
    Django 基于Ajax & form 简单实现文件上传
    自定义分页
    【学员管理系统】0x04 数据库连接优化
    【学员管理系统】0x03 老师信息管理功能
    【学员管理系统】0x02 学生信息管理功能
    【学员管理系统】0x01 班级信息管理功能
    webpack那些事儿
    前端发展趋势
    好人不一定能做一个优秀的管理者
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3391666.html
Copyright © 2011-2022 走看看