QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#376229 | #4209. Stranded Far From Home | Cyanmond | 0 | 693ms | 177676kb | C++20 | 5.2kb | 2024-04-04 00:10:37 | 2024-04-04 00:10:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll inf = 1ll << 50;
#define L(i, l, r) for (int i = (l); i < (r); ++i)
#define R(i, l, r) for (int i = (r - 1); i >= (l); --i)
#define ALL(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int n, m;
vector<ll> s;
vector<int> a, b;
vector<ll> val_s;
int p;
vector<vector<int>> edge_map, ver_map;
vector<vector<int>> g;
vector<vector<int>> origin;
vector<vector<ll>> nxt_weight, all_sum;
struct UnionFind {
int n;
vector<int> data;
vector<set<pair<ll, int>>> rs;
vector<ll> sum;
UnionFind(int n_) : n(n_) {
data.assign(n, -1);
rs.resize(n);
L(i, 0, m) {
rs[a[i]].insert({s[b[i]], b[i]});
rs[b[i]].insert({s[a[i]], a[i]});
}
sum = s;
}
int leader(int v) {
if (data[v] < 0) {
return v;
} else {
return data[v] = leader(data[v]);
}
}
void merge(int a, int b) {
a = leader(a);
b = leader(b);
if (a == b) {
return;
}
if (data[a] > data[b]) {
swap(a, b);
}
data[a] += data[b];
data[b] = a;
for (const auto &e : rs[b]) {
rs[a].insert(e);
}
sum[a] += sum[b];
}
};
constexpr int D = 30;
void chmin(ll &a, const ll b) {
a = min(a, b);
}
void read() {
cin >> n >> m;
s.resize(n);
for (auto &e : s) {
cin >> e;
}
a.resize(m);
b.resize(m);
g.resize(n);
L(i, 0, m) {
cin >> a[i] >> b[i];
--a[i], --b[i];
}
val_s = s;
sort(ALL(val_s));
val_s.erase(unique(ALL(val_s)), val_s.end());
p = sz(val_s);
edge_map.resize(p);
L(i, 0, m) {
const int pos = lower_bound(ALL(val_s), max(s[a[i]], s[b[i]])) - val_s.begin();
edge_map[pos].push_back(i);
}
ver_map.resize(p);
L(i, 0, n) {
const int pos = lower_bound(ALL(val_s), s[i]) - val_s.begin();
ver_map[pos].push_back(i);
}
}
void process() {
vector<bool> ans(n);
int v_id = 0;
nxt_weight.assign(D, vector<ll>(n, inf));
all_sum.assign(D, vector<ll>(n, inf));
origin.assign(D, vector<int>(n, -1));
L(d, 0, D) {
while (v_id != p and val_s[v_id] <= (1 << d)) {
for (const int i : edge_map[v_id]) {
g[a[i]].push_back(b[i]);
g[b[i]].push_back(a[i]);
}
++v_id;
}
auto &ori = origin[d];
L(i, 0, n) {
if (ori[i] != -1) {
continue;
}
queue<int> que;
que.push(i);
ori[i] = i;
while (not que.empty()) {
const int f = que.front();
que.pop();
for (const int t : g[f]) {
if (ori[t] == -1) {
ori[t] = i;
que.push(t);
}
}
}
}
vector<vector<int>> vs(n);
L(i, 0, n) {
vs[ori[i]].push_back(i);
}
L(i, 0, n) {
sort(ALL(vs[i]));
for (const int f : vs[i]) {
all_sum[d][i] += s[f];
for (const int t : g[f]) {
if (binary_search(ALL(vs[i]), t)) {
continue;
}
chmin(nxt_weight[d][i], s[t]);
}
}
}
}
UnionFind uft(n);
L(now_val, 0, p) {
for (const int i : edge_map[now_val]) {
uft.merge(a[i], b[i]);
}
for (const int i : ver_map[now_val]) {
// answer query
int leader = uft.leader(i);
auto &adj_set = uft.rs[leader];
while ((not adj_set.empty()) and adj_set.begin()->first <= val_s[now_val]) {
adj_set.erase(adj_set.begin());
}
ll now_sum = uft.sum[leader];
if (adj_set.empty()) {
ans[i] = true;
continue;
}
if (adj_set.begin()->first > now_sum) {
ans[i] = false;
continue;
}
int d = 0;
while ((1 << d) <= now_sum) {
++d;
}
if (d >= D) {
ans[i] = true;
continue;
}
while (d < D) {
now_sum = all_sum[d][origin[d][i]];
ll nxt_g = nxt_weight[d][origin[d][i]];
if (nxt_g == inf) {
ans[i] = true;
break;
}
if (nxt_g > now_sum) {
ans[i] = false;
break;
}
++d;
}
if (d == D) {
ans[i] = true;
}
}
}
for (const auto e : ans) {
cout << (e ? 1 : 0);
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
read();
process();
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
1622 1998 29021716 740464354 721037601 759718475 973636178 59068917 294028640 285332651 203821680 397646208 488684783 448548424 565347277 112938323 832704692 837437153 508335516 167646888 798998153 86605661 820823253 705498382 116513873 672806499 522392461 568294614 457655230 647498964 577669416 506...
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #15:
score: 0
Time Limit Exceeded
input:
200000 199999 100000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #30:
score: 0
Time Limit Exceeded
input:
199999 199998 758475118 758475116 758475114 758475112 758475110 758475108 758475106 758475104 758475102 758475100 758475098 758475096 758475094 758475092 758475090 758475088 758475086 758475084 758475082 758475080 758475078 758475076 758475074 758475072 758475070 758475068 758475066 758475064 758475...
output:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #39:
score: 30
Accepted
time: 693ms
memory: 177676kb
input:
155555 200000 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735421 473735...
output:
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
result:
ok single line: '111111111111111111111111111111...1111111111111111111111111111100'
Test #40:
score: -30
Time Limit Exceeded
input:
200000 200000 913658453 969766488 685576437 527887525 685576437 495438734 959695974 527887525 913658453 618257806 959695974 959695974 969766488 618257806 90942432 969766488 969766488 527887525 913658453 685576437 913658453 959695974 685576437 495438734 90942432 809500728 809500728 959695974 96976648...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
0%