POJ---3468-A Simple Problem with Integers(线段树)
来源:互联网 发布:中国知网cnki数据库 编辑:程序博客网 时间:2024/06/05 17:44
B - A Simple Problem with Integers
Crawling in process...Crawling failedTime Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u Description
给出了一个序列,你需要处理如下两种询问。
"C a b c"表示给[a, b]区间中的值全部增加c (-10000≤ c ≤ 10000)。
"Q a b" 询问[a, b]区间中所有值的和。
Input
第一行包含两个整数N,Q。1≤ N,Q ≤ 100000.
第二行包含n个整数,表示初始的序列A (-1000000000≤ Ai ≤1000000000)。
接下来Q行询问,格式如题目描述。
Output
对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。
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
线段树:区间更新 = =。
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;#define SIZE 100050int n,m;int num[SIZE];struct Node{ int a,b; long long sum; long long poi;} node[SIZE<<2];void build_tree(int a,int b,int i){ node[i].a=a; node[i].b=b; if(a==b) { node[i].sum=num[a]; node[i].poi=0; return; } int mid=(a+b)/2; build_tree(a,mid,i<<1); build_tree(mid+1,b,i<<1|1); node[i].sum=node[i<<1].sum+node[i<<1|1].sum; node[i].poi=0;}void add_num(int a,int b,long long num,int i){ if(node[i].a==a && node[i].b==b) { node[i].poi+=num; return; } node[i].sum+=num*(b-a+1); int mid=(node[i].a+node[i].b)>>1; if(b<=mid)add_num(a,b,num,i<<1); else if(a>mid)add_num(a,b,num,i<<1|1); else { add_num(a,mid,num,i<<1); add_num(mid+1,b,num,i<<1|1); }}long long query(int a,int b,int i){ if(node[i].a==a && node[i].b==b) return node[i].sum+node[i].poi*(b-a+1); if(node[i].poi) { node[i].sum+=node[i].poi*(node[i].b-node[i].a+1); node[i<<1].poi+=node[i].poi; node[i<<1|1].poi+=node[i].poi; node[i].poi=0; } int mid=(node[i].a+node[i].b)>>1; if(b<=mid)return query(a,b,i<<1); else if(a>mid)return query(a,b,i<<1|1); else return query(a,mid,i<<1) + query(mid+1,b,i<<1|1);}int main(){ int i,j,k,x,y,z; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1; i<=n; ++i) scanf("%d",&num[i]); build_tree(1,n,1); char str; for(i=0; i<m; ++i) { cin>>str; if(str=='Q') { scanf("%d%d",&x,&y); cout<<query(x,y,1)<<endl; } else { scanf("%d%d%d",&x,&y,&z); add_num(x,y,z,1); } } } return 0;}
0 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(线段树)
- (java)Majority Element
- hdu2032
- ajaxfileupload.js异步上传图片
- 微信公众号开发---微信请求服务端取值问题
- 二叉树的创建和三种遍历方式
- POJ---3468-A Simple Problem with Integers(线段树)
- 【调试】Linux下超强内存检测工具Valgrind
- 编程思维的点滴
- map端执行流程分析
- 屏幕旋转概述
- maven-surefire-customresult插件
- n个数逆序
- CSDN Markdown简明教程1-关于Markdown
- loadrunner之协议选择