#POJ 3468 A Simple Problem with Integers 【区间加减法】【水】
来源:互联网 发布:生产数据化管理 编辑:程序博客网 时间:2024/06/05 07:57
题目:
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
Hint
Source
#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <vector>#include <algorithm>#define MAXN 200000long long lazy[2 * MAXN];long long t[2 * MAXN], a[2 * MAXN];//int be[MAXN], ed[MAXN];//int hips[2 * MAXN];//double endhips[4 * MAXN];//int maxn;using namespace std;void BuildTree(int l, int r, int x){if (l == r){t[x] = a[l];return;}int m = (l + r) >> 1;BuildTree(l, m, x << 1);BuildTree(m + 1, r, x << 1 | 1);t[x] = t[x << 1] + t[x << 1 | 1];return;}void PushDown(int l, int r, int x){int m = (l + r) >> 1;if (lazy[x]){t[x << 1] += lazy[x] * (m - l + 1);t[x << 1 | 1] += lazy[x] * (r - m);lazy[x << 1 | 1] += lazy[x];lazy[x << 1] += lazy[x];lazy[x] = 0;}return;}void Modify(int pos, int val, int l, int r, int x){if (pos == l && r == l){t[x] = val;return;}int m = (l + r) >> 1;if (pos <= m){Modify(pos, val, l, m, x << 1);}else Modify(pos, val, m + 1, r, x << 1 | 1);t[x] = t[x << 1] + t[x << 1 | 1];return;}void SegModify(int L, int R, int val, int l, int r, int x){if (l == L&&r == R){t[x] += (R - L + 1)*val;lazy[x] += val;return;}PushDown(l, r, x);int m = (l + r) >> 1;if (R <= m)SegModify(L, R, val, l, m, x << 1);else if (L>m)SegModify(L, R, val, m + 1, r, x << 1 | 1);else {SegModify(L, m, val, l, m, x << 1);SegModify(m + 1, R, val, m + 1, r, x << 1 | 1);}t[x] = t[x << 1] + t[x << 1 | 1];return;}long long Query(int L, int R, int l, int r, int x){if (L == l && R == r)return t[x];PushDown(l, r, x);int m = (l + r) >> 1;if (R <= m)return Query(L, R, l, m, x << 1);else if (L>m)return Query(L, R, m + 1, r, x << 1 | 1);else return Query(L, m, l, m, x << 1) + Query(m + 1, R, m + 1, r, x << 1 | 1);}int main(){int nn, mm;while (cin >> nn >> mm){memset(t, 0, sizeof(t));//memset(hips, 0, sizeof(hips));//memset(endhips, 0, sizeof(endhips));memset(lazy, 0, sizeof(lazy));//maxn = 0;int n = 1;while (n < MAXN / 2){n *= 2;}for (size_t i = 0; i < nn; i++){scanf("%I64d", &a[i + 1]);}BuildTree(1, n, 1);long long be, ed, tran;char op;while (mm--){cin >> op;if (op == 'C'){scanf("%I64d%I64d%I64d", &be, &ed, &tran);SegModify(be, ed, tran, 1, n, 1);}else{scanf("%I64d%I64d", &be, &ed);cout << Query(be, ed, 1, n, 1) << "\n";}}}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
- POJ-3468-A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- 配置Bugzilla 5 用SMTP方式发送邮件
- C++ Virtual详解
- 懒人爱家务_动画效果编程基础--AnimationAndroid
- expdp与impdp参数用法
- locationManager获取位置信息
- #POJ 3468 A Simple Problem with Integers 【区间加减法】【水】
- iOS Images.xcassets: None of the input catalogs contained a matching app icon set named "AppIcon".
- 【B/S】——Html基础
- Python去掉字符串中空格的方法
- Android蓝牙4.0API-类-BluetoothGattCharacteristic
- 【C#-Socket监听消息处理】
- C++11获取时间戳和时间戳转日期(毫秒精度)
- CSS3新特性与选择器
- SimpleDateFormat格式化日期