poj 3468 A Simple Problem with Integers
来源:互联网 发布:打字员软件 编辑:程序博客网 时间:2024/06/06 03:05
链接:点击打开链接
有N个数,A1~An,有两种操作,C A B C代表A到B区间所有的数都加C,Q A B是求A到B区间的和,是一道简单的成段更新,懒惰记忆。记得用long long就行
#include<iostream>#include<cstring>#include<cstdio>using namespace std;#define N 100010struct node{int l;int r; long long sum;long long num;}anode[4*N];int data[N];void bulid(int l,int r,int n){int mid;mid=(l+r)>>1;anode[n].num=0;if(l==r){anode[n].l=l; anode[n].r=r;anode[n].sum=data[l];}else{anode[n].l=l;anode[n].r=r;bulid(l,mid,2*n);bulid(mid+1,r,2*n+1);anode[n].sum=anode[2*n].sum+anode[2*n+1].sum; } }long long query(int l,int r,int n,long long m){int mid;mid=(anode[n].l+anode[n].r)>>1;if(anode[n].l==l&&anode[n].r==r){ return anode[n].sum+(r-l+1)*m;}if(r<=mid)return query(l,r,2*n,m+anode[n].num);else if(l>mid)return query(l,r,2*n+1,m+anode[n].num);elsereturn query(l,mid,2*n,m+anode[n].num)+query(mid+1,r,2*n+1,m+anode[n].num); }void update(int l,int r,int n,int m){int mid;mid=(anode[n].l+anode[n].r)>>1;anode[n].sum+=(r-l+1)*m;if(anode[n].l==l&&anode[n].r==r){ anode[n].num+=m;return; } if(r<=mid) update(l,r,2*n,m); else if(l>mid) update(l,r,2*n+1,m); else{update(l,mid,2*n,m);update(mid+1,r,2*n+1,m);}}int main(){int n,q,a,b,c,i;char str;while(~scanf("%d %d",&n,&q)){for(i=1;i<=n;i++)scanf("%d",&data[i]);bulid(1,n,1);//getchar();while(q--){getchar(); scanf("%c",&str); if(str=='Q'){scanf("%d %d",&a,&b);printf("%lld\n",query(a,b,1,0));}else if(str=='C'){scanf("%d %d %d",&a,&b,&c);update(a,b,1,c);} }}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
- 如何在MFC中加入工具栏和停靠窗口
- C++ STL简介
- 25个增强iOS应用程序性能的提示和技巧-初级
- 如何从Windows7系统找回Windows.old文件夹【xitongshoucang】
- js中this的用法
- poj 3468 A Simple Problem with Integers
- 软件设计之UML—UML中的六大关系
- NSString
- 贪心 CF 333B Chips
- 最小值堆-Java实现
- Ubuntu下配置JDK
- android接触--新版本编译OK,系统却起不来
- 长按手势获取UITableView的indexPath值,删除某行
- linux下安装配置R环境相关依赖包