SPOJ GSS7 - Can you answer these queries VII
板的不能再板,链剖+线段树或者是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的更多相关文章
- SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树
[题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- SP6779 GSS7 - Can you answer these queries VII
纯数据结构题,没有思维难度.直接用线段树求最大子段和的方法完成树上路径的合并.注意链上合并顺序要符合序列的前后顺序. #include <cstdio> #include <cstr ...
- 题解 SP6779 【GSS7 - Can you answer these queries VII】
题目传送门 题目大意 给出一个\(n\)个点的树,每个点有权值.有\(m\)次操作,每次要么查询一条链上的最大子段和,要么把一条链的权值都修改为一个常数. \(n,m\le 10^5\) 思路 如果是 ...
- SP6779 GSS7 - Can you answer these queries VII(线段树,树链剖分)
水题,只是坑点多,\(tag\)为\(0\)时可能也要\(pushdown\),所以要\(bool\)标记是否需要.最后树链剖分询问时注意线段有向!!! #include <cstring> ...
- 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 ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- 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 ...
- 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 ...
随机推荐
- 手动安装mysql-5.0.45.tar.gz
Linux下编译安装 安装环境:VMware9(桥接模式) + Linux bogon 2.6.32-642.3.1.el6.x86_64(查看linux版本信息:uname -a) 先给出MySQL ...
- luogu 3375 KMP模板题
#include<bits/stdc++.h> using namespace std; ,M = ; int next[N]; void getNext(char s[]) //找nex ...
- POJ2985 The k-th Largest Group (并查集+treap)
Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is ...
- codevs 2503 失恋28天-缝补礼物
题目描述 Description 话说上回他给女孩送了n件礼物,由于是廉价的所以全部都坏掉了,女孩很在意这些礼物,所以决定自己缝补,但是人生苦短啊,女孩时间有限,她总共有m分钟能去缝补礼物.由于损坏程 ...
- CF 19E Fairy——树上差分
题目:http://codeforces.com/contest/19/problem/E 去掉一条边,使无向图变成二分图. 该边应该被所有奇环经过,且不被偶环经过. 因为一条非树边一定只在一个环里. ...
- springboot+springcloud config
参考:sorry,全找不到了,当时没记录,最后后知后觉觉得应该记录,所以后面的都有在asfood父项目中的doc文件夹下记录,望见谅. 1. springconfig server 1.1. pom. ...
- Oracle logminer 分析redo log(TOAD与PLSQL)
Oracle logminer 分析redo log Oracle 11g r2 RAC centos 6.5 设置时间格式 select to_char(sysdate,'yyyy-mm-dd hh ...
- UE4材质初探
转自:http://www.unrealchina.net/portal.php?mod=view&aid=233 UE4的材质表面上看起来很简单,可是到了用的时候却总是没有办法实现好的效果. ...
- appium运行时启动失败
1.检查服务是否开启 2.简单Android设备是否连接成功 3.检查4723端口是否被占用: netstat -ano|findstr '4723' 查到被占用后,找到pid,进入任务管理器查看该p ...
- EM算法以及推导
EM算法 Jensen不等式 其实Jensen不等式正是我们熟知的convex函数和concave函数性质,对于convex函数,有 \[ \lambda f(x) + (1-\lambda)f(y) ...