A Simple Problem with Integers(线段树)
来源:互联网 发布:深圳众诚网络 编辑:程序博客网 时间:2024/06/10 19:47
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 abc" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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
解题思路:
区间更新,区间求和的模板题。
注意:数据会超Int范围。
AC代码:
#include <iostream>#include <cstdio>using namespace std;#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1const int maxn = 100005;__int64 add[maxn << 2], sum[maxn << 2];void PushUp(int rt){ sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];}void PushDown(int rt,int m){ if (add[rt]) { add[rt << 1] += add[rt]; add[rt << 1 | 1] += add[rt]; sum[rt << 1] += add[rt] * (m - (m >> 1)); sum[rt << 1 | 1] += add[rt] * (m >> 1); add[rt] = 0; }}void build(int l,int r,int rt){ add[rt] = 0; if (l == r) { scanf("%I64d", &sum[rt]); return ; } int m = (l + r) >> 1; build(lson); build(rson); PushUp(rt);}void update(int L, int R, __int64 c, int l, int r, int rt){ if (L <= l && r <= R) { add[rt] += c; sum[rt] += c * (r - l + 1); return ; } PushDown(rt , r - l + 1); int m = (l + r) >> 1; if (L <= m) update(L , R , c , lson); if (m < R) update(L , R , c , rson); PushUp(rt);}__int64 query(int L,int R,int l,int r,int rt){ if (L <= l && r <= R) { return sum[rt]; } PushDown(rt , r - l + 1); int m = (l + r) >> 1; __int64 ret = 0; if (L <= m) ret += query(L , R , lson); if (m < R) ret += query(L , R , rson); return ret;}int main(){ int n, q, a, b, c; char ord[2]; scanf("%d%d", &n, &q); build(1, n, 1); while(q--) { scanf("%s", ord); if(ord[0] == 'C') { scanf("%d%d%d", &a, &b, &c); update(a, b, c, 1, n, 1); } else { scanf("%d%d", &a, &b); printf("%I64d\n", query(a, b, 1, n, 1)); } } return 0;}
- A Simple Problem with Integers(线段树)
- A Simple Problem with Integers----线段树
- 线段树 A Simple Problem with Integers
- 线段树 A Simple Problem with Integers
- POJ 3468 A SIMPLE PROBLEM WITH INTEGERS(线段树)
- Poj A Simple Problem with Integers(lazy线段树)
- 【hdu】A Simple Problem with Integers (线段树)
- hdu 4267 A Simple Problem with Integers(线段树)
- pku 3468 A Simple Problem with Integers(线段树)
- poj3468 A Simple Problem with Integers(spaly&&线段树)
- poj 3468 A Simple Problem with Integers ( 线段树 )
- POJ 3468 A Simple Problem with Integers (线段树)
- POJ 3468 A Simple Problem with Integers(线段树)
- poj3511--A Simple Problem with Integers(线段树求和)
- poj3468 A Simple Problem with Integers(线段树)
- A Simple Problem with Integers(线段树)
- poj 3468 A Simple Problem with Integers(线段树)
- poj3468(A Simple Problem with Integers)线段树实现
- jQuery中常用的函数方法总结
- hdu-2079-选课时间(题目已修改,注意读题)【母函数】
- COM问题初步
- NodeJs环境搭建
- oracle 11g physical standby switchover
- A Simple Problem with Integers(线段树)
- [Java基础]用流进行byte字节流的读写
- VS2005 start debuging出现错误code 0x80004002
- 音视频开发技术,让智能家居更智能!
- 【002】【Xcode6-Beta5】IOS静态库的制作与使用
- 读取本地文件存放list集合中
- D3D 纹理 例子程序
- 最长回文子串
- 集合框架(二)--Map集合