POJ 3468 A Simple Problem with Integers
来源:互联网 发布:域名论坛网站 编辑:程序博客网 时间:2024/06/03 13:22
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
【分析】
此题是线段树区间更新问题和延迟标记。延迟标记:当我们在对某个节点rt进 行更新时,先不向其子节点更新(如果向其子节点更新,更新到叶子节点,那么更 新操作的时间复杂度就达到了O(n)),当我们要用到该节点的子代的时候,再将 该延迟标记向下移动,这样更新操作就仍为O(logn)的时间复杂度。
【代码】
/*延迟标记: 当我们在对某个节点rt进行更新时,先不向其子节点更新(如果向其子节点更新,更新到叶子节点,那么更新操作的时间复杂度就达到了O(n)),当我们要用到该节点的子代的时候,再将该延迟标记向下移动,这样更新操作就仍为O(logn)的时间复杂度。例:if(tree[t].add)就是用到子代后,再向下移动;if( left <= tree[t].l && right >= tree[t].r )表示找到对应的结点,标记,对此结点进行更新,不对子结点更新*/#include <stdio.h>#define L(t) ((t) << 1) // 左子树#define R(t) ((t) << 1 | 1) // 右子树#define MAXN 100010struct SegTree{ int l,r; long long add, sum; // 保存结点的值,和子树的和 int getMid() // 线段树中点 { return ( l + r) >> 1; } int getDis() { return r - l + 1; // 线段树的长度 }}tree[MAXN << 2];int arr[MAXN]; // 储存输入的数组// 函数功能:建立线段树void build(int left, int right, int t){ //递归构造 tree[t].l = left; tree[t].r = right; tree[t].add = 0; if(left == right) { tree[t].sum = arr[left]; //叶结点是数列中的一个数 return; } int mid = tree[t].getMid(); build(left, mid, L(t)); build(mid + 1, right, R(t)); tree[t].sum = tree[L(t)].sum + tree[R(t)].sum; //更新sum}/* 输入的命令需要等到下一个命令输入进来时才能完成修改,无论下一个命令是修改还是输出指令都会修改上次需要修改的区间 */// 函数功能:对给定区间进行加入输入的数void update(int left, int right, int a, int t){ //若当前子树被目标区间覆盖 更新子树的sum和增量(在本次查询中,增量下行到此为之) if( left <= tree[t].l && right >= tree[t].r ) { /* add!=0表示需要修改,但是此次命令时没有修改,只是把要修改的值保持到符合条件区间的第一个出现的位置, 下个命令后判断add成立,修改并使子树也不等于0,递归时修改子树并置回0,再表示不用修改 */ tree[t].add += a; tree[t].sum += a * tree[t].getDis(); return; } if( tree[t].add ) //向子结点传递增量,并更新其sum,最后清空自己的增量 { tree[L(t)].sum += tree[L(t)].getDis() * tree[t].add; tree[R(t)].sum += tree[R(t)].getDis() * tree[t].add; tree[L(t)].add += tree[t].add; tree[R(t)].add += tree[t].add; tree[t].add = 0; } int mid = tree[t].getMid(); if(right <= mid ) { update(left, right, a, L(t)); //目标区间仅在左子树上 } else if (left > mid ) { update(left, right, a, R(t)); //目标区间仅在右子树上 } else { update(left, mid, a, L(t)); //目标区间同时在左右子树上 update(mid + 1, right, a, R(t)); } tree[t].sum = tree[L(t)].sum + tree[R(t)].sum ; //更新父结点的sum}// 函数功能:查找输出的区间的和long long query(int left, int right, int t){ //printf("%d %d %d\n",left,right,tree[t].add); if(left <= tree[t].l && right >= tree[t].r ) { return tree[t].sum; } if( tree[t].add )//这一段和update函数一样,是一个pushDown { tree[L(t)].sum += tree[L(t)].getDis() * tree[t].add; tree[R(t)].sum += tree[R(t)].getDis() * tree[t].add; tree[L(t)].add += tree[t].add; tree[R(t)].add += tree[t].add; tree[t].add = 0; } int mid = tree[t].getMid(); if(right <= mid ) { return query(left, right, L(t)); // 递归传入的是左右区间值,没有中间值,再在L(t)里判断左右区间 } else if( left > mid ) { return query(left, right, R(t)); } else /* 只有当中间值在区间里时,才会在参数中加入中间值,并返回两个区间的加和 */ { return query(left, mid ,L(t)) +query(mid + 1, right, R(t)); }}int main(){ char op[5]; int n, q; scanf("%d%d",&n, &q); for(int i = 1; i <= n; ++i) { scanf("%d",&arr[i]); } build(1, n, 1);int a,b,c; for(int i = 0; i < q; ++i) { scanf("%s ",&op); // 输入字符串就不用考虑getchar() if(op[0] == 'Q') { scanf("%d %d",&a, &b); long long sum = query(a, b, 1); printf("%lld\n",sum); } else { scanf("%d %d %d",&a, &b, &c); update(a, b, c, 1); } } return 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
- MyEclipse编码设置,中文乱码解决方法,UTF-8,GBK
- poj 2192 Zipper
- final关键字
- 数据结构之链表
- shape和selector和layer-list
- POJ 3468 A Simple Problem with Integers
- 系统架构师成长之路(四)
- 点类上 加圆类
- VS 2008 编译iconv库
- Bug:“iterator_category”: 不是*的成员——当自定义函数与系统函数重名
- LR参数化的方式和具体实现步骤
- 清华梦的粉碎——写给清华大学的退学申请
- HZAU 1003 Alien invasion
- java26包和访问权限(二)