Can you answer these queries III(luogu)

Description

维护一个长度为n的序列A,进行q次询问或操作

0 x y:把Ax改为y

1 x y:询问区间【l,r】的最大子段和

数据范围:n,q<=5e4,-1e4<=Ai<=1e4;

Solution

线段树处理区间最大子段和

  • res为区间最大子串和
  • sum为区间和
  • prel和prer分别为从区间左端点和右端点开始的最大子串和

Code

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define ll long long
using namespace std;
const int N=5e4+;
struct node
{
ll sum,res,prel,prer;
int l,r,lc,rc;
}f[N*],t;
int opt,tot,rt,n,q,x,y;
ll d[N];
void push_up(int g)
{
int lc=f[g].lc,rc=f[g].rc;
if(!lc) return ;
f[g].sum=f[lc].sum+f[rc].sum;
f[g].prel=max(f[lc].prel,f[lc].sum+f[rc].prel);
f[g].prer=max(f[rc].prer,f[rc].sum+f[lc].prer);
f[g].res=max(max(f[lc].res,f[rc].res),f[lc].prer+f[rc].prel);
}
void build(int &g,int l,int r)
{
g=++tot;
f[g].l=l,f[g].r=r;
if(l==r)
{
f[g].sum=f[g].prel=f[g].prer=f[g].res=d[l];
return ;
}
int mid=(l+r)>>;
build(f[g].lc,l,mid),build(f[g].rc,mid+,r);
push_up(g);
}
void change(int g,int x,int y)
{
if(f[g].l==f[g].r)
f[g].sum=f[g].res=f[g].prel=f[g].prer=y;
else
{
int mid=(f[g].l+f[g].r)>>;
if(x<=mid) change(f[g].lc,x,y);
else change(f[g].rc,x,y);
push_up(g);
}
}
ll get(int g,int l,int r,node &a)
{
if(f[g].l==l && f[g].r==r)
{
a=f[g];
return a.res;
}
else
{
int mid=(f[g].l+f[g].r)>>;
if(r<=mid) return get(f[g].lc,l,r,a);
else if(l>mid) return get(f[g].rc,l,r,a);
else
{
node b,c;
get(f[g].lc,l,mid,b);
get(f[g].rc,mid+,r,c);
a.sum=b.sum+c.sum;
a.prel=max(b.prel,b.sum+c.prel);
a.prer=max(c.prer,c.sum+b.prer);
a.res=max(max(b.res,c.res),b.prer+c.prel);
return a.res;
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&d[i]);
build(rt,,n);
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&opt,&x,&y);
if(!opt) change(rt,x,y);
else printf("%lld\n",get(rt,x,y,t));
}
return ;
}

维护一个长度为 nn 的序列 AA,进行 mm 次询问或操作:

  • 0 x y:将 A_xAx​ 单调修改为 yy
  • 1 x y:求出 \max\{\sum_{k=i}^j A_k\}(x\le i\le j\le y)max{∑k=ij​Ak​}(x≤i≤j≤y)。

数据范围:N,M\le 5\times 10^4N,M≤5×104,|A_i|\le 10^4∣Ai​∣≤104

Can you answer these queries III(线段树)的更多相关文章

  1. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  2. SPOJ GSS3 Can you answer these queries III ——线段树

    [题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...

  3. spoj 1557 GSS3 - Can you answer these queries III 线段树

    题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...

  4. SP1716 GSS3 - Can you answer these queries III 线段树

    问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...

  5. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

  6. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  7. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  8. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  9. 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树

    [BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...

  10. HDU 4027 Can you answer these queries?(线段树区间开方)

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...

随机推荐

  1. 【ZooKeeper系列】3.ZooKeeper源码环境搭建

    前文阅读: [ZooKeeper系列]1.ZooKeeper单机版.伪集群和集群环境搭建 [ZooKeeper系列]2.用Java实现ZooKeeper API的调用 在系列的前两篇文章中,介绍了Zo ...

  2. eclipse中如何配置maven

    1.首先需要在自己电脑中安装Maven,下载maven的路径:http://maven.apache.org/download.cgi 2.我们把下载好的文件解压到自己电脑的任意一个盘符中去,我的是e ...

  3. RabbitMQ、Kafka、RocketMQ的优劣势

    今天我们一起来探讨: 全量的消息队列究竟有哪些? Kafka.RocketMQ.RabbitMQ的优劣势比较 以及消息队列的选型 最全MQ消息队列有哪些 那么目前在业界有哪些比较知名的消息引擎呢?如下 ...

  4. hadoop传递参数方法总结

    转自:http://blog.csdn.net/xichenguan/article/details/22162813 写MapReduce程序通常要传递各种各样的参数,选择合适的方式来传递参数既能提 ...

  5. zabbix监控web应用日志报警并发送消息到钉钉

    首先在钉钉上开启钉钉机器人功能 说明:自定义关键词是zabbix发送过来的消息内容必须含有你定义的ERROR或者error字段,否则消息无法发送过来 ip地址段:一般都是zabbix-server的I ...

  6. 基于GMC/umat的复合材料宏细观渐近损伤分析(一)

    近期在开展基于GMC/umat的复合材料宏细观渐近损伤分析,一些技术细节分享如下: 1.理论基础 针对连续纤维增强复合材料,可以通过离散化获得如下的模型: (a)(b)(c) 图1 连续纤维增强复合材 ...

  7. KMP 和 扩展KMP

    KMP:在主串S中找子串T的位置KMP算法的时间复杂度O(|S|+|T|). #define maxn 1000 char s[maxn],t[maxn];//s为主串,t为子串 int net[ma ...

  8. 变量键盘读取、数组与宣告:read,array,declare

    1.read 2.declare/typeset 宣告变量的类型 3.数组(array)变量类型 4.与文件系统及程序的限制关系:ulimit 限制用户的某些系统资源,包括,可以开启的文件的数量,可以 ...

  9. Spring AOP 基于AspectJ

    简介 AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持.因为Spring1.0的时候Aspectj还未出现; AspectJ1.5中新增了对 ...

  10. 使用redis的zset实现高效分页查询(附完整代码)

    一.需求 移动端系统里有用户和文章,文章可设置权限对部分用户开放.现要实现的功能是,用户浏览自己能看的最新文章,并可以上滑分页查看. 二.数据库表设计 涉及到的数据库表有:用户表TbUser.文章表T ...