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 ...
随机推荐
- Python装饰器探究——装饰器参数
Table of Contents 1. 探究装饰器参数 1.1. 编写传参的装饰器 1.2. 理解传参的装饰器 1.3. 传参和不传参的兼容 2. 参考资料 探究装饰器参数 编写传参的装饰器 通常我 ...
- Toolbar中menu菜单文字颜色的修改
Toolbar菜单中menu当中我们大多数都使用图片来按钮,可是有些时候我们也会直接使用文字,文字的颜色如何修改呢. 其实很简单,我们只要修改styles.xml文件中,添加一句 <item n ...
- java从数据库读取菜单,递归生成菜单树
首先看一下菜单的样子 根据这个样子我们定义菜单类 public class Menu { // 菜单id private String id; // 菜单名称 private String name; ...
- python中全局变量的修改
对于全局变量的修改,如果全局变量是int或者str,那么如果想要在函数中对函数变量进行修改,则需要先在函数内,声明其为global,再进行修改 如果是list或者dict则可以直接修改 a = 1 b ...
- spring里面的context:component-scan
原文:http://jinnianshilongnian.iteye.com/blog/1762632 component-scan的作用的自动扫描,把扫描到加了注解Java文件都注册成bean &l ...
- bootstrap设计横线上加字
1.给横线上加字 . 2.思路:通过z-index实现,可以将父元素的z-index设置成2,而横线的z-index设置成-1,这样有字的地方就可以覆盖横线,再设置字的padding达到合理的宽度 ( ...
- DNSSec
Domain Name System Security Extensions (DNSSEC)DNS安全扩展,是由IETF提供的一系列DNS安全认证的机制(可参考RFC2535).它提供了一种来源鉴定 ...
- CodeForces-999D Equalize the Remainders
题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...
- CI框架入门
本人最近在学习CI框架,网上找到一些个人觉得入门比较好的资料,记录一下: 兄弟连的CI框架入门系类: [军哥谈CI框架]之入门教程之第一讲:codeigniter的介绍和安装配置:http://bbs ...
- fragment中的WebView返回上一页
public final class Text1Fm extends Fragment { static WebView mWeb; private View mContentView; privat ...