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 ...
随机推荐
- luogu 3375 KMP模板题
#include<bits/stdc++.h> using namespace std; ,M = ; int next[N]; void getNext(char s[]) //找nex ...
- python在字符串中查找字符
两类函数: find(),rfind() index(),rindex() 找到了都返回下标. find找不到返回-1,index找不到抛出ValueError. 带r的表示从右向左找. 都可以使用第 ...
- [独孤九剑]Oracle知识点梳理(一)表空间、用户
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- POJ3111(最大化平均值)
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 8458 Accepted: 2163 Case Time ...
- Java基础--单例类创建和测试
单例模式的主要作用是保证在Java程序中,某个类只有一个实例存在.单例模式有很多好处,它能够避免实例对象的重复创建,不仅可以减少每次创建对象的时间开销,还可以节约内存空间:能够避免由于操作多个实例导致 ...
- Day2-VIM(四):修改
字符替换 r 单个字符替换 R 连续替换 - 更改大小写 很简单,多试试就行了 tips:4-更改连续4个字符的大小写,很有意思 单词修改 cw 从光标处修改到单词结尾 cb 从光标处修改到单词开头 ...
- linux指令 apt-grt指令使用
apt-get 是linux的一条指令,主流的linux版本Debian和ubuntu都使用apt-get来安装软件.那么,需安装的软件都放在哪里呢??? apt-get 利用软件安装源来安装软件,其 ...
- 2015 浙江省赛 Beauty of Array (思维题)
Beauty of Array Edward has an array A with N integers. He defines the beauty of an array as the summ ...
- RabbitMQ 消息队列 安装及使用
RabbitMQ 消息队列安装: linux版本:CentOS 7 安装第一步:先关闭防火墙 1.Centos7.x关闭防火墙 [root@rabbitmq /]# systemctl stop fi ...
- Struts旅程(六)Struts页面转发控制ActionForward和ActionMapping
转自:https://blog.csdn.net/lovesummerforever/article/details/19125933