zoukankan      html  css  js  c++  java
  • POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数

    问区间最大值-区间最小值

     1 // #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <sstream>
     6 #include <string>
     7 #include <algorithm>
     8 #include <list>
     9 #include <map>
    10 #include <vector>
    11 #include <queue>
    12 #include <stack>
    13 #include <cmath>
    14 #include <cstdlib>
    15 // #include <conio.h>
    16 using namespace std;
    17 #define clc(a,b) memset(a,b,sizeof(a))
    18 #define inf 0x3f3f3f3f
    19 #define lson l,mid,rt<<1
    20 #define rson mid+1,r,rt<<1|1
    21 const int N=200010;
    22 const int MOD = 1e9+7;
    23 #define LL long long
    24 double const pi = acos(-1);
    25 void fre() {
    26     freopen("in.txt","r",stdin);
    27 }
    28 // inline int r() {
    29 //     int x=0,f=1;char ch=getchar();
    30 //     while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
    31 //     while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
    32 // }
    33 int a[N];
    34 int Min,Max;
    35 struct Edge{
    36     int l,r;
    37     int minx,maxx;
    38 }e[4*N];
    39 
    40 void pushup(int rt){
    41      e[rt].minx=min(e[rt<<1].minx,e[rt<<1|1].minx);
    42      e[rt].maxx=max(e[rt<<1].maxx,e[rt<<1|1].maxx);
    43 }
    44 
    45 void build(int l,int r,int rt){
    46      e[rt].l=l;
    47      e[rt].r=r;
    48      if(l==r){
    49          e[rt].minx=e[rt].maxx=a[l];
    50          return;
    51      }
    52      int mid=(l+r)>>1;
    53      build(lson);
    54      build(rson);
    55      pushup(rt);
    56 }
    57 
    58 void query(int l,int r,int rt,int L,int R){
    59      if(e[rt].maxx<=Max&&e[rt].minx>=Min) return;
    60      if(L<=l&&R>=r){
    61          Min=min(e[rt].minx,Min);
    62          Max=max(e[rt].maxx,Max);
    63          return;
    64      }
    65      int mid=(l+r)>>1;
    66      if(L<=mid) query(l,mid,rt<<1,L,R);
    67      if(R>mid) query(mid+1,r,rt<<1|1,L,R);
    68      // if(r<=mid) query(lson,L,R);
    69      // else if(l>mid) query(rson,L,R);
    70      // else{
    71      //     query(lson,L,R);
    72      //     query(rson,L,R);
    73      // }
    74 }
    75 
    76 int main(){
    77      // fre();
    78      int n,q;
    79      scanf("%d%d",&n,&q);
    80      for(int i=1;i<=n;i++){
    81          scanf("%d",&a[i]);
    82      }
    83      build(1,n,1);
    84      while(q--){
    85          int L,R;
    86          scanf("%d%d",&L,&R);
    87          Min=inf,Max=-inf;
    88          // cout<<"!!"<<endl;
    89          // getch();
    90          query(1,n,1,L,R);
    91          printf("%d
    ",Max-Min);
    92      }
    93      return 0;
    94 }
  • 相关阅读:
    【原】IOS文件操作
    【原】UIWebView加载本地pdf、doc等文件
    【转】好东西!sqlite3中BLOB数据类型存储大对象运用示例
    ASP.NET环境下配置FCKEditor并上传图片及其它文件
    iis php环境配置完整版
    js校验服务器控件是否为空
    vim 分屏显示
    各大搜索引擎网站提交端口的快速通道
    unix动态链接库操作
    回车(CR)与换行(LF), '\r'和'\n'的区别
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5616803.html
Copyright © 2011-2022 走看看