Segment Tree
姑且叫这种数据结构这个名字
#include<iostream>
#include<cstdio>
#define N 200005
#define Lson ret<<1
#define Rson ret<<1|1
#define lson l,mid,ret<<1
#define rson mid+1,r,ret<<1|1
using namespace std;
typedef long long ll;
int n,m,x,y;
struct node{
int l,r;
ll w,f;
}t[N<<2];
inline ll Merge(int LS,int RS){
return t[LS].w+t[RS].w;
}
inline void Build(int l,int r,int ret){
t[ret].l=l;t[ret].r=r;
if(l==r){
scanf("%lld",&t[ret].w);
return;
}
int mid=(l+r)>>1;
Build(lson);
Build(rson);
t[ret].w=Merge(Lson,Rson);
}
inline void Pushdown(int ret){
t[Lson].f+=t[ret].f;
t[Rson].f+=t[ret].f;
t[Lson].w+=(ll)t[ret].f*(t[Lson].r-t[Lson].l+1);
t[Rson].w+=(ll)t[ret].f*(t[Rson].r-t[Rson].l+1);
t[ret].f=0;
}
inline ll Quary(int l,int r,int ret,int L,int R){
ll ans=0;
if(t[ret].f)Pushdown(ret);
if(t[ret].l>=L&&t[ret].r<=R)
return t[ret].w;
int mid=(l+r)>>1;
if(L<=mid)ans+=Quary(lson,L,R);
if(R>=mid+1)ans+=Quary(rson,L,R);
return ans;
}
inline void Update(int l,int r,int ret,int Ch,int L,int R){
if(l>=L&&r<=R){
t[ret].f+=Ch;
t[ret].w+=(ll)(r-l+1)*Ch;
return ;
}
if(t[ret].f)Pushdown(ret);
int mid=(l+r)>>1;
if(L<=mid)Update(lson,Ch,L,R);
if(R>=mid+1)Update(rson,Ch,L,R);
t[ret].w=Merge(Lson,Rson);
}
int main(){
scanf("%d%d",&n,&m);
Build(1,n,1);
int opt,a,b;ll c;
while(m--){
scanf("%d%d%d",&opt,&a,&b);
if(opt==1){
scanf("%d",&c);
Update(1,n,1,c,a,b);
}
else printf("%lld\n",Quary(1,n,1,a,b));
}
return 0;
}
Segment Tree的更多相关文章
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- Lintcode: Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
随机推荐
- [bzoj2733]永无乡&&[bzoj3545]Peaks
并不敢说完全会了线段树合并,只是至少知道原理写法了...还是太菜了,每天被大佬吊锤qwq 我看到的几道线段树合并都是权值线段树的合并.这个算法适用范围应该只是01线段树的. 这两道算入门题了吧... ...
- CSS计数器(自定义列表)
概念 CSS3计数器(CSS Counters)可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能(自定义有序列表) 与有序列表相比,它的突出特性在于可以对任意元素计数,同时实 ...
- android gridview 停止滚动
http://blog.csdn.net/yaphetzhao/article/details/50544105 参考上面的博客,关键代码我就贴出来吧: public void stopGridVie ...
- Python数据类型一
一.整型 在Python内部对整数的处理分为普通整数和长整数,普通整数长度为机器位长,通常都是32位,超过这个范围的整数就自动当长整数处理,而长整数的范围几乎完全没限制Python可以处理任意大小的整 ...
- Erlang中atom的实现[转]
转自: http://www.cnblogs.com/zhengsyao/p/3424539.html 在 Erlang 中,使用 atom 既方便又高效,我们就来看看 atom 是怎么实现的.ato ...
- 为什么rows这么大,在mysql explain中---写在去acumg听讲座的前一夜
这周五下班前,发现了一个奇怪问题,大概是这个背景 一张表,结构为 Create Table: CREATE TABLE `out_table` ( `id` ) NOT NULL AUTO_INCRE ...
- Python网络编程(子进程的创建与处理、简单群聊工具)
前言: 昨天我们已经了解了多进程的原理以及它的实际使用 Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函数调用,调用一次,返回一次, 但是fork()调用一次,返回两次 ...
- shell文本处理工具总结
shell文本处理工具总结 为了效率,应该熟练的掌握自动化处理相关的知识和技能,能力就表现在做同样的一件事情,可以做的很好的同时,耗时还很短. 再次总结shell文本处理的相关规则,对提高软件调试效率 ...
- ImportError: libcudart.so.8.0: cannot open shared object file: No such file or directory
这是在python中使用caffe产生的错误. 程序很普通: #-*-coding=utf-8-*- import numpy as npimport matplotlib.pyplot as plt ...
- HDU 4731 Minimum palindrome (找规律)
M=1:aaaaaaaa…… M=2:DFS+manacher, 暴出N=1~25的最优解,找规律.N<=8的时候直接输出,N>8时,头两个字母一定是aa,剩下的以aababb循环,最后剩 ...