操作说明:

  • segtree<T>tree(len) =>创建一个内部元素类型为T、区间为1~len的线段树tree
  • tree.build(l,r) =>以[l,r]区间建立线段树
  • tree.update(l,r,val,1,len) =>区间[l,r]所有元素加上val
  • tree.query(l,r,1,len) =>计算区间[l,r]的元素和
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#define range(i,a,b) for(auto i=a;i<=b;++i)
#define LL long long
#define itrange(i,a,b) for(auto i=a;i!=b;++i)
#define rerange(i,a,b) for(auto i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
template <class T>
class segtree{
private:
T* add,*sum;
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int m){
if(add[rt]){
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=add[rt]*(m-(m>>));
sum[rt<<|]+=add[rt]*(m>>);
add[rt]=;
}
}
public:
explicit segtree(int len=int(1e5+)){
add=new T[len<<];
sum=new T[len<<];
fill(add,);fill(sum,);
}
void build(int l,int r,int rt=){
add[rt]=;
if(l==r){
sum[rt]=;
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
}
void update(int L,int R,T c,int l,int r,int rt=){
if(L<=l&&r<=R){
add[rt]+=c;
sum[rt]+=c*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)update(L,R,c,l,m,rt<<);
if(m<R)update(L,R,c,m+,r,rt<<|);
pushup(rt);
}
T query(int L,int R,int l,int r,int rt=){
if(L<=l&&r<=R)return sum[rt];
pushdown(rt,r-l+);
int m=(l+r)>>;
T ret=;
if(L<=m)ret+=query(L,R,l,m,rt<<);
if(m<R)ret+=query(L,R,m+,r,rt<<|);
return ret;
}
};
int main() {
segtree<int>tree();
tree.build(,);
tree.update(,,,,);
cout<<tree.query(,,,)<<endl;
return ;
}

C++线段树模板(区间和、区间加)的更多相关文章

  1. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  2. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  3. 洛谷3372线段树模板题 对区间+k或者查询区间和

    #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...

  4. hdu1754 I hate it线段树模板 区间最值查询

    题目链接:这道题是线段树,树状数组最基础的问题 两种分类方式:按照更新对象和查询对象 单点更新,区间查询; 区间更新,单点查询; 按照整体维护的对象: 维护前缀和; 维护区间最值. 线段树模板代码 # ...

  5. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

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

  6. HDU 1698 Just a Hook (线段树模板题-区间求和)

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

  7. [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

    可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...

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

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

  9. HDU 1754 I Hate It(线段树之单点更新 区间最值查询)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

随机推荐

  1. MFC Button控件自绘

    文章参考地址:  http://blog.csdn.net/yue7603835/article/details/6649458    VC下的界面着实难看 有时候我们不得不自己进行控件的绘制 以前 ...

  2. BZOJ3671 [Noi2014]随机数生成器 【贪心】

    题目链接 BZOJ3671 题解 模拟题意生成矩阵贪心从小选择即可 每选择一个,就标记其左下右上矩阵 由于每次都是标记一个到边界的矩阵,所以一旦遇到标记过就直接退出即可,可以保证复杂度 还有就是空间和 ...

  3. BZOJ3648 寝室管理 【点分治 + 环套树】

    3648: 寝室管理 Time Limit: 40 Sec  Memory Limit: 512 MB Submit: 366  Solved: 152 [Submit][Status][Discus ...

  4. [NOIP2016]换教室 期望dp

    先弗洛伊德,然后把状态拆分遗传 #include<iostream> #include<cstdio> #include<cstring> #include< ...

  5. 使用babel把es6代码转成es5代码

    第一步:创建一个web项目 使用命令:npm init 这个命令的目的是生成package.json. 执行第二步中的命令后生成的package.json的文件的内容是: { "name&q ...

  6. bzoj 5094 [Lydsy1711月赛]硬盘检测 概率dp

    [Lydsy1711月赛]硬盘检测 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 273  Solved: 75[Submit][Status][Dis ...

  7. Input操作文件

    在HTML表单中,可以上传文件的唯一控件就是<input type="file">. 注意:当一个表单包含<input type="file" ...

  8. 使用MAT分析内存泄露

    使用MAT分析内存泄露 对于大型服务端应用程序来说,有些内存泄露问题很难在测试阶段发现,此时就需要分析JVM Heap Dump文件来找出问题.随着单机内存越来越大,应用heap也开得越来越大,动辄十 ...

  9. linux crontab执行shell脚本中包含相对路径的问题

    实例一 test.sh文件 echo `date`>test.log 配置crontab 设置 */1 * * * * sh /data/test.sh 在/data/目录下,未找到test.l ...

  10. C# 程序Hello World

    先创建一个工程文件->选择的是console application. 然后开始写代码如下: using System; using System.Collections.Generic; us ...