线段树模板(二)——成段更新 + POJ 3468 A Simple Problem with Integers
来源:互联网 发布:js调用api接口实例 编辑:程序博客网 时间:2024/06/05 06:24
http://poj.org/problem?id=3468
/*2266ms,4508KB*/#include <cstdio>#include <algorithm>using namespace std;#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1#define root 1, N, 1#define LL long longconst int maxn = 100000;LL sum[maxn << 2], add[maxn << 2];inline void pushup(int rt){sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}inline void pushdown(int rt, int m){if (add[rt]){add[rt << 1] += add[rt];add[rt << 1 | 1] += add[rt];///更新左右子区间addsum[rt << 1] += add[rt] * (m - (m >> 1));sum[rt << 1 | 1] += add[rt] * (m >> 1);///平分父节点add,更新左右子区间和add[rt] = 0;///父节点add清零}}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 ql, int qr, int c, int l, int r, int rt){if (ql <= l && r <= qr){add[rt] += c;///存至此,不再往下更新sum[rt] += (LL)c * (r - l + 1);return;}pushdown(rt , r - l + 1);///用父节点add往下细分计算 int m = (l + r) >> 1;if (ql <= m) update(ql, qr, c, lson);if (qr > m) update(ql, qr, c, rson);pushup(rt);}LL query(int ql, int qr, int l, int r, int rt){if (ql <= l && r <= qr){return sum[rt];}pushdown(rt , r - l + 1);///用父节点add往下细分计算 int m = (l + r) >> 1;LL ret = 0;if (ql <= m) ret += query(ql, qr, lson);if (qr > m) ret += query(ql, qr, rson);return ret;}int main(){int N, Q, a, b, c;scanf("%d%d", &N, &Q);build(root);while (Q--){getchar();if (getchar() == 'Q'){scanf("%d%d", &a, &b);printf("%lld\n", query(a, b, root));}else{scanf("%d%d%d", &a, &b, &c);update(a, b, c, root);}}return 0;}
0 0
- 线段树模板(二)——成段更新 + POJ 3468 A Simple Problem with Integers
- 线段树 POJ 3468 A Simple Problem with Integers 线段树 成段更新入门
- POJ 3468 A Simple Problem with Integers(线段树 or 树状数组—区间求和,成段更新)
- poj 3468 A Simple Problem with Integers(线段树|成段更新,区间查询)
- POJ 3468 A Simple Problem with Integers 线段树 成段更新
- poj 3468 A Simple Problem with Integers(线段树,成段更新)
- POJ 3468 A Simple Problem with Integers 线段树 (成段更新)
- [ACM] poj 3468 A Simple Problem with Integers(线段树,成段更新,懒惰标记)
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
- 线段树(成段更新) 之 poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers (线段树---成段更新)
- POJ 3468-A Simple Problem with Integers(线段树:成段更新,区间求和)
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
- POJ 3468 A Simple Problem with Integers(线段树 成段更新)
- POJ 3468 A Simple Problem with Integers 线段树的成段更新
- POJ - 3468 - A Simple Problem with Integers (线段树 - 成段更新)
- POJ 3468 A Simple Problem with Integers (线段树,成段更新,区间求和)
- POJ 3468 A Simple Problem with Integers 线段树(成段更新)
- 如何备份MySql的数据库
- 内存问题
- System.ICloneable接口
- Android4.0中Contacts通话记录界面剖析(源码)
- 2014市场预测:大数据让企业获得“大智能”
- 线段树模板(二)——成段更新 + POJ 3468 A Simple Problem with Integers
- python下的MySQLdb使用
- PC浏览器安装vlc插件播放rtsp流
- 自定义位置反编码的用法
- 消息称即刻搜索即将关闭
- 修改Linux SSH默认22端口的几种方法
- ARM处理器NEON编程及优化技巧—左移右移等移位操作
- Epoll模型详解
- bean(2)