spoj 375 QTREE - Query on a tree 树链剖分

题目链接

给一棵树, 每条边有权值, 两种操作, 一种是将一条边的权值改变, 一种是询问u到v路径上最大的边的权值。

树链剖分模板。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
int head[maxn*], son[maxn], sz[maxn], deep[maxn], top[maxn], w[maxn], f[maxn], cnt, num;
int edge[maxn][], maxx[maxn<<];
struct node
{
int to, nextt;
}e[maxn*];
void init() {
mem1(head);
num = cnt = ;
}
void add(int u, int v, int w) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void dfs1(int u, int fa) {
sz[u] = ;
deep[u] = deep[fa]+;
son[u] = -;
f[u] = fa;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
dfs1(v, u);
sz[u] += sz[v];
if(son[u]==-||sz[v]>sz[son[u]])
son[u] = v;
}
}
void dfs2(int u, int tp) {
w[u] = ++cnt, top[u] = tp;
if(~son[u])
dfs2(son[u], tp);
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == f[u]||v == son[u])
continue;
dfs2(v, v);
}
}
void pushUp(int rt) {
maxx[rt] = max(maxx[rt<<], maxx[rt<<|]);
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
maxx[rt] = val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
pushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return maxx[rt];
}
int m = l+r>>, ret = ;
if(L<=m)
ret = max(ret, query(L, R, lson));
if(R>m)
ret = max(ret, query(L, R, rson));
return ret;
}
int find(int u, int v) {
int f1 = top[u], f2 = top[v], ret = ;
while(f1 != f2) {
if(deep[f1]<deep[f2]) {
swap(f1, f2);
swap(u, v);
}
ret = max(ret, query(w[f1], w[u], , cnt, ));
u = f[f1];
f1 = top[u];
}
if(u == v)
return ret;
if(deep[u]>deep[v])
swap(u, v);
return max(ret, query(w[son[u]], w[v], , cnt, ));
}
int main()
{
int t, n, u, v, val;
cin>>t;
while(t--) {
scanf("%d", &n);
init();
deep[] = ;
for(int i = ; i<n; i++) {
scanf("%d%d%d", &u, &v, &val);
add(u, v, val);
add(v, u, val);
edge[i][] = u, edge[i][] = v, edge[i][] = val;
}
dfs1(, );
dfs2(, );
for(int i = ; i<n; i++) {
if(deep[edge[i][]]>deep[edge[i][]]) {
swap(edge[i][], edge[i][]);
}
update(w[edge[i][]], edge[i][], , cnt, );
}
char s[];
while(scanf("%s", s)) {
if(s[] == 'D')
break;
int x, y;
scanf("%d%d", &x, &y);
if(s[]=='Q') {
printf("%d\n", find(x, y));
} else {
update(w[edge[x][]], y, , cnt, );
}
}
}
return ;
}
上一篇:安全测试===Mysql 注入技巧学习 MySQL注入技巧(2)


下一篇:安全测试===Mysql 注入技巧学习 MySQL注入技巧(1)