POJ-3468-A Simple Problem with Integers (线段树 区间求和)
来源:互联网 发布:网上预约软件 编辑:程序博客网 时间:2024/04/29 22:41
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
一天了。。。研究这么点东西。。。sad。。。
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>using namespace std;#define LL long longconst int inf = 111111;LL add[inf << 2];LL sum[inf << 2];LL anst;void pushup (int ii)//区间向上更新{ sum[ii] = sum[ii * 2] + sum[ii * 2 + 1];}void pushdown (int ii,int m)//区间下放{ if ( add[ii] ) { add[ii * 2 ] += add[ii]; add[ii * 2 + 1] += add[ii]; sum[ii * 2] += add[ii] * (m-(m >> 1)); sum[ii * 2 + 1] += add[ii] * (m >> 1);//因为在建树的时候右边总是 m>>1 左边不确定有多少个 add[ii] = 0; }}void build (int ii,int l,int r){ add[ii] = 0; if ( l == r ) { scanf ("%I64d",&sum[ii]); return ; } int m = ( l + r ) >> 1; build (ii * 2,l,m); build (ii * 2 + 1,m + 1,r); pushup (ii);}LL query (int ii,int l,int r,int a,int b){ if ( a <= l && r <= b) { return sum[ii]; } pushdown (ii,r - l + 1); int m = (l + r)>>1; LL ans = 0; if ( a <= m ) ans += query (ii * 2,l,m,a,b); if ( b > m) ans += query (ii * 2 + 1,m + 1,r,a,b); return ans;}void updata (int ii,int l,int r,int a,int b,int z){ if ( a <= l && r <= b)//区间被完全覆盖 { add[ii] += z; sum[ii] += (LL) ((r - l + 1) * z);//当前节点的值 也就是此节点以下值得和 都加上z r - l + 1就代表区间长度 return ; } pushdown (ii,r - l + 1); int m = ( r + l )>>1; if ( a <= m ) updata (ii * 2,l,m,a,b,z); if ( b > m) updata ( ii * 2 + 1,m + 1,r,a,b,z); pushup (ii);}int main (){ int n,q; scanf ("%d%d",&n,&q); build (1,1,n); while (q --) { char op[5]; int a,b,c; scanf ("%s",op); if ( op[0] == 'Q') { scanf ("%d%d",&a,&b); anst = query (1,1,n,a,b);//从1号节点开始 树的区间从1到n 待查询区间 从a到b printf ("%I64d\n",anst); } else { scanf ("%d%d%d",&a,&b,&c); updata (1,1,n,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(线段树,树状数组区间求和)
- A Simple Problem with Integers(线段树,区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 POJ 3468 A Simple Problem with Integers
- Sprite Sheet Animation
- 跨平台UDP组播(多播)编程
- (基础dp).Apple Catching
- VS2010编译运行的程序输出的中文乱码,无法显示中文
- 如何使用CBO,CBO与RULE的区别
- POJ-3468-A Simple Problem with Integers (线段树 区间求和)
- ZOJ-1236
- HDU1019
- Hash Table Lab
- UVA - 10112 Myacm Triangles
- 多个C文件都会用到的结构体定义
- BFS算法
- iOS开发多线程篇—线程的状态
- 通过分类为数组添加一个倒序的一个方法. 比如: 数组中元素为 @”aa”, @”bb”, @”cc”, @”dd”, @”ee”, 倒序完之后为: @”ee”, @”dd”,@”cc”,@”bb”,@