QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#191078 | #7103. Red Black Tree | Geospiza | TL | 3ms | 8368kb | C++20 | 3.7kb | 2023-09-29 17:29:16 | 2023-09-29 17:29:18 |
Judging History
answer
#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll N=2e5+5;
vector<int>dfn,pos,mp;
vector<int>vec[N];
void DFS(int u,int pre)
{
dfn.push_back(u);
pos[u]=dfn.size()-1;
mp[dfn.size()-1]=u;
for(auto v:vec[u])if(v!=pre)
{
DFS(v,u);
dfn.push_back(u);
}
}
template <typename T>
class SparseTable
{
using VT = vector<T>;
using VVT = vector<VT>;
using func_type = function<T(const T &, const T &)>;
VT V;
VVT ST;
static T default_func(const T &t1, const T &t2) { return min(t1, t2); }
func_type op;
public:
SparseTable(const vector<T> &v, func_type _func = default_func) {
V = v;
op = _func;
int len = v.size(), l1 = ceil(log2(len)) + 1;
ST.assign(len, VT(l1, 0));
for (int i = 0; i < len; ++i) {
ST[i][0] = v[i];
}
for (int j = 1; j < l1; ++j) {
int pj = (1 << (j - 1));
for (int i = 0; i + pj < len; ++i) {
ST[i][j] = op(ST[i][j - 1], ST[i + (1 << (j - 1))][j - 1]);
}
}
}
T query(int l, int r) {
if (l == r)return V[l];
int lt = r - l + 1;
int q = ceil(log2(lt)) - 1;
return op(ST[l][q], ST[r - (1 << q) + 1][q]);
}
};
int LCA(int x,int y,SparseTable<int>& st)
{
--x,--y;
x=pos[x],y=pos[y];
if(x==y)return mp[x]+1;
return mp[st.query(min(x,y),max(x,y))]+1;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int T=1;
cin>>T;
while(T--){
ll n,m,q;
cin>>n>>m>>q;
dfn.clear();
pos.clear();
mp.clear();
vector<ll>color(n+5),dis(n+5,1e16),dep(n+5),des(n+5);
for(int i=1;i<=m;i++){
int r; cin>>r;
color[r]=1;
dis[r]=0;
}
for(int i=0;i<=n;++i)vec[i].clear();
vector<vector<pll>>ed(n+5);
vector<vector<ll>>fa(n+5,vector<ll>(20));
for(int i=1;i<n;i++){
int u,v,w;
cin>>u>>v>>w;
ed[u].push_back({v,w});
ed[v].push_back({u,w});
--u,--v;
vec[u].push_back(v);
vec[v].push_back(u);
}
pos.resize(n);
mp.resize(2*n);
DFS(0,-1);
for(auto &x:dfn)x=pos[x];
SparseTable<int> st(dfn);
function<void(ll)>dfs1=[&](int u){
for(auto [v,w]:ed[u]){
if(v==fa[u][0])
continue;
fa[v][0]=u;
dep[v]=dep[u]+1,des[v]=des[u]+w;
dfs1(v);
}
};
auto dist=[&](int u,int v)->ll
{
//cout<<LCA(u,v,st)<<endl;
//cout<<u<<" "<<v<<endl;
return des[u]+des[v]-2*des[LCA(u,v,st)];
};
dfs1(1);
function<void(ll fa,ll u,ll now)>dfs=[&](ll fa,ll u,ll now)
{
if(color[u]==1){
now=0;
}
dis[u]=now;
for(auto [v,w]:ed[u]){
if(v==fa)
continue;
dfs(u,v,now+w);
}
};
dfs(0,1,0);
while(q--){
ll k;
cin>>k;
vector<ll>v(k+5);
vector<pll>val(k+5);
for(int i=1;i<=k;i++){
cin>>v[i];
val[i]={dis[v[i]],v[i]};
}
sort(val.begin()+1,val.begin()+1+k,[&](pll x,pll y){
return x.first>y.first;
});
auto check=[&](ll x)->bool
{
int p=val[1].second;
//cout<<x<<" "<<p<<"\n";
for(int i=2;i<=k;i++){
if(val[i].first<=x){
break;
}
//cout<<val[i].second<<" "<<p<<"\n";
p=LCA(p,val[i].second,st);
//cout<<"p="<<p<<endl;
}
//cout<<"\n";
for(int i=2;i<=k;i++){
if(val[i].first<=x){
break;
}
if(dist(val[i].second,p)>x){
return 0;
}
}
return 1;
};
//vector<ll>vis(k+5);
ll l=0,r=val[1].first;
while(l<=r){
ll mid=(l+r)>>1;
if(check(mid)){
r=mid-1;
}
else{
l=mid+1;
}
}
cout<<l<<"\n";
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 8368kb
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
Time Limit Exceeded
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:
129225499 129225499 0 317070839 317070839 255904892 317070839 1096238166 1096238166 873176972 546644500 455103436 285643094 285643094 285643094 317919339 0 712913890 514087057 401528523 479058444 371688030 280460858 476229270 910180170 910180170 898824885 121535083 166647862 166647862 166647862 1666...