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 ...
随机推荐
- 1022: [SHOI2008]小约翰的游戏John9(Auti_SG)
1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3150 Solved: 2013[Submit] ...
- js 实现5秒倒计时后跳转页面
<script type="text/javascript"> function countDown(secs, surl) { var jumpTo = docume ...
- Hyper-v Server 2012 R2增强会话模式
从Windows Server 2012 R2开始,通过使用Hyper-v增强会话模式Hyper-v中的虚拟机允许重定向虚拟机连接会话中的本地资源.这是因为Windows Server 2012 及早 ...
- C# 中的 #region 和 #endregion 的作用
C#中的 #region 和 #endregion 表示一块区域,这样在 Visual Studio 中可以将这块区域的代码折叠起来,便于查看. 虽然Visual Studio 也响应大括号的折叠,但 ...
- nyoj 题目7 街区最短路径问题
街区最短路径问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...
- ansible Failed to connect to the host via ssh: no such identity: /root/.ssh/id_rsa.bak
中控机和远端主机配置完密钥后,在中控台上通过ansible检测联通性 ansible -i inventory.cfg all -m ping 10.1.1.1 | UNREACHABLE! => ...
- hibernate级联查询映射的两种方式
Hibernate主要支持两种查询方式:HQL查询和Criteria查询.前者应用较为广发,后者也只是调用封装好的接口. 现在有一个问题,就是实现多表连接查询,且查询结果集不与任何一个实体类对应,怎么 ...
- BootStrap导入及其使用
BootStrap主要是一个CSS框架,用于页面布局 <!DOCTYPE html> <html lang="en"> <head> <m ...
- WebGIS的开发方式
(转)摘抄自<基于WebGIS的校友资源管理系统的设计与实现>_陈刚
- 几种API接口
实用号码归属地查询(IP 地址,手机号码): 默认格式: http://api.liqwei.com/location/ (使用来访者的 IP 地址) 指定 IP 地址格式: http://api.l ...