BZOJ 1984月下“毛景树” LCT维护边权 + 下传标记
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
对于毛毛虫的每个询问操作,输出一个答案。
题解:树剖太简单了,练一练 LCT
#include <bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 100050
#define inf 0x3f
using namespace std;
int n;
char str[10];
namespace LCT
{
#define lson ch[x][0]
#define rson ch[x][1]
int maxv[maxn << 1], tag[maxn << 1], rev[maxn << 1], addv[maxn << 1], val[maxn << 1], siz[maxn << 1], f[maxn << 1], sta[maxn << 1], ch[maxn << 1][2]; int isRoot(int x)
{
return !(ch[f[x]][0] == x || ch[f[x]][1] == x);
}
int get(int x)
{
return ch[f[x]][1] == x;
}
void mark_tag(int x, int delta)
{
if(!x) return;
addv[x] = 0;
tag[x] = (siz[x] ? delta : 0);
maxv[x] = (siz[x] ? delta : 0);
if(x > n) val[x] = delta;
}
void mark_add(int x, int delta)
{
if(!x) return;
addv[x] += (siz[x] ? delta : 0);
maxv[x] += (siz[x] ? delta : 0);
if(x > n) val[x] += delta;
}
void mark_rev(int x)
{
if(!x) return;
swap(lson, rson), rev[x] ^= 1;
}
void pushup(int x)
{
maxv[x] = max(val[x], max(maxv[lson], maxv[rson]));
siz[x] = (x > n) + siz[lson] + siz[rson];
}
void pushdown(int x)
{
if(tag[x]) mark_tag(lson, tag[x]), mark_tag(rson, tag[x]), tag[x] = 0;
if(addv[x]) mark_add(lson, addv[x]), mark_add(rson, addv[x]), addv[x] = 0;
if(rev[x]) mark_rev(lson), mark_rev(rson), rev[x] = 0;
}
void rotate(int x)
{
int old = f[x], fold = f[old], which = get(x);
if(!isRoot(old)) ch[fold][ch[fold][1] == old] = x;
ch[old][which] = ch[x][which ^ 1], f[ch[old][which]] = old;
ch[x][which ^ 1] = old, f[old] = x, f[x] = fold;
pushup(old), pushup(x);
}
void splay(int x)
{
int u = x, v = 0;
sta[++v] = u;
while(!isRoot(u)) sta[++v] = f[u], u = f[u];
while(v) pushdown(sta[v--]);
u = f[u];
for(int fa; (fa = f[x]) != u; rotate(x))
if(f[fa] != u) rotate(get(fa) == get(x) ? fa : x);
}
void Access(int x)
{
int y = 0;
while(x) splay(x), rson = y, pushup(x), y = x, x = f[x];
}
void MakeRoot(int x)
{
Access(x), splay(x), mark_rev(x);
}
// y is new root
void split(int x, int y)
{
MakeRoot(x), Access(y), splay(y);
}
void link(int x, int y)
{
MakeRoot(y), f[y] = x;
}
};
int main()
{
// setIO("input");
LCT :: maxv[0] = -inf;
scanf("%d",&n);
for(int i = 1; i < n; ++i)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
LCT :: val[i + n] = w;
LCT :: link(u, i + n);
LCT :: link(i + n, v);
}
while(1)
{
scanf("%s",str);
int u, v, k, w;
if(str[0] == 'A')
{
scanf("%d%d%d",&u,&v,&w), LCT :: split(u, v), LCT :: mark_add(v, w);
}
if(str[0] == 'M')
{
scanf("%d%d",&u,&v), LCT :: split(u, v), printf("%d\n",LCT :: maxv[v]);
}
if(str[1] == 'o')
{
scanf("%d%d%d",&u,&v,&w), LCT :: split(u, v), LCT :: mark_tag(v, w);
}
if(str[1] == 'h')
{
scanf("%d%d",&k,&w);
LCT :: Access(k + n), LCT :: splay(k + n), LCT :: val[k + n] = w, LCT :: pushup(k + n);
}
if(str[0] == 'S') break;
}
return 0;
}
BZOJ 1984月下“毛景树” LCT维护边权 + 下传标记的更多相关文章
- Bzoj 1984: 月下“毛景树” 树链剖分
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1282 Solved: 410[Submit][Status][Discu ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
- BZOJ 1984 月下“毛景树”
我觉得我要把BZOJ上的链剖写完了吧.... #include<iostream> #include<cstdio> #include<cstring> #incl ...
- BZOJ 1984: 月下“毛景树” (树链剖分+线段树)
注意赋值和加法的标记下传优先级.具体看代码. CODE #include <vector> #include <queue> #include <cstdio> # ...
- 【BZOJ】1984 月下“毛景树”
[算法]树链剖分+线段树 [题解]线段树的区间加值和区间覆盖操作不能同时存在,只能存在一个. 修改:从根节点跑到目标区域路上的标记全部下传,打完标记再上传回根节点(有变动才需要上传). 询问:访问到目 ...
- 【BZOJ-1984】月下“毛景树” 树链剖分
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1314 Solved: 416[Submit][Status][Discu ...
- BZOJ1984: 月下“毛景树”
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 713 Solved: 245[Submit][Status] Descri ...
- 树剖+线段树||树链剖分||BZOJ1984||Luogu4315||月下“毛景树”
题面:月下“毛景树” 题解:是道很裸的树剖,但处理的细节有点多(其实是自己线段树没学好).用一个Dfs把边权下移到点权,用E数组记录哪些边被用到了:前三个更新的操作都可以合并起来,可以发现a到b节点间 ...
- 【BZOJ1984】月下“毛景树” 树链剖分+线段树
[BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...
随机推荐
- const关键字作用
const int a; int const a; const int *a; int * const a; int const * a const; /******/ 前两个的作用是一样,a是一个常 ...
- 数组优化 Dijkstra 最短路
//============================================================================// Name : POJ.cpp// Au ...
- D. Multiplication Table 二分查找
刚做这道题根本没想到二分,最关键是没想出来怎样统计在这个矩阵中比一个数小的有几个怎么算.造成自己想了好久最后看了别人的提示才做出来.哎.好久不做题太弱了 #include<iostream> ...
- 各个领域常见的一些bug汇总
一 Android系统功能测试设计的测试用例 a.对所测APP划分模块 b.详细列出每个模块的功能点(使用Xmind绘制功能图) c.使用等价类划分.边界值.场景发等对各功能点编写测试用例(考虑中断功 ...
- jQuery Validate Ajax 验证
jQuery Validate Ajax 验证 <script type="text/javascript"> $(function() { $('#formCityL ...
- oc44--多对象内存管理
// Room.h #import <Foundation/Foundation.h> @interface Room : NSObject @property int no;// 房间号 ...
- Linux:命令gedit
首先,gedit是一个GNOME桌面环境下兼容UTF-8的文本编辑器.它使用GTK+编写而成,因此它十分的简单易用,有良好的语法高亮,对中文支持很好,支持包括gb2312.gbk在内的多种字符编码. ...
- bzoj2935 [Poi1999]原始生物——欧拉回路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2935 考察欧拉回路性质的题目呢: TJ:https://blog.csdn.net/u014 ...
- K-means algorithm----PRML读书笔记
The K-means algorithm is based on the use of squared Euclidean distance as the measure of dissimila ...
- Find Minimum in Rotated Sorted Array 典型二分查找
https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...