QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#348781 | #8235. Top Cluster | Freeuni1 | TL | 11ms | 81640kb | C++14 | 3.7kb | 2024-03-09 21:08:40 | 2024-03-09 21:08:41 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define ll int
#define fr first
#define sc second
#define pb push_back
#define MAX ((ll)(1e9 + 100))
#define MX ((ll)(5e5 + 100))
#define ARRS ((ll)(1e6 + 100))
#define MOD ((ll)(1e9 + 7))
#define PI ((double)3.141592653)
#define EP ((double)(1e-9))
#define LG 21
ll w[ARRS];
ll sz[ARRS];
ll f[ARRS];
vector<pair<ll, ll>> v[ARRS];
pair<ll, ll> cpr[ARRS];
ll fc(ll x, ll pr, ll n) {
sz[x] = 1;
ll ok = 1;
ll ans = -1;
for (auto y : v[x]) {
if (f[y.fr] || y.fr == pr)
continue;
ans = max(ans, fc(y.fr, x, n));
ok &= sz[y.fr] <= (n / 2);
sz[x] += sz[y.fr];
}
ok &= (n - sz[x]) <= (n / 2);
if (ok)
return x;
else
return ans;
}
ll CS;
multiset<pair<ll, pair<ll, ll>>> ms;
void go(ll x, ll pr, ll d) {
ms.insert({d, {w[x], CS}});
sz[x] = 1;
for (auto y : v[x]) {
if (f[y.fr] || y.fr == pr)
continue;
go(y.fr, x, d + y.sc);
sz[x] += sz[y.fr];
}
}
vector<pair<ll, ll>> upd(vector<pair<ll, ll>> mns, pair<ll, ll> nw) {
mns.pb(nw);
sort(mns.begin(), mns.end());
if (mns[0].sc == mns[1].sc) {
return {mns[0], mns[2]};
} else {
return {mns[0], mns[1]};
}
}
map<ll, vector<pair<ll, ll>>> rmin[ARRS];
ll DTF;
ll dist(ll x, ll pr, ll tgt) {
if (x == tgt)
return 0;
for (auto y : v[x]) {
if (f[y.fr] <= DTF || y.fr == pr)
continue;
ll d = dist(y.fr, x, tgt);
if (d != -1) {
return d + y.sc;
}
}
return -1;
}
ll buildTree(ll somerandomx, ll n, ll DDD) {
auto C = fc(somerandomx, -1, n);
vector<pair<ll, pair<ll, ll>>> subs;
CS = 0;
for (auto y : v[C]) {
if (f[y.fr])
continue;
CS++;
go(y.fr, C, y.sc);
subs.pb({y.fr, {sz[y.fr], y.sc}});
}
ms.insert({{-1, {w[C], -5}}});
// cout << "--------- " << C << " ------------" << endl;
vector<pair<ll, ll>> v = {{MAX, -1}, {MAX + 1, -2}};
for (auto it = ms.rbegin(); it != ms.rend(); it++) {
auto cur = *it;
v = upd(v, cur.sc);
rmin[C][cur.fr] = v;
}
// for (auto x : rmin[C]) {
// cout << x.fr << ":" << endl;
// for (auto mn : x.sc) {
// cout << mn.fr << ", " << mn.sc << endl;
// }
// }
// cout << "---------" << endl;
f[C] = DDD;
for (auto sub : subs) {
ll Cc = buildTree(sub.fr, sub.sc.fr, DDD + 1);
DTF = DDD;
ll dst = dist(sub.fr, C, Cc) + sub.sc.sc;
cpr[Cc] = {C, dst};
// cout << C << " -> " << Cc << " ; " << dst << endl;
}
return C;
}
int main() {
ios::sync_with_stdio(0);
ll n, q;
cin >> n >> q;
vector<ll> tv;
for (int i = 0; i < n; i++) {
cin >> w[i];
tv.pb(w[i]);
}
sort(tv.begin(), tv.end());
ll mex = n;
for (int i = 0; i < n; i++) {
if (i != tv[i]) {
mex = i;
break;
}
}
// cout << mex << endl;
for (int i = 0; i < n - 1; i++) {
ll k, p, l;
cin >> k >> p >> l;
k--;
p--;
v[k].pb({p, l});
v[p].pb({k, l});
}
// cout << n << endl;
ll C = buildTree(0, n, 1);
cpr[C] = {-1, -1};
// cout<<"**********"<<endl;
q=min(q, 10);
while (q--) {
ll x, r;
cin >> x >> r;
x--;
ll ans = mex;
ll lasts = -100;
while (x != -1) {
auto cs = (*rmin[x].lower_bound(r)).sc;
auto cans = cs[0].fr;
// cout << x << " " << r << endl;
// cout << cans << endl;
if (cs[0].sc == lasts)
cans = cs[1].fr;
// cout << cans << endl;
// cout << endl;
ans = min(ans, cans);
r -= cpr[x].fr;
x = cpr[x].fr;
}
cout << ans << endl;
// cout << "--" << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 11ms
memory: 81640kb
input:
5 4 3 9 0 1 2 1 2 10 3 1 4 3 4 3 3 5 2 3 0 1 0 4 6 4 7
output:
1 0 3 4
result:
ok 4 number(s): "1 0 3 4"
Test #2:
score: -100
Time Limit Exceeded
input:
500000 500000 350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...