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 ...
随机推荐
- 自定义控件的getChildCount
我真的是一步一步走过来,看过来的代码.不是能力问题,而是他们用的,我没用过,我用的他们不用.然后一句一句的问为什么,然后一句一句的去想为什么. 只有这样,才能慢慢的熟悉,东一榔头西一棒子,不是分模块再 ...
- HEXO next live2d插件删除问题
title: HEXO next live2d插件删除问题 date: 2018-03-06 13:09:12 updated: tags: [hexo,next,建站,学习,前端技术,疑问] des ...
- 剑指Offer - 九度1516 - 调整数组顺序使奇数位于偶数前面
剑指Offer - 九度1516 - 调整数组顺序使奇数位于偶数前面2013-11-30 02:17 题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部 ...
- USACO Section2.3 Controlling Companies 解题报告 【icedream61】
concom解题报告------------------------------------------------------------------------------------------ ...
- 自动化测试学习之路--json、dom编程
1.json: json是JavaScript Object Notation,是一种数据传输格式. 以下程序都是在浏览器的Console下执行的. 创建一个javaScript的对象: var st ...
- 浅谈 css 之 position用法
在 css中, position 属性有四个值可用: static(默认值).absolute.relative.fixed. relative:相对定位(相对于自身进行在常规流中的位置进行定位,保留 ...
- selenium初识(二)——之webdriver API
配置完的环境之后,我们先来写一个小脚本: # __Author__:"Jim_xie" from selenium import webdriver from time impor ...
- tomcat运行solr
https://blog.csdn.net/u010346953/article/details/67640036
- 孤荷凌寒自学python第三十八天初识python的线程控制
孤荷凌寒自学python第三十八天初识python的线程控制 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.线程 在操作系统中存在着很多的可执行的应用程序,每个应用程序启动后,就可以看 ...
- (转\整)UE4游戏优化 多人大地型游戏的优化(三)GPU的优化
施主分享随缘,评论随心,@author:白袍小道 小道暗语: 1.因为小道这里博客目录没自己整,暂时就用随笔目录结构,所以二级目录那啥就忽略了.标题格式大致都是(原or转) 二级目录 (标题) 2.因 ...