zoukankan      html  css  js  c++  java
  • poj3264线段数求最大最小值

    链接:https://vjudge.net/contest/66989#problem/G

    完完全全的水题,还是被坑了,一个return忘了写,de了半天bug!!

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define ll long long
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    const int maxn=50010;
    const ll inf=1e15;
    ll value1[maxn<<2],value2[maxn<<2];
    void pushup(int rt)
    {
        value1[rt]=max(value1[rt<<1],value1[rt<<1|1]);
        value2[rt]=min(value2[rt<<1],value2[rt<<1|1]);
    }
    void btree(int l,int r,int rt)
    {
        if(l==r)
        {
            scanf("%lld",&value1[rt]);
            value2[rt]=value1[rt];
            return ;
        }
        int m=(l+r)>>1;
        btree(ls);
        btree(rs);
        pushup(rt);
    }
    ll query1(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)return value1[rt];
        int m=(l+r)>>1;
        ll ans=0;
        if(L<=m)ans=max(ans,query1(L,R,ls));
        if(R>m)ans=max(ans,query1(L,R,rs));
        return ans;
    }
    ll query2(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)return value2[rt];
        int m=(l+r)>>1;
        ll ans=inf;
        if(L<=m)ans=min(ans,query2(L,R,ls));
        if(R>m)ans=min(ans,query2(L,R,rs));
        return ans;
    }
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            btree(1,n,1);
            while(m--){
                int l,r;
                scanf("%d%d",&l,&r);
                printf("%lld
    ",query1(l,r,1,n,1)-query2(l,r,1,n,1));
            }
        }
    }
  • 相关阅读:
    delphi实现FTP下载
    delphi中ClientDataSet的隐含功能
    删除注册的ODBC
    ZOJ 1041 Transmitters
    POJ 3232 Accelerator
    POJ 3460 Booksort
    UVa 11552 Fewest Flops
    SOJ 8064 Whack the Groundhog
    BNU OJ 29355 手速为王
    POJ 3322 Bloxorz I
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6497321.html
Copyright © 2011-2022 走看看