zoukankan      html  css  js  c++  java
  • TOJ1698/POJ3264Balanced Lineup (线段树 or RMQ-ST)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1698

    时间限制(普通/Java):5000MS/50000MS     内存限制:65536KByte

    描述

    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.

    输入

    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.

    输出

    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.

    样例输入

    6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

    样例输出

    6
    3
    0

    思路:题目大意就是,给n个数m个查询,下面行输入n个数。m行输入m个查询,查询最大值和最小值的差。

             rmq-st模板题。拿来练手的。作为丢人的初学线段树选手,也附上手打的线段树代码。

    RMQ-ST代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<set>
    #include<map> 
    #include<vector>
    #define LL long long
    #include<assert.h>
    using namespace std;
    int a[50001],dm[100000][20],dx[100000][20];
    void rmq(int num){
        for(int i = 0 ; i < num ; i++){
            dm[i][0] = dx[i][0] = a[i];
        }
        for(int j = 1 ; (1<<j) <= num ; j++){
            for(int i = 0 ;i+(1<<j)-1 < num ;i ++){
                dx[i][j] = max(dx[i][j-1],dx[i+(1<<(j-1))][j-1]);
                dm[i][j] = min(dm[i][j-1],dm[i+(1<<(j-1))][j-1]);
            }
        }
    }
    int qmax(int st,int ed){
        int k = 0;
        while((1<<(k+1))<= ed - st + 1)k++;
        return max(dx[st][k],dx[ed - (1<<k) + 1][k]);
    }
    int qmin(int st,int ed){
        int k = 0;
        while((1<<(k+1))<= ed - st + 1)k++;
        return min(dm[st][k],dm[ed - (1<<k) + 1][k]);
    }
    int main(){
        int n,k;
        while(~scanf("%d %d",&n,&k)){
            memset(dx,0,sizeof(dx));
            memset(dm,0,sizeof(dm));
            for(int i = 0 ; i < n ; i++)scanf("%d",&a[i]);
            rmq(n);
            while(k--){
                int x,y;
                scanf("%d %d",&x,&y);
                printf("%d
    ",qmax(x-1,y-1)-qmin(x-1,y-1));
            }
        }
    }

    线段树代码:

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int maxn = 100000;
    struct note{
        int l,r;
        int nMin,nMax;
    }segTree[maxn<<2];
    int Max,Min;
    int a[maxn];
    void build(int i,int l,int r){
        segTree[i].l = l;
        segTree[i].r = r;
        if(l==r){
            segTree[i].nMin = segTree[i].nMax = a[l];
            return;
        }
        int mid = (l+r)>>1;
        build(i<<1,l,mid);
        build(i<<1|1,mid+1,r);
        segTree[i].nMax = max(segTree[i<<1].nMax,segTree[i<<1|1].nMax);
        segTree[i].nMin = min(segTree[i<<1].nMin,segTree[i<<1|1].nMin);
    }
    void query(int i,int l,int r){
        if(segTree[i].nMax <= Max && segTree[i].nMin >= Min){
            return;
        }
        if(segTree[i].l == l && segTree[i].r == r){
            Max = max(segTree[i].nMax,Max);
            Min = min(segTree[i].nMin,Min);
            return;
        }
        int mid = (segTree[i].l + segTree[i].r) >> 1;
        if(r <= mid)
            query(i<<1,l,r);
        else if(l > mid) 
            query(i<<1|1,l,r);
        else{
            query(i<<1,l,mid);
            query(i<<1|1,mid+1,r);
        }
    }
    int main(){
        int n,m;
        while(~scanf("%d %d",&n,&m)){
            for(int i = 1 ; i <= n ;i++)scanf("%d",&a[i]);
            build(1,1,n);
            while(m--){
                int x,y;
                Max = -1000000;Min = 1000000;
                scanf("%d %d",&x,&y);
                query(1,x,y);
                printf("%d
    ",Max-Min);
            }
        }
    }
  • 相关阅读:
    配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法
    js 获取cookie
    jQuery+Ajax+PHP 制作简单的异步数据传输(测试用户名是否可用)
    既之前的基础,先写个简单的PHP 与数据库 的数据交流
    Web 后端--PHP 与数据库的交互
    MySQL 忘记密码怎么办?
    MySQL 继续-- Win7 安装及后续工作
    数据库初识--从MySQL 出发
    在PHP中使用MySQL Mysqli操作数据库 ,以及类操作方法
    电脑使用--快捷键等
  • 原文地址:https://www.cnblogs.com/Esquecer/p/8641798.html
Copyright © 2011-2022 走看看