結果
| 問題 |
No.2643 Many Range Sums Problems
|
| コンテスト | |
| ユーザー |
shobonvip
|
| 提出日時 | 2024-02-11 18:35:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,137 bytes |
| コンパイル時間 | 2,930 ms |
| コンパイル使用メモリ | 224,416 KB |
| 最終ジャッジ日時 | 2025-02-19 05:17:54 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 WA * 5 RE * 3 |
ソースコード
#include<bits/stdc++.h>
#include<atcoder/segtree>
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, 0);
auto add = [&](int i, int x, int c){
int tar = jisu[i][x];
assert(-1 <= c && c <= 1);
assert(-1 <= tar && tar <= 1);
assert(-1 <= tar + c && tar + c <= 1);
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]
);
}
//cout << i << ' ' << x << ' ' << tar - c << ' ' << tar << '\n';
};
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, - ub);
M_ub = min(M_ub, - lb + 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, lb - k);
M_ub = min(M_ub, ub);
}
if (M_lb <= M_ub){
// There is valid M
ans[i] = 1;
}
};
dfs2(dfs2, n, -1);
for (int i=0; i<n; i++){
if (ans[i]){
cout << "Yes\n";
}else{
cout << "No\n";
}
}
}
shobonvip