QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#95462 | #5102. Dungeon Crawler | marcosk | WA | 3ms | 3732kb | C++14 | 2.6kb | 2023-04-09 19:34:25 | 2023-04-09 19:34:28 |
Judging History
answer
#include <bits/stdc++.h>
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,ThxDem=b;i<ThxDem;++i)
#define pb push_back
#define ALL(s) s.begin(),s.end()
#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define SZ(s) int(s.size())
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
struct STree{
vector<ll> t; int n;
STree(int n):n(n),t(2*n+5,-1e18){}
void upd(int p, ll v){
p+=n;
for(t[p]=v;p>1;p>>=1) t[p>>1]=max(t[p],t[p^1]);
}
ll query(int l, int r){ // [l, r)
ll res=-1e18;
for(l+=n,r+=n;l<r;l>>=1,r>>=1){
if(l&1) res=max(res,t[l++]);
if(r&1) res=max(res,t[--r]);
}
return res;
}
};
const int K=11, MAXN=1<<K;
vector<ii> g[MAXN];
vector<pair<ii,int>> op[MAXN];
int idd,in[MAXN],sz[MAXN],bad[200010],n;
ll pt[MAXN],ans[200010];
int F[K][1<<K],D[1<<K];
void lca_dfs(int x){
fore(i,0,g[x].size()){
int y=g[x][i].fst;if(y==F[0][x])continue;
F[0][y]=x;D[y]=D[x]+1;lca_dfs(y);
}
}
void lca_init(int rt){
D[rt]=0;F[0][rt]=-1;
lca_dfs(rt);
fore(k,1,K)fore(x,0,n)
if(F[k-1][x]<0)F[k][x]=-1;
else F[k][x]=F[k-1][F[k-1][x]];
}
int lca(int x, int y){
if(D[x]<D[y])swap(x,y);
for(int k=K-1;k>=0;--k)if(D[x]-(1<<k)>=D[y])x=F[k][x];
if(x==y)return x;
for(int k=K-1;k>=0;--k)if(F[k][x]!=F[k][y])x=F[k][x],y=F[k][y];
return F[0][x];
}
int jump(int x, int k){
fore(i,0,K) if(k&(1<<i)) x=F[i][x];
return x;
}
int dfs(int pos, int p=-1, ll d=0){
sz[pos]=1; in[pos]=idd++;
pt[pos]=d;
for(auto x:g[pos]) if(x.fst!=p) sz[pos]+=dfs(x.fst,pos,d+x.snd);
return sz[pos];
}
int has(int x, int y){
return in[x] <= in[y] && in[y]<in[x]+sz[x];
}
void doit(int s){
idd=0; dfs(s);
ll tot=0;
fore(i,0,n) for(auto x:g[i]) tot+=x.snd;
STree st(n);
fore(i,0,n) st.upd(in[i],pt[i]);
lca_init(s);
for(auto pp:op[s]){
int me=pp.fst.fst, he=pp.fst.snd, id=pp.snd;
//imposible
if(has(he,me)){
bad[id]=1;
continue;
}
//puedo cualquiera
if(has(me,he)){
ans[id]=tot-st.query(0,n);
continue;
}
//cualquiera que no vaya desde el lca hasta me
int low=jump(me, D[me]-D[lca(me,he)]-1);
ll bst=max(st.query(0,in[low]), st.query(in[low]+sz[low],n));
ans[id]=tot-bst;
}
}
int main(){FIN;
int q; cin>>n>>q;
fore(i,1,n){
int x,y,w; cin>>x>>y>>w; x--; y--;
g[x].pb({y,w}); g[y].pb({x,w});
}
fore(i,0,q){
int s,k,t; cin>>s>>k>>t; s--; k--; t--;
op[s].pb({{k,t},i});
}
fore(i,0,n) doit(i);
fore(i,0,q){
if(bad[i])cout<<"impossible\n";
else cout<<ans[i]<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3540kb
input:
22 5 1 2 7 2 3 5 3 4 8 3 6 6 3 7 3 2 5 6 5 8 2 8 9 11 2 10 16 1 11 12 11 12 4 11 13 9 1 14 25 14 15 4 15 16 5 15 17 6 15 18 1 15 19 8 14 20 7 20 21 9 20 22 17 1 19 9 1 9 19 1 8 9 1 9 8 2 22 11
output:
316 293 293 impossible 314
result:
ok 5 lines
Test #2:
score: -100
Wrong Answer
time: 3ms
memory: 3732kb
input:
100 100 1 2 289384 2 3 930887 2 4 692778 4 5 636916 4 6 747794 4 7 238336 4 8 885387 8 9 760493 8 10 516650 8 11 641422 8 12 202363 8 13 490028 8 14 368691 8 15 520060 8 16 897764 16 17 513927 16 18 180541 16 19 383427 16 20 89173 16 21 455737 16 22 5212 16 23 595369 16 24 702568 16 25 956430 16 26 ...
output:
103526917 103484292 106288816 104379596 104405611 104775512 106850550 105291604 103838430 106026228 107121708 104175650 105894571 104509242 103971939 105376499 105223283 104153426 105082245 106829056 104130613 104800548 106846555 104138329 103769253 106879186 104044745 104385328 106973740 105460460 ...
result:
wrong answer 7th lines differ - expected: '105434682', found: '106850550'