#include <cstdio>int n, m;
template<typename T, int len>
class fenwick{
protected:
T fw[len];
public:fenwick(){}
fenwick(const fenwick<T, len> &other){
for (int i = 0; i < len; ++i){
fw[i] = other.fw[i];
}
}
void add(int x, T v){
while (x < len){
fw[x] += v;
x += x & -x;
}
}
void set(int x, T v){
add(x, v - fw[x]);
}
T query(int x){
T sum = 0;
while (x){
sum += fw[x];
x ^= x & -x;
}
return sum;
}
// T &operator[](int x){// return query(x) - query(x - 1);// }T operator()(int l, int r){
return query(r) - query(l - 1);
}
};
fenwick<int, 500005> c;
void input(){
int i, v;
scanf("%d%d", &n, &m);
++n;
for (i = 1; i < n; ++i){
scanf("%d", &v);
c.add(i, v);
}
}
void output(){
int o, x, y;
while (m--){
scanf("%d%d%d", &o, &x, &y);
if (o & 1){
c.add(x, y);
}
else{
printf("%d\n", c(x, y));
}
}
}
int main(){
input();
output();
return 0;
}
#include <cstdio>int n, m;
template<typename T, int len>
class fenwick{
private:class element{
private:
fenwick<T, len> *fw;
int x;
public:element(fenwick<T, len> *m_fw, int m_x): fw(m_fw), x(m_x){}
operator T(){
return fw -> _query(fw -> a, x);
}
// element &operator=(const T &v){// fw -> _set(fw -> a, x, v);// return *this;// }element &operator+=(T v){
fw -> _add(fw -> a, x, v);
fw -> _add(fw -> b, x, v * x);
fw -> _add(fw -> a, x + 1, -v);
fw -> _add(fw -> b, x + 1, -v * (x + 1));
return *this;
}
};
protected:
T a[len], b[len];
void _add(T *c, int x, T v){
while (x < len){
c[x] += v;
x += x & -x;
}
}
T _query(T *c, int x){
T sum = 0;
while (x){
sum += c[x];
x ^= x & -x;
}
return sum;
}
void _set(T *c, int x, T v){
T temp = v - a[x];
_add(c, x, temp);
_add(c, x + 1, -temp);
}
public:fenwick(){}
fenwick(const fenwick<T, len> &other){
for (int i = 0; i < len; ++i){
a[i] = other.a[i];
b[i] = other.b[i];
}
}
void add(int l, int r, T v){
_add(a, l, v);
_add(b, l, v * l);
_add(a, ++r, v = -v);
_add(b, r, v * r);
}
// void set(int l, int r, T v){// _add(a, l, v);// _add(b, l, v * l);// _add(a, ++r, v = -v);// _add(b, r, v * r);// }element operator[](int x){
return element(this, x);
}
T operator()(int l, int r){
return _query(a, r) * (r + 1) - _query(a, l - 1) * l - _query(b, r) + _query(b, l - 1);
}
};
fenwick<long long, 500005> c;
void input(){
long long v, x = 0ll;
int i;
scanf("%d%d", &n, &m);
++n;
for (i = 1; i < n; ++i){
scanf("%lld", &v);
c[i] += x = v;
}
}
void output(){
long long k;
int o, x, y;
while (m--){
scanf("%d%d%d", &o, &x, &y);
if (o & 1){
scanf("%lld", &k);
c.add(x, y, k);
}
else{
printf("%lld\n", c(x, y));
}
}
}
int main(){
input();
output();
return 0;
}
Python
class fenwick(object):
def __init__(self, n):
if (type(n) == fenwick):
self.a = [i for i in n.a]
self.b = [i for i in n.b]
self.len = n.len
else:
self.a = [0] * n
self.b = [0] * n
self.len = n
def _add(self, c, x, v):
while x < self.len:
c[x] += v
x += x & -x
def _query(self, c, x):
s = 0while x:
s += c[x]
x ^= x & -x
return s
def add(self, l, r, v):
self._add(self.a, l, v)
self._add(self.b, l, v * l)
r += 1
v = -v
self._add(self.a, r, v)
self._add(self.b, r, v * r)
def __getitem__(self, x):
return self._query(self.a, x)
def __call__(self, l, r):
return self._query(self.a, r) * (r + 1) - self._query(self.a, l - 1) * l - self._query(self.b, r) + self._query(self.b, l - 1)