poj 3468 A Simple Problem with Integers
来源:互联网 发布:java target runtime 编辑:程序博客网 时间:2024/05/29 19:54
原题链接:http://poj.org/showsource?solution_id=13997740
线段树,区间求和,成段更新,若有不明白的单步调式观察中间值,很好理解的。
不知道是我写的烂,还是线段树常数太大,反正程序跑的很慢o(╯□╰)o。
具体的看代码吧。
#include<stdio.h>#include<stdlib.h>#include<string.h>#define Max_N 200000typedef long long ll;typedef struct _seg{ ll val, addMark;}SegTree;SegTree seg[Max_N << 2];void built(ll root, ll x, ll y){ seg[root].addMark = 0; if (x == y){ scanf("%lld", &seg[root].val); return; } else { ll mid = (x + y) >> 1; built(root << 1, x, mid); built(root << 1 | 1, mid + 1, y); seg[root].val = seg[root << 1].val + seg[root << 1 | 1].val; }}void pushDown(ll root, ll len){ if (seg[root].addMark != 0){ seg[root << 1].addMark += seg[root].addMark; seg[root << 1 | 1].addMark += seg[root].addMark; seg[root << 1].val += (len - (len >> 1)) * seg[root].addMark; seg[root << 1 | 1].val += (len >> 1) * seg[root].addMark; seg[root].addMark = 0; }}void update(ll root, ll _x, ll _y, ll x, ll y, ll val){ if (x > _y || y < _x) return; if (x <= _x && _y <= y){ seg[root].addMark += val; seg[root].val += val * (_y - _x + 1); return; } pushDown(root, _y - _x + 1); ll mid = (_x + _y) >> 1; update(root << 1, _x, mid, x, y, val); update(root << 1 | 1, mid + 1, _y, x, y, val); seg[root].val = seg[root << 1].val + seg[root << 1 | 1].val;}ll query(ll root, ll _x, ll _y, ll x, ll y){ if (x > _y || y < _x) return 0; if (x <= _x && _y <= y) return seg[root].val; pushDown(root, _y - _x + 1); ll mid = (_x + _y) >> 1; ll v1 = query(root << 1, _x, mid, x, y); ll v2 = query(root << 1 | 1, mid + 1, _y, x, y); return v1 + v2;}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout);#endif char ch; ll a, b, c, N, Q; while (~scanf("%lld %lld", &N, &Q)){ built(1, 1, N); while (Q--){ getchar(); scanf("%c", &ch); if ('Q' == ch){ scanf("%lld %lld", &a, &b); printf("%lld\n", query(1, 1, N, a, b)); } else { scanf("%lld %lld %lld", &a, &b, &c); update(1, 1, N, a, b, 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
- [NFC]NCI简单介绍
- URAL 1025 — Democracy in Danger
- 【Unity5】实现24小时日出日落的效果 24小时天幕系统
- (初来乍到,请多关照)有关C语言链表的建立,排序问题
- android和iOS平台的崩溃捕获和收集
- poj 3468 A Simple Problem with Integers
- 建议8.8:表的设计要尽量满足第二范式(2NF),基于提升性能的考虑可以适当增加冗余而不必满足第三范式(3NF)。(2)
- php系统共享模板问题
- Hello Baidu Map
- VM虚拟机克隆无法设置静态IP
- 建议8.8:表的设计要尽量满足第二范式(2NF),基于提升性能的考虑可以适当增加冗余而不必满足第三范式(3NF)。(3)
- hough图像处理并非使用matlab自带函数(容易理解)
- VS2008 未找到编译器可执行文件 csc.exe【当网上其他方法试玩了之后不起作用的时候再用这个方法】
- [NFC]NFC启动流程1