Poj 3468 A Simple Problem with Integers
来源:互联网 发布:exe软件看源码 编辑:程序博客网 时间:2024/06/07 04:49
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
题意:线段树区间更新。
如果不太理解,可以输出中间变量,有助于理解。
代码:
#include <cstdio>#include <cstring>using namespace std;typedef long long ll;typedef struct node{int l, r;ll s, add;// s 为点 l到r 数值之和。 // add 为点 l到r 需要增加的数。 }Tree;const int N = 1000000;Tree T[N];void build(int l, int r,int i){T[i].l = l;T[i].r = r;T[i].add = 0;T[i].s = 0;if(r == l) return ;int mid = (l+r) >> 1;build(l, mid, i<<1);build(mid+1, r, i<<1|1);T[i].s = T[i<<1].s + T[i<<1|1].s;} void update(int l, int r, int i,int add){if(T[i].l > r || T[i].r < l) return ;if(T[i].l >= l && T[i].r <= r){T[i].s += (T[i].r - T[i].l + 1)*add;T[i].add += add;return ;}if(T[i].add) { T[i<<1].s += (T[i<<1].r - T[i<<1].l + 1) * T[i].add; T[i<<1].add += T[i].add; T[i<<1|1].s += (T[i<<1|1].r - T[i<<1|1].l + 1) * T[i].add; T[i<<1|1].add += T[i].add; T[i].add = 0;} update(l, r, i<<1, add); update(l, r, i<<1|1, add);T[i].s = T[i<<1].s + T[i<<1|1].s;}ll ans = 0;void query(int l, int r, int i){if(T[i].l > r || T[i].r < l) return ; if(T[i].l >= l && T[i].r <= r){ans += T[i].s;return ;}if(T[i].add){T[i<<1].s += (T[i<<1].r - T[i<<1].l + 1) * T[i].add;T[i<<1].add += T[i].add;T[i<<1|1].s += (T[i<<1|1].r - T[i<<1|1].l + 1) * T[i].add;T[i<<1|1].add += T[i].add;T[i].add = 0; }query(l, r, i<<1);query(l, r, i<<1|1);T[i].s = T[i<<1].s + T[i<<1|1].s;}int main(){ int n, q;scanf("%d %d",&n, &q); build(1, n, 1);for(int i=1; i<=n; i++){int tem;scanf("%d",&tem);update(i, i, 1, tem);}for(int i=1; i<=q; i++){getchar();char op;scanf("%c", &op);if(op == 'Q'){ans = 0;int l, r;scanf("%d %d",&l, &r);query(l, r, 1); printf("%lld\n",ans);} else{int a, b, c;scanf("%d %d %d",&a, &b, &c);update(a, b, 1, c); } }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
- 一名3年工作经验的程序员应该具备的技能
- java对象的特例:基本类型
- pymysql.connect()参数说明
- MySQL卸载重安装异常解决
- NOIP模拟赛 鸽巢原理 + 众数 + Trie
- Poj 3468 A Simple Problem with Integers
- C++集锦三 操作符重载
- Docker 使用指南 (四)—— 数据卷的使用
- HDFS结构介绍
- 报错:不能找到系统文件properties
- <学习html>第六天笔记-表单标签(input控件、label标签)
- Spring框架详解(1)
- 210串口控制台-210移植printf不好使
- HDU 1166 敌兵布阵