POJ A Simple Problem with Integers | 线段树基础练习
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef long long ll;
#define N 100010
using namespace std;
struct node
{
ll l,r,lz,sum;
}t[*N];
ll read()
{
ll ret=,neg=;
char j=getchar();
for (;j>'' || j<'';j=getchar())
if (j=='-') neg=-;
for (;j<='' && j>='';j=getchar())
ret=ret*+j-'';
return ret*neg;
}
ll n,q,a[N],l,r,k;
void pushup(ll p)
{
t[p].sum=t[*p].sum+t[*p+].sum;
}
void pushdown(ll p)
{
if (t[p].l==t[p].r || t[p].lz==) return;
ll tmp=t[p].lz,mid=(t[p].r+t[p].l)>>;
t[*p].sum+=(mid-t[p].l+)*tmp;
t[*p+].sum+=(t[p].r-mid)*tmp;
t[*p].lz+=tmp;
t[*p+].lz+=tmp;
t[p].lz=;
}
void build(ll p,ll l,ll r)
{
t[p].l=l,t[p].r=r;
if (l!=r)
{
ll mid=l+r>>;
build(*p,l,mid);
build(*p+,mid+,r);
pushup(p);
}
else
t[p].sum=a[l];
}
void modify(ll p,ll l,ll r,ll k)
{
ll L=t[p].l,R=t[p].r,mid=(L+R)>>;
if (l==L && r==R)
{
t[p].sum+=(r-l+)*k;
t[p].lz+=k;
return ;
}
pushdown(p);
if (r<=mid)
modify(*p,l,r,k);
else if (l>mid)
modify(*p+,l,r,k);
else modify(*p,l,mid,k),modify(*p+,mid+,r,k);
pushup(p);
}
ll query(ll p,ll l,ll r)
{
ll L=t[p].l,R=t[p].r,mid=(L+R)>>;
if (l==L && r==R)
return t[p].sum;
pushdown(p);
if (r<=mid)
return query(*p,l,r);
if (l>mid) return query(*p+,l,r);
return query(*p,l,mid)+query(*p+,mid+,r);
}
char op[N];
int main()
{
n=read(),q=read();
for (int i=;i<=n;i++)
a[i]=read();
build(,,n);
while (q--)
{
scanf("%s",op);
if (op[]=='Q')
l=read(),r=read(),printf("%lld\n",query(,l,r));
else l=read(),r=read(),k=read(),modify(,l,r,k);
}
return ;
}
POJ A Simple Problem with Integers | 线段树基础练习的更多相关文章
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
随机推荐
- 理解css中的position属性
理解css中的position 两种类型的定位 static类型:只有一个值position: static.position默认值 relative类型:包括三个值,这三个值会相互影响,允许你以特定 ...
- 51nod 1298 圆与三角形——计算几何
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1298 转化成判断三条线段和圆是否
- 基础篇(1):c++程序基本结构
本人是初中生,原用Pascal语言,现转c++,所以写几篇博客,分享一下. 补一句,我是一边转c++一边写博客,所以可能会有错误,望过路大神能指出. 参考书籍:<信息学奥赛一本通>< ...
- VM12虚拟机安装os x 10.11系统以及注意事项
一.安装步骤 原文链接:https://blog.csdn.net/soachenshui/article/details/49251513 https://blog.csdn.net/soachen ...
- django中的分页管理
有时,展示的对象太多,需要对他们进行分页展示,不能一页把所有的结果都展示出来吧,那样的话,哈哈,挺逗 使用Django分页器功能 从Django中导入Paginator模块(没有的话,自行下载,我是w ...
- python__系统 : socket_UDP相关
socket.socket() 可以创建一个套接字: from socket import * from threading import Thread udp_socket = None dest_ ...
- vim编辑器使用习惯问题
Ubuntu中vi在编辑状态下方向键不能用,一按方向键盘就出ABCD,想插入个字母还非常麻烦,还有回格键不能删除等我们平时习惯的一些键都不能使用. 解决办法: 可以安装vim full版本,在full ...
- 数据分析处理库Pandas——merge操作
有一列列名相同值也相同 有两列列名相同值也相同 按其中一列合并 按两列合并 有一列列名相同值也相同,有一列列名相同值不相同,按两列合并 列名相同值不相同的行删掉 保留所有行 保留所有行并显示合并后该值 ...
- pytorch中词向量生成的原理
pytorch中的词向量的使用 在pytorch我们使用nn.embedding进行词嵌入的工作. 具体用法就是: import torch word_to_ix={'hello':0,'world' ...
- 4525: [Cerc2012]Kingdoms
4525: [Cerc2012]Kingdoms 题意 n个国家,两两之间可能存在欠债或者被欠债的关系,一个国家破产:其支出大于收入.问一个国家能否坚持到最后. 思路 很有意思的一道题. dp[s]表 ...