Poj 3468 A Simple Problem with Integers【线段树】
来源:互联网 发布:函数式编程思想 pdf 编辑:程序博客网 时间:2024/05/29 14:46
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
题目大意:
线段树区间更新,区间查询。
Ac代码:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;typedef long long ll;const int maxn=100000+10;ll sum[maxn*4],add[maxn*4];void pushUp(int k){ sum[k]=sum[k*2]+sum[k*2+1];}void pushDown(int k,int l,int r){ if(add[k]) { int lc=k*2,rc=k*2+1,m=(l+r)/2; add[lc]+=add[k]; add[rc]+=add[k]; sum[lc]+=add[k]*(m-l+1); sum[rc]+=add[k]*(r-m); add[k]=0; }}void build(int k,int l,int r){ if(l==r) { scanf("%lld",&sum[k]); add[k]=0; return ; } int m=(l+r)/2; build(k*2,l,m); build(k*2+1,m+1,r); pushUp(k);}void update(int a,int b,ll v,int k,int l,int r){ if(a<=l && r<=b) { add[k]+=v; sum[k]+=v*(r-l+1); return ; } pushDown(k,l,r); int m=(l+r)/2; if(a<=m) update(a,b,v,k*2,l,m); if(b>m) update(a,b,v,k*2+1,m+1,r); pushUp(k);}ll ask(int a,int b,int k,int l,int r){ if(a<=l && r<=b) return sum[k]; pushDown(k,l,r); int m=(l+r)/2; ll res=0; if(a<=m) res+=ask(a,b,k*2,l,m); if(b>m) res+=ask(a,b,k*2+1,m+1,r); pushUp(k); return res;}int main(){ int i,j,n,q,a,b; char op[3]; scanf("%d%d",&n,&q); build(1,1,n); while(q--) { scanf("%s%d%d",op,&a,&b); if(op[0]=='C') { ll v; scanf("%lld",&v); update(a,b,v,1,1,n); } else { printf("%lld\n",ask(a,b,1,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(线段树)
- 454. 4Sum II
- 21:三角形最佳路径问题
- 两只老虎
- Java 判断一个序列是否是搜索二叉树的后序遍历结果
- 2017CCCC天梯赛决赛 赛后总结
- Poj 3468 A Simple Problem with Integers【线段树】
- golang mgo 使用
- 配置服务器 —— 安装vsftpd及配置
- 写出这个数(20)
- %格式化符在policy GenericCheck神奇用法
- hdu 1251(字典树)(3种方法)
- JAVA操作oracle将数据导入Excel
- 爬楼梯
- Spanning Tree Fraction