POJ-3468 A Simple Problem with Integers (区间求和,成段加减)
来源:互联网 发布:工程图纸软件 编辑:程序博客网 时间:2024/06/03 13:38
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.
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.
You need to answer all Q commands in order. One answer in a line.
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
455915
区间求和,成段加减
#include#include #include using namespace std;#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1typedef long long LL;const int N = 100000 + 5;LL T[N<<2],add[N<<2];void PushUP(int rt){ T[rt] = T[rt<<1] + T[rt<<1|1];}void PushDown(int rt, int m){ if(add[rt]){ add[rt<<1] += add[rt]; add[rt<<1|1] += add[rt]; T[rt<<1] += add[rt]*(LL)(m - (m >> 1)); T[rt<<1|1] += add[rt]*(LL)(m >> 1); add[rt] = 0; }}void Build(int l, int r, int rt){ add[rt] = 0; if(l == r){ scanf("%lld",&T[rt]); return ; } int m = (l + r) >> 1; Build(lson); Build(rson); PushUP(rt);}void Updata(int L,int R, int c,int l, int r, int rt){ if(L <= l && r <= R){ add[rt] += c; T[rt] += (LL)c*(r - l + 1); return ; } PushDown(rt, r - l + 1); int m = (l + r) >> 1; if(L <= m) Updata(L, R, c, lson); if(R > m) Updata(L, R, c, rson); PushUP(rt);}LL Query(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) return T[rt]; PushDown(rt, r - l + 1); int m = (l + r) >> 1; LL ret = 0; if(L <= m) ret += Query(L, R, lson); if(R > m) ret += Query(L, R, rson); return ret;}int main(){ int n,m; while(scanf("%d %d",&n,&m)==2){ Build(1, n, 1); char ch[5]; int a,b,c; while(m--){ scanf("%s %d %d",ch,&a,&b); if(ch[0] == 'Q')printf("%lld\n",Query(a, b, 1, n, 1)); else{ scanf("%d",&c); Updata(a, b, c, 1, n, 1); } } } 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 (splay 成段更新、区间求和)
- POJ 3468 A Simple Problem with Integers(段更新的区间求和&Lazy思想&线段树)
- poj-3468 A Simple Problem with Integers-线段树-整段区间增加求和
- A Simple Problem with Integers 【线段树】-区间加减求和
- POJ - 3468 A Simple Problem with Integers (区间求和)
- POJ 3468 A Simple Problem with Integers(线段树 or 树状数组—区间求和,成段更新)
- poj 3468 A Simple Problem with Integers(线段树)(第二部分 成段更新,区间求和)
- poj 3468__A Simple Problem with Integers(区间求和,成段更新)
- POJ3468 A Simple Problem with Integers 线段树 区间成段更新+区间求和
- Android 多渠道打包最佳实践
- java面试题总结
- 排列组合 C(n,m)
- C语言中的可变参数-printf的实现原理 在C/C++中,对函数参数的扫描是从后向前的。C/C++的函数参数是通过压入堆栈的方式来给函数传参数的(堆栈是一种先进后出的数据结构),最先压入的参数最后出
- window.open 打开页面的方式
- POJ-3468 A Simple Problem with Integers (区间求和,成段加减)
- 制作虚线
- linux进程间通信方法
- HDU_1141 求n! <= 2^x,最大的n
- 从新建的layout中获取组件
- java中volatile关键字的含义
- MyBaits 插入多方数据出错之keyProperty的运用
- HDU 6113 度度熊的01世界【2017"百度之星"】【简单搜索判断联通块】
- pycharm 使用教程