一開始度错题了,题意是求一段和最大的【子序列】,要求相邻两个元素的位置必须互为奇偶。

这样我们能够使用线段树维护4个值:

一段区间内开头结尾元素为:

奇奇

奇偶

偶奇

偶偶

的最大值

之后在pushup的的时候依据题目所给的意思进行合并。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
typedef long long LL;
const int maxn = 100005;
const LL INF = 9999999999999999LL;
int n,m;
struct Node{
LL oe;
LL oo;
LL ee;
LL eo;
}node[maxn << 2];
LL value[maxn];
Node pushnode(Node p,Node q){
Node newNode;
newNode.oe = max(max(p.oe,q.oe),max(p.oo + q.ee,p.oe + q.oe));
newNode.oo = max(max(p.oo,q.oo),max(p.oo + q.eo,p.oe + q.oo));
newNode.ee = max(max(p.ee,q.ee),max(p.eo + q.ee,p.ee + q.oe));
newNode.eo = max(max(p.eo,q.eo),max(p.eo + q.eo,p.ee + q.oo));
return newNode;
}
void pushup(int pos){
node[pos].oe = max(max(node[lson].oe,node[rson].oe),max(node[lson].oo + node[rson].ee,node[lson].oe + node[rson].oe));
node[pos].oo = max(max(node[lson].oo,node[rson].oo),max(node[lson].oo + node[rson].eo,node[lson].oe + node[rson].oo));
node[pos].ee = max(max(node[lson].ee,node[rson].ee),max(node[lson].eo + node[rson].ee,node[lson].ee + node[rson].oe));
node[pos].eo = max(max(node[lson].eo,node[rson].eo),max(node[lson].eo + node[rson].eo,node[lson].ee + node[rson].oo));
}
void build(int l,int r,int pos){
if(l == r){
if(l & 1){
node[pos].oe = node[pos].ee = node[pos].eo = -INF;
node[pos].oo = value[l];
}
else{
node[pos].oe = node[pos].oo = node[pos].eo = -INF;
node[pos].ee = value[l];
}
return;
}
int mid = (l + r) >> 1;
build(l,mid,lson);
build(mid + 1,r,rson);
pushup(pos);
}
void update(int l,int r,int pos,int to,int v){
if(l == r){
if(l & 1){
node[pos].oe = node[pos].ee = node[pos].eo = -INF;
node[pos].oo = v;
}
else{
node[pos].oe = node[pos].oo = node[pos].eo = -INF;
node[pos].ee = v;
}
return;
}
int mid = (l + r) >> 1;
if(to <= mid)
update(l,mid,lson,to,v);
else
update(mid + 1,r,rson,to,v);
pushup(pos);
}
Node query(int l,int r,int L,int R,int pos){
if(L <= l && r <= R){
return node[pos];
}
int mid = (l + r) >> 1;
if(R <= mid)
return query(l,mid,L,R,lson);
else if(L > mid)
return query(mid + 1,r,L,R,rson);
else{
Node v1 = query(l,mid,L,R,lson);
Node v2 = query(mid + 1,r,L,R,rson);
return pushnode(v1,v2);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++){
scanf("%I64d",&value[i]);
}
build(1,n,1);
for(int i = 0; i < m; i++){
int op;
LL a,b;
scanf("%d%I64d%I64d",&op,&a,&b);
if(op == 0){
Node nans = query(1,n,a,b,1);
LL ans = max(max(nans.oo,nans.eo),max(nans.ee,nans.oe));
printf("%I64d\n",ans);
}
else if(op == 1)
update(1,n,1,a,b);
}
}
return 0;
}

最新文章

  1. 如何:加载分页结果(WCF 数据服务)
  2. Windows7台式电脑怎么调节屏幕亮度
  3. 基于谷歌地图的Dijkstra算法水路路径规划
  4. Linux之硬件管理(不断更新中)
  5. C语言预处理命令之条件编译
  6. 表(list)
  7. 记录关于使用ADO.NET 连接池连接Oracle时Session信息不更新的坑
  8. poj1236 Network of Schools【强连通分量(tarjan)缩点】
  9. 家庭洗车APP --- Androidclient开展 之 网络框架包介绍(一)
  10. openMP编程(上篇)之指令和锁
  11. ZooKeeper安装(Windows)
  12. 【MM系列】SAP MB1A MB1B MB1C MB11 MIGO的区别解析
  13. 移动APP用例设计中的关键点(转载)
  14. 2017-12-14python全栈9期第一天第四节之python分类
  15. MySQL数据库下载、安装
  16. 对ES6的一次小梳理
  17. win32: 查询滚动条相关信息的注意事项
  18. Java在Service层异常封装
  19. QQ帐户的申请与登陆-(字符串操作)
  20. 【10-2】复杂业务状态的处理(从状态者模式到FSM)

热门文章

  1. Netty源码学习(零)前言
  2. struts2进阶
  3. spark-join算子
  4. Spring Boot特点
  5. DotnetBrowser高级教程-(4)使用MVC框架3-文件上传
  6. lamp+nginx代理+discuz+wordpress+phpmyadmin
  7. git学习——查看提交历史
  8. 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT-3 读写注册表
  9. phpcms前台任意代码执行漏洞(php&lt;5.3)
  10. Tomcat Connector的三种不同的运行模式