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类型的物品.完成所有发放后,每个点存放最 ...
随机推荐
- Codeforces Round #377 (Div. 2) C. Sanatorium 水题
C. Sanatorium time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- CSS笔记(十一)CSS3之边框
参考:http://www.w3school.com.cn/css3/css3_border.asp 圆角边框 <!DOCTYPE html> <html> <head& ...
- More Effective C++ (2)
接下来的是more effective c++ 11至20条款: 11.禁止异常信息(exceptions)传递到析构函数外.析构函数的调用情况可能有两种:(1)对象正常销毁 (2)异常传播过程中的栈 ...
- 学会使用JDK API
api是字典,知识过了一遍之后,剩下的就是实践+百度+api了
- Java源码初学_HashSet&LinkedHashSet
一.概述 HashSet是建立在HashMap的基础上的,其内部存在指向一个HashMap对象的引用,操作HashSet实际上就是操作HashMap,而HashMap中所有键的值都指向一个叫做Dumm ...
- epoll的lt和et模式的实验
针对epoll api的两种触发模式,lt和et,仿照一些例子写了代码进行实验. #include <sys/types.h> #include <sys/socket.h> ...
- ajax跨域jsonp
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8&qu ...
- Android事件传递机制(转)
Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和多指操作.所有这些都构成了Android中的事件响应.总的来说,所有的事件都 ...
- Spring MVC 之请求处理方法可接收参数(三)
请求处理方法可接收参数 今天学习了前三个方法. 1.作用域对象2.单个表单提交数据3.表单数据封装的Bean对象 首先创建一个实体对象. package com.cy.springannotation ...
- JMX 简介 和 实例
What is JMX? 10 mins Quick Start JMX Tutorial 这篇文章作为入门,非常清楚