POJ A Simple Problem with Integers 3468(线段树区间更新)
来源:互联网 发布:淘宝运营公司那家强 编辑:程序博客网 时间:2024/06/05 14:37
注意在查询的时候还要进行更新。
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>#include<math.h>#define LL long long#define inf 0x3f3f3f3f#define ls l,mid,rt<<1#define rs mid+1,r,rt<<1|1LL n,sum[1000100],la[1000100];using namespace std;void up(LL rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void pushdown(LL rt,LL len){ if(la[rt]) { la[rt<<1]+=la[rt]; la[rt<<1|1]+=la[rt]; sum[rt<<1]+=la[rt]*(len-(len>>1)); sum[rt<<1|1]+=la[rt]*(len>>1); la[rt]=0; }}void bu(LL l,LL r,LL rt){ la[rt]=0; if(l==r) { scanf("%lld",&sum[rt]); return ; } LL mid=(l+r)>>1; bu(ls); bu(rs); up(rt);}void ch(LL a,LL b,LL c,LL l,LL r,LL rt){ if(a<=l&&r<=b) { la[rt]+=c; sum[rt]+=(r-l+1)*c; return ; } pushdown(rt,r-l+1); LL mid=(l+r)>>1; if(a<=mid) ch(a,b,c,ls); if(b>mid) ch(a,b,c,rs); up(rt);}LL Q(LL a,LL b,LL l,LL r,LL rt){ if(a<=l&&r<=b) { return sum[rt]; } pushdown(rt,r-l+1); LL mid=(l+r)>>1; LL s=0; if(a<=mid) s+=Q(a,b,ls); if(mid<b) s+=Q(a,b,rs); return s;}int main(){ LL m,i,j,cla,a,b,c;char st[10]; scanf("%lld%lld",&n,&m); getchar(); bu(1,n,1); while(m--) { scanf("%s",&st); if( !strcmp( st,"Q" ) ) { scanf("%lld%lld",&a,&b); LL p=Q(a,b,1,n,1); printf("%lld\n",p); } else if(!strcmp(st,"C") ) { scanf("%lld%lld%lld",&a,&b,&c); ch(a,b,c,1,n,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
- A Simple Problem with Integers +poj+线段树区间更新
- 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(线段树_区间更新+lazy标记)
- POJ 3468-A Simple Problem with Integers(线段树:成段更新,区间求和)
- POJ 3468 A Simple Problem with Integers 线段树(区间更新,查询,lazy数组)
- poj 3468 A Simple Problem with Integers (线段树区间更新入门)
- poj 3468 A Simple Problem with Integers(线段树区间更新)
- Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)
- Ubuntu 14.04.2 移植FFmpeg 2.8.3 到Android平台
- TCP/IP详解卷1 读书笔记:第十八章 TCP连接的建立与终止
- 终于学会了在github上创建一个release、tag了
- Android下的图片缓存简单实现(避免软引用缓存)
- oauth认证四种模式中的第一种oauthation code模式
- POJ A Simple Problem with Integers 3468(线段树区间更新)
- 在MAC上配置SVN服务器 图文教程
- 安卓开发技术:监听软键盘的显示与隐藏
- JDBCUtils动手实现自己的JDBC工具类之二(实践篇)
- 单链表 C++实现
- cocos2d-x 学习笔记3
- 仿蘑菇街个人主页
- MVC简介
- 度量学习