CodeForces 295A Greg and Array(线段树)

#include <iostream>
#define LL long long
using namespace std;

const int N = 100001;
//两颗线段树维护一下就OK了

struct seg{
	LL a[N], add[N << 2], sum[N << 2];
	int l[N << 2], r[N << 2];

	void pushDown(int i){
		add[i * 2] += add[i];
		sum[i * 2] += (add[i] * (r[i * 2] - l[i * 2] + 1));
		add[i * 2 + 1] += add[i];
		sum[i * 2 + 1] += (add[i] * (r[i * 2 + 1] - l[i * 2 + 1] + 1));
		add[i] = 0;
	}

	void build(int ll, int rr, int i){
		add[i] = 0;
		l[i] = ll, r[i] = rr;
		if (ll == rr){
			sum[i] = a[ll];    //!!!
			return;
		}
		build(ll, (ll + rr) / 2, i * 2);
		build((ll + rr) / 2 + 1, rr, i * 2 + 1);
		sum[i] = sum[i * 2] + sum[i * 2 + 1];
	}

	void update(int id, int v, int i){
		if (l[i] == id&&l[i] == r[i]){
			sum[i] += v;
			return;
		}
		
		int mid = (l[i] + r[i]) / 2;
		if (id <= mid)update(id, v, i * 2);
		else update(id, v, i * 2 + 1);
		sum[i] = sum[i * 2] + sum[i * 2 + 1];
	}

	void update(int ll, int rr, LL v, int i){
		if (ll <= l[i] && rr >= r[i]){
			sum[i] += ((r[i] - l[i] + 1)*v);
			add[i] += v;
			return;
		}

		if (add[i] != 0)pushDown(i);

		int mid = (l[i] + r[i]) / 2;
		if (ll <= mid)update(ll, rr, v, i * 2);
		if (rr > mid)update(ll, rr, v, i * 2 + 1);
		
		sum[i] = sum[i * 2] + sum[i * 2 + 1];
	}

	LL find(int ll, int rr, int i){
		if (ll == l[i] && rr == r[i]){
			return sum[i];
		}

		if (add[i] != 0)pushDown(i);

		int mid = (l[i] + r[i]) / 2;

		if (rr <= mid)return find(ll, rr, i * 2);
		else if (ll > mid)return find(ll, rr, i * 2 + 1);
		else return find(ll, mid, i * 2) + find(mid + 1, rr, i * 2 + 1);
	}

};

seg x, y;

int l[N], r[N], d[N];

int main(){
	int n, m, k;
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++)cin >> x.a[i];
	x.build(1, n, 1);

	for (int i = 1; i <= m; i++){
		y.a[i] = 0;
		cin >> l[i] >> r[i] >> d[i];
	}
	y.build(1, m, 1);

	for (int i = 1; i <= k; i++){
		int ll, rr;
		cin >> ll >> rr;
		y.update(ll, rr, 1, 1);
	}

	for (int i = 1; i <= m; i++){
		x.update(l[i], r[i], d[i] * y.find(i, i, 1), 1);
	}

	cout << x.find(1, 1, 1);
	if (n == 1)cout << endl;
	else{
		for (int i = 2; i <= n; i++)cout << ' ' << x.find(i, i, 1);
		cout << endl;
	}

}

你可能感兴趣的:(CodeForces 295A Greg and Array(线段树))