比赛 cdcqの省选膜你赛 评测结果 WWWWWWWWWWWWWWWWWWWW
题目名称 新史「新幻想史 -现代史-」 最终得分 0
用户昵称 1 运行时间 0.012 s
代码语言 C++ 内存使用 2.99 MiB
提交时间 2017-04-11 20:47:39
显示代码纯文本
#include<fstream>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
long long nodes, queries;
long long sum[100010], time_zero[100010];
class Persistent_BIT {
	struct Node {
		long long data, time_data;
		long long version;
	};
	vector<Node> bit[100010];
	long long lowbit(long long x) { return x & (-x); }
public:
	void init() {
		for (long long i = 1; i <= nodes; i++)
			bit[i].clear(),
			bit[i].push_back({ 0LL, 0LL, 0 });
	}
	long long Size;
	void update(long long position, long long delta, long long now_time) {
		long long size;
		long long time_delta = delta * position;
		while (position <= Size) {
			size = bit[position].size();
			bit[position].push_back(bit[position][size - 1]);
			bit[position][size].data += delta;
			bit[position][size].time_data += time_delta;
			bit[position][size].version = now_time;
			size++;
			position += lowbit(position);
		}
	}
	long long getsum(long long position, long long now_time) {
		if (now_time == 0)return 0;
		long long data_sum = 0, time_data_sum = 0;
		long long size, ox = position;
		while (position > 0) {
			size = bit[position].size();
			while (bit[position][size - 1].version > now_time)
				size--;
			data_sum += bit[position][size - 1].data;
			time_data_sum += bit[position][size - 1].time_data;
			position -= lowbit(position);
		}
		return (ox + 1) * data_sum - time_data_sum;
	}
	inline void back(long long timelabel) {
		long long size;
		for (long long i = 1; i <= nodes; ++i) {
			size = bit[i].size();
			while (bit[i][size - 1].version > timelabel) {
				bit[i].pop_back();
				size--;
			}
		}
	}
}BIT;
inline long long getans(long long x, long long y, long long now_time) {
	return sum[y] - sum[x - 1] + BIT.getsum(y, now_time) - BIT.getsum(x - 1, now_time);
}
void change(long long x, long long y, long long delta, long long time) {
	BIT.update(x, delta, time);
	BIT.update(y + 1, -delta, time);
}
int main() {
	ifstream fin("cdcq_a.in");
	ofstream fout("cdcq_a.out");
	char cmd[2]; long long a, b, c, d, e, f, g;
	cin >> nodes >> queries; BIT.Size = nodes;
	BIT.init();
	for (long long i = 1; i <= nodes; i++) {
		cin >> time_zero[i];
		sum[i] = time_zero[i] + sum[i - 1];
	}
	for (long long i = 1; i <= queries; i++) {
		cin >> cmd;
		switch (cmd[0]) {
		case 'Q':
			cin >> a >> b >> c;
			cout << getans(a, b, c) << endl;
			break;
		case 'M':
			cin >> a >> b >> c >> d >> e >> f >> g;
			change(a, b, -c, g);
			change(d, e, f, g);
			break;
		}
	}
}