POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意:
给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和
代码:
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = + ;
struct{
long long val, addMark;
}segTree[maxn << ];
long long a[maxn];
int n , m;
void build(int root, int l, int r){
segTree[root].addMark = ; if(l == r){
segTree[root].val = a[l];
return;//记得return
}
int mid = (l+r) >> ;
build(root*, l,mid);
build(root*+, mid + , r);
segTree[root].val = segTree[root*].val + segTree[root*+].val; //回溯时候更新root
}
void push_down(int root,int L, int R){//传入L, R是为了计算左右子树的和, 分别是(mid - L + 1)、(R-mid)
if(segTree[root].addMark != ){
int mid = L + R >> ;
segTree[root*].addMark += segTree[root].addMark;
segTree[root*+].addMark += segTree[root].addMark; segTree[root*].val += segTree[root].addMark * (mid - L + );
segTree[root*+].val += segTree[root].addMark * (R-mid); segTree[root].addMark = ;
}
}
long long query(int root, int L, int R, int QL, int QR){
if(L > QR || R < QL) return ; if(QL <= L && QR >= R) {
return segTree[root].val;
}
push_down(root,L,R);//如果要向下计算记得先pushdown
int mid = L + R >> ;
return query(root*,L,mid,QL,QR) + query(root*+,mid+,R,QL,QR);
}
void update(int root, int L ,int R, int QL, int QR, int val){
if(L > QR || R < QL) return; if(QL <= L && QR >= R){ segTree[root].val += val * (R-L+);
segTree[root].addMark += val;
return;
} push_down(root,L,R);//如果要向下计算记得先pushdown
int mid = L + R >> ;
update(root*, L, mid, QL , QR , val);
update(root*+,mid+, R, QL,QR,val);
segTree[root].val = segTree[root*].val + segTree[root*+].val;//回溯更新root
}
int main()
{
// freopen("1.txt","r", stdin);
while(~scanf("%d %d", &n , &m)){
memset(segTree,,sizeof(segTree)); for(int i = ; i <= n; i++){
scanf("%lld", &a[i]);
}
build(,,n);
while(m--){
char cho[];
scanf("%s", cho);
int x, y;
scanf("%d %d", &x, &y);
if(cho[] == 'C'){
int v;
scanf("%d", &v);
update(,,n,x,y,v);
}
else{
printf("%lld\n",query(,,n,x,y));
}
}
}
}
POJ 3468 A Simple Problem with Integers (线段树多点更新模板)的更多相关文章
- [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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #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 ...
随机推荐
- the little schemer 笔记(8)
第八章 lambda the ultimate 还记得我们第五章末的rember和insertL吗 我们用equal?替换了eq? 你能用你eq?或者equal?写一个函数rember-f吗 还不能, ...
- ie下,php HTTP_REFERER获取失败的整理
HTTP_REFERER有效的情况1.以iframe 形式调用地址2.以window.open调用,打开新页面window.open(url);3.使用window.location.replace在 ...
- Android-apk文件反编译
一:工具介绍及下载 1:apktool 作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 2:dex2jar 作用:将apk反编译成java源码(classes ...
- OC的单例模式
原文: http://www.galloway.me.uk/tutorials/singleton-classes/ 在iOS开发中,单例是最有用的设计模式之一.它是在代码间共享数据而不需要手动传递参 ...
- PT2264解码心得
PT2264解码心得 最近闲暇时间在琢磨无线RF解码程序,正好在数码之家论坛中翻出大佬的解码程序(http://bbs.mydigit.cn/read.php?tid=245739),于是乎,慢慢学习 ...
- Spring boot Jpa添加对象字段使用数据库默认值
Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
- Redis学习笔记(四)集合进阶
1.组合与关联多个集合 差集: SDIFF key1 [key2...](返回存在于key1但不存在其他集合中的元素) SDIFFSTORE destination key1 [key2...](将存 ...
- http接口调用,传递json格式带双引号问题
springmvc 配置好会自动转换json格式,只要配置他转格式之前,在转次String类型就好
- AWS Data Lake Service Stack
- GitHub简单命令行# 使用命令行传代码到GitHub
第一次提交代码到Github 第一步: 建立本地仓库cd到你的本地项目根目录下,执行git命令 cd到本地项目 git init 第二步: 将本地项目工作区的所有文件添加到暂存区 git add . ...