Language: C++


#include <vector>
using namespace std;

//Base class for Segment Trees
template <class T>
class segtree_base
{
protected:
	size_t width;
	vector<T> vec;
	T def;
	virtual T query_raw(int l_, int r_, int a_, int b_, int k_) = 0;
public:
	segtree_base(size_t size_, const T& def_ = T()) : def(def_)
	{
		width = 1;
		while (width < size_)width *= 2;
		vec.resize(width * 2 - 1, def);
	}
	T query(int left, int right)
	{
		return query_raw(left, right, 0, width, 0);
	}
	virtual ~segtree_base() {}
};


#include <algorithm>
//Range Sum + Range Add Tree
template <class T>
class rsaq_tree : public segtree_base<T>
{
protected:
	vector<T>lazy;
public:
	rsaq_tree(size_t size_, const T& def = 0) :segtree_base<T>(size_, def)
	{
		lazy.resize(vec.size());
	}
	void init(vector<int>base)
	{
		for (int i = 0; i < width; i++) vec[i + n - 1] = base[i];
		for (int i = n - 2; i >= 0; i--) vec[i] = vec[i * 2 + 1] + vec[i * 2 + 2];
	}
	void update(int left, int right, int val)
	{
		update_raw(left, right, val, 0, width, 0);
	}
private:
	void eval(int k_, int a_, int b_)
	{
		if (lazy[k_] != 0) {
			vec[k_] += lazy[k_];
			if (b_ - a_ > 1) {
				lazy[2 * k_ + 1] += lazy[k_] / 2;
				lazy[2 * k_ + 2] += lazy[k_] / 2;
			}
			lazy[k_] = 0;
		}
	}
	void update_raw(int l_, int r_, int x_, int a_, int b_, int k_)
	{
		eval(k_, a_, b_);
		if (r_ <= a_ || b_ <= l_) return;
		if (l_ <= a_ && b_ <= r_) {
			lazy[k_] += (b_ - a_) * x_;
			eval(k_, a_, b_);
		}
		else {
			update_raw(l_, r_, x_, a_, (a_ + b_) / 2, 2 * k_ + 1);
			update_raw(l_, r_, x_, (a_ + b_) / 2, b_, 2 * k_ + 2);
			vec[k_] = vec[2 * k_ + 1] + vec[2 * k_ + 2];
		}
	}
	T query_raw(int l_, int r_, int a_, int b_, int k_)override
	{
		eval(k_, a_, b_);
		if (r_ <= a_ || b_ <= l_) return def;
		if (l_ <= a_ && b_ <= r_) return vec[k_];
		T vl = query_raw(l_, r_, a_, (a_ + b_) / 2, 2 * k_ + 1);
		T vr = query_raw(l_, r_, (a_ + b_) / 2, b_, 2 * k_ + 2);
		return vl + vr;
	}
};

#include <iostream>

int main()
{
	int n, q;
	cin >> n >> q;
	rsaq_tree<long long>tree(n);
	for (int i = 0; i < q; ++i)
	{
		int com;
		cin >> com;
		if (com)
		{
			int s, t;
			cin >> s >> t;
			cout << tree.query(s - 1, t) << endl;
		}
		else
		{
			int s, t, x;
			cin >> s >> t >> x;
			tree.update(s - 1, t, x);
		}
	}
	return 0;
}

Compiler output:
a.cc: In constructor 'rsaq_tree<T>::rsaq_tree(size_t, const T&)':
a.cc:39:29: error: 'vec' was not declared in this scope
   39 |                 lazy.resize(vec.size());
      |                             ^~~
a.cc: In member function 'void rsaq_tree<T>::init(std::vector<int>)':
a.cc:43:37: error: 'width' was not declared in this scope
   43 |                 for (int i = 0; i < width; i++) vec[i + n - 1] = base[i];
      |                                     ^~~~~
a.cc:43:49: error: 'vec' was not declared in this scope
   43 |                 for (int i = 0; i < width; i++) vec[i + n - 1] = base[i];
      |                                                 ^~~
a.cc:43:57: error: 'n' was not declared in this scope
   43 |                 for (int i = 0; i < width; i++) vec[i + n - 1] = base[i];
      |                                                         ^
a.cc:44:30: error: 'n' was not declared in this scope
   44 |                 for (int i = n - 2; i >= 0; i--) vec[i] = vec[i * 2 + 1] + vec[i * 2 + 2];
      |                              ^
a.cc:44:50: error: 'vec' was not declared in this scope
   44 |                 for (int i = n - 2; i >= 0; i--) vec[i] = vec[i * 2 + 1] + vec[i * 2 + 2];
      |                                                  ^~~
a.cc: In member function 'void rsaq_tree<T>::update(int, int, int)':
a.cc:48:49: error: 'width' was not declared in this scope
   48 |                 update_raw(left, right, val, 0, width, 0);
      |                                                 ^~~~~
a.cc: In member function 'void rsaq_tree<T>::eval(int, int, int)':
a.cc:54:25: error: 'vec' was not declared in this scope
   54 |                         vec[k_] += lazy[k_];
      |                         ^~~
a.cc: In member function 'void rsaq_tree<T>::update_raw(int, int, int, int, int, int)':
a.cc:73:25: error: 'vec' was not declared in this scope
   73 |                         vec[k_] = vec[2 * k_ + 1] + vec[2 * k_ + 2];
      |                         ^~~
a.cc: In member function 'T rsaq_tree<T>::query_raw(int, int, int, int, int)':
a.cc:79:50: error: 'def' was not declared in this scope
   79 |                 if (r_ <= a_ || b_ <= l_) return def;
      |                                                  ^~~
a.cc:80:50: error: 'vec' was not declared in this scope
   80 |                 if (l_ <= a_ && b_ <= r_) return vec[k_];
      |                                                  ^~~