板子
实数二分 最大化:
ld l=0,r=1e4,mid;
while(l<r-E){
mid=(l+r)/2;
if(check(mid)){
l=mid;
}else{
r=mid;
}
}
pt("%Lf\n",l);
01bfs https://www.luogu.com.cn/problem/P4554
次短路 https://www.luogu.com.cn/problem/P3605
k短 https://www.luogu.com.cn/problem/P2901
分治 https://www.luogu.com.cn/problem/P2727
(平衡 | 线段)树合并 https://www.luogu.com.cn/problem/P3605
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define fr(ii, ss, tt) for (int ii = ss; ii <= tt; ii++)
#define pii pair<int, int>
#define pt printf
#define fi first
#define eb emplace_back
#define int long long
#define se second
using namespace std;
void rd() {}
template <typename T, typename... args>
void rd(T& x, args&... y) {
char ch;
bool f = 0;
for (ch = getchar(); ch < '0' || '9' < ch; ch = getchar())
if (ch == '-') f = 1;
for (x = 0; '0' <= ch && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + ch - '0';
if (f) x = -x;
rd(y...);
}
const int N = 1e6 + 33, inf = 2e9;
int dfn,son[N],sz[N],top[N],dep[N],fa[N],nw[N],id[N],v[N],n,m;int r,P;
vector<int> eg[N];
namespace cut {
void dfs(int now, int pa) {
fa[now] = pa, dep[now] = dep[pa] + 1, sz[now] = 1;
for (int& x : eg[now]) {
if (x == pa) continue;
dfs(x, now);
sz[now] += sz[x];
if (sz[x] > sz[son[now]]) son[now] = x;
}
}/// right
void dfs2(int now, int tp) {
top[now] = tp, id[now] = ++dfn;nw[dfn] = v[now];
if (!son[now]) return;
dfs2(son[now], tp);
for (int& x : eg[now]) {
if (x == fa[now] || x == son[now]) continue;
dfs2(x, x);
}
}
}
void ckadd(int& x, int v) {
x += v;
}
namespace sgt {
struct Node {int l, r, v, tag;} t[4 * N];
#define ls (i << 1)
#define rs (i << 1 | 1)
void ad(int i, int v) {
ckadd(t[i].v ,(t[i].r - t[i].l + 1) * v);
t[i].tag += v;
}
void down(int i) {
int& x = t[i].tag;
ad(ls, x);
ad(rs, x);
x = 0;
}
void up(int i) { t[i].v = t[ls].v + t[rs].v; }
void bt(int i, int l, int r) {
t[i].l = l, t[i].r = r;
if (l == r) {
t[i].v = nw[t[i].l];
return;
}
int mid = (l + r) >> 1;
bt(ls, l, mid);
bt(rs, mid + 1, r);
up(i);
}
void add(int i, int l, int r, int v) {
if (l <= t[i].l && t[i].r <= r) {
ad(i, v);
return;
}
down(i);
if (l <= t[ls].r) add(ls, l, r, v);
if (t[rs].l <= r) add(rs, l, r, v);
up(i);
}
int sum(int i, int l, int r) {
if (l <= t[i].l && t[i].r <= r) {return t[i].v;}
down(i);
int ret = 0;
if (l <= t[ls].r) ckadd(ret, sum(ls, l, r));
if (t[rs].l <= r) ckadd(ret, sum(rs, l, r));
return ret;
}
} // namespace sgt
int q_path(int u, int v) {
int ret = 0;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
ckadd(ret, sgt::sum(1, id[top[u]], id[u]));
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
ckadd(ret, sgt::sum(1, id[v], id[u]));
return ret;
}
int q_son(int u) { return sgt::sum(1, id[u], id[u] + sz[u] - 1); }
void u_path(int u, int v, int val) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
sgt::add(1, id[top[u]], id[u], val);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
sgt::add(1, id[v], id[u], val);
}
void u_son(int u, int val) { sgt::add(1, id[u], id[u] + sz[u] - 1,val); }
signed main() {
rd(n, m,r,P);
fr(i, 1, n) { rd(v[i]); }
fr(i, 1, n - 1) {
int a, b;
rd(a, b);
eg[a].eb(b);
eg[b].eb(a);
}
cut::dfs(r, 0);
cut::dfs2(r,r);
sgt::bt(1, 1, n);
fr(i, 1, m) {
int op, u, v, val;
rd(op);
switch (op) {
case 1:
rd(u, v, val);
u_path(u, v, val);
break;
case 2:
rd(u, v);
pt("%lld\n", q_path(u, v));
break;
case 3:
rd(u, val);
u_son(u, val);
break;
case 4:
rd(u);
pt("%lld\n", q_son(u));
break;
}
}
return 0;
}
cut tree LCA
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define fr(ii,ss,tt) for(int ii=ss;ii<=tt;ii++)
#define pii pair<int,int>
#define pt printf
#define fi first
#define eb emplace_back
#define Int long long
#define se second
using namespace std;
void rd(){}template<typename T, typename... args> void rd(T&x, args&...y) {char ch;bool f = 0;for (ch = getchar(); ch < '0' || '9' < ch; ch = getchar())if (ch == '-')f = 1;for (x = 0; '0' <= ch && ch <= '9'; ch = getchar())x = (x << 1) + (x << 3) + ch - '0';if (f)x = -x;rd(y...);}
const int N = 1e6 + 33,inf=2e9;
int fa[N],dep[N],son[N],top[N],sz[N],n,m,s;vector<int> eg[N];
void dfs1(int now,int pa){
fa[now]=pa,dep[now]=dep[pa]+1,sz[now]=1;
for(int&x:eg[now]){
if(x==pa)continue;
dfs1(x,now);
sz[now]+=sz[x];
if(sz[x]>sz[son[now]])son[now]=x;
}
}
void dfs2(int now,int tp){
top[now]=tp;
if(!son[now])return;
dfs2(son[now],tp);
for(int&x:eg[now]){
if(x==fa[now]||x==son[now])continue;
dfs2(x,x);
}
}
int lca(int a,int b){
// pt("lca:%d %d\n",a,b);
while(top[a]!=top[b]){
if(dep[top[a]]<dep[top[b]])swap(a,b);
//pt("%d jump%d\n",a,fa[top[a]]);
a=fa[top[a]];
}
return dep[a]<dep[b]?a:b;
}
int main() {
rd(n,m,s);
fr(i,1,n-1){
int a,b;
rd(a,b);
eg[a].eb(b);
eg[b].eb(a);
}
dfs1(s,0);
dfs2(s,s);
fr(i,1,m){
int a,b;
rd(a,b);
pt("%d\n",lca(a,b));
}
return 0;
}