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 ...
随机推荐
- HDU 3333 Turing Tree 莫队算法
题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...
- itchat 总结(转)
python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...
- SpringMVC基本概念
DispatcherServlet:MVC的前端控制器,浏览器用户的请求经过DispatcherServlet的分发,到达合适的controller,生产业务数据所需要的model,model通过Di ...
- 剖析微软Hyper-V的最佳部署方式
剖析微软Hyper-V的最佳部署方式 2014-04-24 10:53 布加迪编译 51CTO.com 字号:T | T 微软Hyper-V有两种不同的版本.既可以安装到Windows Server的 ...
- ios开发学习笔记001-C语言基础知识
先来学习一下C语言基础知识,总结如下: 在xcode下编写代码. 1.编写代码 2.编译:cc –c 文件名.c 编译成功会生成一个 .o的目标文件 3.链接:把目标文件.o和系统自带的库合并在一起, ...
- 区分Activity的四种加载模式【转载】
此文为转载,文章来源:http://marshal.easymorse.com/archives/2950 文章作者: Marshal's Blog 参考文章:http://blog.csdn.n ...
- [python][django 1.10中文文档]
https://docs.djangoproject.com/en/1.10/ 官方文档,点我下载 推荐一个翻译django 1.8.2的网址: 推荐一个翻译django 1.10的博客:(着重推荐 ...
- maven学习(三)——修改maven本地默认仓库
修改从Maven中心仓库下载到本地的jar包的默认存储位置 从Maven中心仓库下载到本地的jar包的默认存放在”${user.home}/.m2/repository”中,${user.home}表 ...
- 【bzoj1095】[ZJOI2007]Hide 捉迷藏 动态点分治+堆
题目描述 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条双向走廊组成,这 ...
- 【Luogu】P2469星际竞速(费用流)
题目链接 费用流,类似最小路径覆盖. 从起点向i连一条容量1费用0的边,从i'向终点连一条容量1费用0的边: 从起点向i'连一条容量1费用为瞬移的边,从i向j'连一条容量1费用为边权的边. 然后跑就可 ...