当前位置:网站首页>Poj 3237 Tree (Tree Chain Split)
Poj 3237 Tree (Tree Chain Split)
2022-07-05 21:42:00 【Chef de station du programmeur de pile complète】
Bonjour tout le monde,On se revoit,Je suis le chef de l'armée.,C'est pour aujourd'hui.IdeaCode d'inscription.
Liens vers les sujets:poj 3237 Tree
Objet:Un arbre donné,Trois opérations:
- CHANGE i v:Oui.iLe poids du noeud devientv
- NEGATE a b:Oui.abLes poids de tous les noeuds sur le chemin deviennent inverses
- QUERY a b:RequêteabValeur maximale du poids du noeud sur le chemin.
Comment résoudre le problème:Division de la chaîne des arbres.Ensuite, maintenez le poids du noeud avec l'arbre de segment,Requête de mise à jour finale.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10005;
const int INF = 0x3f3f3f3f;
int id, far[maxn], son[maxn], cnt[maxn], idx[maxn], top[maxn], dep[maxn];
int N, en, first[maxn], jump[maxn * 2], val[maxn];
struct Edge {
int u, v, w;
void set (int u, int v, int w) {
this->u = u;
this->v = v;
this->w = w;
}
}ed[maxn * 2];
void dfs_fir (int u, int pre, int d) {
dep[u] = d;
far[u] = pre;
cnt[u] = 1;
son[u] = 0;
for (int i = first[u]; i + 1; i = jump[i]) {
int v = ed[i].v;
if (v == pre)
continue;
dfs_fir(v, u, d + 1);
cnt[u] += cnt[v];
if (cnt[son[u]] < cnt[v])
son[u] = v;
}
}
void dfs_sec(int u, int rot) {
idx[u] = id++;
top[u] = rot;
if (son[u])
dfs_sec(son[u], rot);
for (int i = first[u]; i + 1; i = jump[i]) {
int v = ed[i].v;
if (v == far[u] || v == son[u])
continue;
dfs_sec(v, v);
}
}
#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2], filp[maxn << 2], Ma[maxn << 2], Mi[maxn << 2];
inline void maintain (int u) {
filp[u] ^= 1;
swap(Ma[u], Mi[u]);
Ma[u] = -Ma[u];
Mi[u] = -Mi[u];
}
inline void pushup(int u) {
Ma[u] = max(Ma[lson(u)], Ma[rson(u)]);
Mi[u] = min(Mi[lson(u)], Mi[rson(u)]);
}
inline void pushdown (int u) {
if (filp[u]) {
maintain(lson(u));
maintain(rson(u));
filp[u] = 0;
}
}
void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
filp[u] = 0;
if (l == r) {
Ma[u] = Mi[u] = val[l];
return;
}
int mid = (l + r) / 2;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
pushup(u);
}
void modify (int u, int x, int w) {
if (lc[u] == x && rc[u] == x) {
Ma[u] = Mi[u] = w;
return;
}
pushdown(u);
int mid = (lc[u] + rc[u]) / 2;
if (x <= mid)
modify(lson(u), x, w);
else
modify(rson(u), x, w);
pushup(u);
}
void splay(int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r) {
maintain(u);
return;
}
pushdown(u);
int mid = (lc[u] + rc[u]) / 2;
if (l <= mid)
splay(lson(u), l, r);
if (r > mid)
splay(rson(u), l, r);
pushup(u);
}
int query (int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r)
return Ma[u];
pushdown(u);
int mid = (lc[u] + rc[u]) / 2, ret = -INF;
if (l <= mid)
ret = max(ret, query(lson(u), l, r));
if (r > mid)
ret = max(ret, query(rson(u), l, r));
pushup(u);
return ret;
}
inline void add_Edge(int u, int v, int w) {
ed[en].set(u, v, w);
jump[en] = first[u];
first[u] = en++;
}
void init () {
en = 0;
id = 1;
memset(first, -1, sizeof(first));
scanf("%d", &N);
int u, v, w;
for (int i = 0; i < N - 1; i++) {
scanf("%d%d%d", &u, &v, &w);
add_Edge(u, v, w);
add_Edge(v, u, w);
}
dfs_fir(1, 0, 0);
dfs_sec(1, 1);
for (int i = 0; i < N - 1; i++) {
int t = i * 2;
if (dep[ed[t].u] < dep[ed[t].v])
swap(ed[t].u, ed[t].v);
val[idx[ed[t].u]] = ed[t].w;
}
build(1, 1, N);
}
int query (int u, int v) {
int p = top[u], q = top[v], ret = -INF;
while (p != q) {
if (dep[p] < dep[q]) {
swap(p, q);
swap(u, v);
}
ret = max(ret, query(1, idx[p], idx[u]));
u = far[p];
p = top[u];
}
//printf("%d %d\n", u, v);
if (u == v)
return ret;
if (dep[u] > dep[v])
swap(u, v);
ret = max(ret, query(1, idx[son[u]], idx[v]));
return ret;
}
void modify (int u, int v) {
int p = top[u], q = top[v];
while (p != q) {
if (dep[p] < dep[q]) {
swap(p, q);
swap(u, v);
}
splay(1, idx[p], idx[u]);
u = far[p];
p = top[u];
}
if (u == v)
return;
if (dep[u] > dep[v])
swap(u, v);
splay(1, idx[son[u]], idx[v]);
}
void solve () {
int u, v;
char op[20];
while (scanf("%s", op), strcmp(op, "DONE") != 0) {
scanf("%d%d", &u, &v);
if (op[0] == 'C')
modify(1, idx[ed[u*2-2].u], v);
else if (op[0] == 'Q')
printf("%d\n", query(u, v));
else
modify(u, v);
}
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
solve();
}
return 0;
}
Avis de copyright:Article original du blog.Blogs,Sans consentement,Non reproduit.
Éditeur:Programmeur de pile complète,Veuillez indiquer la source de la réimpression.:https://javaforall.cn/117584.htmlLien vers le texte original:https://javaforall.cn
边栏推荐
- 华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
- Wood board ISO 5660-1 heat release rate mapping test
- PostGIS installation geographic information extension
- Longest swing sequence [greedy practice]
- 张丽俊:穿透不确定性要靠四个“不变”
- [daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
- EN 438-7 laminated sheet products for building covering decoration - CE certification
- Huawei fast game failed to call the login interface, and returned error code -1
- kingbaseES V8R3数据安全案例之---审计记录清除案例
- MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
猜你喜欢
校招期间 准备面试算法岗位 该怎么做?
"Grain mall" -- Summary and induction
What should I do to prepare for the interview algorithm position during school recruitment?
2.2 basic grammar of R language
EasyExcel的讀寫操作
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
uni-app 蓝牙通信
R language learning notes
Chapter 05_ Storage engine
Recursive query of multi-level menu data
随机推荐
Introduction of ArcGIS grid resampling method
xlrd常见操作
Teach yourself to train pytorch model to Caffe (2)
Uni app Bluetooth communication
Golang(1)|从环境准备到快速上手
总结出现2xx、3xx、4xx、5xx状态码的原因
2022-07-03-cka- latest feedback from fans
QML reported an error expected token ";", expected a qualified name ID
Poj3414广泛搜索
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
MATLAB | App Designer·我用MATLAB制作了一款LATEX公式实时编辑器
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
SQL knowledge leak detection
华为云ModelArts文本分类–外卖评论
华为联机对战如何提升玩家匹配成功几率
如何组织一场实战攻防演练
datagrid直接编辑保存“设计缺陷”
HDU 4391 Paint The Wall 段树(水
EN 438-7 laminated sheet products for building covering decoration - CE certification
Making global exception handling classes with aspect