A - A Simple Problem with Integers (线段树的区间修改与区间查询)
You have N integers, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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 (线段树的区间修改与区间查询)的更多相关文章
- A Simple Problem with Integers(线段树,区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 83822 ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
- 3468-A Simple Problem with Integers 线段树(区间增减,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 110077 ...
- 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 线段树区间修改
http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和 C A B ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- MySQL 字符串索引优化方案
字符串建立索引的优化 1. 建立前缀索引 假设建立一个支持邮箱登录的用户表,对于邮件字段来说,可以有以下几种建立索引的方式: 直接对整个字符串建立索引 alter table SUser add in ...
- SpringCloud项目配置加载顺序
bootstrap.yml:位于jar包外的优先级最高 application.yml: 配置中心的文件 > JVM参数配置> 本地active指定文件 > 本地default文件, ...
- JVM源码分析之Java对象头实现
原创申明:本文由公众号[猿灯塔]原创,转载请说明出处标注 “365篇原创计划”第十一篇. 今天呢!灯塔君跟大家讲: JVM源码分析之Java对象头实现 HotSpot虚拟机中,对象在内存中的布局分为三 ...
- 洛谷 P4408 [NOI2003]逃学的小孩
题目传送门 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:“喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?”一听说要考试,Chris的父母就心急如焚, ...
- 攻防世界-新手篇(Mise)~~~
Mise this_is_flag 签到题flag{th1s_!s_a_d4m0_4la9} pdf 打开图片,flag值在图片底下,wps将pdf转为word格式后,将图片拉开发现flag flag ...
- day48 work
1 navicat自己玩一玩 2 练习题一定要搞懂 照着我的思路一遍遍的看敲 3 熟悉pymysql的使用 4 sql注入产生的原因和解决方法 了解 5 思考:如何结合mysql实现用户的注册和登录功 ...
- mysql两种重要的引擎
其中MyISAM:不支持事物,表锁 .frm : 表结构定义文件 .MYD: 表数据 .MYI:索引文件 InnoDB:支持事物,行锁 .frm : 表结构定义文件 .ibd:表空间(数据和索引)
- Navigation Nightmare POJ - 1984
Navigation Nightmare Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usual ...
- MYSQL 之 JDBC(九):增删改查(七)DAO的补充和重构
DAO重构后的代码 package com.litian.jdbc; import org.apache.commons.beanutils.BeanUtils; import java.sql.*; ...
- python 装饰器(七):装饰器实例(四)类装饰器装饰类以及类方法
类装饰器装饰类方法 不带参数 from functools import wraps import types class CatchException: def __init__(self,orig ...