zoukankan      html  css  js  c++  java
  • spoj 1043 Can you answer these queries I

    http://www.spoj.com/problems/GSS1/

    My Code
     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 #define lson l,m,rt<<1
     5 #define rson m+1,r,rt<<1|1
     6 #define maxn 50010
     7 struct node
     8 {
     9     int lmax,rmax,max,sum;
    10 }setree[maxn<<2];
    11 int ans;
    12 void pushup(int rt)
    13 {
    14     setree[rt].lmax=max(setree[rt<<1].lmax,setree[rt<<1].sum+setree[rt<<1|1].lmax);
    15     
    16     setree[rt].rmax=max(setree[rt<<1|1].rmax,setree[rt<<1|1].sum+setree[rt<<1].rmax);
    17     
    18     setree[rt].sum=setree[rt<<1].sum+setree[rt<<1|1].sum;
    19     
    20     setree[rt].max=max(setree[rt<<1].max,setree[rt<<1|1].max);
    21     setree[rt].max=max(setree[rt].max,setree[rt<<1].rmax+setree[rt<<1|1].lmax);
    22 }
    23 void build(int l,int r,int rt)
    24 {
    25     if(l==r){
    26         scanf("%d",&setree[rt].sum);
    27         setree[rt].lmax=setree[rt].sum;
    28         setree[rt].rmax=setree[rt].sum;
    29         setree[rt].max=setree[rt].sum;
    30         return;
    31     }
    32     int m=(l+r)>>1;
    33     build(lson);
    34     build(rson);
    35     pushup(rt);
    36 }
    37 int query(int l,int r,int rt,int flag,int L,int R)
    38 {
    39     if(L==l&&r==R){
    40         ans=max(ans,setree[rt].max);
    41         return flag==-1?setree[rt].lmax:setree[rt].rmax;
    42     }
    43     int m=(l+r)>>1;
    44     if(R<=m)
    45     return query(lson,-1,L,R);
    46     else if(L>m)
    47     return query(rson,1,L,R);
    48     else{
    49         int ln=query(lson,1,L,m);
    50         int rn=query(rson,-1,m+1,R);
    51         ans=max(ans,ln+rn);
    52         if(flag == -1)
    53             return max(setree[rt<<1].lmax, setree[rt<<1].sum + rn);
    54         else
    55             return max(setree[rt<<1|1].rmax, setree[rt<<1|1].sum + ln);
    56 
    57     }
    58     return ans;
    59 }
    60 int main()
    61 {
    62     int n;
    63     while(~scanf("%d",&n)){
    64         build(1,n,1);
    65         int m;
    66         scanf("%d",&m);
    67         while(m--){
    68             int a,b;
    69             ans=-(1<<30);
    70             scanf("%d%d",&a,&b);
    71             int tmp=query(1,n,1,0,a,b);
    72             printf("%d\n",ans);
    73         }
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    python——协程
    解读python中SocketServer源码
    python——初识socket
    python的类和对象——类的静态字段番外篇
    python的类和对象——类成员番外篇
    python的类和对象——进阶篇
    初识python中的类与对象
    python中lambda表达式应用
    python——挖装饰器祖坟事件
    python的基础类源码解析——collection类
  • 原文地址:https://www.cnblogs.com/kim888168/p/2917668.html
Copyright © 2011-2022 走看看