【BZOJ1984】月下“毛景树”

Description

毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园。 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里。爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的“毛景树”下面,发现树上长着他最爱吃的毛毛果~~~ “毛景树”上有N个节点和N-1条树枝,但节点上是没有毛毛果的,毛毛果都是长在树枝上的。但是这棵“毛景树”有着神奇的魔力,他能改变树枝上毛毛果的个数:  Change k w:将第k条树枝上毛毛果的个数改变为w个。  Cover u v w:将节点u与节点v之间的树枝上毛毛果的个数都改变为w个。  Add u v w:将节点u与节点v之间的树枝上毛毛果的个数都增加w个。 由于毛毛虫很贪,于是他会有如下询问:  Max u v:询问节点u与节点v之间树枝上毛毛果个数最多有多少个。

Input

第一行一个正整数N。 接下来N-1行,每行三个正整数Ui,Vi和Wi,第i+1行描述第i条树枝。表示第i条树枝连接节点Ui和节点Vi,树枝上有Wi个毛毛果。 接下来是操作和询问,以“Stop”结束。

Output

对于毛毛虫的每个询问操作,输出一个答案。

Sample Input

4
1 2 8
1 3 7
3 4 9
Max 2 4
Cover 2 4 5
Add 1 4 10
Change 1 16
Max 2 4
Stop

Sample Output

9
16

【Data Range】
1<=N<=100,000,操作+询问数目不超过100,000。
保证在任意时刻,所有树枝上毛毛果的个数都不会超过10^9个。

题解:树剖+线段树裸题,不过注意是边权,把边权转换成点权,去掉最上面的点即可。还要注意一下cover和add两个标签的处理顺序。

#include <stdio.h>
#include <string.h>
#include <iostream>
#define lson x<<1
#define rson x<<1|1
using namespace std;
const int maxn=150010;
int n,cnt,tot;
int s[maxn<<2],p[maxn],tc[maxn<<2],ts[maxn<<2];
int to[maxn<<1],next[maxn<<1],val[maxn<<1],v[maxn],u[maxn],fa[maxn],head[maxn];
int deep[maxn],size[maxn],son[maxn],top[maxn];
char str[10];
int readin()
{
int ret=0,sig=1; char gc;
while(gc<'0'||gc>'9') sig=(gc=='-')?-1:1,gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*sig;
}
void add(int a,int b,int c)
{
to[cnt]=b;
val[cnt]=c;
next[cnt]=head[a];
head[a]=cnt++;
}
void dfs1(int x)
{
size[x]=1;
for(int i=head[x];i!=-1;i=next[i])
{
if(to[i]!=fa[x])
{
fa[to[i]]=x;
deep[to[i]]=deep[x]+1;
u[to[i]]=val[i];
dfs1(to[i]);
size[x]+=size[to[i]];
if(size[to[i]]>size[son[x]]) son[x]=to[i];
}
}
}
void dfs2(int x,int tp)
{
top[x]=tp;
p[x]=++tot;
v[p[x]]=u[x];
if(son[x]) dfs2(son[x],tp);
for(int i=head[x];i!=-1;i=next[i])
if(to[i]!=son[x]&&to[i]!=fa[x])
dfs2(to[i],to[i]);
}
void pushup(int x)
{
s[x]=max(s[lson],s[rson]);
}
void pushdown(int x)
{
if(tc[x]!=-1) //注意顺序
{
s[lson]=s[rson]=tc[lson]=tc[rson]=tc[x];
ts[lson]=ts[rson]=0;
tc[x]=-1;
}
if(ts[x])
{
s[lson]+=ts[x],s[rson]+=ts[x],ts[lson]+=ts[x],ts[rson]+=ts[x];
ts[x]=0;
}
}
void build(int l,int r,int x)
{
if(l==r)
{
s[x]=v[l];
return ;
}
int mid=l+r>>1;
build(l,mid,lson),build(mid+1,r,rson);
pushup(x);
}
int query(int l,int r,int x,int a,int b)
{
pushdown(x);
if(a<=l&&r<=b) return s[x];
int mid=l+r>>1;
if(b<=mid) return query(l,mid,lson,a,b);
if(a>mid) return query(mid+1,r,rson,a,b);
return max(query(l,mid,lson,a,b),query(mid+1,r,rson,a,b));
}
void upcover(int l,int r,int x,int a,int b,int c)
{
pushdown(x);
if(a<=l&&r<=b)
{
s[x]=tc[x]=c;
return ;
}
int mid=l+r>>1;
if(b<=mid) upcover(l,mid,lson,a,b,c);
else if(a>mid) upcover(mid+1,r,rson,a,b,c);
else upcover(l,mid,lson,a,b,c),upcover(mid+1,r,rson,a,b,c);
pushup(x);
}
void updata(int l,int r,int x,int y,int c)
{
if(l==r)
{
s[x]=c;
return ;
}
pushdown(x);
int mid=l+r>>1;
if(y<=mid) updata(l,mid,lson,y,c);
else updata(mid+1,r,rson,y,c);
pushup(x);
}
void upadd(int l,int r,int x,int a,int b,int c)
{
pushdown(x);
if(a<=l&&r<=b)
{
s[x]+=c,ts[x]+=c;
return ;
}
int mid=l+r>>1;
if(b<=mid) upadd(l,mid,lson,a,b,c);
else if(a>mid) upadd(mid+1,r,rson,a,b,c);
else upadd(l,mid,lson,a,b,c),upadd(mid+1,r,rson,a,b,c);
pushup(x);
}
void getmax()
{
int x=readin(),y=readin(),ans=0;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
ans=max(ans,query(1,n,1,p[top[x]],p[x]));
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x!=y) ans=max(ans,query(1,n,1,p[x]+1,p[y])); //p[x]要+1,取下面那一个点
printf("%d\n",ans);
}
void Cover()
{
int x=readin(),y=readin(),z=readin();
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
upcover(1,n,1,p[top[x]],p[x],z);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x!=y) upcover(1,n,1,p[x]+1,p[y],z);
}
void Change()
{
int x=readin(),y=readin();
updata(1,n,1,max(p[to[x*2-2]],p[to[x*2-1]]),y);
}
void Add()
{
int x=readin(),y=readin(),z=readin();
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
upadd(1,n,1,p[top[x]],p[x],z);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x!=y) upadd(1,n,1,p[x]+1,p[y],z);
}
int main()
{
n=readin();
int i,a,b,c;
memset(head,-1,sizeof(head));
memset(tc,-1,sizeof(tc));
for(i=1;i<n;i++)
{
a=readin(),b=readin(),c=readin();
add(a,b,c),add(b,a,c);
}
deep[1]=1;
dfs1(1);
dfs2(1,1);
build(1,n,1);
while(scanf("%s",str))
{
switch(str[1])
{
case 'a':getmax(); break;
case 'o':Cover(); break;
case 'd':Add(); break;
case 'h':Change(); break;
case 't':return 0;
}
}
}

