D - Snacks HDU - 5692(dfs 序+线段树维护树上前缀和)
作者:互联网
题意
- 给我们一棵有 n 个节点的树,结点点编号从 0 开始,每个节点 i 有权值 a [i],给我们 2 种操作,
- 0 x y 第一种:修改 x 点的权值为 y
- 1 x 第二种:从 0 结点出发经过 x 的路线中,点权和最大的路线的,点权和是多少?,
- 有 m 次询问
思路
- 这题真是一个很不错的题,里面的思路很巧妙,但也有 坑
首先坑我最狠的一个坑是:懒标 lazy 的数据类型是 long long。
- 首先我们先对这棵树跑个 dfs 序,在跑 dfs 序的时候,顺带求出树上前缀和,
- 之后用线段树维护树上前缀和最大值,之后查询和修改时,根据已经求得的 dfs 序,定位到对应区间来修改即可。
代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <vector>
using namespace std;
void fre() { system("clear"), freopen("A.txt", "r", stdin); freopen("Ans.txt","w",stdout); }
void Fre() { system("clear"), freopen("A.txt", "r", stdin);}
void Run(int x = 0) {
#ifdef ACM //宏定义免注释 freopen
if (! x) fre(); else Fre();
#endif
}
#define ios ios::sync_with_stdio(false)
#define Pi acos(-1)
#define pb push_back
#define fi first
#define se second
#define db double
#define ll long long
#define ull unsigned long long
#define Pir pair<ll, ll>
#define m_p make_pair
#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)
#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)
#define memset(a, b, c) memset(a, (int)b, c);
#define size() size() * 1LL
#define sc scanf
#define pr printf
#define sd(a) sc("%lld", &a)
#define ss(a) sc("%s", a)
#define __ pr( "------------------------\n" );
#define ___ pr("\n------------------------\n");
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define esp 1e-7
#define mod (ll)(1e9 + 7)
/*=========================ACMer===========================*/
const ll mxn = 2e5 + 10;
int a[mxn];
int L[mxn], R[mxn], idx[mxn], tim;
ll pm[mxn]; //从根节点到当前节点的前缀和
vector<int> e[mxn];
void init(int n)
{
for_(i, 0, n + 10) e[i].clear();
tim = 0;
}
struct Tree
{
int l, r;
ll lazy;
ll sum;
} tree[mxn << 2];
void dfs(int u, int p)
{
L[u] = ++ tim;
idx[tim] = u;
pm[u] = pm[p] + a[u];
for_(i, 0, e[u].size() - 1)
{
int v = e[u][i];
if(v == p) continue;
dfs(v, u);
}
R[u] = tim;
}
void push_up(int rt)
{
tree[rt].sum = max(tree[rt << 1].sum, tree[rt << 1 | 1].sum);
}
void push_down(int rt)
{
if(tree[rt].lazy)
{
tree[rt << 1].lazy += tree[rt].lazy;
tree[rt << 1 | 1].lazy += tree[rt].lazy;
tree[rt << 1].sum += tree[rt].lazy;
tree[rt << 1 | 1].sum += tree[rt].lazy;
tree[rt].lazy = 0;
}
}
void build(int rt, int l, int r)
{
tree[rt].l = l, tree[rt].r = r, tree[rt].lazy = 0;
tree[rt].sum = 0;
if(l == r)
{
tree[rt].sum = pm[idx[l]];
return;
}
int md = (l + r) >> 1;
build(rt << 1, l, md);
build(rt << 1 | 1, md + 1, r);
push_up(rt);
}
void update(int rt, int l, int r, int k)
{
if(tree[rt].l > r || tree[rt].r < l) return;
if(tree[rt].l >= l && tree[rt].r <= r)
{
tree[rt].sum += k;
tree[rt].lazy += k;
return;
}
push_down(rt);
update(rt << 1, l, r, k);
update(rt << 1 | 1, l, r, k);
push_up(rt);
}
ll query(ll rt, ll l, ll r)
{
if(tree[rt].l > r || tree[rt].r < l) return -INF;
if(tree[rt].l >= l && tree[rt].r <= r)
{
return tree[rt].sum;
}
push_down(rt);
return max(query(rt << 1, l, r), query(rt << 1 | 1, l, r));
}
int main()
{
Run();
int T, cas = 1; sc("%d", &T);
while(T --)
{
int n, m;
sc("%d %d", &n, &m);
init(n);
int u, v;
for_(i, 1, n - 1)
{
sc("%d %d", &u, &v);
e[u].pb(v);
e[v].pb(u);
}
for_(i, 0, n - 1) sc("%d", &a[i]);
dfs(0, mxn - 1);
build(1, 1, n);
pr("Case #%d:\n", cas ++);
int op, x, y;
while(m --)
{
sc("%d", &op);
if(op == 0)
{
sc("%d %d", &x, &y);
int c = y - a[x];
update(1, L[x], R[x], c);
a[x] = y;
}
else
{
sc("%d", &x);
ll ans = query(1, L[x], R[x]);
pr("%lld\n", ans);
}
}
}
return 0;
}
标签:rt,HDU,Snacks,int,ll,tree,dfs,include,define 来源: https://blog.csdn.net/qq_34261446/article/details/114335455