题目链接

给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间。

开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值。 合并起来好麻烦......

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int maxn = 5e4+;
int sum[maxn<<], suf_max[maxn<<], pre_max[maxn<<], maxx[maxn<<];
void pushUp(int rt) {
sum[rt] = sum[rt<<] + sum[rt<<|];
suf_max[rt] = max(suf_max[rt<<|], suf_max[rt<<]+sum[rt<<|]);
pre_max[rt] = max(pre_max[rt<<], pre_max[rt<<|]+sum[rt<<]);
maxx[rt] = max(maxx[rt<<], maxx[rt<<|]);
maxx[rt] = max(maxx[rt], suf_max[rt<<]+pre_max[rt<<|]);
}
void build(int l, int r, int rt) {
if(l == r) {
scanf("%d", &maxx[rt]);
sum[rt] = pre_max[rt] = suf_max[rt] = maxx[rt];
return ;
}
int m = l+r>>;
build(lson);
build(rson);
pushUp(rt);
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
sum[rt] = pre_max[rt] = suf_max[rt] = maxx[rt] = val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
pushUp(rt);
}
int query_sum(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return sum[rt];
}
int m = l+r>>, ret = ;
if(L<=m)
ret += query_sum(L, R, lson);
if(R>m)
ret += query_sum(L, R, rson);
return ret;
}
int query_suf(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return suf_max[rt];
}
int m = l+r>>;
if(R<=m)
return query_suf(L, R, lson);
if(L>m)
return query_suf(L, R, rson);
return max(query_suf(L, m, lson)+query_sum(m+, R, rson), query_suf(m+, R, rson));
}
int query_pre(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return pre_max[rt];
}
int m = l+r>>;
if(R<=m)
return query_pre(L, R, lson);
if(L>m)
return query_pre(L, R, rson);
return max(query_pre(L, m, lson), query_pre(m+, R, rson)+query_sum(L, m, lson));
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return maxx[rt];
}
int m = l+r>>;
if(R<=m)
return query(L, R, lson);
if(L>m)
return query(L, R, rson);
return max(max(query(L, m, lson), query(m+, R, rson)), query_pre(m+, R, rson)+query_suf(L, m, lson));
}
int main()
{
int n, q, x, y, sign;
scanf("%d", &n);
build(, n, );
scanf("%d", &q);
while(q--) {
scanf("%d%d%d", &sign, &x, &y);
if(sign)
printf("%d\n", query(x, y, , n, ));
else
update(x, y, , n, );
}
return ;
}

spoj 1557 GSS3 - 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. SP1716 GSS3 - Can you answer these queries III 线段树

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

  4. 数据结构(线段树):SPOJ GSS3 - Can you answer these queries III

    GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers bet ...

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

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

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

    SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] ...

  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. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...

随机推荐

  1. javascript高级知识分析——定义函数

    代码信息来自于http://ejohn.org/apps/learn/. 可以使用哪些方式来声明函数? function isNimble(){ return true; } var canFly = ...

  2. Semantic UI基础使用教程

    自己今后要使用Semantic UI进行项目开发了,一步步的记录下来,供大家参考,也让自己去简单的学习一下,有空了就会更新一点东西,大家有什么问题可以相互交流一下,文采不是很好,希望大家要多多见谅,这 ...

  3. IOS app启动过程

    1.main函数   2.UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象   3.delegate对象开始处理(监 ...

  4. C#:判断一个String是否为数字

    方案一:Try...Catch(执行效率不高)private bool IsNumberic(string oText){          try         {                 ...

  5. sqlite3插入日期时间出错解决

    正确写法 insert into hhf_records(RegistrationNumber,MachinesNumber,InDataTime,Flag,CType) values (11,1,d ...

  6. 加密传输SSL协议7_SSL协议概述

    SSL(Secure Sockets Layer) SSL的功能,可以在通信的双方中建立一个加密的通信通道 同时还可以确认通信的双方是不是就是其声称的人,防止被钓鱼. SSL在网络协议栈中的位置:可以 ...

  7. client|server 最简单的聊天

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/socket ...

  8. FAQ:Python环境变量配置

    Python安装安装成,需要配置环境变量: 默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1. 首先需要在系统中注册 ...

  9. eclipse 快捷方式大全

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  10. sim卡中的汉字存储格式

    Sim卡中的ucs2格式 Sim卡中的中文都是以ucs2格式存储的,ucs2和unicode只是字节序不同,unicode是小头在前,ucs2是大头在前. Ucs2与GB2312互换可以用VC中的Wi ...