#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 | 线段树基础练习的更多相关文章

  1. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  2. POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 105742 ...

  3. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  4. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  5. 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 ...

  6. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  7. 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 ...

  8. 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 ...

  9. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

随机推荐

  1. (三)Swagger配置多项目共用

    重构了多个项目后,在联调接口时,查看api会发现Swagger在几个项目可用,有几个不可用,配置都一样,扫描也充分,那问题出在哪里呢?先仔细找了下Docket的源码,发现有这么个方法: /** * P ...

  2. 使用cookie登陆知乎

    只是想说明一个问题,Cookie可以维持登录状态,有些网页当中,访问之后的cookie里面带有登陆账号,和登陆密码,这样可以使用cookie直接访问网页,如知乎,首先登录知乎,将Headers中的Co ...

  3. python之doctest的用法

    doctest是python自带的一个模块,你可以把它叫做“文档测试”(doctest)模块. doctest的使用有两种方式:一个是嵌入到python源中.另一个是放到一个独立文件. doctest ...

  4. PHP continue和break的用法(深入理解)

    对于刚入门的PHP童鞋们来说,在循环体中的continue和break的作用总是分不清到底是什么意思, 怎么用, 两者到底有什么区别? 接下来说几个例子,其实它们是很好区分的. <?php $t ...

  5. 小明的存钱计划 南阳acm54

    小明的存钱计划 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 小明的零花钱一直都是自己管理.每个月的月初妈妈给小明300元钱,小明会预算这个月的花销,并且总能做到实际花 ...

  6. 理解线程3 c语言示例线程基本操作

    Table of Contents 1. 基本线程的动作 1.1. 设置线程属性 1.1.1. 设置脱离状态 1.1.2. 设置调度属性 1.2. 取消线程 1.3. 主线程创建多个线程示例 2. 了 ...

  7. POJ 3461 Oulipo(字符串hash)

    题目链接 字符串hash判断字符串是否相等. code #include<cstdio> #include<algorithm> #include<cstring> ...

  8. DOS程序员手册(六)

    217页 程序的主要部分后面是主程序所使用的许多小的扩充内存功能.将这些功能组合起 来这些功能便覆盖了扩充内存的操作,尽管还可能想向它们添加错误检查. 程序所包含的函数有: emmtest   检验内 ...

  9. 更改maven本地仓库地址

    1.进入maven安装conf文件中,编辑settings.xml文件,新增图中的圈出的内容(我想要存放的地址是D:\HMY\m2\repository) 2.复制settings.xml文件至D:\ ...

  10. codeblocks17.12 debug 报错:ERROR: You need to specify a debugger program in the debuggers's settings.

    DebugERROR: You need to specify a debugger program in the debuggers's settings.(For MinGW compilers, ...