Can you answer these queries III(线段树)
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 单调修改为 yy1 x y
:求出 \max\{\sum_{k=i}^j A_k\}(x\le i\le j\le y)max{∑k=ijAk}(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(线段树)的更多相关文章
- 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 ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- spoj 1557 GSS3 - Can you answer these queries III 线段树
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...
- SP1716 GSS3 - Can you answer these queries III 线段树
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- 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 ...
- 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 ...
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...
- HDU 4027 Can you answer these queries?(线段树区间开方)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
随机推荐
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
- 阿里云基于OSS的云上统一数据保护方案2.0正式发布
近年来,随着越来越多的企业从传统经济向数字经济转型,云已经渐渐成为数据经济IT新常态.核心业务系统上云,云上的业务创新,这些都产生了大量的业务数据,这些数据也成为了企业最重要的资产.资源.阿里云基于O ...
- video实现有声音自动播放
video实现自动播放有声音 需求:老板见人家可以的,我们的也要可以!!! 前端:自动播放,简单... 要实现:鼠标移入视频播放同时有声音,移出让你暂停,,,,, 问题集合 1- 自动播放实现没有声音 ...
- 【他山之石】IntelliJ Idea 内存设置
最近一次使用idea,删掉target目录内容,准备让项目重新编译的时候,整个mac系统崩溃然后黑屏重启了.紧接着就是重启后自动恢复原先打开的程序,结果再次黑屏重启.最开始以为是系统问题,还怀疑过最近 ...
- $loj10156/$洛谷$2016$ 战略游戏 树形$DP$
洛谷loj Desription Bob 喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的方法.现在他有个问题. 现在他有座古城堡,古城堡的路形成一棵树.他要在这棵树的节点上放置最少数 ...
- python版飞机大战代码简易版
# -*- coding:utf-8 -*- import pygame import sys from pygame.locals import * from pygame.font import ...
- Python for Data Analysis 学习心得(四) - 数据清洗、接合
一.文字处理 之前在练习爬虫时,常常爬了一堆乱七八糟的字符下来,当时就有找网络上一些清洗数据的方式,这边pandas也有提供一些,可以参考使用看看.下面为两个比较常见的指令,往往会搭配使用. spli ...
- Linux安装MySQL及基本操作(Centos)
安装: 系统:CentOS-7-x86_64-DVD-1810.iso 安装命令: wget http://repo.mysql.com/mysql-community-release-el7-5.n ...
- docker 修改实例名称
docker 容器(服务)重命名只要一个命令就可以: docker rename 原容器名 新容器名 如:
- Http GetPost网络请求
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System ...