1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <algorithm>
#include <cstdio>
#include <vector>

/*
template<typename VALUE>
class Interval
{
	std::vector<VALUE> values;

public:
	Interval(int, int hi) : values(hi+1) { }

	void add(int a, int b, VALUE v) {
		for (int i=a; i<=b; ++i) {
			values[i] += v;
		}
	}

	VALUE get(int x) const {
		return values[x];
	}
};
*/

template<typename VALUE>
class Interval
{
	VALUE value;

	const int lo, hi, mi;
	Interval<VALUE>* left;
	Interval<VALUE>* right;

public:
	// zakładamy, że lo <= hi
	Interval(int lo, int hi) : value(0),
		lo(lo), hi(hi), mi((lo + hi) / 2),
		left(nullptr), // left będzie od lo do mi
		right(nullptr) // right będzie od mi+1 do hi
	{ }

	// zwiększ od a do b włącznie, zakładamy że a <= b
	void add(int a, int b, VALUE v) {
		if (a <= lo && hi <= b) {
			value += v;
		} else if (lo < hi) {
			if (lo <= b && a <= mi) {
				if (!left) {
					left = new Interval(lo, mi);
				}
				left->add(a, b, v);
			}
			if (mi < b && a <= hi) {
				if (!right) {
					right = new Interval(mi+1, hi);
				}
				right->add(a, b, v);
			}
		}
	}

	VALUE get(int x) const {
		VALUE result = 0;
		if (lo <= x && x <= hi) {
			result += value;
			if (left) {
				result += left->get(x);
			}
			if (right) {
				result += right->get(x);
			}
		}
		return result;
	}
};

using ull = unsigned long long;

Interval<ull>* plan;

struct Element
{
	int max_tree;
	int day;

	Element(int max_tree, int day = 0) : max_tree(max_tree), day(day)
	{ }

	bool operator<(const Element& other) const {
		return max_tree < other.max_tree;
	}
};

/*
class SetInterval
{
	std::vector<Element> elements;

	const int lo, hi, mi;
	SetInterval* left;
	SetInterval* right;

public:
	// zakładamy, że lo <= hi
	SetInterval(int lo, int hi) :
		lo(lo), hi(hi), mi((lo + hi) / 2)
	{
		if (lo < hi) {
			int mi = ((lo + hi) / 2);
			left = new SetInterval(lo, mi);
			right = new SetInterval(mi+1, hi);
		}
	}

	// dodaj od a do b włącznie, zakładamy że a <= b
	void add(int day, int max_tree) {
		if (lo <= day && day <= hi) {
			elements.emplace_back(max_tree, day);
		}
		if (lo < hi) {
			left->add(day, max_tree);
			right->add(day, max_tree);
		}
	}

	void sort() {
		std::sort(elements.begin(), elements.end());
		if (lo < hi) {
			left->sort();
			right->sort();
		}
	}

	ull calculate_sum(int a, int b, int tree_min) const {
		ull sum = 0;
		if (a <= lo && hi <= b) {
			// cały przedział
			auto it = std::lower_bound(elements.begin(), elements.end(), tree_min);
			while (it != elements.end()) {
				sum += plan->get(it->day);
				++it;
			}
			//for (const Element& e : elements) {
			//	if (e.max_tree >= tree_min) {
			//		sum += plan->get(e.day);
			//	}
			//}
		} else if (lo < hi) {
			if (lo <= b && a <= mi) {
				sum += left->calculate_sum(a, b, tree_min);
			}
			if (mi < b && a <= hi) {
				sum += right->calculate_sum(a, b, tree_min);
			}
		}
		return sum;
	}
};
*/

class SetInterval2
{
	const int CHUNK_SIZE = 200;

	std::vector<std::vector<Element>> chunks;

public:
	// zakładamy, że lo <= hi
	SetInterval2(int, int hi)
	{
		int last_c = hi / CHUNK_SIZE;
		chunks.resize(1 + last_c);
	}

	// dodaj od a do b włącznie, zakładamy że a <= b
	void add(int day, int max_tree) {
		int c = day / CHUNK_SIZE;
		chunks[c].emplace_back(max_tree, day);
	}

	void sort() {
		for (auto& chunk : chunks) {
			std::sort(chunk.begin(), chunk.end());
		}
	}

	ull calculate_sum(int a, int b, int tree_min) const {
		int first_c = a / CHUNK_SIZE;
		int last_c = b / CHUNK_SIZE;
		ull sum = 0;
		for (int c=first_c; c<=last_c; ++c) {
			const auto& chunk = chunks[c];
			auto it = std::lower_bound(chunk.begin(), chunk.end(), tree_min);
			while (it != chunk.end()) {
				int day = it->day;
				if (a <= day && day <= b) {
					sum += plan->get(it->day);
				}
				++it;
			}
		}
		return sum;
	}
};


int A[10000001];

int main() {
	int N, M, Z; scanf("%d%d%d", &N, &M, &Z);
	for (int i=1; i<=N; ++i) {
		scanf("%d", &A[i]);
	}

	plan = new Interval<ull>(1, N);

	SetInterval2 trips(1, N);
	for (int i=1; i<=N; ++i) {
		trips.add(i, A[i]);
	}
	trips.sort();

	int Q = M + Z;
	while (Q-- > 0) {
		//if (!(Q % 1000)) fprintf(stderr, "Q=%d\n", Q);
		int typ, d, v;
		scanf("%d%d%d", &typ, &d, &v);
		if (typ == 1) {
			plan->add(1, d, v);
		}
		if (typ == 2) {
/*
			ull suma = 0;
			for (int i=1; i<=d; ++i) {
				if (A[i] >= v) {
					suma += plan->get(i);
				}
			}
*/
			printf("%llu\n", trips.calculate_sum(1, d, v));
		}
	}
}