poj 3468 A Simple Problem with Integers(区间更新 区间求和)
来源:互联网 发布:java线程面试题 编辑:程序博客网 时间:2024/05/16 08:18
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
给出N个数的初始值 两种操作 一种是把一个区间内所有的所有的数都加c 一种是求出一个区间的和
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 100010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;struct node{ ll l,r; ll lazy,num;};node no[MAXN*4];ll a[MAXN];void build(ll l,ll r,ll k){ if(l==r) { no[k].l=l; no[k].r=r; no[k].num=a[l]; no[k].lazy=0; return; } ll mid=(l+r)/2; build(l,mid,k*2); build(mid+1,r,k*2+1); no[k].l=l; no[k].r=r; no[k].lazy=0; no[k].num=no[k*2].num+no[k*2+1].num;}void pushdown(ll k){ if(no[k].lazy) { no[k*2].lazy+=no[k].lazy; no[k*2].num+=(no[k*2].r-no[k*2].l+1)*no[k].lazy; no[k*2+1].lazy+=no[k].lazy; no[k*2+1].num+=(no[k*2+1].r-no[k*2+1].l+1)*no[k].lazy; no[k].lazy=0; return; }}void update(ll l,ll r,ll num,ll k){ if(no[k].l>=l&&no[k].r<=r) { ll len=no[k].r-no[k].l+1; no[k].num+=len*num; no[k].lazy+=num; return; } pushdown(k); ll mid=(no[k].r+no[k].l)/2; if(r<=mid) update(l,r,num,k*2); else if(l>mid) update(l,r,num,k*2+1); else { update(l,mid,num,k*2); update(mid+1,r,num,k*2+1); } no[k].num=no[k*2].num+no[k*2+1].num;}ll ans;void query(ll l,ll r,ll k){ if(no[k].l>=l&&no[k].r<=r) { ans+=no[k].num; return; } pushdown(k); int mid=(no[k].l+no[k].r)/2; if(r<=mid) query(l,r,k*2); else if(l>mid) query(l,r,k*2+1); else { query(l,mid,k*2); query(mid+1,r,k*2+1); } no[k].num=no[k*2].num+no[k*2+1].num;}int main(){// fread; ll n,q; while(scanf("%I64d%I64d",&n,&q)!=EOF) {// cout<<n<<q<<endl; for(ll i=1;i<=n;i++) scanf("%I64d",&a[i]); build(1,n,1); while(q--) { char ch[10]; ll l,r,num; scanf("%s",ch); if(ch[0]=='C') { scanf("%I64d%I64d%I64d",&l,&r,&num); update(l,r,num,1); } else { scanf("%I64d%I64d",&l,&r);// cout<<l<<" "<<r<<endl; ans=0; query(l,r,1); printf("%I64d\n",ans); } } } 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 (splay 成段更新、区间求和)
- 20140719 「线段树 - 区间更新,区间求和」 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 (Splay 区间更新、区间求和)
- POJ 3468 A Simple Problem with Integers(线段树区间更新+区间求和)
- POJ 3468 A Simple Problem with Integers(区间更新)
- adb命令查看报名和查看手机分辨率
- C++ 智能指针详解
- 菜鸟学数据库(三)——存储过程
- 第6周-打印金字塔形的数字
- leetcode19
- poj 3468 A Simple Problem with Integers(区间更新 区间求和)
- Math.floor,Math.ceil,Math.rint,Math.round用法详解
- border-radius
- 浅析C++中的初始化列表(区别赋值和初始化)
- HDU 2527 Safe Or Unsafe(哈夫曼编码)
- 项目框架设计
- java实现杨辉三角,输出对齐
- 转录组小知识点收集
- leetcode20