zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup

    #include <functional>  
    #include <algorithm>  
    #include <iostream>  
    #include <fstream>  
    #include <sstream>  
    #include <iomanip>  
    #include <numeric>  
    #include <cstring>  
    #include <cassert>  
    #include <cstdio>  
    #include <string>  
    #include <vector>  
    #include <bitset>  
    #include <queue>  
    #include <stack>  
    #include <cmath>  
    #include <ctime>  
    #include <list>  
    #include <set>  
    #include <map>  
    using namespace std;
    //typedef long long LL;
    //typedef __int64 LL; 
    //typedef long double DB;  
    //typedef unisigned __int64 LL; 
    //typedef unsigned long long ULL;
    #define EPS  1e-8
    #define MAXN 500050
    #define INF  0x3f3f3f3f
    #define PI   acos(-1.0)
    //#define MOD  99991
    //#define MOD  99990001 
    //#define MOD  1000000007
    #define max(a,b) ((a)>(b)?(a):(b))
    #define min(a,b) ((a)<(b)?(a):(b))
    #define max3(a,b,c) (max(max(a,b),c))
    #define min3(a,b,c) (min(min(a,b),c))
    #define xabs(a) ((a<0)?(-a):a)
    #define L(t) (t << 1)  //Left son t*2
    #define R(t) (t << 1 | 1) //Right son t*2+1
    #define Mid(a,b) ((a+b)>>1) //Get Mid
    #define lowbit(a) (a&-a) //Get Lowbit
    int gcd(int a,int b){return b?gcd(b,a%b):a;}
    int lcm(int a,int b){return a*b/gcd(a,b);}
    struct node
    {
        int l,r,maxval,minval;
    } tree[52005*4];
    
    void build(int l,int r,int root)
    {
        tree[root].l = l;
    	tree[root].r = r;
        tree[root].maxval = 0;
    	tree[root].minval = INF;
        if(l == r) return;
        int m = Mid(l,r);
        build(l,m,L(root));
        build(m+1,r,R(root));
    }
    
    void modify(int l,int r,int root,int val)
    {
    	tree[root].maxval=max(val,tree[root].maxval);
    	tree[root].minval=min(val,tree[root].minval);
        if(tree[root].l == l && tree[root].r == r)
        {       
            return;
        }
        int m=Mid(tree[root].l,tree[root].r);
        if(r <= m)
    		modify(l,r,L(root),val);
        else if(l > m)
    		modify(l,r,R(root),val);
        else
        {
            modify(l,m,L(root),val);
            modify(m+1,r,R(root),val);
        }
    }
    int query(int l,int r,int root)
    {
        if(tree[root].l == l && tree[root].r == r)
        {       
            return tree[root].maxval;
        }
        int m=Mid(tree[root].l,tree[root].r);
    	if(r <= m)
    		return query(l,r,L(root));
        else if(l > m)
    		return query(l,r,R(root));
        else
        {
            return max(query(l,m,L(root)),query(m+1,r,R(root)));
        }
    }
    int query1(int l,int r,int root)
    {
        if(tree[root].l == l && tree[root].r == r)
        {       
            return tree[root].minval;
        }
        int m=Mid(tree[root].l,tree[root].r);
    	if(r <= m)
    		return query1(l,r,L(root));
        else if(l > m)
    		return query1(l,r,R(root));
        else
        {
            return min(query1(l,m,L(root)),query1(m+1,r,R(root)));
        }
    }
    int main()
    {
    //  freopen("in.txt","r",stdin);
    //  freopen("out.txt,"w",stdout);
    	int i,n,m,x,y;
        scanf("%d%d",&n,&m);
        build(0,52000,1);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            modify(i,i,1,x);
        }
    	for(i=0;i<m;i++)
    	{
    		int a,b;
    		scanf("%d%d",&a,&b);
    		printf("%d\n",xabs(query(a,b,1)-query1(a,b,1)));
    	}
    	return 0;
    }

    线段树乱搞,就求一个区间最大最小值,每次插入维护区间就行了


  • 相关阅读:
    PHP5.6 和PHP7.0区别
    怎么清除火狐浏览器的cookie?
    PHP 7.0新增特性详解
    一个较好的基础的数据库连接池知识 规格严格
    Iptables 规格严格
    收藏一个Man网址 规格严格
    AIX配置SNMP V3 规格严格
    Tomcat Firewall JMX RMI 规格严格
    在来一个IPTables 规格严格
    java.lang.ClassNotFoundException: listeners.ContextListener . 规格严格
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2979641.html
Copyright © 2011-2022 走看看