【BZOJ1984】月下“毛景树” 树链剖分+线段树的更多相关文章

  1. BZOJ2157旅游——树链剖分+线段树

    题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路 ...

  2. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  3. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  4. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

  5. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  6. bzoj4034 (树链剖分+线段树)

    Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...

  7. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  8. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  9. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  10. HDU 2460 Network(双连通+树链剖分+线段树)

    HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...

随机推荐

  1. Jquery.Datatables 基本设置的中文注解

    $(document).ready(function() { $('#example').dataTable({ "sScrollX": "100%", //表 ...

  2. Android系统中默认值的意义列表

    转自:http://blog.csdn.net/yabg_zhi_xiang/article/details/51727844 在SettingsProvider中设置系统中默认值,我们可以在fram ...

  3. Linux系统启动过程分析

    [原创]Linux系统启动过程分析-wjlkoorey258-ChinaUnix博客http://blog.chinaunix.net/uid-23069658-id-3142047.html 经过对 ...

  4. Delphi面向对象的属性

    可以把属性看成是能对类中的数据进行修改和执行代码的特殊的辅助域.对于组件来说,属性就是列在Object Inspector窗口的内容.下面的例子定义了一个有属性的简单对象 TMyObject = cl ...

  5. [LeetCode] Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. Windows和Windows Phone应用终于可以使用FFmpeg了

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:曾经在Windows Phone上想开发一个支持多种格式的媒体播放器是比较困难的一件事 ...

  7. 安卓app设计规范整理和Android APP设计篇(转)

    随着安卓智能手机不停的更新换代.安卓手机系统越来越完美,屏幕尺寸也越来越大啦!比如最近小米的miui 6的发布和魅族手机系统的更新等等. 以小米MIUI6的安卓手机来说,MIUI6进行了全新设计,坚持 ...

  8. c语言的字符串操作(比较详细)

    1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...

  9. Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树

    E. Sign on Fence   Bizon the Champion has recently finished painting his wood fence. The fence consi ...

  10. 汇编学习(四)——算术运算程序

    (一)跳转指令 一.无条件跳转指令(不管标志寄存器,执行到这句直接跳转) 1.段内直接跳转指令 (1)指令格式: JMP SHORT short_label; IP<--IP+DB,即代码直接跳 ...