线段树
来源:互联网 发布:php生成8位唯一邀请码 编辑:程序博客网 时间:2024/05/23 18:20
#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;const int maxn=262150;int n,m;int tree[maxn];void build(int node = 1, int l = 1, int r = n){ if(l==r) { scanf("%d",&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];}int g_x, g_index;void update(int node = 1, int l = 1, int r = n){ if(l==r) { tree[node]+=g_x; return; } int mid=(l+r)/2; int lc=node<<1; int rc=(node<<1)+1; if(g_index<=mid) { update(lc,l,mid); } else if(g_index>mid) { update(rc,mid+1,r); } tree[node]=tree[lc]+tree[rc];}int g_L,g_R;int 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; int ans=0; if(g_L<=mid) { ans+=query(lc,l,mid); } if(g_R>mid) { ans+=query(rc,mid+1,r); } return ans;}int main(){ scanf("%d",&n); build(); scanf("%d",&m); while(m--) { int param[3]; for(int i=0; i<3; i++) { scanf("%d",¶m[i]); } if(param[0]==1) { g_index=param[1]; g_x=param[2]; update(); } else if(param[0]==2) { g_L=param[1]; g_R=param[2]; printf("%d\n",query()); } } return 0;}
阅读全文
0 0
- 线段树?线段树!
- 线段树?线段树!
- 线段_线段树
- 线段_线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- 线段树
- mr中的combiner
- 内存监视
- Cookie应用:显示上次访问页面时间
- bzoj 4278: [ONTAK2015]Tasowanie&bzoj 1692: [Usaco2007 Dec]队列变换 后缀数组+贪心
- POJ3276-Face The Right Way
- 线段树
- 用java编程模仿DOS下的dir命令,列出某个目录下的内容
- Kotlin配置与使用
- 彻头彻尾理解 HashMap
- 我的mongodb操作手册
- 浅谈迭代器Iterator
- C# 实现Winform全屏后不遮挡任务栏,显示任务栏
- sping+springmvc+mybatis+shiro,配置文件
- js 获取字符串某字符首次出现的位置