板的不能再板,链剖+线段树或者是LCT随便维护。

感觉唯一要注意的是跳链的时候要对$x$向上跳和$y$向上跳的情况分开讨论,而不能直接$swap$,因为只有两段接触的端点才能相互合并,而且每一次向上跳的线段要放在已经合并完成之后的左端。

最后输出答案的时候要注意这时候$x$和$y$合并好的树链上其实左端点都是在上面的,而$x$和$y$其实是左端点相接触的,所以最后合并的时候$x.lmax + y.lmax$可能成为最优答案而不是$x.rmax + y.lmax$。

一开始线段树写手残了……

时间复杂度$O(nlog^2n)$。

Code:

#include <cstdio>
#include <cstring>
using namespace std; const int N = 1e5 + ; int n, qn, tot = , head[N], a[N], w[N];
int dfsc = , id[N], siz[N], dep[N], son[N], fa[N], top[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void swap(int &x, int &y) {
int t = x; x = y; y = t;
} inline int max(int x, int y) {
return x > y ? x : y;
} inline int max(int x, int y, int z) {
return max(max(x, y), z);
} void dfs1(int x, int fat, int depth) {
dep[x] = depth, fa[x] = fat, siz[x] = ;
int maxson = -;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs1(y, x, depth + ); siz[x] += siz[y];
if(siz[y] > maxson)
maxson = siz[y], son[x] = y;
}
} void dfs2(int x, int topf) {
top[x] = topf, w[id[x] = ++dfsc] = a[x];
if(!son[x]) return;
dfs2(son[x], topf);
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fa[x] || y == son[x]) continue;
dfs2(y, y);
}
} struct Node {
int lmax, rmax, sum, res, val;
bool tag; inline void init() {
lmax = rmax = sum = res = val = ;
tag = ;
} }; inline Node merge(Node x, Node y) {
Node ans; ans.init();
ans.sum = x.sum + y.sum;
ans.lmax = max(x.lmax, x.sum + y.lmax);
ans.rmax = max(y.rmax, y.sum + x.rmax);
ans.res = max(x.res, y.res, x.rmax + y.lmax);
return ans;
} namespace SegT {
Node s[N << ]; #define lc p << 1
#define rc p << 1 | 1
#define mid ((l + r) >> 1)
#define lmax(p) s[p].lmax
#define rmax(p) s[p].rmax
#define sum(p) s[p].sum
#define res(p) s[p].res
#define tag(p) s[p].tag
#define val(p) s[p].val inline void up(int p) {
if(!p) return;
lmax(p) = max(lmax(lc), sum(lc) + lmax(rc));
rmax(p) = max(rmax(rc), sum(rc) + rmax(lc));
sum(p) = sum(lc) + sum(rc);
res(p) = max(res(lc), res(rc), rmax(lc) + lmax(rc));
} inline void down(int p, int l, int r) {
if(!tag(p)) return;
sum(lc) = (mid - l + ) * val(p), sum(rc) = (r - mid) * val(p);
res(lc) = lmax(lc) = rmax(lc) = max(, val(p) * (mid - l + ));
res(rc) = lmax(rc) = rmax(rc) = max(, val(p) * (r - mid));
val(lc) = val(rc) = val(p);
tag(lc) = tag(rc) = , tag(p) = ;
} void build(int p, int l, int r) {
s[p].init();
if(l == r) {
sum(p) = w[l];
lmax(p) = rmax(p) = res(p) = max(, w[l]);
return;
} build(lc, l, mid);
build(rc, mid + , r);
up(p);
} void modify(int p, int l, int r, int x, int y, int v) {
if(x <= l && y >= r) {
sum(p) = (r - l + ) * v;
lmax(p) = rmax(p) = res(p) = max(, sum(p));
tag(p) = , val(p) = v;
return;
} down(p, l, r);
if(x <= mid) modify(lc, l, mid, x, y, v);
if(y > mid) modify(rc, mid + , r, x, y, v);
up(p);
} /* Node query(int p, int l, int r, int x, int y) {
if(x <= l && y >= r) return s[p]; down(p, l, r); if(y <= mid) return query(lc, l, mid, x, y);
if(x > mid) return query(rc, mid + 1, r, x, y); Node res, ln = query(lc, l, mid, x, mid), rn = query(rc, mid + 1, y, mid + 1, y);
res.init(); res.sum = ln.sum + rn.sum;
res.lmax = max(ln.lmax, ln.sum + rn.lmax);
res.rmax = max(rn.rmax, rn.sum + ln.rmax);
res.res = max(ln.res, rn.res, ln.rmax + rn.lmax); return res;
} */ Node query(int p, int l, int r, int x, int y) {
if(x <= l && y >= r) return s[p]; down(p, l, r); Node ln, rn, ans;
ln.init(), rn.init(), ans.init(); if(x <= mid) ln = query(lc, l, mid, x, y);
if(y > mid) rn = query(rc, mid + , r, x, y); ans = merge(ln, rn);
return ans;
} } using namespace SegT; inline void mTree(int x, int y, int v) {
for(; top[x] != top[y]; ) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
modify(, , n, id[top[x]], id[x], v);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
modify(, , n, id[x], id[y], v);
} inline void qTree(int x, int y) {
Node ln, rn; ln.init(), rn.init();
for(; top[x] != top[y]; ) {
if(dep[top[x]] > dep[top[y]])
ln = merge(query(, , n, id[top[x]], id[x]), ln), x = fa[top[x]];
else
rn = merge(query(, , n, id[top[y]], id[y]), rn), y = fa[top[y]];
}
if(dep[x] > dep[y])
ln = merge(query(, , n, id[y], id[x]), ln);
else
rn = merge(query(, , n, id[x], id[y]), rn); printf("%d\n", max(ln.res, rn.res, ln.lmax + rn.lmax));
} int main() {
read(n);
for(int i = ; i <= n; i++) read(a[i]);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
} dfs1(, , ), dfs2(, );
build(, , n); read(qn);
for(int op, x, y; qn--; ) {
read(op), read(x), read(y);
if(op == ) {
int v; read(v);
mTree(x, y, v);
} else qTree(x, y);
} return ;
}

