线段树
来源:互联网 发布:国家发改委 大数据 编辑:程序博客网 时间:2024/06/07 16:17
#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <iostream>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <deque>#include <map>#include <set>using std::cin;using std::cout;using std::endl;typedef long long ll;const int maxn=524300;int n,m;ll tree[maxn];ll lazy[maxn];void build(int node = 1, int l = 1, int r = n){ if(l==r) { scanf("%lld",&tree[node]); return; } int mid=(l+r)/2; int lc=node<<1; int rc=(node<<1)+1; build(lc, l, mid); build(rc, mid+1, r); tree[node] = tree[lc] + tree[rc];}void pushdown(int node, int l, int r){ if(lazy[node]) { int lc=node<<1; int rc=(node<<1)+1; lazy[lc]+=lazy[node]; lazy[rc]+=lazy[node]; int mid=(l+r)/2; tree[lc]+=lazy[node] * (mid-l+1); tree[rc]+=lazy[node] * (r-mid); lazy[node]=0; }}int g_L,g_R;ll g_x;void update(int node = 1, int l = 1, int r = n){ if(g_L<=l && r<=g_R) { tree[node]+=g_x * (r-l+1); lazy[node]+=g_x; return; } int mid=(l+r)/2; int lc=node<<1; int rc=(node<<1)+1; pushdown(node, l, r); if(g_L<=mid) { update(lc,l,mid); } if(g_R>mid) { update(rc,mid+1,r); } tree[node]=tree[lc]+tree[rc];}ll query(int node = 1, int l = 1, int r = n){ if(g_L<=l && r<=g_R) { return tree[node]; } int mid=(l+r)/2; int lc=node<<1; int rc=(node<<1)+1; pushdown(node, l, r); ll sum=0; if(g_L<=mid) { sum+=query(lc,l,mid); } if(g_R>mid) { sum+=query(rc,mid+1,r); } return sum;}int main(){ scanf("%d",&n); build(); scanf("%d",&m); while(m--) { int ins; scanf("%d",&ins); if(ins==1) { scanf("%d%d%lld",&g_L,&g_R,&g_x); update(); } else if(ins==2) { scanf("%d%d",&g_L,&g_R); printf("%lld\n",query()); } } return 0;}
阅读全文
0 0
- 线段树?线段树!
- 线段树?线段树!
- 线段_线段树
- 线段_线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- bzoj2582[Usaco2012Jan]Bovine Alliance
- c++标准库笔记:13.4.4 Stream的状态和异常
- 运算符
- LeetCode 43.Multiply Strings & 46.Permutations & 47.Permutations II
- 自动化测试基础
- 线段树
- java面试基础题之二
- Housewife Wind 【LCA转RMQ 求最短路+边权修改】or 【树链剖分】
- 玩转Spring Boot 集成Dubbo
- leofs Gateway介绍
- DHUOJ 2016060702
- Dubbo在Spring和Spring Boot中的使用
- springMVC同属性名的多对象
- Linux中Hard link和Symbol link的区别