POJ 3468-A Simple Problem with Integers
来源:互联网 发布:方舟生存进化优化太差 编辑:程序博客网 时间:2024/06/11 05:21
题目链接:点击打开链接
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 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
题意:
本题有 n 个数,对这 n 个数有两种操作,第一种是区间更新,第二种是区间查询,有 m 个询问,C 代表区间更新,这时会给你三个数 a , b , c ,表示将区间 [a,b] 里面所有的元素都加上 c,Q 代表查询,会给你两个数 a,b,让你求区间 [a,b] 的元素的加和。
分析:
上周和本周我们在学习树状数组和线段树,我们在练专题,本题身边很多人都用树状数组,但是我感觉树状数组学的不是很理解,现在时间比较紧,好多人说树状数组能写的题,线段树都能写,但是反过来就不成立,所以我的计划是将本次专题都用线段树写,增强对线段树的理解和运用,因为之前有接触过并且也写过两题,所以再学习起来不是太困难,至于树状数组,我想留着寒假有时间再补,恩恩,好好努力吧。
#include<iostream>#include<stdio.h>using namespace std;#define LL long long int#define RL root<<1///root的左孩子节点编号#define RR root<<1|1///root的右孩子节点编号struct tree{ int l,r;///节点区间的左边界和右边界 LL sum,lazy,length; ///sum是节点区间数值的和,lazy是下推标记,length是节点区间的长度}s[400005];///数组开到叶子节点的四倍void build(int l,int r,int root)///建树{ s[root].l=l; s[root].r=r; s[root].lazy=0; s[root].length=r-l+1; if(l==r) { scanf("%lld",&s[root].sum); return; } int mid=(l+r)>>1; build(l,mid,RL); build(mid+1,r,RR); s[root].sum=s[RL].sum+s[RR].sum;///更新父节点的sum值}void pushdown(int l,int r,int root)///将lazy下推{ if(s[root].lazy) { s[RL].lazy += s[root].lazy; s[RR].lazy += s[root].lazy; s[RL].sum += s[root].lazy*s[RL].length; s[RR].sum += s[root].lazy*s[RR].length; s[root].lazy = 0; }}void update(int a,int b,int c,int l,int r,int root){ ///到达子区间,修改节点值并留下标记 if(a<=s[root].l && s[root].r<=b) { s[root].sum += s[root].length*c; s[root].lazy += c; return; } pushdown(l,r,root);///下推 int mid = (l+r)>>1; if(a<=mid)update(a,b,c,l,mid,RL); if(b>mid)update(a,b,c,mid+1,r,RR); s[root].sum = s[RL].sum + s[RR].sum;}LL Find(int a,int b,int l,int r,int root){ if(a<=s[root].l&&s[root].r<=b) { return s[root].sum;///返回子区间的值 } pushdown(l,r,root); ///每次查询前先下推标记 int mid = (l+r)>>1; long long ans = 0; if(a<=mid) ans += Find(a,b,l,mid,RL); if(b>mid) ans += Find(a,b,mid+1,r,RR); return ans;}int main(){ int n,m; scanf("%d %d",&n,&m); build(1,n,1);///建树 int a,b,c; char p; while(m--) { getchar(); scanf("%c",&p); if(p=='C') { scanf("%d %d %d",&a,&b,&c); update(a,b,c,1,n,1); } else { scanf("%d %d",&a,&b); printf("%lld\n",Find(a,b,1,n,1)); } } 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
- 安卓集成eclipse 中的 The import android.support.v7.app cannot be resolved 问题解决方法
- phpcms 专题中添加字段
- 关于STL中vector容器的一些总结
- RGB图像区域直方图统计方法
- VS2017 Web项目添加引用项目后,引用上有黄色的感叹号小图标
- POJ 3468-A Simple Problem with Integers
- 11、新手入手树莓派教程--Qt中使用wiringPi来驱动RGB灯、使用opencv打开摄像头
- Lintcode:插入区间
- SolidWorks二次开发——套合件(零件嵌入零件)
- 软件行业里学历和资历哪个更重要?
- js中typeof用法详细介绍
- 快速打造自己开发环境 vagrant + virtualbox
- ReactJs中的网络请求fetch 使用及封装
- ViewPager+Fragment嵌套ViewPager+Fragment