【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree
Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 31 Solved: 19
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2
1 3
2 4
2 5
1 4 5 1
2 5 3 1
2 5 1 -2
1 4 3 0
Sample Output
0
1
3
HINT
Source
Solution
就是两个操作:1.树链覆盖C 2.树链+C,如果有边+C<0 则C=-minx
随便链剖一下+线段树维护一下就可以了
维护最小值minx,最小值个数minn
覆盖的时候直接覆盖,+C的时候先查询minx再看看是否修改C,然后+C...输出答案就是,如果minx=0,ans+=minn....
边权的树链剖分,就是把边权下放至点权
边<u,v>的值,由u,v中较深的点保存,这样显然除了root每条边和每个点是一一对应的.
这里查询所有边的值,因为默认1为root,所以直接查询[2,N]的就可以了。修改的时候稍微做一下修改就可以了
所以说随便写写就A了.
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
inline int read()
{
int x=,f=; char ch=getchar();
while (ch<''||ch>'') {if(ch=='-') f=-; ch=getchar();}
while (ch>=''&&ch<='') {x=*x+ch-''; ch=getchar();}
return x*f;
}
#define MAXN 100010
struct EdgeNode{int next,to;}edge[MAXN<<];
int head[MAXN],cnt=;
void AddEdge(int u,int v) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;}
void InsertEdge(int u,int v) {AddEdge(u,v); AddEdge(v,u);}
int N,M;
#define INF 0x7fffffff
namespace SegmentTree
{
struct SegmentTreeNode{int l,r,size,minx,minn,tag,del;}tree[MAXN<<];
#define ls now<<1
#define rs now<<1|1
inline void Update(int now)
{
tree[now].minx=min(tree[ls].minx,tree[rs].minx);
tree[now].minn=tree[ls].minx<tree[rs].minx? tree[ls].minn:tree[rs].minn;
if (tree[ls].minx==tree[rs].minx) tree[now].minx=tree[ls].minx,tree[now].minn=tree[ls].minn+tree[rs].minn;
}
inline void PushDown(int now)
{
if (tree[now].del!=-)
{
tree[ls].minx=tree[now].del; tree[ls].minn=tree[ls].size; tree[ls].del=tree[now].del; tree[ls].tag=;
tree[rs].minx=tree[now].del; tree[rs].minn=tree[rs].size; tree[rs].del=tree[now].del; tree[rs].tag=;
tree[now].del=-;
}
if (tree[now].tag)
{
tree[ls].minx+=tree[now].tag; tree[ls].tag+=tree[now].tag;
tree[rs].minx+=tree[now].tag; tree[rs].tag+=tree[now].tag;
tree[now].tag=;
}
}
inline void BuildTree(int now,int l,int r)
{
tree[now].l=l,tree[now].r=r,tree[now].size=r-l+;
tree[now].del=-; tree[now].tag=;
if (l==r) {tree[now].minn=; tree[now].minx=; return;}
int mid=(l+r)>>;
BuildTree(ls,l,mid);
BuildTree(rs,mid+,r);
Update(now);
}
inline void Change(int now,int L,int R,int C)
{
int l=tree[now].l,r=tree[now].r;
if (L<=l && R>=r) {tree[now].tag+=C; tree[now].minx+=C; return;}
PushDown(now);
int mid=(l+r)>>;
if (L<=mid) Change(ls,L,R,C);
if (R>mid) Change(rs,L,R,C);
Update(now);
}
inline void Modify(int now,int L,int R,int C)
{
int l=tree[now].l,r=tree[now].r;
if (L<=l && R>=r) {tree[now].del=C; tree[now].tag=; tree[now].minx=C; tree[now].minn=tree[now].size; return;}
PushDown(now);
int mid=(l+r)>>;
if (L<=mid) Modify(ls,L,R,C);
if (R>mid) Modify(rs,L,R,C);
Update(now);
}
inline int Getmin(int now,int L,int R)
{
int l=tree[now].l,r=tree[now].r;
if (L<=l && R>=r) return tree[now].minx;
PushDown(now);
int mid=(l+r)>>,re=INF;
if (L<=mid) re=min(re,Getmin(ls,L,R));
if (R>mid) re=min(re,Getmin(rs,L,R));
return re;
}
inline int Query(int now,int L,int R)
{
int l=tree[now].l,r=tree[now].r;
if (L<=l && R>=r) return tree[now].minx==? tree[now].minn:;
PushDown(now);
int mid=(l+r)>>,re=;
if (L<=mid) re+=Query(ls,L,R);
if (R>mid) re+=Query(rs,L,R);
return re;
}
}
namespace ChainPartition
{
int size[MAXN],fa[MAXN],deep[MAXN],top[MAXN],pl[MAXN],pr[MAXN],pre[MAXN],dfn,son[MAXN];
void DFS_1(int now)
{
size[now]=;
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=fa[now])
{
deep[edge[i].to]=deep[now]+;
fa[edge[i].to]=now;
DFS_1(edge[i].to);
size[now]+=size[edge[i].to];
if (size[son[now]]<size[edge[i].to]) son[now]=edge[i].to;
}
}
void DFS_2(int now,int chain)
{
top[now]=chain; pl[now]=++dfn; pre[dfn]=now;
if (son[now]) DFS_2(son[now],chain);
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].to!=fa[now] && edge[i].to!=son[now])
DFS_2(edge[i].to,edge[i].to);
pr[now]=dfn;
}
inline int Get(int x,int y)
{
int re=INF;
while (top[x]!=top[y])
{
if (deep[top[x]]<deep[top[y]]) swap(x,y);
re=min(re,SegmentTree::Getmin(,pl[top[x]],pl[x]));
x=fa[top[x]];
}
if (deep[x]>deep[y]) swap(x,y);
if (x!=y) re=min(re,SegmentTree::Getmin(,pl[x]+,pl[y]));
return re;
}
inline void Change(int x,int y,int C)
{
int minx=Get(x,y);
if (minx+C<) C=-minx;
// printf("Change %d %d %d\n",x,y,C);
while (top[x]!=top[y])
{
if (deep[top[x]]<deep[top[y]]) swap(x,y);
SegmentTree::Change(,pl[top[x]],pl[x],C);
x=fa[top[x]];
}
if (deep[x]>deep[y]) swap(x,y);
if (x!=y) SegmentTree::Change(,pl[x]+,pl[y],C);
printf("%d\n",SegmentTree::Query(,,N));
}
inline void Modify(int x,int y,int C)
{
// printf("Modify %d %d %d\n",x,y,C);
while (top[x]!=top[y])
{
if (deep[top[x]]<deep[top[y]]) swap(x,y);
SegmentTree::Modify(,pl[top[x]],pl[x],C);
x=fa[top[x]];
}
if (deep[x]>deep[y]) swap(x,y);
if (x!=y) SegmentTree::Modify(,pl[x]+,pl[y],C);
printf("%d\n",SegmentTree::Query(,,N));
}
}
int main()
{
N=read(),M=read();
for (int x,y,i=; i<=N-; i++) x=read(),y=read(),InsertEdge(x,y);
ChainPartition::DFS_1(); ChainPartition::DFS_2(,);
SegmentTree::BuildTree(,,N);
while (M--)
{
int opt=read(),u=read(),v=read(),C=read();
switch (opt)
{
case : ChainPartition::Modify(u,v,C); break;
case : ChainPartition::Change(u,v,C); break;
}
}
return ;
}
以前看蛋蛋写了好几天....还找Claris要代码.....
1h左右就A了....不过无故弄了个本题第一个RE
ShallWe:"这不是傻逼题么?"
【BZOJ-4353】Play with tree 树链剖分的更多相关文章
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- BZOJ 3083: 遥远的国度 dfs序,树链剖分,倍增
今天再做一天树的题目,明天要开始专攻图论了.做图论十几天之后再把字符串搞搞,区域赛前再把计几看看. 3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 128 ...
- BZOJ 1146: [CTSC2008]网络管理Network 树链剖分+线段树+平衡树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 870 Solved: 299[Submit] ...
- poj 3237 Tree 树链剖分+线段树
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
随机推荐
- 尝试HTML + JavaScript 编写Windows App
一直以来博文中使用最多的就是C# + XAML.进入Windows App时代,又多了一对 Javascript + HTML组合,这对于Web开发的程序员来说再熟悉不过了.其实小编也做过几年的Web ...
- Android设置按钮为透明
设置一个按钮为透明, (1)修改配置文件 <Button android:id="@+id/btnAppMore" android:layout_width=&quo ...
- spring: 加载远程配置
通常在spring应用中,配置中的properties文件,都是打包在war包里的,部署规模较小,只有几台服务器时,这样并没有什么大问题.如果服务器多了,特别是集群部署时,如果要修改某一项配置,得重新 ...
- oracle:db-link使用
二个oracle instance,如果需要在一个instance上,直接查询另一个instance上的数据,就要用到db-link 创建: create public database link 链 ...
- Shell高级编程视频教程-跟着老男孩一步步学习Shell高级编程实战视频教程
Shell高级编程视频教程-跟着老男孩一步步学习Shell高级编程实战视频教程 教程简介: 本教程共71节,主要介绍了shell的相关知识教程,如shell编程需要的基础知识储备.shell脚本概念介 ...
- Webwork 学习之路【04】Configuration 详解
Webwork做为经典的Web MVC 框架,个人觉得源码中配置文件这部分代码的实现十分考究. 支持自定义自己的配置文件.自定义配置文件读取类.自定义国际化支持. 可以作为参考,单独引入到其他项目中, ...
- c#新语法学习笔记
1.匿名类 匿名类编译之后会生成一个具体的泛型类,匿名类的属性是只读的.在临时数据传递时非常方便(linq查询).匿名类中不能有方法.数据传输(json),数据查询(linq) }; 2.匿名方法匿名 ...
- 基于.NET Socket API 通信的综合应用
闲谈一下,最近和客户进行对接Scoket 本地的程序作为请求方以及接受方,对接Scoket 的难度实在比较大,因为涉及到响应方返回的报文的不一致性,对于返回的报文的格式我需要做反序列化的难度增大了不少 ...
- OpenFlow
What is OpenFlow? OpenFlow is an open standard that enables researchers to run experimental protocol ...
- Matlab中的向量
1. 向量的创建 1)直接输入: 行向量:a=[1,2,3,4,5] 列向量:a=[1;2;3;4;5] 2)用“:”生成向量 a=J:K 生成的行向量是a=[J,J+1,…, ...