POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream>
#include <stdio.h>
#include <string.h>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R using namespace std;
const int maxn=;
int n,q;
long long num[maxn];
struct Node{
long long sum,add;
bool lazy;
}tree[maxn<<]; void pushUp(int rt){
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
void pushDown(int rt,int m){
if(tree[rt].lazy){
tree[rt<<].add+=tree[rt].add;
tree[rt<<|].add+=tree[rt].add;
tree[rt<<].sum+=(m-m/)*tree[rt].add; //是加上父亲的add
tree[rt<<|].sum+=m/*tree[rt].add;
tree[rt<<].lazy=tree[rt<<|].lazy=true;
tree[rt].add=;
tree[rt].lazy=false;
}
}
void build(int rt,int L,int R){
if(L==R){
tree[rt].sum=num[L];
tree[rt].add=;
tree[rt].lazy=false;
return ;
}
int mid=(L+R)>>;
build(lson);
build(rson);
pushUp(rt);
} void update(int rt,int L,int R,int l,int r,long long val){
if(l<=L&&R<=r){
tree[rt].sum+=(R-L+)*val;
tree[rt].add+=val;
tree[rt].lazy=true;
return;
}
pushDown(rt,R-L+);
int mid=(L+R)>>;
if(l<=mid)
update(lson,l,r,val);
if(r>mid)
update(rson,l,r,val);
pushUp(rt);
} long long query(int rt,int L,int R,int l,int r){
long long ret=;
if(l<=L&&R<=r){
return tree[rt].sum;
}
pushDown(rt,R-L+); //额,好吧,忘记这里也要初始化了
int mid=(L+R)>>;
if(l<=mid)
ret+=query(lson,l,r);
if(r>mid)
ret+=query(rson,l,r);
return ret;
}
int main()
{
int a,b;
long long c; //c用int也能AC,用long long是防止当(R-L+1)*c的时候超出int范围
char str[];
long long ans;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%I64d",&num[i]);
build(,,n);
for(int i=;i<=q;i++){
scanf("%s",str);
if(str[]=='C'){
scanf("%d%d%I64d",&a,&b,&c);
update(,,n,a,b,c);
}
else{
scanf("%d%d",&a,&b);
ans=query(,,n,a,b);
printf("%I64d\n",ans);
} }
return ;
}
POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- 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 线段树第一次 + 讲解
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 (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- windows phone 手机截图
无聊在商城找了一款小游戏玩,看到一个截屏功能,就google了一下具体实现,还是比较简单的,主要是靠WriteableBitmap(提供一个可写入并可更新的 BitmapSource)类实现.看一下m ...
- u-boot ctr0.S详解 包含_main函数
/** ****************************************************************************** * @author Maox ...
- hadoop2.2.0伪分布式搭建
1.准备Linux环境 1.0点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改subnet ...
- linq 日常关键字使用
1.from var scoreQuery = from student in students from score in student.Scores where score > 90 se ...
- 利用VSCode进行.Net Core初尝试
1.下载.Net Core,https://www.microsoft.com/net/core#windows,按照页面提示进行SDK的安装. 2.下载VsCode,https://www.visu ...
- C# 测试代码运行时间
一.新建一个控制台程序项目Test.exe using System; using System.Collections.Generic; using System.Linq; using Syste ...
- Translation001——android
请尊重原创,转载请注明出处: Author:KillerLegend Link:http://www.cnblogs.com/killerlegend/ BEGIN****************** ...
- TLV格式是什么格式
TLV 格式是什么格式 一种可变格式, TLV 的意思就是: Type 类型, Lenght 长度, Value 值: Type 和 Length 的长度固定,一般那是 2 . 4 个字节: Valu ...
- How to begin Python learning?
如何开始Python语言学习? 1. 先了解它,Wiki百科:http://zh.wikipedia.org/zh-cn/Python 2. Python, Ruby等语言来自开源社区,社区的学法是V ...
- 《零成本实现Web自动化测试--基于Selenium》第一章 自动化测试基础
第一篇 Selenium 和WebDriver工具篇 第一章 自动化测试基础 1.1 初识自动化测试 自动化测试有两种常见方式 1.1.1 代码驱动测试,又叫测试驱动开发(TDD) 1.1.2 ...