3468 A Simple Problem with Integers
来源:互联网 发布:java从入门到精通 编辑:程序博客网 时间:2024/06/07 23:42
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
The sums may exceed the range of 32-bit integers.
线段树区间更新+区间和查询
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxm = 4000005;#define ll long longstruct node{ll l, r, sum, add;}tr[maxm];ll a[maxm];void build(ll x, ll l, ll r){tr[x].l = l, tr[x].r = r;if (l == r){tr[x].sum = a[l];return;}ll mid = (l + r) / 2;build(x * 2, l, mid);build(x * 2 + 1, mid + 1, r);tr[x].sum = tr[x * 2].sum + tr[x * 2 + 1].sum;}void update(ll x, ll l, ll r, ll val){tr[x].sum += (r - l + 1)*val;if (tr[x].l == l&&tr[x].r == r){tr[x].add += val;return;}if (tr[x].l == tr[x].r)return;if (tr[x].add){tr[x * 2].add += tr[x].add;tr[x * 2].sum += (tr[x * 2].r - tr[x * 2].l + 1)*tr[x].add;tr[x * 2 + 1].add += tr[x].add;tr[x * 2 + 1].sum += (tr[x * 2 + 1].r - tr[x * 2 + 1].l + 1)*tr[x].add;tr[x].add = 0;}ll mid = (tr[x].l + tr[x].r) / 2;if (mid >= r) update(x * 2, l, r, val);else if (mid < l) update(x * 2 + 1, l, r, val);else{update(x * 2, l, mid, val);update(x * 2 + 1, mid + 1, r, val);}}ll query(ll x, ll l, ll r){if (tr[x].l == l&&tr[x].r == r)return tr[x].sum;if (tr[x].add){tr[x * 2].add += tr[x].add;tr[x * 2].sum += (tr[x * 2].r - tr[x * 2].l + 1)*tr[x].add;tr[x * 2 + 1].add += tr[x].add;tr[x * 2 + 1].sum += (tr[x * 2 + 1].r - tr[x * 2 + 1].l + 1)*tr[x].add;tr[x].add = 0;}ll mid = (tr[x].l + tr[x].r) / 2;if (mid >= r) return query(x * 2, l, r);else if (mid < l) return query(x * 2 + 1, l, r);else return query(x * 2, l, mid) + query(x * 2 + 1, mid + 1, r);}int main(){ll n, i, j, k, sum, m, x, y;char str[2];ll z;scanf("%lld%lld", &n, &m);for (i = 1;i <= n;i++) scanf("%lld", &a[i]);build(1, 1, n);while (m--){scanf("%s", str);scanf("%lld%lld", &x, &y);if (str[0] == 'Q')printf("%lld\n", query(1, x, y));else{scanf("%lld", &z);update(1, x, y, z);}}return 0;}
- pku 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
- 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
- PKU 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
- 快读 模板
- 数据库笔记3————关系数据库标准语言SQL上(SQL的数据定义和数据更新)
- es6箭头函数
- linux中,一个目录的权限是777,普通用户为什么删除不了它呢?
- STM32CubeMX学习:基本定时器
- 3468 A Simple Problem with Integers
- android MVVM开发框架——(3)DataBinding 应用于RecyclerView
- 快速排序算法总结
- 【git 使用详解(4)】-- 初体验
- 【JZOJ5433】图
- eclipse git报错git The current branch is not configured for pull No value for key branch.master.merge
- HDOJ 1215 七夕节
- Linux中的自动安装脚本
- 2017-10-31每日一练