結果

問題 No.2643 Many Range Sums Problems
ユーザー shobonvipshobonvip
提出日時 2024-02-12 20:03:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 8,000 ms
コード長 2,921 bytes
コンパイル時間 2,784 ms
コンパイル使用メモリ 235,876 KB
実行使用メモリ 7,264 KB
最終ジャッジ日時 2024-02-13 15:30:53
合計ジャッジ時間 4,298 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 25 ms
6,892 KB
testcase_04 AC 22 ms
6,676 KB
testcase_05 AC 21 ms
6,676 KB
testcase_06 AC 26 ms
7,264 KB
testcase_07 AC 11 ms
6,676 KB
testcase_08 AC 25 ms
6,992 KB
testcase_09 AC 25 ms
7,100 KB
testcase_10 AC 10 ms
6,676 KB
testcase_11 AC 7 ms
6,676 KB
testcase_12 AC 15 ms
6,676 KB
testcase_13 AC 29 ms
7,240 KB
testcase_14 AC 25 ms
6,912 KB
testcase_15 AC 26 ms
7,168 KB
testcase_16 AC 21 ms
6,676 KB
testcase_17 AC 22 ms
6,676 KB
testcase_18 AC 18 ms
6,676 KB
testcase_19 AC 6 ms
6,676 KB
testcase_20 AC 8 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 7 ms
6,676 KB
testcase_23 AC 9 ms
6,676 KB
testcase_24 AC 13 ms
6,676 KB
testcase_25 AC 11 ms
6,676 KB
testcase_26 AC 4 ms
6,676 KB
testcase_27 AC 5 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 10 ms
6,676 KB
testcase_31 AC 24 ms
6,844 KB
testcase_32 AC 14 ms
6,784 KB
testcase_33 AC 14 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const long long INF = 1e18;

int main(){
	int n; cin >> n;
	long long k; cin >> k;
	vector<int> r(n);
	vector<long long> x(n);
	for (int i=0; i<n; i++){
		cin >> r[i] >> x[i];
	}

	vector ikeru(n+1, vector<int>(0));
	for (int i=0; i<n; i++){
		ikeru[r[i]].push_back(i);
		ikeru[i].push_back(r[i]);
	}

	vector<long long> weight(n+1);
	auto dfs = [&](auto self, int i, int p) -> void {
		if (i < n) weight[i] += x[i];
		for (int j: ikeru[i]){
			if (j == p) continue;
			weight[j] += weight[i];
			self(self, j, i);
		}
	};

	dfs(dfs, n, -1);
	vector<long long> shoki(n+1);
	int shoki_blacklist_num = 0;
	set<int> blacklist;
	for (int i=0; i<n; i++){
		shoki[i] = weight[i] - weight[i+1];
		if (!(0 <= shoki[i] && shoki[i] <= k)){
			shoki_blacklist_num += 1;
			blacklist.insert(i);
		}
	}

	vector<map<int,int>> jisu(n+1);
	vector iroironahito(n+1, vector<multiset<long long>>(3));
	vector<bool> ans(n);
	vector<int> blacklist_kaihi_nums(n+1, 0);
	
	auto add = [&](int i, int x, int c){
		int tar = jisu[i][x];
		if (tar != 0){
			if (blacklist.find(x) != blacklist.end()){
				blacklist_kaihi_nums[i] -= 1;
			}
			iroironahito[i][tar + 1].erase(
				iroironahito[i][tar + 1].find(shoki[x])
			);
		}
		jisu[i][x] += c;
		tar += c;
		if (tar != 0){
			if (blacklist.find(x) != blacklist.end()){
				blacklist_kaihi_nums[i] += 1;
			}
			iroironahito[i][tar + 1].insert(
				shoki[x]
			);
		}
	};
	
	auto dfs2 = [&](auto self, int i, int p) -> void {
		for (int j: ikeru[i]){
			if (j == p) continue;
			self(self, j, i);
			if ((int)jisu[j].size() > jisu[i].size()){
				swap(jisu[j], jisu[i]);
				swap(blacklist_kaihi_nums[j], blacklist_kaihi_nums[i]);
				swap(iroironahito[j][0], iroironahito[i][0]);
				swap(iroironahito[j][2], iroironahito[i][2]);
			}
			for (auto [x, c]: jisu[j]){
				add(i, x, c);
			}
			jisu[j].clear();
		}

		add(i, i, 1);
		if (i > 0) add(i, i-1, -1);

		if (i == n) return;

		if (blacklist_kaihi_nums[i] < shoki_blacklist_num){
			return;
		}

		long long M_lb = 0;
		long long M_ub = INF;
		if (!iroironahito[i][2].empty()){
			auto itr = iroironahito[i][2].begin();
			long long lb = - x[i] + *itr;
			itr = iroironahito[i][2].end();
			itr--;
			long long ub = - x[i] + *itr;
			// 0 <= shoki[k] - x[i] + M <= K narubesi
			// - shoki[k] + x[i] <= M <= K - shoki[k] + x[i]
			M_lb = max(M_lb, - lb);
			M_ub = min(M_ub, - ub + k);
		}
		if (!iroironahito[i][0].empty()){
			auto itr = iroironahito[i][0].begin();
			long long lb = x[i] + *itr;
			itr = iroironahito[i][0].end();
			itr--;
			long long ub = x[i] + *itr;
			// 0 <= shoki[k] + x[i] - M <= K narubesi
			// shoki[k] + x[i] - K <= M <= shoki[k] + x[i]
			M_lb = max(M_lb, ub - k);
			M_ub = min(M_ub, lb);
		}
		if (M_lb <= M_ub){
			ans[i] = 1;
		}
	};

	dfs2(dfs2, n, -1);
	for (int i=0; i<n; i++){
		if (ans[i]){
			cout << "Yes\n";
		}else{
			cout << "No\n";
		}
	}
}
0