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》——第17章:普通题——题目14
2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
- 《Cracking the Coding Interview》——第14章:Java——题目1
2014-04-26 18:20 题目:从继承的角度,把构造函数设成private有什么意义? 解法:就不能继承了.单体模式里也这么干,目的是为了不让使用者自主生成对象,进行限制. 代码: // 14 ...
- java中封装的概念
封装(Encapsulation)是类的三大特性之一, 就是将类的状态信息隐藏在类的内部,不允许外部程序直接访问, 而是通过该类提供的方法来实现对隐藏信息的操作和访问. 简而言之,就是隐藏内部实现,提 ...
- TIDB介绍
TiDB 是什么? TiDB 是一个分布式 NewSQL 数据库.它支持水平弹性扩展.ACID 事务.标准 SQL.MySQL 语法和 MySQL 协议,具有数据强一致的高可用特性,是一个不仅适合 O ...
- JavaScript简易学习笔记
学习地址:http://www.w3school.com.cn/js/index.asp 文字版: https://github.com/songzhenhua/github/blob/master/ ...
- ADB连接手机遇到的问题:list of devices attached
今天工作时想尝试一下使用ADB无线连接手机,结果遇到了下面这样的问题,浪费了几十分钟的时间,挺闹心的,因此想分享出来... 首先 第一步:使用USB数据线连接手机,手机弹出选项时,选择仅充电,然后wi ...
- java IO小结
package 字符与字节转换; import java.io.*; public class char_byte { public static void main(String[] args) { ...
- 孤荷凌寒自学python第二十九天python的datetime.time模块
孤荷凌寒自学python第二十九天python的datetime.time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) datetime.time模块是专门用来表示纯时间部分的类. ...
- hnust py road
问题 C: Py Road 时间限制: 1 Sec 内存限制: 128 MB提交: 125 解决: 34[提交][状态][讨论版] 题目描述 Life is short,you need Pyth ...
- android ListView与EditText共存错位
在一个ListView中,如果里面有EditText会很麻烦,因为修改EditText里面的数据会发生错位现象. 这时候,需要在适配器BaseAdapter的getView中设置setTag(),将p ...