POJ——3468A Simple Problem with Integers(区间更新)
来源:互联网 发布:java打印高为n的金字塔 编辑:程序博客网 时间:2024/04/29 21:25
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
这题WA了许久,最后发现读入数据时也应该是%I64d,而且用%LLd还不行
/** 线段树:区间更新 *线段树每个节点存储当前区间的和 *标记数组存取当前增量,记得每次更新增量时要自加上之前的该区间增量,因为有可能之前该区间的增量并没有向下更新掉 *注意增量可能是负数 *输入输出用%I64d,%LLD会WA,应该和评测系统有关 *最好不用cin,cout,除非关掉同步 */#include<iostream>#include<stdio.h>#include<string.h>#define rson mid + 1,r,rt << 1 |1#define lson l,mid,rt << 1#define LL long longusing namespace std;const int N = 100005;LL Tree[N << 2];LL col[N << 2];void PushUp(int rt)//向上更新和{ Tree[rt] = Tree[rt << 1] + Tree[rt << 1 | 1];}void PushDown(int rt,int l)//向下更新增量{ if(col[rt]) { col[rt << 1] += col[rt]; col[rt << 1 | 1] += col[rt]; Tree[rt << 1] += (l - (l >> 1)) * col[rt]; Tree[rt << 1 | 1] += (l >> 1) * col[rt]; col[rt] = 0; }}void Build(int l,int r,int rt){ col[rt] = 0; if(l == r) { scanf("%I64d",&Tree[rt]); return; } int mid = (l+r) >> 1; Build(lson); Build(rson); PushUp(rt);}void Upgrade(int ll,int rr,int v,int l,int r,int rt){ if(ll <= l && r <= rr) { Tree[rt] += (v * (r - l + 1)); col[rt] += v; return; } PushDown(rt,r - l + 1);// int mid = (l + r) >> 1; if(ll <= mid) Upgrade(ll,rr,v,lson); if(rr > mid) Upgrade(ll,rr,v,rson); PushUp(rt);}LL Query(int ll,int rr,int l,int r,int rt){ LL sum = 0; if(ll <= l && r <= rr) { sum += Tree[rt]; return sum; } PushDown(rt,r - l + 1); int mid = (l + r) >> 1; if(ll <= mid) sum += Query(ll,rr,lson); if(rr > mid) sum += Query(ll,rr,rson); return sum;}int main(){ int n,a,b,c,q; char temp[2]; //freopen("FileIn.txt","r",stdin);freopen("FileOut.txt","w",stdout); scanf("%d%d",&n,&q); Build(1,n,1); for(int i = 0; i < q; i++) { scanf("%s%d%d",&temp,&a,&b); if(temp[0] == 'C') { scanf("%d",&c); Upgrade(a,b,c,1,n,1); } else { printf("%I64d\n",Query(a,b,1,n,1)); } } //fclose(stdin);fclose(stdout); return 0;}
- POJ 3468 A Simple Problem with Integers(区间更新)
- POJ 3468 A Simple Problem with Integers(区间更新)
- Splay树(区间更新)—— POJ 3468 A Simple Problem with Integers
- POJ——3468A 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(树状数组更新区间查区间)
- Linux安装maven
- Spiral Matrix
- C#跨平台吗
- 感受Vim的强大:进阶技巧
- 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组
- POJ——3468A Simple Problem with Integers(区间更新)
- hdu 5317 RGCDQ 2015 Multi-University Training Contest 3
- C# log4net 不输出日志
- 让你的网站飞起来
- andorid系统启动过程中添加启动脚本
- 在VIM下写C++能有多爽?
- Item 1:将C++视作一系列的语言 Effective C++笔记
- poj 1236 Network of Schools(强连通分量)
- Marvell发布4核A53芯片 基带支持Cat.7 LTE---ESM