#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
using namespace std;
#define maxn 500005
ll n,q;
pll a[maxn];
vector<pll> g[maxn];
ll dis[maxn];
ll ls[maxn],rs[maxn],tsz = 0,root = 0;
ll id[maxn];
void upd(ll x,ll y,ll w){
g[x].pb({y,w});
}
void init(ll &v,ll tl,ll tr){
if(!v) v = ++tsz;
if(tl==tr){
id[tl] = v;
return;
}
ll mid = (tl+tr)/2;
init(ls[v],tl,mid);
init(rs[v],mid+1,tr);
upd(v,ls[v],0);
upd(v,rs[v],0);
}
void upd(ll v,ll tl,ll tr,ll l,ll r,ll u){
if(l>r||l>tr||tl>r||tl>tr) return;
if(tl>=l&&tr<=r){
upd(u,v,0);
return;
}
ll mid = (tl+tr)/2;
upd(ls[v],tl,mid,l,r,u);
upd(rs[v],mid+1,tr,l,r,u);
}
void getdis(ll poc){
for(ll i = 1;i<=tsz;i++) dis[i] = llinf;
dis[poc] = 0;
priority_queue<pll> pq;
pq.push({0,poc});
while(pq.size()){
pll p = pq.top();
ll u = p.sc;
ll cur = -p.fi;
pq.pop();
if(cur!=dis[u]) continue;
for(pll p : g[u]){
ll s = p.fi;
ll w = p.sc;
if(cur+w<dis[s]){
dis[s] = cur+w;
pq.push({-dis[s],s});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n;
init(root,1,n);
for(ll i = 1;i<=n;i++){
cin >> a[i].fi >> a[i].sc;
tsz++;
upd(id[i],tsz,1);
upd(1,1,n,a[i].fi,a[i].sc,tsz);
}
cin >> q;
while(q--){
ll x; cin >> x;
getdis(id[x]);
ll ans = -llinf;
for(ll i = 1;i<=n;i++) ans = max(ans,dis[id[i]]);
if(ans==llinf) ans = -1;
cout<<ans<<endl;
}
return 0;
}
/**
4
1 3
2 4
2 3
4 4
1
1
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12020 KB |
Output is correct |
3 |
Correct |
7 ms |
12012 KB |
Output is correct |
4 |
Runtime error |
259 ms |
138520 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
9 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
12064 KB |
Output is correct |
7 |
Incorrect |
7 ms |
12032 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
9 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
12064 KB |
Output is correct |
7 |
Incorrect |
7 ms |
12032 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12076 KB |
Output is correct |
3 |
Correct |
8 ms |
11988 KB |
Output is correct |
4 |
Correct |
6 ms |
12080 KB |
Output is correct |
5 |
Correct |
9 ms |
11988 KB |
Output is correct |
6 |
Correct |
8 ms |
12064 KB |
Output is correct |
7 |
Incorrect |
7 ms |
12032 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12020 KB |
Output is correct |
3 |
Correct |
7 ms |
12012 KB |
Output is correct |
4 |
Runtime error |
259 ms |
138520 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |