This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using ll=long long;
const int N=1e5+13;
struct Segtree{
ll st[N << 2];
ll lazy[N << 2];
void fix(int id, int l, int r){
if(!lazy[id]) return;
st[id] += lazy[id];
if(l!=r){
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
}
lazy[id]=0;
}
void update(int id, int l, int r, int u, int v, ll val){
fix(id, l, r);
if(l > v || r < u) return;
if(u<=l && r<=v){
lazy[id]+=val;
fix(id, l, r);
return;
}
int mid=l+r >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid+1, r, u, v, val);
st[id]=min(st[id << 1], st[id << 1 | 1]);
}
ll get(int id, int l, int r, int u, int v){
fix(id, l, r);
if(l > v || r < u) return 1e18;
if(u<=l && r<=v){
return st[id];
}
int mid=l+r >> 1;
ll get1=get(id << 1, l, mid, u, v);
ll get2=get(id << 1 | 1, mid+1, r, u, v);
return min(get1, get2);
}
} st;
int n, s, q, h;
vector<pair<int, ll>> g[N];
int x[N], y[N], tmp, _x, _y;
ll w;
ll d[N];
bool mark[N];
ll val[N];
int par[N];
int tin[N];
int tout[N];
int tdfs;
ll res[N];
vector<pair<int, int>> qr[N];
void scan(){
cin >> n >> s >> q >> h;
for(int i=1; i<n; i++){
cin >> x[i] >> y[i] >> w;
g[x[i]].emplace_back(y[i], w);
g[y[i]].emplace_back(x[i], w);
}
while(s--){
cin >> tmp;
mark[tmp]=1;
}
for(int i=1; i<=q; i++){
cin >> _x >> _y;
qr[_y].push_back({_x, i});
}
}
void pre_dfs(int v){
++tdfs;
tin[v]=tdfs;
for(auto c: g[v]){
if(c.fi==par[v]) continue;
par[c.fi]=v;
d[c.fi]=d[v]+c.se;
pre_dfs(c.fi);
}
tout[v]=tdfs;
}
bool insub(int u, int v){
return tin[u] <= tin[v] && tin[v] <= tout[u];
}
void dfs(int v){
for(auto r: qr[v]){
if(par[x[r.fi]]==y[r.fi]) swap(x[r.fi], y[r.fi]); // now x[r] is par
int cc=0;
int ver=y[r.fi];
cc += insub(ver, h);
cc += insub(ver, v);
if(cc!=1){
res[r.se]=-1;
}
else{
if(insub(ver, v)){
res[r.se]=st.get(1, 1, n, tin[ver], tout[ver]);
}
else{
ll val1=st.get(1, 1, n, 1, tin[ver]-1);
ll val2=st.get(1, 1, n, tout[ver]+1, n);
res[r.se]=min(val1, val2);
}
}
}
for(auto c: g[v]){
if(c.fi==par[v]) continue;
st.update(1, 1, n, 1, n, c.se);
st.update(1, 1, n, tin[c.fi], tout[c.fi], -2*c.se);
dfs(c.fi);
st.update(1, 1, n, 1, n, -c.se);
st.update(1, 1, n, tin[c.fi], tout[c.fi], 2*c.se);
}
}
void solve(){
pre_dfs(1);
for(int i=1; i<=n; i++){
if(mark[i]) val[i]=d[i];
else val[i]=1e18;
st.update(1, 1, n, tin[i], tin[i], val[i]);
}
dfs(1);
for(int i=1; i<=q; i++){
if(res[i]==-1) cout << "escaped";
else if(res[i] >= 1e17) cout << "oo";
else cout << res[i];
cout << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
scan();
solve();
}
Compilation message (stderr)
valley.cpp: In member function 'void Segtree::update(int, int, int, int, int, ll)':
valley.cpp:32:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid=l+r >> 1;
| ~^~
valley.cpp: In member function 'll Segtree::get(int, int, int, int, int)':
valley.cpp:44:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int mid=l+r >> 1;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |