POJ 3468 A Simple Problem with Integers 线段树 区间更新
来源:互联网 发布:二手知豆电动汽车价格 编辑:程序博客网 时间:2024/06/07 13:05
原题: http://poj.org/problem?id=3468
题目:
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 78207 Accepted: 24097
Case Time Limit: 2000MS
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
Hint
The sums may exceed the range of 32-bit integers.
思路:
直接套模版。
注意数据会int溢出。
代码:
#include <iostream>#include"cstdio"#include"string.h"using namespace std;typedef long long int lint;const int N= 100005;struct node{ int left; int right; lint add; lint sum;} tree[N*4];int n,m;lint a[N];void init(){ memset(tree,0,sizeof(tree)); memset(a,0,sizeof(a));}//将下面更新的值返回上一层void pushup(int id){ tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;}//将标记区间向下移动void pushdown(int id){ tree[id*2].add=tree[id*2].add+tree[id].add; tree[id*2].sum=tree[id*2].sum+(tree[id*2].right-tree[id*2].left+1)*tree[id].add; tree[id*2+1].add=tree[id*2+1].add+tree[id].add; tree[id*2+1].sum=tree[id*2+1].sum+(tree[id*2+1].right-tree[id*2+1].left+1)*tree[id].add; tree[id].add=0;}//建树的时候每次建完下一层要向上层赋值void build(int id,int l,int r){ tree[id].left=l; tree[id].right=r; tree[id].add=0; tree[id].sum=0; if(l==r) { tree[id].sum=a[l]; return; } int mid=(l+r)/2; build(id*2,l,mid); build(id*2+1,mid+1,r); pushup(id);}void add(int id,int l,int r,lint val){ //如果当前区间完全被包含,只更新当前区间的总和 //并对该区间做增加标记 if(tree[id].left>=l&&tree[id].right<=r) { tree[id].add=tree[id].add+val; tree[id].sum=tree[id].sum+(tree[id].right-tree[id].left+1)*val; return ; } //如果当前区间没有被包含 if(tree[id].right<l||tree[id].left>r) return ; //如果当前区间部分被包含,标记下移 if(tree[id].add) pushdown(id); //更新左右子区间 add(id*2,l,r,val); add(id*2+1,l,r,val); //更新完再把结果返回上层 pushup(id);}lint ans;void query(int id,int l,int r){ //查询区间在该区间外 if(tree[id].left>r||tree[id].right<l) return ; //该区间完全被包含 if(tree[id].left>=l&&tree[id].right<=r) { ans=ans+tree[id].sum; return ; } //查询部分该区间,标记下移 if(tree[id].add) pushdown(id); //左右查找 int mid=(tree[id].left+tree[id].right)/2; if(l<=mid) query(id*2,l,r); if(r>mid) query(id*2+1,l,r);}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d %d",&n,&m)!=EOF) { init(); char s[10]; for(int i=1; i<=n; i++) scanf("%I64d",&a[i]); build(1,1,n); for(int i=1; i<=m; i++) { int a,b,c; scanf("%s",s); if(s[0]=='Q') { ans=0; scanf("%d %d",&a,&b); query(1,a,b); printf("%I64d\n",ans); } if(s[0]=='C') { scanf("%d %d %d",&a,&b,&c); add(1,a,b,c); } } } //fclose(stdin); return 0;}
- A Simple Problem with Integers +poj+线段树区间更新
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- poj 3468 A Simple Problem with Integers (线段树区间更新 + 树状数组区间更新)
- Poj 3468 A Simple Problem with Integers (线段树 区间更新 区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 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(线段树区间更新+求和)
- 换个角度看HTC,或许并没有那么悲观
- java中代码块的使用
- 不要急于求解
- Eclipse出现"Running Android Lint has encountered a problem"解决方案
- 国产手机出海:提着互联网灯笼去印度
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
- PullScrollView进阶(一)----->图片下拉回弹
- 苹果审核reject理由大全(7):引导更新按钮或弹框
- Paratroopers
- iOS声明变量详解【转】
- 零基础学python-2.13 回到我们的游戏 加入循环
- jQuery(2)选择器
- java做图片加水印的功能代码
- 印度将成智能手机市场下一个风口,群雄已开始逐鹿