树状数组详解
https://blog.csdn.net/sjystone/article/details/115396746
简单的整数问题I https://www.acwing.com/problem/content/248/
树状数组拓展
将某一区间的每个数加上c,求a[x] -> 差分
-> 树状数组中存储a[i]的差分
构造差分:
b[i] = a[i] - a[i - 1]
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int a[N], tr[N], n, m;
int lowbit(int x)
{
return x & -x;
}
void add(int x, int c)
{
for ( int i = x; i <= n; i += lowbit(i) ) tr[i] += c;
}
LL sum(int x)
{
LL res = 0;
for ( int i = x; i; i -= lowbit(i) ) res += tr[i];
return res;
}
int main()
{
cin >> n >> m;
for ( int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for ( int i = 1; i <= n; i ++ ) add(i, a[i] - a[i - 1]); //树状数组中存a[i]的差分
while ( m -- )
{
char op[2];
scanf("%s", op);
if ( *op == 'C' )
{
int l, r, d;
scanf("%d%d%d", &l, &r, &d);
add(l, d), add(r + 1, -d); //差分日常操作
}
else
{
int x;
scanf("%d",&x);
printf("%lld\n", sum(x)); //差分数组的前缀和便是a[i]
}
}
return 0;
}