NOI模拟题1 Problem A: sub
题面

Sample Input
5 7
2 -1 -3 1 1
1 2
1 3
3 4
3 5
2
1 3 0
2
1 2 1
2
1 1 -3
2
Sample Output
2
4
5
2
HINT

Solution
首先考虑序列上的这个问题: 给定一个序列, 有两种操作
- 修改一个位置上的值
- 询问某个区间中的连续段的最大权值和
做法是: 用一个线段树维护这个序列, 每个节点所对应的区间记录以下4个信息:
sum表示整个区间的权值和leftMax表示以包含从最左边开始的区间的连续段最大权值和rightMax与上面相反max表示整个区间中连续段最大权值和
则我们可以通过上述信息合并两个区间的信息.
考虑如何转化到树上解决: 我们首先进行树链剖分. 我们用sum[u]记录\(u\)这个点的权值加上以\(u\)的每个字节点为根的连通块的最大权值和. 我们用类似于序列上的方法维护\(sum\)即可. 修改一个点的权值时, 往上跳修改即可.
实现的时候把线段树上的信息合并重载为运算符会比较简洁.
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1; char c;
while(! isdigit(c = getchar())) if(c == '-') sgn *= -1;
while(isdigit(c)) a = a * 10 + c - '0', c = getchar();
return a * sgn;
}
}
const int N = (int)1e5;
int n;
multiset<int> ans;
struct data
{
int leftMax, rightMax, mx, sum;
inline data() { leftMax = rightMax = mx = sum = 0; }
inline data friend operator +(const data &a, const data &b)
{
data res;
res.sum = a.sum + b.sum;
res.leftMax = max(a.leftMax, a.sum + b.leftMax);
res.rightMax = max(b.rightMax, b.sum + a.rightMax);
res.mx = max(res.leftMax, res.rightMax);
res.mx = max(res.mx, a.mx); res.mx = max(res.mx, b.mx);
res.mx = max(res.mx, a.rightMax + b.leftMax);
return res;
}
};
struct segmentTree
{
data nd[N << 2];
void modify(int u, int L, int R, int pos, int x)
{
if (L == R)
{
nd[u].leftMax = nd[u].rightMax = nd[u].mx = max(0, x);
nd[u].sum = x;
return;
}
if (pos <= L + R >> 1) modify(u << 1, L, L + R >> 1, pos, x);
else modify(u << 1 | 1, (L + R >> 1) + 1, R, pos, x);
nd[u] = nd[u << 1] + nd[u << 1 | 1];
}
inline void modify(int pos, int x) { modify(1, 1, n, pos, x); }
data query(int u, int curL, int curR, int L, int R)
{
if (curL >= L && curR <= R) return nd[u];
int mid = curL + curR >> 1;
data res;
if (L <= mid) res = res + query(u << 1, curL, mid, L, R);
if (R > mid) res = res + query(u << 1 | 1, mid + 1, curR, L, R);
return res;
}
inline data query(int L, int R) { return query(1, 1, n, L, R); }
}seg;
struct tree
{
struct node
{
int w;
vector<int> edg; int pre;
int sz, hvy;
int tp, id, lst;
int sum;
inline node() { edg.clear(); }
}nd[N + 1];
inline void addEdge(int u, int v) { nd[u].edg.push_back(v); nd[v].edg.push_back(u); }
void getSize(int u, int pre)
{
nd[u].pre = pre; nd[u].hvy = -1; nd[u].sz = 1;
for (auto v : nd[u].edg) if (v != pre)
{
getSize(v, u); nd[u].sz += nd[v].sz;
if (nd[u].hvy == -1 || nd[v].sz > nd[nd[u].hvy].sz) nd[u].hvy = v;
}
}
int clk;
int work(int u, int tp)
{
nd[u].tp = tp; nd[u].id = ++ clk;
if (~ nd[u].hvy) work(nd[u].hvy, tp);
else
{
for (int v = u; v != nd[u].tp; v = nd[v].pre) nd[v].lst = nd[u].id;
nd[nd[u].tp].lst = nd[u].id;
}
nd[u].sum = nd[u].w;
for (auto v : nd[u].edg) if (v != nd[u].pre && v != nd[u].hvy) nd[u].sum += work(v, v);
seg.modify(nd[u].id, nd[u].sum);
if (u == tp)
{
data res = seg.query(nd[u].id, nd[u].lst);
ans.insert(res.mx);
return res.leftMax;
}
return 0;
}
inline void decomposition() { getSize(1, -1); clk = 0; work(1, 1); }
inline void modify(int u, int x)
{
int tmp = nd[u].w; nd[u].w = x;
for (; ~ u; u = nd[nd[u].tp].pre)
{
nd[u].sum = nd[u].sum - tmp + x;
data res = seg.query(nd[nd[u].tp].id, nd[nd[u].tp].lst);
tmp = res.leftMax;
ans.erase(ans.find(res.mx));
seg.modify(nd[u].id, nd[u].sum);
res = seg.query(nd[nd[u].tp].id, nd[nd[u].tp].lst);
x = res.leftMax;
ans.insert(res.mx);
}
}
}T;
int main()
{
#ifndef ONLINE_JUDGE
freopen("sub.in", "r", stdin);
freopen("sub.out", "w", stdout);
#endif
using namespace Zeonfai;
n = getInt(); int m = getInt();
for (int i = 1; i <= n; ++ i) T.nd[i].w = getInt();
for (int i = 1, u, v; i < n; ++ i) u = getInt(), v = getInt(), T.addEdge(u, v);
ans.clear();
T.decomposition();
for (int i = 0; i < m; ++ i)
{
int opt = getInt();
if (opt == 1)
{
int u = getInt(), x = getInt();
T.modify(u, x);
}
else
{
multiset<int>::iterator p = ans.end();
printf("%d\n", *(-- p));
}
}
}
NOI模拟题1 Problem A: sub的更多相关文章
- NOI模拟题6 Problem C: Circle
Solution 首先这个矩阵, 很明显的就是Vandermonde矩阵. 我们有公式: \[ |F_n| = \prod_{1 \le j < i \le n} (a_i - a_j) \] ...
- NOI模拟题5 Problem A: 开场题
Solution 注意到\(\gcd\)具有结合律: \[ \gcd(a, b, c) = \gcd(a, \gcd(b, c)) \] 因此我们从后往前, 对于每个位置\(L\), 找到每一段不同的 ...
- NOI模拟题4 Problem C: 填格子(board)
Solution 首先我们要有敏锐的直觉: 我们将每一列中不选哪种颜色看作是一个序列, 则我们发现这个序列要求相邻两位的颜色不同. 我们还发现, 一个这样的序列对应两种不同的合法的棋盘, 因此统计合法 ...
- NOI模拟题4 Problem B: 小狐狸(fox)
Solution 考虑分开统计朝向每一个方向的所有狐狸对答案的贡献. 比如说以向右为例, 我们用箭标表示每一只狐狸的方向, 用\('\)表示当前一步移动之前的每一只狐狸的位置. \[ \begin{a ...
- NOI模拟题4 Problem A: 生成树(mst)
Solution 我们考虑答案的表达式: \[ ans = \sqrt{\frac{\sum_{i = 1}^{n - 1} (w_i - \overline{w})^2}{n - 1}} \] 其中 ...
- 花海漫步 NOI模拟题
题目好像难以看懂? 题目大意 给出一个字符串\(S\),统计满足以下条件的\((i,j,p,q)\)的数量. \(i \leq j, p \leq q\) \(S[i..j],S[p..q]\)是回文 ...
- 神奇的矩阵 NOI模拟题
神奇的矩阵 题目大意 有一个矩阵\(A\),第一行是给出的,接下来第\(x\)行,第\(y\)个元素的值为数字\(A_{x-1,y}\)在\(\{A_{x-1,1},A_{x-1,2},A_{x-1, ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem I. Interest Targeting 模拟题
Problem I. Interest Targeting 题目连接: http://codeforces.com/gym/100714 Description A unique display ad ...
随机推荐
- 回顾Scrum学习:《Scrum实战》第4次课【全职的Scrum Master】作业
回顾Scrum学习 1.回顾目标 1.1 期望结果 了解和学习Scrum,为将来换方向打好理论基础 如果能在目前公司引入和推行也很好,但是根据目前公司的文化氛围来看,推行希望不大 把敏捷思想应用到 ...
- KNN算法python实现小样例
K近邻算法概述优点:精度高.对异常数据不敏感.无数据输入假定缺点:计算复杂度高.空间复杂度高适用数据范围:数值型和标称型工作原理:存在一个样本数据集合,也称作训练样本集,并且样本集中每个数据都存在标签 ...
- loj2071 「JSOI2016」最佳团体
分数规划+树形依赖背包orz #include <iostream> #include <cstring> #include <cstdio> #include & ...
- DEDE调用指定文章ID来调用特定文档
http://www.jb51.net/cms/137423.html 代码如下: {dede:arclist row=1 idlist='6'} <li><a href=" ...
- IOS笔记048-数据存储
IOS数据存储的几种方式 XML属性列表(plist) 归档 Preference(偏好设置) NSKeyedArchiver归档(NSCodin ...
- 精通CSS高级Web标准解决方案(5、对列表应用样式和创建导航条)
5.1基本样式列表 去掉列表的默认样式: ul{ margin:; padding:; list-style-type:none; } 添加定制的符号,在列表左边添加填充,为符号留出空间,然后将符号图 ...
- CentOS7 haproxy+keepalived实现高可用集群搭建
一.搭建环境 CentOS7 64位 Keepalived 1.3.5 Haproxy 1.5.18 后端负载主机:192.168.166.21 192.168.166.22 两台节点上安装rabbi ...
- 【bzoj3083】遥远的国度 树链剖分+线段树
题目描述 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn ...
- 【tmux】常用命令
https://www.cnblogs.com/lizhang4/p/7325086.html 复制 prefix [ + vim风格选择复制 新建session tmux new -s name 为 ...
- redis windows安装
下载:https://github.com/MicrosoftArchive/redis/releases 命令行启动:redis-server.exe redis.windows.conf 以服务启 ...