A Simple Problem with Integers 区间更新和查询
来源:互联网 发布:东方的漫画软件 编辑:程序博客网 时间:2024/05/29 19:16
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
标准的模板题,结果我debug了大半天!最终结果是我错写了一个字母!!!QAQ
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std; const int N = 1e5 + 5;struct tree{ int l, r; long long sum, add;}STree[N << 2];long long ans;void Build(int l, int r, int k){ STree[k].l = l; STree[k].r = r; STree[k].add = 0; if(l == r) { scanf("%lld", &STree[k].sum); return; } int mid = (l + r) >> 1; Build(l, mid, k << 1); Build(mid + 1, r, k << 1 | 1); STree[k].sum = STree[k << 1].sum + STree[k << 1 | 1].sum;}void Push_down(int k, int m){ if(STree[k].add) { STree[k << 1].add += STree[k].add; STree[k << 1 | 1].add += STree[k].add; STree[k << 1].sum += STree[k].add * (m - (m >> 1)); STree[k << 1 | 1].sum += STree[k].add * (m >> 1); STree[k].add = 0; }}void Update(int k, int a, int b, int c){ if(a <= STree[k].l && b >= STree[k].r) { STree[k].add += c; STree[k].sum += (STree[k].r - STree[k].l + 1) * c; //就是这里!我把后面的l写成r了... return; } Push_down(k, STree[k].r - STree[k].l + 1); int mid = (STree[k].l + STree[k].r) >> 1; if(a <= mid) Update(k << 1, a, b, c); if(b > mid) Update(k << 1 | 1, a, b, c); STree[k].sum = STree[k << 1].sum + STree[k << 1 | 1].sum;}void Query(int k, int a, int b){ if(STree[k].l >= a && STree[k].r <= b) { ans += STree[k].sum; return; } Push_down(k, STree[k].r - STree[k].l + 1); int mid = (STree[k].l + STree[k].r) >> 1; if(a <= mid) Query(k << 1, a, b); if(b > mid) Query(k << 1 | 1, a, b);}int main() { int n, q, a, b, c; char s[2]; scanf("%d%d", &n, &q); Build(1, n, 1); while(q--) { scanf("%s%d%d", s, &a, &b); if(s[0] == 'Q') { ans = 0; Query(1, a, b); printf("%lld\n", ans); } else { scanf("%d", &c); Update(1, a, b, c); } } return 0; }
- A Simple Problem with Integers 区间更新和查询
- poj3468 A Simple Problem with Integers(线段树,区间更新,区间和查询)
- POJ - 3468 A Simple Problem with Integers(线段树成段更新,查询区间和)
- poj 3468 A Simple Problem with Integers经典区间更新区间查询
- 线段树 区间更新 区间查询 poj3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers (树状数组解法 树状数组区间更新 区间查询)
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
- POJ 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(树状数组,更新区间查询区间模板)
- 线段树 (更新区间查询区间)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(区间更新)
- POJ3468 A simple problem with integers(区间更新)
- A Simple Problem with Integers +poj+线段树区间更新
- 如何使用java代码自动生成对应的krpano全景
- 冯诺依曼结构和哈佛结构
- MySQL锁机制
- 单例模式的几种实现方式
- leetcode 75. Sort Colors 很不错的3种元素排序方法 + O(n)
- A Simple Problem with Integers 区间更新和查询
- 设计模式学习笔记(工厂模式)
- EF映射
- 最长上升子序列(LIS)
- 基于Tensorflow实现基本的线性回归(Linear regression)
- hdu 6197array array array(最长不下降子序列nlogn)
- 07-异常
- 【React Native】react-Navigation之StackNavigator
- java jdbc 增删改查