QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#326296 | #8235. Top Cluster | ucup-team045 | RE | 0ms | 12068kb | C++20 | 3.7kb | 2024-02-12 19:55:40 | 2024-02-12 19:55:42 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
using LL = long long;
const int maxn = 5e5 + 5, INF = 1e9;
vector<pair<int, int> > g[maxn];
bool v[maxn];
int ans[maxn], a[maxn];
vector<pair<LL, int> > que[maxn];
int get_size(int u, int fa){
if (v[u]) return 0;
int sz = 1;
for(auto [j, w] : g[u]){
if (j == fa) continue;
sz += get_size(j, u);
}
return sz;
}
int get_wc(int u, int fa, int tot, int &wc){
if (v[u]) return 0;
int mx = 0, sum = 1;
for(auto [j, w] : g[u]){
if (j == fa) continue;
int t = get_wc(j, u, tot, wc);
mx = max(mx, t);
sum += t;
}
mx = max(mx, tot - sum);
if (mx <= tot / 2) wc = u;
return sum;
}
void dfs(int u, int fa, int dist, vector<pair<LL, int> > &p){
if (v[u]) return;
p.push_back({dist, u});
for(auto [j, w] : g[u]){
if (j == fa) continue;
dfs(j, u, dist + w, p);
}
}
int tr[maxn * 4];
void build(int u, int l, int r, vector<pair<LL, int> > &init){
if (l == r){
tr[u] = a[init[r].second];
return;
}
int mid = (l + r) / 2;
build(2 * u, l, mid, init);
build(2 * u + 1, mid + 1, r, init);
tr[u] = min(tr[2 * u], tr[2 * u + 1]);
}
void modify(int u, int l, int r, int x, int v){
if (l == r){
tr[u] = v;
return;
}
int mid = (l + r) / 2;
if (x <= mid) modify(2 * u, l, mid, x, v);
else modify(2 * u + 1, mid + 1, r, x, v);
tr[u] = min(tr[2 * u], tr[2 * u + 1]);
}
int query(int u, int l, int r, int L, int R){
if (l > R || r < L) return INF;
if (l >= L && r <= R) return tr[u];
int mid = (l + r) / 2;
return min(query(2 * u, l, mid, L, R), query(2 * u + 1, mid + 1, r, L, R));
}
int id[maxn];
void calc(int u){
if (v[u]) return;
get_wc(u, -1, get_size(u, -1), u);
vector<pair<LL, int> > p;
dfs(u, -1, 0, p);
sort(p.begin(), p.end());
for(int i = 0; i < p.size(); i++){
id[p[i].second] = i;
}
const int m = p.size();
auto get = [&](LL x){
return int(lower_bound(p.begin(), p.end(), make_pair(x + 1, 0)) - p.begin());
};
build(1, 0, m - 1, p);
for(auto [d, id] : que[u]){
ans[id] = min(ans[id], query(1, 0, m - 1, get(d), m - 1));
}
for(auto [j, w] : g[u]){
if (v[j]) continue;
vector<pair<LL, int> > q;
dfs(j, u, w, q);
for(auto [dist, x] : q){
modify(1, 0, m - 1, id[x], INF);
}
for(auto [dist, x] : q){
for(auto [d, id] : que[x]){
ans[id] = min(ans[id], query(1, 0, m - 1, get(d - dist), m - 1));
}
}
for(auto [dist, x] : q){
modify(1, 0, m - 1, id[x], a[x]);
}
}
v[u] = true;
for(auto [j, w] : g[u]){
calc(j);
}
}
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
v[a[i]] = true;
}
for(int i = 0; i < n - 1; i++){
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
int mex = 0;
while(v[mex]) mex++;
memset(v, 0, sizeof v);
for(int i = 0; i < q; i++){
int x; LL k;
cin >> x >> k;
ans[i] = mex;
que[x].push_back({k, i});
}
calc(1);
for(int i = 0; i < q; i++){
cout << ans[i] << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12068kb
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
Runtime Error
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...