QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#279432 | #5439. Meet in the Middle | Liuxizai | WA | 101ms | 32424kb | C++17 | 5.4kb | 2023-12-08 18:19:17 | 2023-12-08 18:19:17 |
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], dfn2[N], id[N], siz[N];
ll dep[N], dep2[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]);
}
ll 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;
ll add;
pair<pair<int, ll>, pair<int, ll>> dia;
}t[N << 2];
#define ls p << 1
#define rs p << 1 | 1
void pushup(int p){
vector<pair<int, ll>> 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, ll 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, ll 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, ll 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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 30380kb
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: 30136kb
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: 9ms
memory: 30260kb
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: 101ms
memory: 32424kb
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:
478658260777 -28831390926 444320009289 471178111759 174579574515 618365851437 442018969778 91486960188 12358286740 277502582653 283937920065 424292355091 635074692776 173777211643 -23619406672 373043990539 366825295159 378696738277 22528653085 296825662918 363907214027 470540745916 390954489456 -566...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '478658260777'