poj 3468A Simple Problem with Integers
来源:互联网 发布:杭州电魂网络 编辑:程序博客网 时间:2024/06/05 04:03
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
#include<stdio.h>#include<string.h>#define maxn 100005#define ll long longchar str[10];ll a[maxn];struct node{ll l,r,sum,ans;}b[4*maxn];void build(ll l,ll r,ll i){ ll mid; b[i].l=l; b[i].r=r; b[i].ans=0; if(b[i].l==b[i].r) { b[i].sum=a[l]; return; } mid=(l+r)/2; build(l,mid,2*i); build(mid+1,r,i*2+1); b[i].sum=b[i*2].sum+b[i*2+1].sum;}void pushdown(int i){ if(b[i].ans){ b[i*2].ans+=b[i].ans; b[i*2+1].ans+=b[i].ans; b[i*2].sum+=b[i].ans*(b[i*2].r-b[i*2].l+1); b[i*2+1].sum+=b[i].ans*(b[i*2+1].r-b[i*2+1].l+1); b[i].ans=0; }}void add(ll l,ll r,ll value,ll i){ll mid;if(b[i].l==l && b[i].r==r){ b[i].ans=b[i].ans+value; b[i].sum+=(b[i].r-b[i].l+1)*value; return;}pushdown(i);b[i].sum+=(r-l+1)*value; //这一句写了,下面第二句就不用写了,是同一个意思mid=(b[i].l+b[i].r)/2;if(l>mid)add(l,r,value,i*2+1);else if(r<=mid)add(l,r,value,i*2);else{ add(l,mid,value,i*2); add(mid+1,r,value,i*2+1);}//b[i].sum=b[i*2].sum+b[i*2+1].sum; ---2}ll question(ll l,ll r,ll i){ll mid;if(b[i].l==l && b[i].r==r){ return b[i].sum;}pushdown(i);mid=(b[i].l+b[i].r)/2;if(l>mid)return question(l,r,i*2+1);else if(r<=mid)return question(l,r,i*2);else if(l<=mid && r>mid)return question(l,mid,i*2)+question(mid+1,r,i*2+1);}int main(){ll n,m,c,d,e,i,j;while(scanf("%lld%lld",&n,&m)!=EOF){for(i=1;i<=n;i++)scanf("%lld",&a[i]);build(1,n,1);while(m--){scanf("%s",str);if(str[0]=='Q'){scanf("%lld%lld",&c,&d);printf("%lld\n",question(c,d,1));}else if(str[0]=='C'){scanf("%lld%lld%lld",&c,&d,&e);add(c,d,e,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
- java 中静态方法
- 单链表的实现-功能完善
- C++学习之重复定义的错误
- Java学习笔记之单例模式
- SVG绘图技术
- poj 3468A Simple Problem with Integers
- nopcommerce 电商商城 ASP.NET 开源系统
- Cloudera Manager5之安装部署CDH5.X
- 《从0到1》笔记 第九章 基础决定命运
- java笔记
- 美团面试的两道算法题
- UGUI学习——Sprite Pack
- unity3d中平滑跟随的功能实现!!!!
- c++ 代理类 copy clone