This documentation is automatically generated by online-judge-tools/verification-helper
This project is maintained by tsutaj
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#include "../strc_002_bit.cpp"
#include "../strc_004_bit_sec.cpp"
// Verified on Apr 27, 2019
void DSL_2_G() {
int N, Q; cin >> N >> Q;
using ll = long long int;
BIT_sec<ll> bit(N);
for(int i=0; i<Q; i++) {
int query_type; cin >> query_type;
if(query_type == 0) {
int s, t, x; cin >> s >> t >> x;
bit.add(s, t, x);
}
if(query_type == 1) {
int s, t; cin >> s >> t;
cout << bit.sum(s, t) << endl;
}
}
}
int main() {
DSL_2_G();
return 0;
}
#line 1 "structure/verify/verify_strc_004_bit_sec.cpp"
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#line 1 "structure/strc_002_bit.cpp"
// Binary Indexed Tree (BIT)
// Verified: AOJ DSL_2_B: Range Sum Query (intのみ)
template <typename T>
struct BIT{
private:
vector<T> array;
int n;
public:
// 初期化
BIT() {}
BIT(int _n) : array(_n + 1, 0), n(_n) {}
// 1番目から i番目までの累積和を求める
T sum(int i) {
T s = 0;
while(i > 0) {
s += array[i];
i -= i & -i; // LSB 減算
}
return s;
}
// [i, j] の要素の総和
T sum(int i, int j) {
T ret_i = sum(i-1);
T ret_j = sum(j);
return ret_j - ret_i;
}
// i 番目に 要素 x を追加
void add(int i, T x) {
while(i <= n) {
array[i] += x;
i += i & -i; // LSB 加算
}
}
};
#line 1 "structure/strc_004_bit_sec.cpp"
// 次の2つのクエリに対応するBIT
// ・[a, b) の要素全てに x を加えるクエリ
// ・[a, b) の和を計算するクエリ
// Verified: POJ 3468 (A Simple Problem with Integers)
template <typename U>
class BIT_sec {
public:
int n;
BIT<U> bit0, bit1;
BIT_sec(int n_) {
n = n_;
bit0 = BIT<U>(n);
bit1 = BIT<U>(n);
}
// 最初に要素を追加するときはこっち
void add(int i, int x) {
bit0.add(i, x);
}
// [l, r] の全要素に x を加える
void add(int l, int r, U x) {
bit0.add(l, -x * (l-1));
bit1.add(l, x);
bit0.add(r+1, x*r);
bit1.add(r+1, -x);
}
U sum(int l, int r) {
U res = 0;
res += bit0.sum(r) + bit1.sum(r) * r;
res -= bit0.sum(l-1) + bit1.sum(l-1) * (l-1);
return res;
}
};
#line 7 "structure/verify/verify_strc_004_bit_sec.cpp"
// Verified on Apr 27, 2019
void DSL_2_G() {
int N, Q; cin >> N >> Q;
using ll = long long int;
BIT_sec<ll> bit(N);
for(int i=0; i<Q; i++) {
int query_type; cin >> query_type;
if(query_type == 0) {
int s, t, x; cin >> s >> t >> x;
bit.add(s, t, x);
}
if(query_type == 1) {
int s, t; cin >> s >> t;
cout << bit.sum(s, t) << endl;
}
}
}
int main() {
DSL_2_G();
return 0;
}