题意:给一个array,有两种操作,(1)修改某一个位置的值,(2)询问区间[L,R]内的最大子段和,其中子段需满足相邻两个数的位置的奇偶性不同

思路:假设对于询问操作没有奇偶性的限制,那么记录区间的最大子段和就可以通过合并区间得到答案了。加上奇偶性的限制后,记录的信息必须更加具体,需要把子段的端点的奇偶性加进去,也就是说一个区间需要记录4个值, 分别是奇奇,奇偶,偶偶,偶奇,然后同样可以通过合并区间来得到答案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ******************************************************************************** */
#include <iostream>                                                                 //
#include <cstdio>                                                                   //
#include <cmath>                                                                    //
#include <cstdlib>                                                                  //
#include <cstring>                                                                  //
#include <vector>                                                                   //
#include <ctime>                                                                    //
#include <deque>                                                                    //
#include <queue>                                                                    //
#include <algorithm>                                                                //
using namespace std;                                                                //
                                                                                    //
#define pb push_back                                                                //
#define mp make_pair                                                                //
#define X first                                                                     //
#define Y second                                                                    //
#define all(a) (a).begin(), (a).end()                                               //
#define foreach(i, a) for (typeof(a.begin()) it = a.begin(); it != a.end(); it ++)  //
                                                                                    //
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}    //
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>                    //
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;          //
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>      //
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>              //
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>   //
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //
                                                                                    //
typedef pair<intint> pii;                                                         //
typedef long long ll;                                                               //
typedef unsigned long long ull;                                                     //
                                                                                    //
/* -------------------------------------------------------------------------------- */
                                                                                    //
template<typename T>bool umax(T &a, const T &b) {
    return a >= b? false : (a = b, true);
}
 
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
 
const ll inf = (ll)1e18;
const int maxn = 1e5 + 7;
 
struct SegTree {
private:
    struct Node {
        ll a[4];
    };
    Node tree[maxn << 2];
    int n;
    bool chk(int i, int j) {
        return (i & 1) ^ (j >> 1);
    }
    int get(int i, int j) {
        return (i & 2) | (j & 1);
    }
    Node merge(const Node &nl, const Node &nr) {
        Node ans;
        for (int i = 0; i < 4; i ++) {
            ans.a[i] = nl.a[i];
            umax(ans.a[i], nr.a[i]);
        }
        for (int i = 0; i < 4; i ++) {
            for (int j = 0; j < 4; j ++) {
                if (chk(i, j)) {
                    umax(ans.a[get(i, j)], nl.a[i] + nr.a[j]);
                }
            }
        }
        return ans;
    }
    void build(int l, int r, int rt) {
        if (l == r) {
            int x;
            RI(x);
            int buf = (l & 1) << 1 | (l & 1);
            for (int i = 0; i < 4; i ++) tree[rt].a[i] = i == buf? x : -inf;
            return ;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        tree[rt] = merge(tree[rt << 1], tree[rt << 1 | 1]);
    }
    void update(int p, int x, int l, int r, int rt) {
        if (l == r) {
            tree[rt].a[(p & 1) << 1 | (p & 1)] = x;
            return ;
        }
        int m = (l + r) >> 1;
        if (p <= m) update(p, x, lson);
        else update(p, x, rson);
        tree[rt] = merge(tree[rt << 1], tree[rt << 1 | 1]);
    }
    Node query(int L, int R, int l, int r, int rt) {
        if (L <= l && r <= R) return tree[rt];
        int m = (l + r) >> 1;
        if (R <= m) return query(L, R, lson);
        if (L > m) return query(L, R, rson);
        return merge(query(L, R, lson), query(L, R, rson));
    }
public:
    void build(int nn) { n = nn; build(1, n, 1); }
    void update(int p, int x) { update(p, x, 1, n, 1); }
    ll query(int l, int r) {
        Node buf = query(l, r, 1, n, 1);
        ll ans = buf.a[0];
        for (int i = 1; i < 4; i ++) umax(ans, buf.a[i]);
        return ans;
    }
};
SegTree st;
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
#endif // ONLINE_JUDGE
    int T;
    cin >> T;
    while (T --) {
        int n, m;
        RI(n, m);
        st.build(n);
        for (int i = 0; i < m; i ++) {
            int t, a, b;
            RI(t, a, b);
            if (t) st.update(a, b);
            else printf("%I64d\n", st.query(a, b));
        }
    }
    return 0;                                                                       //
}                                                                                   //
                                                                                    //
                                                                                    //
                                                                                    //
/* ******************************************************************************** */

[hdu5316]线段树的更多相关文章

  1. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  2. 2018.07.08 hdu5316 Magician(线段树)

    Magician Problem Description Fantasy magicians usually gain their ability through one of three usual ...

  3. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  4. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  5. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  6. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  7. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  8. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  9. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

随机推荐

  1. PHP-fpm 远程代码执行漏洞(CVE-2019-11043)复现

    简介 9 月 26 日,PHP 官方发布漏洞通告,其中指出:使用 Nginx + php-fpm 的服务器,在部分配置下,存在远程代码执行漏洞.并且该配置已被广泛使用,危害较大. 漏洞概述 Nginx ...

  2. Joomla 3.4.6 RCE 分析

    Joomla 3.4.6 RCE 漏洞分析,首发先知社区: https://xz.aliyun.com/t/6522 漏洞环境及利用 Joomla 3.4.6 : https://downloads. ...

  3. [linux] [nginx] 一键安装web环境全攻略phpstudy版,超详细!

    找到运行中的服务器(实例). 打开这个主要是看它的IP,是公网ip,公网ip,公网ip,重要的事情说三遍. 接下来我们可以不用在阿里云上操作了,直接用客户端操作,这两个客户端就是Xshell 5和Xf ...

  4. (一)微信小程序:实现引导页

    基本目录结构 index目录下文件操作步骤 1.针对index.wxml <!--index.wxml--> <view class="index-container&qu ...

  5. 前端面试题之HTML和css-很实用的知识点

    display: none; 与 visibility: hidden; 的区别 相同: 它们都能让元素不可见 区别: display:none;会让元素完全从渲染树中消失,渲染的时候不占据任何空间: ...

  6. sqlliab7-8

    less-7 https://www.jianshu.com/p/20d1282e6e1d ?id=0')) union select 1,'2','<?php @eval($_POST[&qu ...

  7. RSA,AES加解密算法的实现

    目录 Python实现RSA公钥加密算法 RSA公钥加密算法原理 RSA算法的Python实现 AES加解密算法实现 AES加解密算法原理 AES加解密算法Python实现 参考文献 Python实现 ...

  8. 酷狗音乐快速转换MP3格式的方法

    喜欢听音乐的朋友们,散步跑步的时候都是随身听,音乐可以给人带来力量,让人心情愉悦,有时候甚至还可以让我们忘记烦恼和忧愁,是一种不错的解压方式,所以热爱运动的宝宝们是离不来音乐的陪伴的,这样说来随身听的 ...

  9. XSS攻击简单介绍

    之前由我负责维护的一个项目被检测出存在可能被XSS攻击的漏洞. 吓得我赶紧恶补了下XSS. XSS,全称为Cross Site Script,跨站脚本攻击,是WEB程序中一种常见的漏洞.其主要的攻击手 ...

  10. 01-复杂度2 Maximum Subsequence Sum

    01-复杂度2 Maximum Subsequence Sum   (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...