Wannafly挑战赛10 D 小H的询问(线段树)
题目链接 Problem D
这个题类似 SPOJ GSS3
做过那个题之后其实就可以秒掉这题了。
考虑当前线段树维护的结点
在那道题的基础上,这个题要多维护几个东西,大概就是左端点的奇偶性,右端点的奇偶性。
以及当前结点代表的区间是否是一个有效的子序列。
时间复杂度$O(nlogn)$
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define ls i << 1
#define rs i << 1 | 1
#define mid ((l + r) >> 1)
#define lson ls, l, mid
#define rson rs, mid + 1, r typedef long long LL; const int N = 1e5 + 10; struct node{
LL c, lc, rc, ret;
int lo, ro, flag;
} t[N << 2]; int n, m; void pushup(int i){
t[i].c = t[ls].c + t[rs].c;
t[i].ret = max(t[ls].ret, t[rs].ret);
t[i].flag = 0;
t[i].lc = t[ls].lc;
t[i].rc = t[rs].rc;
t[i].lo = t[ls].lo;
t[i].ro = t[rs].ro; if (t[ls].ro ^ t[rs].lo){
t[i].ret = max(t[i].ret, t[ls].rc + t[rs].lc);
if (t[ls].flag) t[i].lc = max(t[i].lc, t[ls].c + t[rs].lc);
if (t[rs].flag) t[i].rc = max(t[i].rc, t[rs].c + t[ls].rc);
if (t[ls].flag && t[rs].flag) t[i].flag = 1;
}
} void build(int i, int l, int r){
if (l == r){
scanf("%lld", &t[i].ret);
t[i].c = t[i].lc = t[i].rc = t[i].ret;
t[i].lo = t[i].ro = (t[i].ret & 1);
t[i].flag = 1;
return;
} build(lson);
build(rson);
pushup(i);
} void update(int i, int l, int r, int x, LL val){
if (l == x && l == r){
t[i].ret = t[i].c = t[i].lc = t[i].rc = val;
t[i].lo = t[i].ro = (val & 1);
t[i].flag = 1;
return;
} if (x <= mid) update(lson, x, val);
else update(rson, x, val);
pushup(i);
} node query(int i, int l, int r, int L, int R){
if (L <= l && r <= R) return t[i]; if (R <= mid) return query(lson, L, R);
if (L > mid) return query(rson, L, R); node ta = query(lson, L, mid);
node tb = query(rson, mid + 1, R); node ans;
ans.c = ta.c + tb.c;
ans.ret = max(ta.ret, tb.ret);
ans.flag = 0;
ans.lc = ta.lc;
ans.rc = tb.rc;
ans.lo = ta.lo;
ans.ro = tb.ro;
if (ta.ro ^ tb.lo){
ans.ret = max(ans.ret, ta.rc + tb.lc);
if (ta.flag) ans.lc = max(ans.lc, ta.c + tb.lc);
if (tb.flag) ans.rc = max(ans.rc, tb.c + ta.rc);
if (ta.flag && tb.flag) ans.flag = 1;
} return ans;
} int main(){ scanf("%d%d", &n, &m);
build(1, 1, n); while (m--){
int op;
scanf("%d", &op);
if (op == 0){
int x, y;
scanf("%d%d", &x, &y);
node ans = query(1, 1, n, x, y);
printf("%lld\n", ans.ret);
} else{
int x;
LL y;
scanf("%d%lld", &x, &y);
update(1, 1, n, x, y);
}
} return 0;
}
Wannafly挑战赛10 D 小H的询问(线段树)的更多相关文章
- Wannafly挑战赛2_D Delete(拓扑序+最短路+线段树)
Wannafly挑战赛2_D Delete Problem : 给定一张n个点,m条边的带权有向无环图,同时给定起点S和终点T,一共有q个询问,每次询问删掉某个点和所有与它相连的边之后S到T的最短路, ...
- 【牛客Wannafly挑战赛12】小H和圣诞树
题目 可以考虑边分治,对于某一种颜色,我们处理出分治边左右两边所有以这个颜色为端点的路径长度,之后随便拼一拼就好了 但是这样对于每一组询问都需要边分一遍,这样做复杂度是\(O(nm+n\log n)\ ...
- 【Wannafly挑战赛29F】最后之作(Trie树,动态规划,斜率优化)
[Wannafly挑战赛29F]最后之作(Trie树,动态规划,斜率优化) 题面 牛客 题解 首先考虑怎么计算\([l,r]\)这个子串的不同的串的个数. 如果\(l=1\),我们构建\(Trie\) ...
- 【Wannafly挑战赛10 - B】小H和密码(DP)
试题链接:https://www.nowcoder.com/acm/contest/72/B 题目描述 小H在击败怪兽后,被一个密码锁挡住了去路 密码锁由N个转盘组成,编号为1~N,每 ...
- Wannafly挑战赛10:A题:小H和迷宫
题目描述 小H陷入了一个迷宫中,迷宫里有一个可怕的怪兽,血量有N点,小H有三瓶魔法药水,分别可以使怪兽损失a%.b%.c%的血量(之后怪兽的血量会向下取整),小H想合理地运用这三瓶药水,使 ...
- BZOJ 3065 带插入区间K小值(sag套线段树)
3065: 带插入区间K小值 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 4696 Solved: 1527[Submit][Status][Di ...
- SPOJ-COT-Count on a tree(树上路径第K小,可持久化线段树)
题意: 求树上A,B两点路径上第K小的数 分析: 同样是可持久化线段树,只是这一次我们用它来维护树上的信息. 我们之前已经知道,可持久化线段树实际上是维护的一个前缀和,而前缀和不一定要出现在一个线性表 ...
- 树上第k小,可持久化线段树+倍增lca
给定一颗树,树的每个结点都有权值, 有q个询问,每个询问是 u v k ,表示u到v路径上第k小的权值是多少. 每个结点所表示的线段树,是父亲结点的线段树添加该结点的权值之后形成的新的线段树 c[ro ...
- 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...
随机推荐
- 《Cracking the Coding Interview》——第4章:树和图——题目1
2014-03-19 03:30 题目:判断一个二叉树是否为平衡二叉树,即左右子树高度相差不超过1. 解法:递归算高度并判断即可. 代码: // 4.1 Implement an algorithm ...
- ubuntu下eclipse 安装记录
基本是参考:http://www.metsky.com/archives/611.html 完成. 中间遇到小问题,在此记录下,方便遇到同样问题的难友. 先说下快速打开命令行快捷键:Ctrl+Alt+ ...
- 18、bootStap JavaScript插件
1.模态框 <!--模态框经过了优化,更加灵活,以弹出对话框的形式出现,具有最小和最实用的功能集.--> <button type="button" class= ...
- ASP.net MVC入门及Razor语法
一.MVC入门: 1.MVC简介 约定大于配置 2.MVC访问流程 csthml模板(razor模板)就是简化HTML的拼接的模板,最终还是生成html给浏览器显示,不能直接访问cshtml文件. 二 ...
- GCC 7.3 for Windows
GCC 6.1x Compilers 下载地址1: Mingw gcc 6.30下载 这个是某微软员工编译的版本 MinGW is a port of GCC to Windows. It is fr ...
- Opencv3.4.5安装包
这个资源是Opencv3.4.5安装包,包括Windows软件包,Android软件包,IOS软件包,还有opencv的源代码:需要的下载吧. 点击下载
- 替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child's parent first.
在将一个fragment替换到一个frameLayout的时候报错: code: transaction.replace(R.id.fragment_container, fragment2); 错误 ...
- 原始套接字--icmp相关
icmp请求 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <uni ...
- HDU 3549 基础网络流EK算法 Flow Problem
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit ...
- input file request.files[] 为空
需要在 form 里设置 一句话 : $('form').attr("enctype", "multipart/form-data"); <form e ...