POJ3468 线段树-模板题
2013-02-09 08:15
316 查看
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
Sample Output
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
题意很明显,只有两种操作,需要使用线段树(对于区间的修改~):update()和query() 用到了__int64需要注意
代码如下:
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 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
Hint
The sums may exceed the range of 32-bit integers.
Source
POJ Monthly--2007.11.25, Yang Yi
题意很明显,只有两种操作,需要使用线段树(对于区间的修改~):update()和query() 用到了__int64需要注意
代码如下:
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; const int MAXN = 100000 + 10; struct Node{ int l, r; __int64 k, add; }; Node znode[4*MAXN]; __int64 zfun(__int64 a, __int64 b) { return a+b; } void build(int l, int r, int n){ int mid = (l+r)/2; znode .l = l; znode .r = r; znode .add = 0; znode .k = 0; if(l==r) return; else { build(l, mid, 2*n); build(mid+1, r, 2*n+1); } } void update(int l, int r, int n, __int64 val){ znode .k += (r-l+1)*val; if(l<=znode .l && znode .r<=r) znode .add += val; else{ int mid = (znode .l + znode .r) / 2; if(r<=mid) update(l, r, 2*n, val); else if(l>mid) update(l, r, 2*n+1, val); else { update(l, mid, 2*n, val); update(mid+1, r, 2*n+1, val); } } } __int64 query(int l, int r, int n){ //l、r是待查询的区间端点 int mid = (znode .l + znode .r) / 2; if(znode .l==l && znode .r==r) return znode .k; else{ if(znode .add){ //下放延迟的标记 znode[2*n].add += znode .add; znode[2*n].k += znode .add*(znode[2*n].r-znode[2*n].l+1); znode[2*n+1].add += znode .add; znode[2*n+1].k += znode .add*(znode[2*n+1].r-znode[2*n+1].l+1); znode .add = 0; } if(r<=mid) return query(l, r, 2*n); else if(l>mid) return query(l, r, 2*n+1); else return zfun(query(l, mid, 2*n), query(mid+1, r, 2*n+1)); } } int main(){ // freopen("in.txt", "r", stdin); int id = 1, n, m; while(scanf("%d%d", &n, &m)!=EOF){ build(1, n, 1); int i, s; for(i=1; i<=n; i++){ scanf("%d", &s); update(i, i, 1, s); } char ch[2]; int a, b; __int64 c; for(i=1; i<=m; i++){ scanf("%s%d%d", ch, &a, &b); if(ch[0]=='Q') { __int64 ans = query(a, b, 1); printf("%I64d\n", ans); } else if(ch[0]=='C'){ scanf("%I64d", &c); update(a, b, 1, c); } } } return 0; }
相关文章推荐
- 【洛谷P3372】【模板】线段树1
- 线段树 扫描线求矩阵面积并的模板(zz)
- 【模板】线段树
- Color the ball (线段树,区间更新模板题)
- 线段树单点更新模板
- 线段树染色模板,用于颜色较少的情况。 POJ 2777
- hdu 1166 线段树 点更新(孙启龙模板)
- P3373 【模板】线段树 2
- 线段树基本模板
- 线段树模板
- 洛谷P3374模板线段树1
- hunnu11460—区间求最值(线段树模板)
- hdu1166线段树的点跟新模板
- POJ 2155 Matrix 【二维线段树模板题】
- 洛谷3372[模板]线段树1
- [模板]线段树
- UVa 11297 Census 二维线段树模板
- 线段树(大三的模板)
- I Hate It HDU - 1754(线段树单点修改,区间求最大值模板)
- 线段树模板(POJ3468)