题解 SP6779 【GSS7 - Can you answer these queries VII】
题目大意
给出一个\(n\)个点的树,每个点有权值。有\(m\)次操作,每次要么查询一条链上的最大子段和,要么把一条链的权值都修改为一个常数。
\(n,m\le 10^5\)
思路
如果是一维的话,我们不难列出动态\(\texttt{dp}\)转移式:
\]
不懂得话可以去看一下GSS1的题解。
这道题要求一个链的答案,那我们直接求出这个链的矩阵之积即可,用树剖就好了,修改也很简单。需要注意的是,矩阵乘法有没有交换律的,所以需要维护两种不同方向的矩阵之积。
这道题有点卡常,所以快速幂不能朴素快速幂,而是找一下规律,具体见代码。
\(\texttt{Code}\)
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define INF 0x7f7f7f7f
#define MAXN 100005
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
int n,m,wei[MAXN];
struct Matrix{
int val[3][3];
Matrix(){memset (val,0xcf,sizeof (val));}
int* operator [](int x){return val[x];}
Matrix operator * (const Matrix &p)const{
Matrix New;
for (Int i = 0;i < 3;++ i)
for (Int j = 0;j < 3;++ j)
for (Int k = 0;k < 3;++ k)
New[i][j] = max (New[i][j],val[i][k] + p.val[k][j]);
return New;
}
};
Matrix init (int v){
Matrix A;
A[0][0] = A[0][2] = A[1][2] = A[2][2] = 0,A[0][1] = A[1][1] = v;
return A;
}
Matrix III (){
Matrix res;
for (Int i = 0;i < 3;++ i) res[i][i] = 0;
return res;
}
Matrix qkpow (int v,int k){
Matrix A;
A[0][0] = A[2][2] = 0;
A[0][1] = max (v,k * v),A[0][2] = A[1][2] = max (0,k * v);
A[1][1] = k * v;
return A;
}
struct edge{
int v,nxt;
}e[MAXN << 1];
int toop = 1,head[MAXN];
void Add_Edge (int u,int v){
e[++ toop] = edge {v,head[u]},head[u] = toop;
e[++ toop] = edge {u,head[v]},head[v] = toop;
}
int Index,dep[MAXN],siz[MAXN],son[MAXN],dfn[MAXN],par[MAXN],top[MAXN],tur[MAXN];
void dfs1 (int u,int fa){
par[u] = fa,dep[u] = dep[fa] + 1,siz[u] = 1;
for (Int i = head[u];i;i = e[i].nxt){
int v = e[i].v;
if (v == fa) continue;
dfs1 (v,u),siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2 (int u,int Top){
dfn[u] = ++ Index,tur[Index] = u,top[u] = Top;
if (son[u]) dfs2 (son[u],Top);
for (Int i = head[u];i;i = e[i].nxt){
int v = e[i].v;
if (v == par[u] || v == son[u]) continue;
dfs2 (v,v);
}
}
struct Segment{
#define len(x) (tree[x].r-tree[x].l+1)
struct node{
int l,r,tag;Matrix Sum[2];
}tree[MAXN << 2];
void Pushup (int x){
tree[x].Sum[0] = tree[x << 1].Sum[0] * tree[x << 1 | 1].Sum[0];
tree[x].Sum[1] = tree[x << 1 | 1].Sum[1] * tree[x << 1].Sum[1];
}
void Pushadd (int x,int v){
tree[x].tag = v;
tree[x].Sum[0] = tree[x].Sum[1] = qkpow (v,len (x));
}
void Pushdown (int x){
if (tree[x].tag == INF) return ;
Pushadd (x << 1,tree[x].tag),Pushadd (x << 1 | 1,tree[x].tag);
tree[x].tag = INF;
}
void build (int i,int l,int r){
tree[i].l = l,tree[i].r = r,tree[i].tag = INF;
if (l == r) return tree[i].Sum[0] = tree[i].Sum[1] = init(wei[tur[l]]),void ();
int mid = (l + r) >> 1;
build (i << 1,l,mid),build (i << 1 | 1,mid + 1,r);
Pushup (i);
}
Matrix query (int i,int l,int r,int type){
if (tree[i].l >= l && tree[i].r <= r) return tree[i].Sum[type];
int mid = (tree[i].l + tree[i].r) >> 1;
Pushdown (i);
if (r <= mid) return query (i << 1,l,r,type);
else if (l > mid) return query (i << 1 | 1,l,r,type);
else return !type ? query (i << 1,l,r,type) * query (i << 1 | 1,l,r,type) : query (i << 1 | 1,l,r,type) * query (i << 1,l,r,type);
}
void Change (int i,int l,int r,int v){
if (tree[i].l >= l && tree[i].r <= r) return Pushadd (i,v);
int mid = (tree[i].l + tree[i].r) >> 1;
Pushdown (i);
if (l <= mid) Change (i << 1,l,r,v);
if (r > mid) Change (i << 1 | 1,l,r,v);
Pushup (i);
}
#undef len(x)
}Tree;
int QueryChain (int x,int y){
Matrix A,B;A = B = III();
while (top[x] ^ top[y]){
if (dep[top[x]] > dep[top[y]]){
A = A * Tree.query (1,dfn[top[x]],dfn[x],1);
x = par[top[x]];
}
else{
B = Tree.query (1,dfn[top[y]],dfn[y],0) * B;
y = par[top[y]];
}
}
if (dfn[x] < dfn[y]) B = Tree.query (1,dfn[x],dfn[y],0) * B;
else A = A * Tree.query (1,dfn[y],dfn[x],1);A = A * B;
return max (A[0][1],A[0][2]);
}
void UpdateChain (int x,int y,int v){
while (top[x] ^ top[y]){
if (dep[top[x]] < dep[top[y]]) swap (x,y);
Tree.Change (1,dfn[top[x]],dfn[x],v);
x = par[top[x]];
}
if (dfn[x] > dfn[y]) swap (x,y);
Tree.Change (1,dfn[x],dfn[y],v);
}
signed main(){
read (n);
for (Int i = 1;i <= n;++ i) read (wei[i]);
for (Int i = 2,u,v;i <= n;++ i) read (u,v),Add_Edge (u,v);
dfs1 (1,0),dfs2 (1,1),Tree.build (1,1,n);
read (m);
while (m --){
int opt,a,b,c;
read (opt,a,b);
if (opt == 1) write (QueryChain (a,b)),putchar ('\n');
else read (c),UpdateChain (a,b,c);
}
return 0;
}
题解 SP6779 【GSS7 - Can you answer these queries VII】的更多相关文章
- SP6779 GSS7 - Can you answer these queries VII
纯数据结构题,没有思维难度.直接用线段树求最大子段和的方法完成树上路径的合并.注意链上合并顺序要符合序列的前后顺序. #include <cstdio> #include <cstr ...
- SP6779 GSS7 - Can you answer these queries VII(线段树,树链剖分)
水题,只是坑点多,\(tag\)为\(0\)时可能也要\(pushdown\),所以要\(bool\)标记是否需要.最后树链剖分询问时注意线段有向!!! #include <cstring> ...
- SPOJ GSS7 - Can you answer these queries VII
板的不能再板,链剖+线段树或者是LCT随便维护. 感觉唯一要注意的是跳链的时候要对$x$向上跳和$y$向上跳的情况分开讨论,而不能直接$swap$,因为只有两段接触的端点才能相互合并,而且每一次向上跳 ...
- SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树
[题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- 6779. Can you answer these queries VII - SPOJ
Given a tree with N ( N<=100000 ) nodes. Each node has a interger value x_i ( |x_i|<=10000 ). ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- BZOJ2482: [Spoj1557] Can you answer these queries II
题解: 从没见过这么XXX的线段树啊... T_T 我们考虑离线做,按1-n一个一个插入,并且维护区间[ j,i](i为当前插入的数)j<i的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...
随机推荐
- python实现两台不同主机之间进行通信(客户端和服务端)——Socket
大家好,我是辰哥~ 今天教大家通过Python进行Socket网络编程 (做一个聊天程序) 可以实现在不同的主机(电脑)之间进行通话. 具体效果如何,接着往下看 可以看到客户端(上方)向服务器端(下方 ...
- 虚拟机VMWare开机黑屏 无法进入系统
参考了: https://blog.csdn.net/x534119219/article/details/79497264 可能方案一: 关闭VMware Workstation加速3D图形设置 可 ...
- vue-cli3.x中的webpack配置,优化及多页面应用开发
官方文档 vue-cli3以下版本中,关于webpack的一些配置都在config目录文件中,可是vue-cli3以上版本中,没有了config目录,那该怎么配置webpack呢? 3.x初始化项目后 ...
- maven下载出错
求解
- Java基础(二)——内部类
一.内部类 内部类(Inner Class)就是定义在一个类里面的类.与之对应,包含内部类的类被称为外部类.内部类可以用private修饰. 1.为什么要定义内部类?或者内部类的作用是什么? 内部类提 ...
- Python - typing 模块 —— Union
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- HTTP快速入门
一.tomcat端口号设置为80,访问时候可以不加:http协议1.1版本可以复用连接,请求结束后会稍微等会: 二. 表单,get方式提交: 三.user-agent告诉服务器是哪个浏览器,代码中解决 ...
- [初学Python]编写一个最简单判断SQL注入的检测工具
0x01 背景 15年那会,几乎可以说是渗透最火的一年,各种教程各种文章,本人也是有幸在那几年学到了一些皮毛,中间因学业问题将其荒废至今.当初最早学的便是,and 1=1 和 and 1=2 这最简单 ...
- Docker安装GitLab与Runner(网关),常规设置,自动化用到k8s+token
[转]图文详解k8s自动化持续集成之GitLab CI/CD Windows里面使用Debian命令行工具完成 和Docker网络相关的命令 查看某一个容器的网络 docker inspect 容器I ...
- AS插件快速生成javabean
https://blog.csdn.net/u010227042/article/details/103803198