[冬令营Day1 T2]sequence
|
题目描述 Description
|
||||||||
|
给一个长度为N的序列以及Q的询问,每次两个参数l,r,问你序列[l,r]中的最大连续和
|
||||||||
|
输入描述 Input Description
|
||||||||
|
一行二个正整数N,Q。 |
||||||||
|
输出描述 Output Description
|
||||||||
|
对于每个询问,输出一行一个整数,描述答案。
|
||||||||
|
样例输入 Sample Input
|
||||||||
|
4 3
1 -2 3 2 1 4 1 2 2 2 |
||||||||
|
样例输出 Sample Output
|
||||||||
|
5
1
0
|
||||||||
|
数据范围及提示 Data Size & Hint
|
||||||||
|
线段树。对于每个节点,维护三个值。sum,suml,sumr表示该点的最大连续和,最大前缀和和最大后缀和。
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
inline int read()
{
int x=,f=;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)){x=x*+c-'';c=getchar();}
return x*f;
}
const int oo=;
const int maxn=;
int n,q,a[maxn],x,y;
LL pre[maxn],sum[maxn],suml[maxn],sumr[maxn];
void build(int l,int r,int o)
{
if(l==r)
{
suml[o]=sumr[o]=sum[o]=pre[r]-pre[l-];
return;
}
int mid=(l+r)>>,lo=o<<,ro=lo+;
build(l,mid,lo);
build(mid+,r,ro);
sum[o]=max(sum[lo],sum[ro]);
sum[o]=max(sum[o],sumr[lo]+suml[ro]);
suml[o]=max(suml[lo],pre[mid]-pre[l-]+suml[ro]);
sumr[o]=max(sumr[ro],pre[r]-pre[mid]+sumr[lo]);
return;
}
LL queryl(int x,int y,int l,int r,int o)
{
if(x==l && y==r) return sumr[o];
int mid=(l+r)>>,lo=o<<,ro=lo+;
if(x>mid) return queryl(x,y,mid+,r,ro);
else
{
LL tmp=pre[r]-pre[mid]+queryl(x,mid,l,mid,lo);
return max(sumr[ro],tmp);
}
}
LL queryr(int x,int y,int l,int r,int o)
{
if(x==l && y==r) return suml[o];
int mid=(l+r)>>,lo=o<<,ro=lo+;
if(y<=mid) return queryr(x,y,l,mid,lo);
else
{
LL tmp=pre[mid]-pre[l-]+queryr(mid+,y,mid+,r,ro);
return max(suml[lo],tmp);
}
}
LL query(int x,int y,int l,int r,int o)
{
if(x<=l && y>=r)return sum[o];
int mid=(l+r)>>,lo=o<<,ro=lo+;
if(y<=mid) return query(x,y,l,mid,lo);
else if(x>mid) return query(x,y,mid+,r,ro);
else
{
LL ls,rs;
ls=queryl(x,mid,l,mid,lo);
rs=queryr(mid+,y,mid+,r,ro);
return max(max(query(x,mid,l,mid,lo),query(mid+,y,mid+,r,ro)),rs+ls);
}
}
int main()
{
freopen("sequence.in","r",stdin);
freopen("sequence.out","w",stdout);
n=read();q=read();
for(int i=;i<=n;i++)a[i]=read(),pre[i]=pre[i-]+a[i];
build(,n,);
while(q--)
{
x=read();y=read();
LL tmp=query(x,y,,n,);
if(tmp<)tmp=;
printf("%lld\n",tmp);
}
return ;
}
对于后三个点肯定没戏,毕竟线段树常数那么大。满分做法好像是整体二分,然而并不会,等着以后来填坑吧。
[冬令营Day1 T2]sequence的更多相关文章
- codevs 4511 信息传递(NOIP2015 day1 T2)
4511 信息传递 NOIP2015 day1 T2 时间限制: 1 s 空间限制: 128000 KB 传送门 题目描述 Description 有个同学(编号为 1 到)正在玩一个信息传递的游戏. ...
- 【NOIP2016 Day1 T2】天天爱跑步
题目传送门:https://www.luogu.org/problemnew/show/P1600 感觉这两天在处理边界问题上有点神志不清......为了从80的暴力变成100,花了整整一个下午+一个 ...
- 3728 联合权值[NOIP 2014 Day1 T2]
来源:NOIP2014 Day1 T2 OJ链接: http://codevs.cn/problem/3728/ https://www.luogu.org/problemnew/show/P1351 ...
- [NOIP2016 DAY1 T2]天天爱跑步-[差分+线段树合并][解题报告]
[NOIP2016 DAY1 T2]天天爱跑步 题面: B[NOIP2016 DAY1]天天爱跑步 时间限制 : - MS 空间限制 : 565536 KB 评测说明 : 2s Description ...
- 【NOIP2016】DAY1 T2 天天爱跑步
[NOIP2016]DAY1 T2 天天爱跑步 Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.?天天爱跑步?是一个养成类游戏,需要玩家每天按时 ...
- 国庆集训 Day1 T2 生成图 DP
国庆集训 Day1 T2 生成图 现在要生成一张\(n\)个点的有向图.要求满足: 1.若有 a->b的边,则有 b->a 的边 2.若有 a->b 的边和 b->c 的边,则 ...
- 【 NOIP2015 DAY1 T2 信息传递】带权并查集
题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...
- 【NOIP2015提高组】 Day1 T2 信息传递
题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...
- 【NOIP 2016】Day1 T2 天天爱跑步
Problem Description 小 C 同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...
随机推荐
- Adams宏导出
var set var=ip integer_value=1 var set var=macro_name str="" for variable_name=the_macro o ...
- springboot mybatis常见异常及处理方法
1.in导致的异常 Data truncation: Truncated incorrect DOUBLE value: 异常过程: mapper接口如下: public int updateBatc ...
- Vue.js 源码分析(五) 基础篇 方法 methods属性详解
methods中定义了Vue实例的方法,官网是这样介绍的: 例如:: <!DOCTYPE html> <html lang="en"> <head&g ...
- kubernetes使用阿里云cpfs持久存储
目录 简介 安装cpfs客户端 kubernetes使用cfs作为持久存储 简介 cpfs的具体介绍可参考这里: https://help.aliyun.com/document_detail/111 ...
- pytorch tutorial 2
这里使用pytorch进行一个简单的二分类模型 导入所有我们需要的库 import torch import matplotlib.pyplot as plt import torch.nn.func ...
- 单点登录(sso)入门
单点登录的英文名叫做Single Sign On,简称SSO. 在以前,一般我们就单系统,所有的功能都在同一个系统上. 后来,我们为了合理利用资源和降低耦合性,于是把单系统拆分成多个子系统. 比如阿里 ...
- SSM基本依赖及配置
需要了解具体配置文件的作用到:SSM基本配置详解 示例项目:SSMDemo 依赖 基本依赖 <properties> <spring.version>5.0.6.RELEASE ...
- 11、VUE混合
1.混合的概念(mixture) 混合是以一种灵活的方式,为组件提供代码复用功能.(类似于封装) 混合对象可以包含任意的组件选项.当组件使用了混合对象时,混合对象的所有选项将被“混入”组件自己的选项中 ...
- IdentityService4学习笔记之Authorization Code
前文 本文所有内容来自官方文档,如果有写不明白的地方请下方留言或查看官方文档. 今天介绍Authorization Code模式,适用于保密类型的客户端,保密类型客户端可以理解为在服务器端生成页面(比 ...
- juc包下四大并发工具
juc.CountDownLatch 闭锁 一个线程在等待一组线程后再恢复执行 await()等待其他线程执行完毕 被等待线程执行完毕后计数器-1 如何知道其他线程执行完了? 计数器,若一组线程为,C ...