poj3468 A Simple Problem with Integers
2015-07-24 22:20
281 查看
线段树模板题。
要注意开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);
}
}
}
要注意开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);
}
}
}
相关文章推荐
- 设计模式之行为型模式 - 调用行为的传递问题
- [div+css]晒晒最新制作专题推广页模板
- 2008大学生入党申请书 模板
- IMAIL多语言模板两套Outlook&Gmail模板下载
- 在PHP中使用模板的方法
- 深入解析php模板技术原理【一】
- Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中
- 在ASP中不用模板生成HTML静态页直接生成.html页面
- C#模板方法模式(Template Method Pattern)实例教程
- 关于Asp代码与页面的分离模板技术第1/3页
- php模板原理讲解
- 需要使用php模板的朋友必看的很多个顶级PHP模板引擎比较分析
- DataGrid 动态添加模板列 实现代码
- C++模板之特化与偏特化详解
- vs.net2008添加模板方法
- ThinkPHP模板判断输出Empty标签用法详解
- ThinkPHP模板范围判断输出In标签与Range标签用法详解
- thinkphp模板用法和内容输出实例
- thinkphp模板赋值与替换实例简述
- smarty模板引擎从配置文件中获取数据的方法