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 ...
随机推荐
- hdu1251统计难题(trie)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
- Pascal “熊猫烧香”(骗人的)
Pascal仿熊猫烧香病毒,慎用 program japussy;useswindows, sysutils, classes, graphics, shellapi{, registry};cons ...
- Python 3基础教程9-函数
本文介绍Python中的函数,主要了解如何定义一个函数,如何调用一个函数. 如果上面你不写调用函数这行代码,你运行后,是没有打印输出的.我们这里来,结合前面的if语句来定义一个,两个数比较,判断最大的 ...
- 如何从List中删除元素
从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo { public static void main(Str ...
- BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值
题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...
- POJ 3090 Visible Lattice Points | 其实是欧拉函数
题目: 给一个n,n的网格,点可以遮挡视线,问从0,0看能看到多少点 题解: 根据对称性,我们可以把网格按y=x为对称轴划分成两半,求一半的就可以了,可以想到的是应该每种斜率只能看到一个点 因为斜率表 ...
- 【ZBH选讲·拍照】
[问题描述] 假设这是一个二次元.LYK召集了n个小伙伴一起来拍照.他们分别有自己的身高Hi和宽度Wi.为了放下这个照片并且每个小伙伴都完整的露出来,必须需要一个宽度为ΣWi,长度为max{Hi}的相 ...
- 2017-7-18-每日博客-关于Linux下的通配符.doc
*:匹配零个或多个字符 ?:匹配任意单个字符 ~:当前用户家目录 ~username:用户家目录 ~+:当前工作目录 ~-:上一个工作目录 [0-9]:匹配任意数字范围 [a-z]:匹配任意小写字母( ...
- ACdream 1029 前缀和
Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Subm ...