QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#279429 | #5439. Meet in the Middle | Liuxizai | WA | 100ms | 24192kb | C++17 | 5.4kb | 2023-12-08 18:17:14 | 2023-12-08 18:17:14 |
Judging History
answer
#include <bits/stdc++.h>
#define File(name) freopen(#name".in", "r", stdin); freopen(#name".out", "w", stdout);
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T>
inline T read(){
T n = 0; int f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
n = n * 10 + ch - '0';
ch = getchar();
}
return f * n;
}
template<typename T>
void write(T n){
if(n < 0) return putchar('-'), write(-n);
if(n / 10) write(n / 10);
putchar(n % 10 + '0');
}
void input() {}
template<typename Type, typename... Types>
void input(Type &arg, Types &...args){
arg = read<Type>();
input(args...);
}
namespace Main{
const int N = 100005;
const int Q = 500005;
int n, q, dfn[N], dfnCnt, st[17][N], dep[N], dfn2[N], id[N], dep2[N], siz[N], ans[Q];
vector<pair<int, int>> g1[N], g2[N];
vector<pair<int, int>> ask[N];
void dfs(int u, int fa){
st[0][dfn[u] = ++dfnCnt] = fa;
for(auto [v, w]: g2[u]){
if(v == fa) continue;
dep[v] = dep[u] + w;
dfs(v, u);
}
}
inline int dmin(int u, int v) { return dep[u] < dep[v] ? u : v; }
int lca(int u, int v){
if(u == v) return u;
if(dfn[u] > dfn[v]) swap(u, v);
int lg = log2(dfn[v] - dfn[u]);
return dmin(st[lg][dfn[u] + 1], st[lg][dfn[v] - (1 << lg) + 1]);
}
int dis(int u, int v){
if(!u || !v) return 0;
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
struct segtree{
struct segtree_node{
int l, r;
int add;
pair<pair<int, int>, pair<int,int>> dia;
}t[N << 2];
#define ls p << 1
#define rs p << 1 | 1
void pushup(int p){
vector<pair<int, int>> vec = {t[ls].dia.first, t[ls].dia.second, t[rs].dia.first, t[rs].dia.second};
int len = 0;
for(int i = 0; i < 4; i++){
for(int j = i + 1; j < 4; j++){
int tmp = dis(vec[i].first, vec[j].first) + vec[i].second + vec[j].second;
if(tmp > len) t[p].dia = {vec[i], vec[j]}, len = tmp;
}
}
}
void pushadd(int p, int k){
t[p].add += k;
t[p].dia.first.second += k;
t[p].dia.second.second += k;
}
void pushdown(int p){
if(t[p].add){
pushadd(ls, t[p].add);
pushadd(rs, t[p].add);
t[p].add = 0;
}
}
void build(int p, int l, int r, int dep[]){
t[p].l = l, t[p].r = r;
if(l == r){
t[p].dia.first = {id[l], dep[id[l]]};
return;
}
int mid = t[p].l + t[p].r >> 1;
build(ls, l, mid, dep), build(rs, mid + 1, r, dep);
pushup(p);
}
void modify(int p, int l, int r, int k){
if(l > r) return;
if(t[p].l >= l && t[p].r <= r){
pushadd(p, k);
return;
}
int mid = t[p].l + t[p].r >> 1;
pushdown(p);
if(mid >= l) modify(ls, l, r, k);
if(mid < r) modify(rs, l, r, k);
pushup(p);
}
auto query() { return t[1].dia; }
#undef ls
#undef rs
}tree;
void dfs2(int u, int fa){
dfn2[u] = ++dfnCnt;
id[dfnCnt] = u;
siz[u] = 1;
for(auto [v, w]: g1[u]){
if(v == fa) continue;
dep2[v] = dep2[u] + w;
dfs2(v, u);
siz[u] += siz[v];
}
}
void dfs3(int u, int fa){
auto tmp = tree.query();
for(auto [b, x]: ask[u]){
ans[x] = max(dis(b, tmp.first.first) + tmp.first.second, dis(b, tmp.second.first) + tmp.second.second);
}
for(auto [v, w]: g1[u]){
if(v == fa) continue;
tree.modify(1, dfn[v], dfn[v] + siz[v] - 1, -w);
tree.modify(1, 1, dfn[v] - 1, w);
tree.modify(1, dfn[v] + siz[v], n, w);
dfs3(v, u);
tree.modify(1, dfn[v], dfn[v] + siz[v] - 1, w);
tree.modify(1, 1, dfn[v] - 1, -w);
tree.modify(1, dfn[v] + siz[v], n, -w);
}
}
void Main(){
input(n, q);
for(int i = 1, u, v, w; i < n; i++){
input(u, v, w);
g1[u].emplace_back(v, w);
g1[v].emplace_back(u, w);
}
for(int i = 1, u, v, w; i < n; i++){
input(u, v, w);
g2[u].emplace_back(v, w);
g2[v].emplace_back(u, w);
}
dfs(1, 0);
for(int i = 1; i < 17; i++){
for(int j = 1; j + (1 << (i - 1)) <= n; j++){
st[i][j] = dmin(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
dfnCnt = 0;
dfs2(1, 0);
tree.build(1, 1, n, dep2);
for(int i = 0, a, b; i < q; i++){
input(a, b);
ask[a].emplace_back(b, i);
}
dfs3(1, 0);
for(int i = 0; i < q; i++) write(ans[i]), puts("");
return;
}
} // namespace Main
int main(){
#ifdef Liuxizai
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif // Liuxizai
Main::Main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 21680kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
6 4 5 3
result:
ok 4 number(s): "6 4 5 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 21468kb
input:
2 1 1 2 1 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 0ms
memory: 21404kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 100ms
memory: 24192kb
input:
10000 50000 8101 5996 108427744 5996 7605 870838849 5996 5599 603303696 8101 3006 339377417 8101 6463 442516687 6463 5560 109174079 5560 4063 127596224 3006 1682 947915262 5996 1986 130416311 6463 5455 279771516 6463 2634 516688796 4063 3034 217886863 7605 5092 742375061 5599 2266 193804402 5092 140...
output:
1942906039 1595645324 2065223648 1248937043 2028048335 1130772020 2072412414 2137483595 -526615148 -754083276 637543619 1745660998 1559642632 1990496845 -15913346 1491630946 -380280702 95765193 1053816605 791012859 1483912355 40382492 1967418312 2055735891 1430817569 -1182652245 756384635 1090008531...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '1942906039'