zoukankan      html  css  js  c++  java
  • poj 3246 简单线段树

    线段树还真有点难写。。。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    
    using namespace std;
    typedef long long LL;
    #define oo 0x3f3f3f3f
    #define N 200100
    
    struct node
    {
        int l, r;
        int s, e;
    } tree[N*4];
    
    int A[N];
    
    void build(int l, int r, int rt)
    {
        tree[rt].l=l;
        tree[rt].r=r;
        if(l==r)
        {
            tree[rt].s=tree[rt].e=A[l];
            return ;
        }
        int mid;
        mid=(l+r)/2;
        build(l, mid, rt*2);
        build(mid+1, r, rt*2+1);
        tree[rt].s=max(tree[rt*2].s, tree[rt*2+1].s);
        tree[rt].e=min(tree[rt*2].e, tree[rt*2+1].e);
    
        return ;
    }
    
    int query(int l, int r, int rt)
    {
        if(tree[rt].l>=l&&tree[rt].r<=r)
        {
            return tree[rt].s;
        }
        int mid=(tree[rt].l+tree[rt].r)/2;
        if(r<=mid) return query(l, r, rt*2);
        else if(l>mid) return query(l, r, rt*2+1);
        else
        {
            return max(query(l, mid, rt*2), query(mid+1, r, rt*2+1));
        }
    }
    
    int Query(int l, int r, int rt)
    {
        if(tree[rt].l>=l&&tree[rt].r<=r)
        {
            return tree[rt].e;
        }
        int mid=(tree[rt].l+tree[rt].r)/2;
        if(r<=mid) return Query(l, r, rt*2);
        else if(l>mid) return Query(l, r, rt*2+1);
        else
        {
            return min(Query(l, mid, rt*2), Query(mid+1, r, rt*2+1));
        }
    }
    
    int main()
    {
        int n, m, a, b;
    
        while(~scanf("%d%d", &n, &m))
        {
            for(int i=1; i<=n; i++)
                scanf("%d", &A[i]);
            build(1, n, 1);
    
            while(m--)
            {
                scanf("%d%d", &a, &b);
                printf("%d
    ", query(a, b, 1)-Query(a, b, 1));
            }
        }
        return 0;
    }
  • 相关阅读:
    使用parted对大于2T的磁盘进行分区
    iso系统镜像刻录到光盘和U盘
    戴尔R710服务器安装系统——配置raid
    UltraISO 9.7.1.3519注册码
    H3C交换机配置vlan
    kvm创建新虚拟机
    Windows添加永久静态路由
    gitlab部署步骤+汉化
    php配置php_pdo_mysql模块
    为git服务器配置gitosis管理权限
  • 原文地址:https://www.cnblogs.com/9968jie/p/5661433.html
Copyright © 2011-2022 走看看