POJ 3468 A Simple Problem with Integers?(线段树成段更新求和)
来源:互联网 发布:普通话水平测试软件 编辑:程序博客网 时间:2024/06/05 05:49
POJ 3468 A Simple Problem with Integers?(线段树成段更新求和)
ACM
题目地址:POJ 3468 A Simple Problem with Integers?
题意:
给出n个数,q个操作,'C'对区间每个数加值,'Q'查询区间和。
分析:
裸的线段树成段更新。
代码:
/** Author: illuz <iilluzen[at]gmail.com>* Blog: http://blog.csdn.net/hcbbt* File: 3468.cpp* Create Date: 2014-08-08 15:53:07* Descripton: segment tree*/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)#define lson(x) ((x) << 1)#define rson(x) ((x) << 1 | 1)typedef long long ll;const int N = 100100;const int ROOT = 1;struct seg {ll w;ll flag;};struct segment_tree {seg node[N << 2];void update(int pos) {node[pos].w = node[lson(pos)].w + node[rson(pos)].w;}void build(int l, int r, int pos) {node[pos].flag = 0;if (l == r) {scanf("%lld", &node[pos].w);return;}int m = (l + r) >> 1;build(l, m, lson(pos));build(m + 1, r, rson(pos));update(pos);}void push(int l, int r, int pos) {seg& fa = node[pos];seg& lhs = node[lson(pos)];seg& rhs = node[rson(pos)];int len = r - l + 1;if (fa.flag) {lhs.flag += fa.flag;rhs.flag += fa.flag;lhs.w += fa.flag * (len - (len >> 1));rhs.w += fa.flag * (len >> 1);fa.flag = 0;}}// add the point [x,y] with zvoid modify(int l, int r, int pos, int x, int y, ll z) {if (x <= l && r <= y) {int len = r + 1 - l;node[pos].w += z * len;node[pos].flag += z;return;}push(l, r, pos);int m = (l + r) >> 1;if (x <= m)modify(l, m, lson(pos), x, y, z);if (y > m)modify(m + 1, r, rson(pos), x, y, z);update(pos);}// query the segment [x, y]ll query(int l, int r, int pos, int x, int y) {if (x <= l && r <= y)return node[pos].w;push(l, r, pos);int m = (l + r) >> 1;ll res = 0;if (x <= m)res += query(l, m, lson(pos), x, y);if (y > m)res += query(m + 1, r, rson(pos), x, y);return res;}} sgm;int n, q, ra, rb;ll rc;char op[3];int main() {while (~scanf("%d%d", &n, &q)) {sgm.build(1, n, ROOT);while (q--) {scanf("%s", op);if (op[0] == 'Q') {scanf("%d%d", &ra, &rb);printf("%lld\n", sgm.query(1, n, ROOT, ra, rb));} else {scanf("%d%d%lld", &ra, &rb, &rc);sgm.modify(1, n, ROOT, ra, rb, rc);}}}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 线段树 区间更新求和
- 20140719 「线段树 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers (线段树,成段更新,区间求和)
- POJ 3468 A Simple Problem with Integers(段更新的区间求和&Lazy思想&线段树)
- WebRTC源代码探索之旅——多线程篇(2)
- POJ 1753 Flip Game (DFS)
- 利用LruCache加载网络图片实现图片瀑布流效果(改进版)
- 如何搭建个人服务器,建立自己的网站
- 我的“天气宝”上线啦
- POJ 3468 A Simple Problem with Integers?(线段树成段更新求和)
- python re模块的(...),group(),groups()
- linux 环境的设置
- iOS 工作笔记之PKRevealController和UIScrollView,UITableView的配合使用
- 套接字编程:socket模块和SocketServer模块
- java_Socket
- 解决 IE6 position:fixed 固定定位问题(div固定,不随着滚动条滚动)
- HDU 4950 Monster
- Linux下文件编程(1)