POJ 3468A Simple Problem with Integers(线段树 + Lazy Tag(延迟更新))
来源:互联网 发布:金融大数据就业前景 编辑:程序博客网 时间:2024/05/16 01:41
题目意思还是比较好理解的 大概是给你一些数字然后 操作也比较简单 只有两种操作 查询和更新 不过是需要用到线段树的延迟更新的 比较裸的模板题(羞耻~~)
尝试了好久用树状数组来写这个 但是后来发现树状数组好像是没有办法代替有延迟更新的线段树的 然后就放弃了 感觉还是有什么不知道的黑科技数据结构可以写这道题的吧~~
代码写的可能比较长 不过分块来看还是比较好理解的吧
#include<cstdio>#include<cstring>#define lson l,m,rt << 1#define rson m + 1 , r, rt << 1 | 1typedef long long ll;const int maxn = 100005;ll sum[maxn << 2];ll add[maxn << 2];void Pushup(int rt){ sum[rt] = sum[rt << 1] + sum[rt << 1 |1];}void Pushdown(int rt,int m){ if (add[rt]) { add[rt << 1] += add[rt]; add[rt << 1 | 1] += add[rt]; sum[rt << 1] += add[rt] * (m - (m >> 1)); sum[rt << 1 | 1] += add[rt] *(m >> 1); add[rt] = 0; }}void build(int l,int r,int rt){ add[rt] = 0; if(l == r) { scanf("%lld",&sum[rt]); return ; } int m = (l + r) >> 1; build(lson); build(rson); Pushup(rt);}void Update(int L,int R,int c,int l,int r,int rt) { if (L <= l && r <= R) { add[rt] += c; sum[rt] += (ll)(c * (r - l + 1)); return ; } Pushdown(rt , r - l + 1); int m = (l + r) >> 1; if (L <= m)Update(L , R, c, lson); if (R > m)Update(L , R, c, rson); Pushup(rt);}ll query(int L,int R,int l,int r,int rt){ if (L <= l && r <= R) { return sum[rt]; } Pushdown(rt , r - l + 1); int m = (l + r) >> 1; ll ret = 0; if (L <= m)ret += query(L , R, lson); if(R > m)ret += query(L , R, rson); return ret;}int main(){ char op[10]; int n , m, a, b, c; while (~scanf("%d%d",&n,&m)) { build(1 , n, 1); for (int i = 0; i < m; i ++) { scanf("%s",op); if (op[0] == 'C') { scanf("%d%d%d",&a,&b,&c); Update(a , b, c, 1 , n, 1); } else if(op[0] == 'Q') { scanf("%d%d",&a,&b); printf("%lld\n",query(a , b, 1, n, 1)); } } }}
0 0
- POJ 3468A Simple Problem with Integers(线段树 + Lazy Tag(延迟更新))
- poj 3468 A Simple Problem with Integers(线段树成段更新,延迟标记,Lazy)
- POJ 3468-A Simple Problem with Integers(线段树_区间更新+lazy标记)
- POJ 3468 A Simple Problem with Integers 线段树(区间更新,查询,lazy数组)
- Poj A Simple Problem with Integers(lazy线段树)
- Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)
- poj 3468 A Simple Problem with Integers(线段树、延迟更新)
- poj 3468 A Simple Problem with Integers(线段树成段更新lazy)
- poj 3468 A Simple Problem with Integers LAZY线段树
- poj 3468 A Simple Problem with Integers(线段树+lazy)
- POJ 3468 A Simple Problem with Integers(线段树、lazy思想)
- poj 3468 A Simple Problem with Integers(线段树,lazy思想 经典题)
- POJ-3468-A Simple Problem with Integers(线段树区间维护 重写Lazy)
- poj 3468 A Simple Problem with Integers 【线段树 + 区间更新lazy】
- POJ 3468 A Simple Problem with Integers(段更新的区间求和&Lazy思想&线段树)
- poj3468:A Simple Problem with Integers(线段树lazy)
- C - A Simple Problem with Integers POJ 3468(线段树+延迟标记)
- poj 3468 A Simple Problem with Integers(线段树+延迟标记)
- 转:有关常量的知识点
- android:fitsSystemWindows属性的用法
- 使用Android studio移植FFmpeg3.3进行转码
- mybatis log4j打印sql
- 排队(line)——2015年南海区青少年信息学奥林匹克竞赛试题(小学甲组)
- POJ 3468A Simple Problem with Integers(线段树 + Lazy Tag(延迟更新))
- noi--1159:Maze(bfs)
- MyBatis入门
- 排队(line)——2015年南海区青少年信息学奥林匹克竞赛试题(小学甲组)
- 将cglib动态代理思想带入Android开发
- 微信小程序开发手记之七:一个小程序上线后的总结(上)
- 也许这样理解 HTTPS 更容易
- tensorflow代码尝试
- Sophus李群,李代数