zoukankan      html  css  js  c++  java
  • 线段树模板(例题BZOJ1756)最大子段和

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    int s[2000000],f[2000000],ls[2000000],rs[2000000],a[2000000];
    int n,m,ch,x,y,t;
    void build(int root,int l,int r)
    {
      if (l==r) 
      {
       f[root]=a[l];
       ls[root]=rs[root]=s[root]=f[root]; 
       return;
      }
      int mid=(l+r)/2;
      build(root*2,l,mid);
      build(root*2+1,mid+1,r);
      s[root]=s[root*2]+s[root*2+1];
      f[root]=max(max(f[root*2],f[root*2+1]),rs[root*2]+ls[root*2+1]);
      ls[root]=max(ls[root*2],s[root*2]+ls[root*2+1]);
      rs[root]=max(rs[root*2+1],s[root*2+1]+rs[root*2]);
    }
    void update(int root,int nowl,int nowr,int x,int v)
    {
      if (nowl==nowr)
      {
          s[root]=f[root]=ls[root]=rs[root]=v;
          return;
      }
      int mid=(nowl+nowr)/2;
      if (x<=mid) update(root*2,nowl,mid,x,v);
      else update(root*2+1,mid+1,nowr,x,v);
      s[root]=s[root*2]+s[root*2+1];
      f[root]=max(max(f[root*2],f[root*2+1]),rs[root*2]+ls[root*2+1]);
      ls[root]=max(ls[root*2],s[root*2]+ls[root*2+1]);
      rs[root]=max(rs[root*2+1],s[root*2+1]+rs[root*2]);
    }
    int queryl(int root,int nowl,int nowr,int l,int r)
    {
      if ((nowl==l)&&(nowr==r)) return rs[root];
      int mid=(nowl+nowr)/2;
      if (l>mid) return (queryl(root*2+1,mid+1,nowr,l,r));
      else
      {
          int t1=rs[root*2+1];
          int t2=s[root*2+1]+queryl(root*2,nowl,mid,l,mid);
          return(max(t1,t2)); 
      }
    }
    int queryr(int root,int nowl,int nowr,int l,int r)
    {
      if ((nowl==l)&&(nowr==r)) return ls[root];
      int mid=(nowl+nowr)/2;
      if (r<=mid) return (queryr(root*2,nowl,mid,l,r));
      else
      {
          int t1=ls[root*2];
          int t2=s[root*2]+queryr(root*2+1,mid+1,nowr,mid+1,r);
          return(max(t1,t2)); 
      }
    }
    int query(int root,int nowl,int nowr,int l,int r)
    {
      if ((nowl>=l)&&(nowr<=r)) return f[root];
       if ((nowl>r)||(nowr<l)) return 0;
      int mid=(nowl+nowr)/2;
      if (r<=mid) return query(root*2,nowl,mid,l,r);
      else if (l>mid) return query(root*2+1,mid+1,nowr,l,r);
      else
      {
          int t1=queryl(root*2,nowl,mid,l,mid);
          int t2=queryr(root*2+1,mid+1,nowr,mid+1,r);
          return (max(max(t1+t2,query(root*2,nowl,mid,l,mid)),query(root*2+1,mid+1,nowr,mid+1,r)));
      } 
    }
    int main()
    {
      scanf("%d%d",&n,&m);
      for (int i=1;i<=n;i++) scanf("%d",&a[i]);
      build(1,1,n);
      for (int i=1;i<=m;i++)
      {
          scanf("%d%d%d",&ch,&x,&y);
          if (ch==1) 
        {
          if (x>y) {t=x;x=y;y=t;}
          printf("%d
    ",query(1,1,n,x,y));
        }
          else update(1,1,n,x,y);
      } 
      return 0;
    }
  • 相关阅读:
    Asp.net Core 系列之--5.认证、授权与自定义权限的实现
    Asp.net Core 系列之--4.事务、日志及错误处理
    Asp.net Core 系列之--3.领域、仓储、服务简单实现
    Asp.net Core 系列之--2.ORM初探:Dapper实现MySql数据库各类操作
    Asp.net Core 系列之--1.事件驱动初探:简单事件总线实现(SimpleEventBus)
    Cocos2d-x项目创建
    Cocos2d-x编译Android环境
    Linux 之 RPM
    Channels实现扫码登录
    SQLALchemy中关于复杂关系表模型的映射处理
  • 原文地址:https://www.cnblogs.com/2014nhc/p/6397713.html
Copyright © 2011-2022 走看看