poj3468 A Simple Problem with Integers
来源:互联网 发布:高端游戏本 知乎 编辑:程序博客网 时间:2024/06/18 11:43
线段树模板题。
要注意开long long。
话不多说。
#include<stdio.h>typedef long long ll;ll n,m;ll num[100000],tree[100005*4],sign[100005*4];void pushup(ll node){tree[node]=tree[node<<1]+tree[node<<1|1];return ;}void pushdown(ll node,ll l,ll r){if(sign[node]){sign[node<<1]+=sign[node];sign[node<<1|1]+=sign[node];ll mid=(l+r)>>1;tree[node<<1]+=(mid-l+1)*sign[node];tree[node<<1|1]+=(r-mid)*sign[node];sign[node]=0;}}void build(ll l,ll r,ll node){sign[node]=0;if(l==r){scanf("%lld",&tree[node]);return ;}ll mid=(l+r)>>1;build(l,mid,node<<1);build(mid+1,r,node<<1|1);pushup(node);}void update(ll l,ll r,ll st,ll en,ll add,ll node){if(st>=l&&en<=r){tree[node]+=(en-st+1)*add;sign[node]+=add;return ;}pushdown(node,st,en);ll mid=(st+en)>>1;if(l<=mid)update(l,r,st,mid,add,node<<1);if(r>mid)update(l,r,mid+1,en,add,node<<1|1);pushup(node);}ll query(ll l,ll r,ll st,ll en,ll node){if(st>=l&&en<=r)return tree[node];pushdown(node,st,en);ll mid=(st+en)>>1;ll ret=0;if(mid>=l)ret+=query(l,r,st,mid,node<<1);if(mid<r)ret+=query(l,r,mid+1,en,node<<1|1);return ret;}char s[3];int main(){scanf("%lld %lld",&n,&m);build(1,n,1);for(ll i=1;i<=m;i++){scanf("%s",s);if(s[0]=='Q'){ll a,b;scanf("%lld %lld",&a,&b);printf("%lld\n",query(a,b,1,n,1));}else{ll a,b,c;scanf("%lld %lld %lld",&a,&b,&c);update(a,b,1,n,c,1);}}}
0 0
- poj3468 A Simple Problem with Integers
- POJ3468:A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468-A Simple Problem with Integers
- POJ3468--A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- [POJ3468]A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- Asp.mvc(四) ~ 构建Web层
- static的作用
- linux简单应用 -- 数据包抓取系统(网络嗅探器)
- 【读书笔记】iOS-GCD-使用方法
- 链栈的基本操作
- poj3468 A Simple Problem with Integers
- Latex 中插入 Matlab 代码
- 《C语言及程序设计》实践参考——分数的累加
- 股票学习(技术指标)
- HDU 5303 Delicious Apples(dp)
- Android中Fragment和Activity之间的互操作代码例子
- java内存泄露补充例子
- OpenCV图像处理篇之腐蚀与膨胀
- 素数距离问题