QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#281182 | #5439. Meet in the Middle | Liuxizai | WA | 113ms | 39504kb | C++17 | 5.4kb | 2023-12-09 22:55:07 | 2023-12-09 22:55:07 |
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, ll>> 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 -1e18;
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};
ll len = 0;
for(int i = 0; i < 4; i++){
for(int j = i + 1; j < 4; j++){
ll 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: 3ms
memory: 33284kb
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: 5ms
memory: 32428kb
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: 32660kb
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: 113ms
memory: 39504kb
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:
647838384844 686757084220 583738358512 628088784984 537870312336 633313476112 657632234934 603255785134 644081575470 758986936476 699734831804 718762101622 727958214045 737949395478 624011703338 574193299761 601655717123 697873954064 579317613912 700282158058 654092683945 672057573340 658637133928 6...
result:
wrong answer 2nd numbers differ - expected: '626539793176', found: '686757084220'