树状数组模板1——单点修改区间查询

树状数组的模板,修改单点的值,查询某个区间

 1 #include<cstdio>
 2 #include<cctype>
 3 using namespace std;
 4
 5 int c[500010],n,m;
 6
 7 int read()
 8 {
 9     int x=0,f=1;
10     char c=getchar();
11     while (!isdigit(c))
12         f=c==‘-‘?-1:1,c=getchar();
13     while (isdigit(c))
14         x=(x<<1)+(x<<3)+(c^48),c=getchar();
15     return x*f;
16 }
17
18 int lowbit(int x)
19 {
20     return x&-x;
21 }
22
23 int query(int x)
24 {
25     int ans=0;
26     for (;x;x-=lowbit(x))
27         ans+=c[x];
28     return ans;
29 }
30
31 int update(int x,int val)
32 {
33     for (;x<=n;x+=lowbit(x))
34         c[x]+=val;
35 }
36
37 int main()
38 {
39     int i,j,t;
40     n=read(),m=read();
41     for (i=1;i<=n;i++)
42     {
43         j=read();
44         update(i,j);
45     }
46     while (m--)
47     {
48         t=read();
49         i=read();
50         j=read();
51         if (t==1)
52             update(i,j);
53         else
54             printf("%d\n",query(j)-query(i-1));
55     }
56     return 0;
57 }

原文地址:https://www.cnblogs.com/Ronald-MOK1426/p/8848825.html

时间: 2024-08-05 08:22:23

树状数组模板1——单点修改区间查询的相关文章

树状数组的建树 单点修改 单点查询 区间修改 区间查询

单点修改  单点查询   用普通数组就能写出来 单点修改  区间查询   用线段树  树状数组: 区间修改  区间查询   用线段树  树状数组: 区间修改  单点查询   用线段树  树状数组: 建树 #include<bits/stdc++.h> using namespace std; const int maxn=1e5; struct node { int l,r,w; }tree[4*maxn+1]; void build(int l,int r,int k) { tree[k].

树状数组,适用于单点修改,区间查询

int c[50005]; //对应原数组和树状数组 int lowbit(int x){ return x&(-x); } void updata(int i,int k){ //在i位置加上k while(i <= n){ c[i] += k; i += lowbit(i); } } int getsum(int i){ //求A[1 - i]的和 int res = 0; while(i > 0){ res += c[i]; i -= lowbit(i); } return re

树状数组模板(区间修改+单点查询)

很巧妙的用了差分建树,解决区间修改的问题 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=5e5+5; 5 6 int n,m; 7 int a[maxn]; 8 ll tree[maxn]; 9 10 int lowbit(int x){ 11 return x&(-x); 12 } 13 14 void add(int idx,int v){ 15

HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)

题目链接 ac代码(注意字符读入前需要注意回车的影响) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<c

树状数组模板(持续更新)

树状数组题目(持续更新) \(1.\) 树状数组 \(1\) :单点修改,区间查询 \(2.\) 树状数组 \(2\) :区间修改,单点查询 \(3.\) 树状数组 \(3\) :区间修改,区间查询 树状数组单点修改,区间查询和 $View$ $Code$ //省略头文件 using namespace std; inline int read() { int ret=0,f=1; char ch=getchar(); while(ch>'9'||ch='0'&&ch 树状数组区间修

二维树状数组模板(区间修改+区间查询)

二维树状数组模板(区间修改+区间查询) 例题:JOIOI上帝造题的七分钟 一共两种操作: \(L\ x_1\ y_1\ x_2\ y_2\ d\):把\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素加\(d\). \(k\ x_1\ y_1\ x_2\ y_2\):查询\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素的和. 代码如下: #include<bits/stdc++.h> #define RG register #define IL i

hdu 3584 二进制0,1反转 三维树状数组 及三维树状数组模板

先贴自己类比二维树状数组写的三维树状数组模板: 开始的时候循环体内j=y,k=z,没写,以为自己思路错了,,,hehe..... 更高维的树状数组以此类比 const int MAXN = 100+10; int c[MAXN][MAXN][MAXN];int X,Y,Z; int N; inline int lowbit(int x){return x&(-x);} void update(int x, int y, int z, int v) { int i=x,j=y,k=z; while

poj 1195 二维树状数组 及二维树状数组模板

http://poj.org/problem?id=1195 求矩阵和的时候,下标弄错WA了一次... 求矩形(x1,y1) (x2,y2)的sum |sum=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1) 二维树状数组讲解:http://blog.csdn.net/u011026968/article/details/38532117 二维树状数组模板: /*========================================

A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4032    Accepted Submission(s): 1255 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with