QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303862 | #7103. Red Black Tree | kevinshan# | WA | 1480ms | 59232kb | C++17 | 3.9kb | 2024-01-13 02:34:07 | 2024-01-13 02:34:08 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define ff first
#define ss second
using namespace std;
const int maxn = 1e5 + 10, init = 0;
vector<ii> adj[maxn];
int in[maxn], dp[maxn], out[maxn], dep[maxn], depth[maxn], mark[maxn];
int p[maxn][20], w[maxn][20], seg[2*maxn];
int n, m, q, k, cnt;
void DFS(int u = 1, int pa = 0, int cur = 0)
{
if (mark[u]) cur = 0;
in[u] = ++cnt;
dp[u] = cur;
for (auto [v, c]: adj[u]) if (v != pa)
{
p[v][0] = u;
w[v][0] = c;
dep[v] = dep[u] + 1;
depth[v] = depth[u] + c;
DFS(v, u, cur + c);
}
out[u] = cnt;
}
inline int lca(int u, int v)
{
if (dep[u] > dep[v]) swap(u, v);
int dif = dep[v] - dep[u];
for (int i=k; i>=0; i--) if (dif >> i & 1) v = p[v][i];
if (v == u) return u;
for (int i=k; i>=0; i--) if (p[v][i] != p[u][i]) v = p[v][i], u = p[u][i];
return p[u][0];
}
inline void upd(int pos, int val)
{
seg[pos+=n] = val;
while (pos >>= 1) seg[pos] = max(seg[pos<<1], seg[pos<<1|1]);
}
inline int qry(int l, int r)
{
int ans = 0;
for (l+=n, r+=n+1; l<r; l>>=1, r>>=1)
{
if (l & 1) ans = max(ans, seg[l++]);
if (r & 1) ans = max(ans, seg[--r]);
}
return ans;
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
int tst; cin>>tst; while (tst--)
{
cin>>n>>m>>q;
for (int i=1; i<=n; i++) mark[i] = 0, adj[i].clear();
for (int i=1; i<=(n<<1|1); i++) seg[i] = init;
for (int i=1, x; i<=m; i++) cin>>x, mark[x] = 1;
for (int i=1, u, v, c; i<n; i++)
{
cin>>u>>v>>c;
adj[u].emplace_back(v, c);
adj[v].emplace_back(u, c);
}
cnt = 0;
dep[1] = depth[1] = 0;
DFS();
k = log2(n);
for (int i=1; i<=k; i++)
for (int j=1; j<=n; j++)
p[j][i] = p[p[j][i-1]][i-1],
w[j][i] = w[j][i-1] + w[p[j][i-1]][i-1];
while (q--)
{
vector<int> v, v2;
int x;
cin>>x;
v.resize(x);
for (int &i: v) cin>>i;
sort(v.begin(), v.end(), [](int x, int y) {return dp[x] > dp[y];});
int cur = v[0], ans = dp[v[0]];
for (int i=1; i<x; i++)
if (dp[v[i]] == ans) cur = lca(cur, v[i]);
else break;
sort(v.begin(), v.end(), [](int x, int y) {return in[x] < in[y];});
for (int i: v) v2.emplace_back(in[i]);
vector<int> pref(x), suf(x);
pref[0] = dp[v[0]];
suf[x-1] = dp[v[x-1]];
for (int i=1; i<x; i++) pref[i] = max(pref[i-1], dp[v[i]]);
for (int i=x-2; i>=0; i--) suf[i] = max(suf[i+1], dp[v[i]]);
for (int i: v) upd(in[i], depth[i]);
// cout<<"ANS: "<<ans<<"\n";
for (int i: v)
{
int tmp = lca(cur, i);
int le = in[tmp], ri = out[tmp];
int posl = lower_bound(v2.begin(), v2.end(), le) - v2.begin();
int posr = upper_bound(v2.begin(), v2.end(), ri) - v2.begin() - 1;
int prel = (posl > 0 ? pref[posl-1] : 0);
int sufr = (posr < x - 1 ? suf[posr+1] : 0);
int mid = qry(le, ri) - depth[tmp];
ans = min(ans, max({prel, sufr, mid}));
// cout<<tmp<<" "<<ans<<"\n";
}
for (int i: v) upd(in[i], 0);
cout<<ans<<"\n";
}
}
}
/**
* 2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15396kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: -100
Wrong Answer
time: 1480ms
memory: 59232kb
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...
result:
wrong answer 37th lines differ - expected: '45883979', found: '48209646'