POJ-3468-线段树-A Simple Problem with Integers
来源:互联网 发布:虚拟设计软件 编辑:程序博客网 时间:2024/06/03 20:24
题意
求区间和
C abc表示给区间[a,b]里的数都加上c
思路
线段树+区间更新
区间更新就是在update时不一下全部更新 而是在区间上留下标记当查询区间时,再将标记向下传递 更新
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <vector>#include <string.h>using namespace std;const int maxn = 100100;long long sum[maxn << 2];long long addmark[maxn << 2];void pushup(int root){ sum[root] = sum[root << 1] + sum[root << 1 | 1];}void pushdown(int root,int m){ if (addmark[root]) { addmark[root << 1] += addmark[root]; addmark[root << 1 | 1] += addmark[root]; sum[root << 1] += addmark[root] * (m - (m >> 1)); sum[root << 1 | 1] += addmark[root] * (m >> 1); addmark[root] = 0; }}void build(int l, int r, int root){ addmark[root] = 0; if (r == l) { scanf("%lld", &sum[root]); return; } int m = (l + r) >> 1; build(l, m, root << 1); build(m + 1, r, root << 1 | 1); pushup(root);}void update(int L, int R, int l, int r, int root,int c){ if (L <= l&&r <= R) { addmark[root] += c; sum[root] += (long long)c*(r - l + 1); return; } pushdown(root, r - l + 1); int m = (l + r) >> 1; if (L <= m) update(L, R, l, m, root << 1,c); if (R > m) update(L, R, m + 1, r, root << 1 | 1, c); pushup(root);}long long query(int L, int R, int l, int r, int root){ if (L <= l&&r <= R) { return sum[root]; } pushdown(root, r - l + 1); int m = (l + r) >> 1; long long ans = 0; if (L <= m) ans += query(L, R, l, m, root << 1); if (m < R) ans += query(L, R, m + 1, r, root << 1 | 1); return ans;}int main(void){ int n,q; cin >> n >> q; build(1, n, 1); while (q--) { char s[5]; int a, b, c; scanf("%s", s); if (s[0] == 'Q') { scanf("%d%d", &a, &b); cout << query(a, b, 1, n, 1) << endl; } else { scanf("%d%d%d", &a, &b, &c); update(a, b, 1, n, 1, c); } } 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(线段树)
- 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(线段树区区)
- 线段树 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(线段树)
- 严重: Exception starting filter struts2 java.lang.NullPointerException
- 解决 百度地图+slidingmenu 切换Fragment闪黑屏问题
- hdu1754I Hate It(线段树)
- java 嵌入式的Web服务器jetty使用例子
- 缓冲区溢出分析第07课:MS06-040漏洞研究——静态分析
- POJ-3468-线段树-A Simple Problem with Integers
- 文本二叉树
- 挂载和卸载u盘(centos)
- 关于女人强势表现和强势心理
- Spring注解详解
- 缓冲区溢出分析第08课:MS06-040漏洞研究——动态调试
- java加密技术比较好的博客地址
- 求1+2!+3!+...+20!的和
- 物理学家:用数学理论证明人工智能无法超越人类智慧