差分数组可以单点修改、区间查询。
这个题要区间修改,所以用差分的思想,存差分数组。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int n, m;
int a[N];
LL t[N];
int lowbit(int x)
{
return x & -x;
}
void add(int x, int c)
{
for (int i = x; i <= n; i += lowbit(i)) t[i] += c;
}
LL sum(int x)
{
LL res = 0;
for (int i = x; i >= 1; i -= lowbit(i)) res += t[i];
return res;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) add(i, a[i] - a[i - 1]);
while (m--)
{
char c;
int l, r, d;
cin >> c >> l;
if (c == 'C')
{
cin >> r >> d;
add(l, d), add(r + 1, -d);
}
else
{
cout << sum(l) << endl;
}
}
return 0;
}