You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15
代码如下
#include
#include
typedef long long ll;
const int N=1e5+7;
int n,q;
int a[N];
struct node{ //建树
int l,r,len;//多了一个区间长度len和懒标记
ll sum,lazy;
}tree[N*4];
void pushup(int now){//向上更新sum
tree[now].sum=tree[now<<1].sum+tree[now<<1|1].sum;
}
void buildtree(int now, int l,int r){
tree[now].l=l;
tree[now].r=r;
tree[now].lazy=0;// 建树时懒标记置零
tree[now].len=r-l+1;
if(l==r){
tree[now].sum=a[l];
return;
}
int mid=(l+r)>>1;
buildtree(now<<1,l,mid);
buildtree(now<<1|1,mid+1,r);
pushup(now);
}
void pushdown(int now){ //向子孙传递懒标记
if(tree[now].lazy){
tree[now<<1].sum+=tree[now].lazy*tree[now<<1].len;
tree[now<<1].lazy+=tree[now].lazy;
tree[now<<1|1].sum+=tree[now].lazy*tree[now<<1|1].len;
tree[now<<1|1].lazy+=tree[now].lazy;
tree[now].lazy=0;
}
}
void add(int now,int l,int r,int v){
int L=tree[now].l,R=tree[now].r;
if(l<=L&&R<=r){//如果现区间已经完全在所要修改的区间以内,则不需要继续向下修改,进行懒标记就好
tree[now].sum+=tree[now].len*v;
tree[now].lazy+=v;
return;
}
pushdown(now);//懒标记由父辈传给子辈
int mid=(L+R)>>1;
if(mid>=l)add(now<<1,l,r,v);//与左儿子有交集
if(mid<r)add(now<<1|1,l,r,v);//与右儿子有交集
pushup(now);
}
ll query(int now,int l,int r){
int L=tree[now].l,R=tree[now].r;
if(l==L&&r==R)return tree[now].sum;//现区间的边界与需要查询的边界刚刚好相同直接返回
pushdown(now);//真正懒的所在,需要用到的时候懒标记才会继续向下传递,add里面并没有将子孙更新完全
int mid=(L+R)>>1;
if(l>mid)return query(now<<1|1,l,r);
else if(r<=mid)return query(now<<1,l,r);
else return query(now<<1,l,mid)+query(now<<1|1,mid+1,r);
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
buildtree(1,1,n);
while(q--){
char str[3];
scanf("%s",str);
if(str[0]=='C'){
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
add(1,x,y,v);
}
else{
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",query(1,x,y));
}
}
return 0;
}

A - A Simple Problem with Integers (线段树的区间修改与区间查询)的更多相关文章

  1. A Simple Problem with Integers(线段树,区间更新)

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

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

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

  3. 3468-A Simple Problem with Integers 线段树(区间增减,区间求和)

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

  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 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  8. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

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

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

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

随机推荐

  1. 队列的顺序存储与链式存储c语言实现

    一. 队列 1.队列定义:只允许在表的一端进行插入,表的另一端进行删除操作的线性表. 2.循环队列:把存储队列的顺序队列在逻辑上视为一个环. 循环队列状态: 初始时:Q.front=Q.rear=0 ...

  2. 看完这篇Redis缓存三大问题,保你面试能造火箭,工作能拧螺丝。

    前言 日常的开发中,无不都是使用数据库来进行数据的存储,由于一般的系统任务中通常不会存在高并发的情况,所以这样看起来并没有什么问题. 一旦涉及大数据量的需求,如一些商品抢购的情景,或者主页访问量瞬间较 ...

  3. Syntax error, insert "}" to complete MethodBody

    jsp中代码在Eclipse中打开正常,导入项目导入MyEclipse后显示如下异常: Syntax error, insert "}" to complete MethodBod ...

  4. Nginx 从入门到放弃(四)

    前面我们学习了nginx的基本操作和日志管理,今天我们学习一下生产环境经常会用到的路由定位location设置,在工作中,经常可能会出现怎么设置的路由访问不到网页呀?总是出现404错误啊,这些都很有可 ...

  5. 标记下 'net 查壳/脱壳/加壳' 工具

    net查壳工具 DotNet Id v1.0.0.3 该net程序集被“MaxToCode”加壳了(也不能完全相信). 加壳工具就是列表中列出的了. de4Net.exe脱壳工具版本de4dot-v3 ...

  6. Spring 5.2.x 源码环境搭建(Windows 系统环境下)

    前期准备 1.确保本机已经安装好了 Git 2.Jdk 版本至少为 1.8 3.安装好 IntelliJ IDEA (其他开发工具,如 eclipse.Spring Tool Suite 等也是可以的 ...

  7. JavaScript图形实例:随机SierPinski三角形

    在“JavaScript图形实例:SierPinski三角形”中,我们介绍了SierPinski三角形的基本绘制方法,在“JavaScript图形实例:迭代函数系统生成图形”一文中,介绍了采用IFS方 ...

  8. ASP.NET CORE MVC用时分析工具MiniProfiler

    ASP.NET CORE MVC用时分析工具MiniProfiler MiniProfiler(https://miniprofiler.com/)是一个轻量级且简单易用的分析工具库,它可以用来分析A ...

  9. redis(二十四):Redis分布式锁以及实现(python)

    阅读目录 什么事分布式锁 基于redis实现分布式锁 一.什么是分布式锁 我们在开发应用的时候,如果需要对某一个共享变量进行多线程同步访问的时候,可以使用我们学到的锁进行处理,并且可以完美的运行,毫无 ...

  10. CRM【第三篇】: crm业务

    1. 项目背景 crm系统是某某教育公司正在使用的项目,系统主要为 销售部.运营部.教质部门提供管理平台,随着公司规模的扩展,对公司员工的业务信息量化以及信息化建设越来越重要. crm系统为不同角色的 ...