QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#279402 | #5148. Tree Distance | Mars_Dingdang | TL | 3ms | 38392kb | C++14 | 3.0kb | 2023-12-08 17:41:53 | 2023-12-08 17:41:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(ii,aa,bb) for(re int ii = aa; ii <= bb; ii++)
#define Rep(ii,aa,bb) for(re int ii = aa; ii >= bb; ii--)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, ll> PII;
const int maxn = 2e5 + 5;
const ll inf = 1ll << 60;
namespace IO_ReadWrite {
#define re register
#define gg (p1 == p2 && (p2 = (p1 = _buf) + fread(_buf, 1, 1<<21, stdin), p1 == p2) ? EOF :*p1++)
char _buf[1<<21], *p1 = _buf, *p2 = _buf;
template <typename T>
inline void read(T &x){
x = 0; re T f=1; re char c = gg;
while(c > 57 || c < 48){if(c == '-') f = -1;c = gg;}
while(c >= 48 &&c <= 57){x = (x<<1) + (x<<3) + (c^48);c = gg;}
x *= f;return;
}
inline void ReadChar(char &c){
c = gg;
while(!isalpha(c)) c = gg;
}
template <typename T>
inline void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x/10);
putchar('0' + x % 10);
}
template <typename T>
inline void writeln(T x){write(x); putchar('\n');}
}
using namespace IO_ReadWrite;
int n, sz[maxn], rt, S, f[maxn], dis[maxn];
vector <PII> e[maxn], ds, d[maxn];
bool vis[maxn];
inline void findrt(int u, int fa) {
sz[u] = 1, f[u] = 0;
for(auto [v, w] : e[u]) {
if(v == fa || vis[v]) continue;
findrt(v, u);
sz[u] += sz[v];
f[u] = max(f[u], sz[v]);
}
f[u] = max(f[u], S - sz[u]);
if(f[u] < f[rt]) rt = u;
}
inline void get_dis(int u, int fa) {
ds.push_back({u, dis[u]});
for(auto [v, w] : e[u]) {
if(v == fa || vis[v]) continue;
dis[v] = dis[u] + w;
get_dis(v, u);
}
}
int stk[maxn], top;
inline void calc(int u) {
dis[u] = 0;
ds.clear();
get_dis(u, 0);
sort(ds.begin(), ds.end());
top = 0;
for(auto [v, w] : ds) {
while(top && w < dis[stk[top]]) top --;
if(top) d[v].push_back({stk[top], w + dis[stk[top]]});
stk[++ top] = v;
}
top = 0;
reverse(ds.begin(), ds.end());
for(auto [v, w] : ds) {
while(top && w < dis[stk[top]]) top --;
if(top) d[stk[top]].push_back({v, w + dis[stk[top]]});
stk[++ top] = v;
}
}
inline void solve(int u) {
vis[u] = 1;
calc(u);
for(auto [v, w] : e[u]) {
if(vis[v]) continue;
S = sz[v], f[rt = 0] = n + 1;
findrt(v, u);
solve(rt);
}
}
vector <PII> qry[1000005];
int q;
ll bit[maxn], ans[maxn];
inline int lowbit(int x) {return x & (-x);}
inline void upd(int x, ll v) {
for(; x; x -= lowbit(x))
bit[x] = min(bit[x], v);
}
inline ll qMin(int x) {
ll res = inf;
for(; x <= n; x += lowbit(x)) {
res = min(res, bit[x]);
}
return res;
}
int main () {
read(n);
fill(bit, bit + n + 2, inf);
rep(i, 1, n - 1) {
int u, v, w;
read(u); read(v); read(w);
e[u].push_back({v, w});
e[v].push_back({u, w});
}
S = n;
f[rt = 0] = n + 1;
findrt(1, 0);
solve(rt);
read(q);
rep(i, 1, q) {
int l, r;
read(l); read(r);
qry[r].push_back({l, i});
}
rep(r, 1, n) {
for(auto [u, w] : d[r]) upd(u, w);
for(auto [l, id] : qry[r]) {
if(l == r) ans[id] = -1;
else ans[id] = qMin(l);
}
}
rep(i, 1, q) writeln(ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 38392kb
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
Time Limit Exceeded
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...