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 ...
随机推荐
- 剑指offer--13.二进制中1的个数
就是猜测试数据没有负数,哈哈 ----------------------------------------------------------------- 时间限制:1秒 空间限制:32768K ...
- 2018.7.30 Designing a Qi-compliant receiver coil for wireless power systems
1) 找资料: http://www.mouser.cn/datasheet/2/389/stwlc33-1156583.pdf https://training.ti.com/wireless-po ...
- Django REST_framework Quickstart
局部避免crsf的方式 针对视图函数: from django.views.decorators.csrf import csrf_exempt @csrf_exempt def foo(reques ...
- uva11729 - Commando War(贪心)
贪心法,执行任务的时间J越长的应该越先交待.可以用相邻交换法证明正确性.其实对于两个人,要让总时间最短,就要让同一时间干两件事的时间最长. #include<iostream> #incl ...
- UVA - 11214 Guarding the Chessboard (可重复覆盖,DLX+IDA*)
题目链接 正解是IDA*+四个方向判重,但由于是个裸的可重复覆盖问题,可以用DLX水过~ 每个格子与放上皇后能干掉的标记连边,跑可重复覆盖DLX.注意要用IDA*来优化,否则会超时. #include ...
- UVA - 1602 Lattice Animals (暴力+同构判定)
题目链接 题意:求能放进w*h的网格中的不同的n连通块个数(通过平移/旋转/翻转后相同的算同一种),1<=n<=10,1<=w,h<=n. 刘汝佳的题真是一道比一道让人自闭.. ...
- [转] AS3地图拼接与战争迷雾的实现
在开发游戏的过程中,特别是地图编辑器中,需要利用最少的资源,实现最丰富的地形地貌.虽然现在众多的RPG开始使用整图,但是我们偶尔还是需要能够让玩家自己编辑地图,或者其他需要自动进行地图构建的功能.另外 ...
- BZOJ2054:疯狂的馒头
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- 1、Monkey入门准备教程
1.前提需要Android环境 ADT:链接: https://pan.baidu.com/s/1QN6EJh46cJGvUBaMZjtiWw 密码: a7zu Eclipse:https://www ...
- Day1--Python基础1--上半部分
一.第一个python程序 在linux下创建一个文件叫做hello.py,并输入 print "Hello World" 然后执行命令:python hello.py,输出 [r ...