BZOJ3307 雨天的尾巴
首先考虑序列怎么做。。。
只要把操作差分了,记录在每个点上
然后维护一棵权值线段树,表示每个颜色出现的次数,支持单点修改和查询最大值操作
只要把序列扫一遍就好了,时间复杂度$O(n + m*logZ)$,其中$n$表述序列长度,$m$表示操作次数,$Z$表示颜色集合大小
于是树形的时候,先树链剖分,然后把操作离线,对每一条链都扫一遍就好了,时间复杂度$O(n + m*logn*logZ)$
/**************************************************************
Problem: 3307
User: rausen
Language: C++
Result: Accepted
Time:5120 ms
Memory:86068 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
const int N = 1e5 + ;
const int Z = 1e9 + ; inline int read(); struct seg {
seg *ls, *rs;
int mx, wmx;
int tag; seg() {} #define Len (1 << 16)
inline void* operator new(size_t) {
static seg *mempool, *c;
if (mempool == c)
mempool = (c = new seg[Len]) + Len;
return c++;
}
#undef Len
inline void operator = (const seg &s) {
mx = s.mx, wmx = s.wmx;
} inline void update() {
if (!ls && !rs) this -> mx = ;
else if (!ls || !rs) *this = (ls ? *ls : *rs);
else if (ls -> mx >= rs -> mx) *this = *ls;
else *this = *rs;
}
inline void clear() {
tag = , mx = ;
}
inline void push() {
if (tag) {
if (ls) ls -> clear();
if (rs) rs -> clear();
tag = ;
}
} #define mid (l + r >> 1)
void modify(int l, int r, int pos, int d) {
if (l == r) {
mx += d, wmx = l;
return;
}
push();
if (pos <= mid) {
if (!ls) ls = new()seg;
ls -> modify(l, mid, pos, d);
} else {
if (!rs) rs = new()seg;
rs -> modify(mid + , r, pos, d);
}
update();
}
#undef mid
} *T; struct edge {
int next, to;
edge(int _n = , int _t = ) : next(_n), to(_t) {}
} e[N << ];
int first[N], tot; struct oper {
int next, z, d;
oper(int _n = , int _z = , int _d = ) : next(_n), z(_z), d(_d) {}
} op[N << ];
int First[N], tot_op; struct tree_node {
int fa, son, top;
int sz, dep;
} tr[N]; int n, m;
int ans[N]; inline void Add_Edges(int x, int y) {
e[++tot] = edge(first[x], y), first[x] = tot;
e[++tot] = edge(first[y], x), first[y] = tot;
} #define y e[x].to
void dfs(int p) {
int x;
tr[p].sz = ;
for (x = first[p]; x; x = e[x].next)
if (y != tr[p].fa) {
tr[y].fa = p, tr[y].dep = tr[p].dep + ;
dfs(y);
tr[p].sz += tr[y].sz;
if (tr[tr[p].son].sz < tr[y].sz) tr[p].son = y;
}
} void DFS(int p) {
int x;
if (!tr[p].son) return;
tr[tr[p].son].top = tr[p].top, DFS(tr[p].son);
for (x = first[p]; x; x = e[x].next)
if (y != tr[p].fa && y != tr[p].son)
tr[y].top = y, DFS(y);
}
#undef y inline void Add_oper(int x, int y, int z) {
y = tr[y].son;
op[++tot_op] = oper(First[x], z, ), First[x] = tot_op;
op[++tot_op] = oper(First[y], z, -), First[y] = tot_op;
} inline void work(int x, int y, int z) {
while (tr[x].top != tr[y].top) {
if (tr[tr[x].top].dep < tr[tr[y].top].dep) swap(x, y);
Add_oper(tr[x].top, x, z);
x = tr[tr[x].top].fa;
}
if (tr[x].dep < tr[y].dep) swap(x, y);
Add_oper(y, x, z);
} void get_ans(int p) {
int x;
for (x = First[p]; x; x = op[x].next)
T -> modify(, Z, op[x].z, op[x].d);
ans[p] = T -> mx == ? : T -> wmx;
if (tr[p].son) get_ans(tr[p].son);
} int main() {
int i, x, y, z;
n = read(), m = read();
for (i = ; i < n; ++i) Add_Edges(read(), read());
dfs();
tr[].top = , DFS();
for (i = ; i <= m; ++i) {
x = read(), y = read(), z = read();
work(x, y, z);
}
T = new()seg;
for (i = ; i <= n; ++i)
if (tr[i].top == i) {
T -> clear();
get_ans(i);
}
for (i = ; i <= n; ++i)
printf("%d\n", ans[i]);
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}
(p.s. 窝比较懒,所以没有把颜色离散化,直接搞了动态开点线段树)
BZOJ3307 雨天的尾巴的更多相关文章
- [BZOJ3307] 雨天的尾巴(树上差分+线段树合并)
[BZOJ3307] 雨天的尾巴(树上差分+线段树合并) 题面 给出一棵N个点的树,M次操作在链上加上某一种类别的物品,完成所有操作后,要求询问每个点上最多物品的类型. N, M≤100000 分析 ...
- [bzoj3307]雨天的尾巴_线段树合并
雨天的尾巴 bzoj-3307 题目大意:N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. ...
- BZOJ3307雨天的尾巴——线段树合并
题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入 第一行数字N,M接下来N ...
- [洛谷P4556][BZOJ3307]雨天的尾巴-T3订正
线段树合并+树上差分 题目链接(···) 「简单」「一般」——其实「一般」也只多一个离散化 考试时想法看>这里< 总思路:先存所有的操作,离散化,然后用树上差分解决修改,用权值线段树维护每 ...
- [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)
题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...
- bzoj3307 雨天的尾巴 题解(线段树合并+树上差分)
Description N个点,形成一个树状结构.有M次发放,每次选择两个点x,y 对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成 所有发放后,每个点存放最多的是哪种物品. Input ...
- bzoj3307雨天的尾巴(权值线段树合并/DSU on tree)
题目大意: 一颗树,想要在树链上添加同一物品,问最后每个点上哪个物品最多. 解题思路: 1.线段树合并 假如说物品数量少到可以暴力添加,且树点极少,我们怎么做. 首先在一个树节点上标记出哪些物品有多少 ...
- bzoj3307 雨天的尾巴题解及改题过程(线段树合并+lca+树上差分)
题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式 第一行数字N,M接下 ...
- [BZOJ3307] 雨天的尾巴-----------------线段树进阶
虽然是个板子,但用到了差分思想. Description N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最 ...
随机推荐
- NoSQL数据库:数据的一致性
NoSQL数据库:数据的一致性 读取一致性 强一致性 在任何时间访问集群中任一结点,得到的数据结果一致: 用户一致性 对同一用户,访问集群期间得到的数据一致: 解决用户一致性:使用粘性会话,将会话绑定 ...
- iOS - Swift NSEnumerator 迭代器
前言 public class NSEnumerator : NSObject, NSFastEnumeration 1.迭代器 let arr:NSArray = ["bei", ...
- CentOS 7 下的软件安装建议
https://seisman.info/how-to-install-softwares-under-centos-7.html https://seisman.info/linux-environ ...
- (二)ubuntu学习前传—uboot常见命令
1.uboot命令类似于linux行缓冲命令行,当我们向终端命令行输入命令的时候,这些命令没有立即被系统识别,而是被缓冲到一个缓存区(也就是系统认为我换没有输入完),当我们按下回车键(换行)后,系统就 ...
- Css_Backgroud-position(背景图片)定位问题详解
background-position的说明: 设置或检索对象的背景图像位置.必须先指定 background-image 属性.该属性定位不受对象的补丁属性( padding )设置影响. ...
- 【图像处理Matlab】2 灰度变换 imadjust stretchlim
f=imread('123.tif'); % 读入一幅图片 g=imadjust(f,[ ],[ ]); % 负片显示 [imadjust] imadjust(f, [low_in high_in], ...
- phalcon: plugin 结合Manager事件管理、dispatcher调度控制器 监听sql日志记录或其他拦截出来
可能用到的类 phalcon\mvc\use\plugin Phalcon\Mvc\Dispatcher as MvcDispatcher Phalcon\Events\Manager as Even ...
- VC++全局变量初始化
目录 第1章说明 2 1.1 程序启动 2 1.2 强符号.弱符号 2 1.3 动态初始化顺序 3 1.4 exe调用dll 4 1.5 禁用动态初始化 4 1.6 ...
- 让页面滑动流畅得飞起的新特性:Passive Event Listeners
版权声明:本文由陈志兴原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/153 来源:腾云阁 https://www.qclo ...
- VirtualBox相关问题总结
欢迎关注我的社交账号: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://gith ...