GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII
给出一棵树,树的节点有权值,有两种操作:
1.询问节点x,y的路径上最大子段和,可以为空
2.把节点x,y的路径上所有节点的权值置为c
分析:
修改树路径的信息,可以考虑一下树链剖分、动态树。
这题可以用树链剖分的方式来做,不会的可以看看这篇 树链剖分---模板。其实树链剖分不难理解,一小时左右就能学会了。
对于在一段区间的最大子段和问题,可以参考GSS1 spoj 1043 Can you answer these queries I 最大子段和
由于x,y可能不在同一个重链上,所以在询问时需要分两段进行统计,合并的时候跟GSS1基本一样。对于求完lca之后,我们可以注意一下两个链的合并方向。
建立lazy标记的时候,下沉时需要把子节点的值相应的更新,这时注意到可以为空,所以需要判断值是否为负数。
其他的没什么了。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 200005; int son[MAXN],sz[MAXN],top[MAXN],fa[MAXN],tid[MAXN],dep[MAXN],tim;
bool use[MAXN];
int a[MAXN],in[MAXN];
int po[MAXN],tol; struct Edge{
int y,next;
}edge[MAXN<<1]; struct segTree{
int l,r,lx,rx,mx,sum;
int same;
bool cov; segTree(){
l = r = lx = rx = mx = sum = 0;
}
inline int mid(){
return (l+r)>>1;
}
inline int dis(){
return (r-l+1);
}
}tree[MAXN<<2]; inline void add(int x,int y){
edge[++tol].y = y;
edge[tol].next = po[x];
po[x] = tol;
} // 树链剖分部分 ok
void dfsFind(int x,int pa,int depth){
dep[x] = depth;
fa[x] = pa;
sz[x] = 1;
son[x] = 0;
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(y==pa)continue;
dfsFind(y,x,depth+1);
sz[x] += sz[y];
if(sz[y]>sz[ son[x] ])
son[x] = y;
}
} void dfsCon(int x,int pa){
use[x] = true;
top[x] = pa;
tid[x] = ++ tim;
if(son[x])dfsCon(son[x],pa);
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(use[y])continue;
dfsCon(y,y);
}
} inline void update(segTree &now,segTree l,segTree r){
now.sum = l.sum+r.sum;
now.lx = max( l.lx , l.sum+r.lx );
now.rx = max( r.rx , r.sum+l.rx );
now.mx = max( l.rx+r.lx , max(l.mx,r.mx) );
now.same = now.cov = 0;
} void setc(segTree &now,int same){
now.sum = now.dis()*same;
now.lx = now.rx = now.mx = max(0,now.sum);
now.cov = true;
now.same = same;
} inline void pushDown(int rt){
if(tree[rt].cov==false)return;
setc(tree[rt<<1],tree[rt].same);
setc(tree[rt<<1|1],tree[rt].same);
tree[rt].same = tree[rt].cov = 0;
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
if(l==r){
setc(tree[rt],a[l]);
tree[rt].cov = 0;
return;
}
int mid = tree[rt].mid();
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
update(tree[rt],tree[rt<<1],tree[rt<<1|1]);
} void modify(int l,int r,int c,int rt){
if(l<=tree[rt].l&&tree[rt].r<=r){
setc(tree[rt],c);
return;
}
pushDown(rt);
int mid = tree[rt].mid();
if(r<=mid)modify(l,r,c,rt<<1);
else if(l>mid)modify(l,r,c,rt<<1|1);
else{
modify(l,r,c,rt<<1);
modify(l,r,c,rt<<1|1);
}
update(tree[rt],tree[rt<<1],tree[rt<<1|1]);
} segTree ask(int l,int r,int rt){
if(l<=tree[rt].l&&tree[rt].r<=r)
return tree[rt];
pushDown(rt);
int mid = tree[rt].mid();
segTree ans;
if(r<=mid)ans = ask(l,r,rt<<1);
else if(l>mid)ans = ask(l,r,rt<<1|1);
else{
segTree a = ask(l,r,rt<<1);
segTree b = ask(l,r,rt<<1|1);
update(ans,a,b);
}
update(tree[rt],tree[rt<<1],tree[rt<<1|1]);
return ans;
} inline void modify(int x,int y,int c){
while(top[x]!=top[y]){
if(dep[ top[x] ]<dep[ top[y] ])
swap(x,y);
modify(tid[top[x]],tid[x],c,1);
x = fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
modify(tid[x],tid[y],c,1);
} void out(segTree l){
cout<<l.lx<<" "<<l.rx<<" "<<l.mx<<endl;
} inline int ask(int x,int y){
segTree l,r;
while(top[x]!=top[y]){
if(dep[top[x]]>=dep[top[y]]){
segTree tmp = ask(tid[top[x]],tid[x],1);
update(l,tmp,l);
x = fa[top[x]];
}else{
segTree tmp = ask(tid[top[y]],tid[y],1);
update(r,tmp,r);
y = fa[top[y]];
}
} if(dep[x]>=dep[y]){
segTree tmp = ask(tid[y],tid[x],1);
update(l,tmp,l);
}else{
segTree tmp = ask(tid[x],tid[y],1);
update(r,tmp,r);
}
return max( l.lx+r.lx,max(l.mx,r.mx) );
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int x,y,c,op,n,m;
//int ncase = 0;
while(~RD(n)){
//ncase?puts("----------------------"):ncase = 1;
rep1(i,n)
RD(in[i]); Clear(po);
tol = 0;
REP(i,2,n){
RD2(x,y);
add(x,y);
add(y,x);
} dfsFind(1,1,1); tim = 0;
Clear(use);
dfsCon(1,1); rep1(i,n)
a[ tid[i] ] = in[i]; build(1,n,1); RD(m);
while(m--){
RD3(op,x,y);
if(op==1){
printf("%d\n",ask(x,y));
}else{
RD(c);
modify(x,y,c);
}
}
}
return 0;
}
GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树的更多相关文章
- SPOJ QTREE Query on a tree 树链剖分+线段树
题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...
- SPOJ QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
- SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树
[题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...
- QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树
Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...
- SPOJ 375 (树链剖分+线段树)
题意:一棵包含N 个结点的树,每条边都有一个权值,要求模拟两种操作:(1)改变某条边的权值,(2)询问U,V 之间的路径中权值最大的边. 思路:最近比赛总是看到有树链剖分的题目,就看了论文,做了这题, ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- 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 ). ...
- SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树
传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...
随机推荐
- 图片 文字 input等垂直居中对齐
span::before { width:100%; height:1px; font-size:1em; content:''; background:#fff; position:absolute ...
- non-manifold Mesh(非流形网格)
三角网格曲面中,大多的算法基于流形网格manifold mesh,其定义如下: 1)Each edge is incident to only one or two faces: 一条网格边为一个或两 ...
- Ajax的常用框架有哪些?
AJAX(Asynchronous JavaScript and XML,异步JavaScript和XML),是创建交互式Web应用的主要开发技术.互联网中也有大量的关于AJAX的框架,本文汇总了最常 ...
- ITEXT学习手册
本系列内容来自<iText in Action 2nd>,希望有时间的读者能够自己读一遍这本书 所有的文章相关的例子:IText.7z ITEXT基础 ITEXT学习手册——创建一个简单的 ...
- MVC网站发布常见问题
直接发布的时候生成的bin会漏掉一些文件,从而导致网站无法访问: 解决方法:发布之后,再在本地运行一下网站,然后将运行后生成的bin文件夹下的文件拷贝到发布的文件夹目录下进行覆盖,就可以了
- cdoj 25 点球大战(penalty) 模拟题
点球大战(penalty) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/2 ...
- poj 1941 The Sierpinski Fractal 递归
//poj 1941 //sep9 #include <iostream> using namespace std; const int maxW=2048; const int maxH ...
- Android自己主动化測试解决方式
如今,已经有大量的Android自己主动化測试架构或工具可供我们使用,当中包含:Activity Instrumentation, MonkeyRunner, Robotium, 以及Robolect ...
- 微信lbs---返回两个经纬度坐标点的距离
微信开发:lbs附近的商家,在数据库里记录商家的坐标,lbs设置里管理搜索半径,查询的时候,查询 客户当前坐标的半径内的所有商家列表.个人喜欢不一样,我选择了执行sql ,毕竟效果高点.微信开发必须得 ...
- h5 -1
<header></header> 页眉 主要用于页面的头部的信息介绍,也可用于板块头部 <hgroup></hgroup> 页面上的一个标题组合 一个 ...