QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#334180 | #5148. Tree Distance | lmeowdn | WA | 1090ms | 161604kb | C++14 | 3.0kb | 2024-02-21 12:44:00 | 2024-02-21 12:44:00 |
Judging History
answer
//Shirasu Azusa 2024.02.19
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
return x*w;
}
const int N=2e5+5,inf=0x3f3f3f3f3f3f3f3f;
int n,q,ql[N],id[N],ans[N],sz[N],rt,rsz,vst[N],d[N],msz[N];
vp e[N],g[N]; vi p,t[N];
void dfs1(int u,int fa) {
sz[u]=1; msz[u]=0;
for(auto [v,w]:e[u]) if(v!=fa&&!vst[v]) {
dfs1(v,u); sz[u]+=sz[v];
chmax(msz[u],sz[v]);
} chmax(msz[u],sz[0]-sz[u]);
if(chmin(rsz,msz[u])) rt=u;
}
void dfs2(int u,int fa) {
p.eb(u); sz[u]=1;
for(auto [v,w]:e[u]) if(v!=fa&&!vst[v]) {
d[v]=d[u]+w; dfs2(v,u); sz[u]+=sz[v];
}
}
void dfs3(int u) {
//cout<<"DD "<<u<<endl;
sz[0]=sz[u]; rsz=n+1; dfs1(u,0);
d[rt]=0; p.clear(); dfs2(rt,0);
sort(p.begin(),p.end());
static int st[N]; int top=0;
//cout<<" rt="<<rt<<endl;
//for(int x:p) cout<<x<<" "; puts("");
for(int x:p) {
while(top&&d[st[top]]>d[x]) --top;
if(top) g[x].eb(st[top],d[x]+d[st[top]]);
st[++top]=x;
}
reverse(p.begin(),p.end()); top=0;
for(int x:p) {
while(top&&d[st[top]]>d[x]) --top;
if(top) g[st[top]].eb(x,d[x]+d[st[top]]);
st[++top]=x;
}
vst[rt]=1;
for(auto [v,w]:e[rt]) if(!vst[v]) dfs3(v);
}
int s[N];
void clear() {rep(i,0,n+1) s[i]=inf;}
void add(int x,int y) {for(;x;x-=x&-x) chmin(s[x],y);}
int qry(int x,int y=inf) {for(;x<=n;x+=x&-x) chmin(y,s[x]); return y;}
signed main() {
n=read();
rep(i,2,n) {
int u=read(), v=read(), w=read();
e[u].eb(v,w), e[v].eb(u,w);
}
sz[1]=n; dfs3(1);
q=read();
rep(i,1,q) {
ql[i]=read(); int qr=read();
t[qr].eb(i);
} clear();
rep(i,1,n) {
for(auto [l,w]:g[i]) add(l,w);
for(int x:t[i]) ans[x]=qry(ql[x]);
}
rep(i,1,q) {
if(ans[i]==inf) puts("-1");
else printf("%lld\n",ans[i]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17888kb
input:
5 1 2 5 1 3 3 1 4 4 3 5 2 5 1 1 1 4 2 4 3 4 2 5
output:
-1 3 7 7 2
result:
ok 5 number(s): "-1 3 7 7 2"
Test #2:
score: -100
Wrong Answer
time: 1090ms
memory: 161604kb
input:
199999 31581 23211 322548833 176307 196803 690953895 34430 82902 340232856 36716 77480 466375266 7512 88480 197594480 95680 61864 679567992 19572 14126 599247796 188006 110716 817477802 160165 184035 722372640 23173 188594 490365246 54801 56250 304741654 10103 45884 643490340 127469 154479 214399361...
output:
29573323 1178569098929 4088 65959 4366 7019193245 760172089 4867978 328055210 55721881562 2062364707 339719 92126 92126 4366 138216269 8212187 9404444928 2681285 4366 710854 114886 65959 1818252547 92126 91087 8367049186 26776689 5718199 710854 92126 1162886184 365255209 92126 710854 92126 710854 43...
result:
wrong answer Answer contains longer sequence [length = 999999], but output contains 74864 elements