# HDU 5692 Snacks dfs序 + 线段树

237人阅读 评论(0)

Problem Description

Input

#pragma comment(linker, "/STACK:1024000000,1024000000")

Output

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;

typedef long long ll;
const int N = 100010;
const ll INF = 1e18;
struct edge
{
int to, next;
}g[N*2];
struct node
{
int l, r;
ll val, mark;
}s[N*4];
ll tmp[N], val[N];
int n, m, cnt, num, _case = 0;
{
g[cnt].to = u;
}
void dfs(int v, int fa, ll w)
{
in[v] = ++num, val[num] = w;
for(int i = head[v]; i != -1; i = g[i].next)
{
int u = g[i].to;
if(u != fa)
dfs(u, v, w + tmp[u]);
}
out[v] = num;
}
void push_up(int k)
{
s[k].val = max(s[k<<1].val, s[k<<1|1].val);
}
void push_down(int k)
{
if(s[k].mark)
{
s[k<<1].val += s[k].mark;
s[k<<1|1].val += s[k].mark;
s[k<<1].mark += s[k].mark;
s[k<<1|1].mark += s[k].mark;
s[k].mark = 0;
}
}
void build(int l, int r, int k)
{
s[k].l = l, s[k].r = r, s[k].mark = 0;
if(l == r)
{
s[k].val = val[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, k << 1);
build(mid + 1, r, k << 1|1);
push_up(k);
}
void update(int l, int r, ll c, int k)
{
if(l <= s[k].l && s[k].r <= r)
{
s[k].val += c;
s[k].mark += c;
return;
}
push_down(k);
int mid = (s[k].l + s[k].r) >> 1;
if(l <= mid) update(l, r, c, k << 1);
if(r > mid) update(l, r, c, k << 1|1);
push_up(k);
}
ll query(int l, int r, int k)
{
if(l <= s[k].l && s[k].r <= r)
return s[k].val;
push_down(k);
int mid = (s[k].l + s[k].r) >> 1;
ll ans = -INF;
if(l <= mid) ans = max(ans, query(l, r, k << 1));
if(r > mid) ans = max(ans, query(l, r, k << 1|1));
return ans;
}
int main()
{
int t, a, b;
ll c;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
cnt = num = 0;
for(int i = 0; i < n - 1; i++)
{
scanf("%d%d", &a, &b);
}
for(int i = 0; i < n; i++)
scanf("%I64d", &tmp[i]);
dfs(0, -1, tmp[0]);
build(1, num, 1);
printf("Case #%d:\n", ++_case);
for(int i = 0; i < m; i++)
{
scanf("%d%d", &a, &b);
if(a == 1) printf("%I64d\n", query(in[b], out[b], 1));
else
{
scanf("%I64d", &c);
update(in[b], out[b], c - tmp[b], 1);
tmp[b] = c;
}
}
}
return 0;
}

0
0

个人资料
• 访问：170703次
• 积分：7494
• 等级：
• 排名：第3479名
• 原创：603篇
• 转载：5篇
• 译文：0篇
• 评论：28条
~~~ACMER~~~
最新评论