poj 3468 A Simple Problem with Integers
来源:互联网 发布:cad画网络拓扑结构图 编辑:程序博客网 时间:2024/06/06 01:04
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 abc" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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<iostream>#include<cstdio>#include<cstring>using namespace std;#define maxn 200005#define inf 0x3f3f3f3ftypedef long long lls;lls a[maxn];lls sum[maxn<<2],add[maxn<<2],ll[maxn<<2],rr[maxn<<2];inline void pushup(int i){ sum[i]=sum[i<<1]+sum[i<<1|1];}inline void pushdown(lls i,lls m){ if(add[i]){ sum[i<<1]+=add[i]*(m-(m>>1)); sum[i<<1|1]+=add[i]*(m>>1); add[i<<1]+=add[i]; add[i<<1|1]+=add[i]; add[i]=0; }}void build(lls l,lls r,lls i){ ll[i]=l; rr[i]=r; add[i]=0; if(l==r){ sum[i]=a[l]; return; } lls m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1; build(l,m,ls); build(m+1,r,rs); pushup(i);}void update(lls l,lls r,lls v,lls i){ if(ll[i]>=l&&rr[i]<=r){ add[i]+=v; sum[i]+=(lls)(rr[i]-ll[i]+1)*v; return ; } pushdown(i,rr[i]-ll[i]+1); lls m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1; if(l<=m)update(l,r,v,ls); if(m<r)update(l,r,v,rs); pushup(i);}lls query(lls l,lls r,lls i){ if(ll[i]>=l&&rr[i]<=r){ return sum[i]; } pushdown(i,rr[i]-ll[i]+1); lls m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1; lls ans=0; if(l<=m)ans+=query(l,r,ls); if(m<r)ans+=query(l,r,rs); return ans;}int main(){ int n,qq; int u,v,c; //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&qq)){ for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } build(1,n,1); char q[2]; while(qq--){ scanf("%s",&q); if(q[0]=='Q'){ scanf("%d%d",&u,&v); printf("%lld\n",query(u,v,1)); } else { scanf("%d%d%d",&u,&v,&c); update(u,v,c,1); } } }}
- 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
- tftp服务器完成linux内核下载
- Codeforces 559B Equivalent Strings 等价串
- 前端工具bower wiredep
- altium designer 原理图复制出错
- fragment学两个demo就会用(二)
- poj 3468 A Simple Problem with Integers
- [Leetcode 105, Medium] Construct Binary Tree from Preorder and Inorder Traversal
- Unity3D之文件读写文件
- [Leetcode 106, Medium] Construct Binary Tree from Inorder and Postorder Traversal
- 百练oj2703
- Codeforces Round #313 (Div. 2) (ABCDE题解)
- [Leetcode 50, Medium] Pow(x, n)
- HDU 1707 Spring-outing Decision
- [Leetcode 108, Meidum] Convert Sorted Array to Binary Search Tree