SPOJ GSS7 - Can you answer these queries VII的更多相关文章

  1. SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树

    [题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...

  2. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  3. SP6779 GSS7 - Can you answer these queries VII

    纯数据结构题,没有思维难度.直接用线段树求最大子段和的方法完成树上路径的合并.注意链上合并顺序要符合序列的前后顺序. #include <cstdio> #include <cstr ...

  4. 题解 SP6779 【GSS7 - Can you answer these queries VII】

    题目传送门 题目大意 给出一个\(n\)个点的树,每个点有权值.有\(m\)次操作,每次要么查询一条链上的最大子段和,要么把一条链的权值都修改为一个常数. \(n,m\le 10^5\) 思路 如果是 ...

  5. SP6779 GSS7 - Can you answer these queries VII(线段树,树链剖分)

    水题,只是坑点多,\(tag\)为\(0\)时可能也要\(pushdown\),所以要\(bool\)标记是否需要.最后树链剖分询问时注意线段有向!!! #include <cstring> ...

  6. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  7. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  8. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  9. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

随机推荐

  1. Ajax做日期选择

    1.主页面 引入JS Jquery bootstrap 包 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  2. nginx配置允许指定域名下所有二级域名跨域请求

    核心原理是根据请求域名匹配是否是某域名的二级域名判断是否添加允许跨越头. #畅游www server { listen 8015; server_name test-tl.changyou.com; ...

  3. java 收集linux信息

    public class MachineCollector  implements Runnable{  private static int DEFAULT_INTERVAL = 30;  priv ...

  4. Maven实现直接部署Web项目到Tomcat7

    如题目,自动部署到Web服务器,直接上过程. 1.Tomcat7的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <role rolena ...

  5. BZOJ1901:Dynamic Rankings

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  6. JavaScript正则常用知识总结

    一.JavaScript正则相关方法 str.match(regexp)与regexp.exec(str)功能类似. str.search(regexp)与regexp.test(str)功能类似. ...

  7. 操作Oracle 一条龙

    1 引用Oracle.DataAccess.dll 2 App.Config中配置连接字符串: Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TC ...

  8. 断路器之一:Hystrix 使用与分析

    一:为什么需要Hystrix? 在大中型分布式系统中,通常系统很多依赖(HTTP,hession,Netty,Dubbo等),如下图: 在高并发访问下,这些依赖的稳定性与否对系统的影响非常大,但是依赖 ...

  9. 2015 浙江省赛 H - May Day Holiday

    H - May Day Holiday As a university advocating self-learning and work-rest balance, Marjar Universit ...

  10. python with open as f 写韩文中文乱码

    python3和python2的写法不一样具体如下: python3: with open(r'd:\ssss.txt','w',encoding='utf-8') as f: f.write(u'中 ...