結果

問題 No.1000 Point Add and Array Add
ユーザー polylogKpolylogK
提出日時 2020-02-28 21:38:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 3,974 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 183,224 KB
実行使用メモリ 28,928 KB
最終ジャッジ日時 2024-04-21 18:08:32
合計ジャッジ時間 7,017 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 261 ms
21,984 KB
testcase_17 AC 239 ms
18,264 KB
testcase_18 AC 381 ms
27,792 KB
testcase_19 AC 385 ms
27,792 KB
testcase_20 AC 387 ms
28,928 KB
testcase_21 AC 333 ms
27,492 KB
testcase_22 AC 395 ms
28,928 KB
testcase_23 AC 347 ms
27,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename Monoid>
class segment_tree {
	using value_type = Monoid;
	using size_type = size_t;

	using binary_function = std::function<value_type(value_type, value_type)>;
	using checker = std::function<bool(value_type)>;
	
	const size_type size_;
	size_type height_;

	const value_type id;
	const binary_function bi_func;
	std::vector<value_type> data;
	
	private:
	const size_type get_height(const size_type& size) const {
		size_type height = 1;
		while(1 << height <= size) height++;
		return height;
	}
	const size_type base_size() const {
		return 1 << height_;
	}
	void meld(const size_type& index) {
		data[index] = bi_func(data[index << 1 ^ 0], data[index << 1 ^ 1]);
	}
	
	public:
	segment_tree(const size_type& size, const binary_function& bi_func, const value_type& id)
		: size_(size),
		  bi_func(bi_func),
		  id(id) {
		height_ = get_height(size);
		data.assign(base_size() << 1, id);
	}
	value_type fold(size_type left, size_type right) {
		value_type l_value = id,
				   r_value = id;

		for(left += base_size(), right += base_size();
			left < right;
			left >>= 1, right >>= 1) {
			if(left & 1)  l_value = bi_func(l_value, data[left++]);
			if(right & 1) r_value = bi_func(data[--right], r_value); 
		}
		return bi_func(std::move(l_value), std::move(r_value));
	}
	void update(size_type index, const value_type& value) {
		index += base_size();
		data[index] = bi_func(data[index], value);

		while(index >>= 1) meld(index);
	}
	void change(size_type index, const value_type& value) {
		index += base_size();
		data[index] = value;

		while(index >>= 1) meld(index);
	}
	const size_type search(const size_type & left, const checker & check) {
		value_type val = id;
		size_type k = left + base_size();
		while(true) {
			if(check(bi_func(val, data[k]))) {
				val = bi_func(val, data[k]);
				if(k & 1) {
					if((k + 1) & k) k = (k + 1) >> 1;
					else return size();
				} else {
					k = k + 1;
				}
			} else {
				if(k < base_size()) {
					k = k << 1 ^ 0;
				} else {
					return k - base_size();
				}
			}
		}
	}
	
	value_type operator[](const size_type& index) const {
		return data[index + base_size()];
	}
	const size_type size() const {
		return size_;
	}
	const bool empty() const {
		return data.empty();
	}
};

int main() {
	int n, q; scanf("%d%d", &n, &q);
	std::vector<std::vector<std::pair<int, i64>>> a(n);
	for(int i = 0; i < n; i++) {
		int x; scanf("%d", &x);

		a[i].push_back({0, x});
	}

	std::vector<char> type(q);
	std::vector<int> x(q), y(q);
	std::vector<std::pair<int, int>> vec;
	for(int i = 0; i < q; i++) {
		cin >> type[i] >> x[i] >> y[i];
		x[i]--;

		if(type[i] == 'A') {
			a[x[i]].push_back({i + 1, a[x[i]].back().second + y[i]});
		} else {
			vec.push_back({x[i], i});
		}
	}
	sort(begin(vec), end(vec));

	for(int i = 0; i < n; i++) a[i].push_back({q + 1, -1});

	int cnt = 0;
	using P = std::pair<int, int>;
	std::priority_queue<P, std::vector<P>, std::greater<P>> qu;

	std::vector<i64> b(n);
	auto f = [&](int a, int b) { return a + b; };
	segment_tree<int> seg(q + 2, f, 0);
	for(int i = 0; i < n; i++) {
		while(cnt < vec.size() and vec[cnt].first == i) {
			int id = vec[cnt++].second;

			qu.push({y[id], id});
			seg.change(id + 1, 1);
		}
		while(!qu.empty() and qu.top().first == i) {
			int id = qu.top().second; qu.pop();

			seg.change(id + 1, 0);
		}
		
		for(int j = 0; j < (int)a[i].size() - 1; j++) {
			i64 val = a[i][j].second;
			int L = a[i][j].first;
			int R = a[i][j + 1].first;

			b[i] += val * seg.fold(L, R);
		}
	}

	for(auto v: b) printf("%ld ", v);
	printf("\n");
	return 0;
}
0