zoukankan      html  css  js  c++  java
  • POJ3264Balanced Lineup 线段树练手

    题目意思:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,
    多次求任一区间Ai-Aj中最大数和最小数的差

    //1085422276
    #include <iostream>
    #include <stdio.h>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <stack>
    #include <math.h>
    #include <vector>
    #include <string.h>
    using namespace std;
    
    typedef __int64 ll;
    const int inf = (int)1E9+10;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    
    //*******************************
    struct ss
    {
        int l,r,mn,ma;
    }tr[800001];
    int n,q,a[200001],nn,mm;
    void build(int k,int s,int t)
    {
        tr[k].l=s;
        tr[k].r=t;
        if(s==t){
            tr[k].mn=tr[k].ma=a[s];
            return ;
        }
        int mid=(s+t)>>1;
        build(k<<1,s,mid);
        build(k<<1|1,mid+1,t);
        tr[k].ma=max(tr[k<<1].ma,tr[k<<1|1].ma);
        tr[k].mn=min(tr[k<<1].mn,tr[k<<1|1].mn);
    }
    void ask(int k,int s,int t)
    {
        if(tr[k].ma<=nn&&tr[k].mn>=mm)return;
        if(s==tr[k].l&&t==tr[k].r)
        {
            nn=max(tr[k].ma,nn);
            mm=min(tr[k].mn,mm);
            return;
        }
        int mid=(tr[k].l+tr[k].r)>>1;
        if(t<=mid) ask(k<<1,s,t);
        else if(s>mid) ask(k<<1|1,s,t);
        else {
           ask(k<<1,s,mid);
           ask(k<<1|1,mid+1,t);
        }
    }
    
    int main()
    {
    
        while(scanf("%d%d",&n,&q)!=EOF)
        {
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }build(1,1,n);int x,y;
            for(int i=1;i<=q;i++){
                    nn=-inf;
                   mm=inf;
                scanf("%d%d",&x,&y);
                ask(1,x,y);
                printf("%d
    ",nn-mm);
            }
        }
        return 0;
    }
  • 相关阅读:
    原子核壳模型程序 BigStick 的用法
    c++ 中的下三角阵矩阵元标记
    BCS方程和Bogoliubov变换
    圆膜振动问题
    核结构单体跃迁算符
    python画球谐函数
    gnuplot 绘制球谐函数图
    shell 脚本小知识集锦
    6.12学习总结
    PHP网上商城
  • 原文地址:https://www.cnblogs.com/zxhl/p/4764877.html
Copyright © 2011-2022 走看看