BZOJ 3531: [Sdoi2014]旅行 (树剖+动态开点线段树)
对于每种信仰维护一棵动态开点线段树就行了…
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
inline void read(int &num) {
char ch; int flg=1; while(!isdigit(ch=getchar()))if(ch=='-')flg=-flg;
for(num=0; isdigit(ch); num=num*10+ch-'0',ch=getchar()); num*=flg;
}
const int MAXN = 100005;
const int INF = 1e9;
int n, m, cnt, tmr, fir[MAXN], fa[MAXN], dfn[MAXN], top[MAXN], sz[MAXN], son[MAXN], dep[MAXN];
struct edge { int to, nxt; }e[MAXN<<1];
inline void Add(int u, int v) {
e[cnt] = (edge){ v, fir[u] }, fir[u] = cnt++;
}
void dfs(int x) {
dep[x] = dep[fa[x]] + (sz[x]=1);
for(int v, i = fir[x]; ~i; i = e[i].nxt)
if((v=e[i].to) != fa[x]) {
fa[v] = x, dfs(v), sz[x] += sz[v];
if(sz[v] > sz[son[x]]) son[x] = v;
}
}
void dfs2(int x, int tp) {
top[x] = tp; dfn[x] = ++tmr;
if(son[x]) dfs2(son[x], tp);
for(int v, i = fir[x]; ~i; i = e[i].nxt)
if((v=e[i].to) != son[x] && v != fa[x]) dfs2(v, v);
}
namespace SegmentTree {
struct seg {
seg *ls, *rs;
int mx, sum;
inline void* operator new(size_t, seg *_ls, seg *_rs, int _mx, int _sum) {
static seg *mempool, *C;
if(mempool == C) mempool = (C = new seg[1<<15]) + (1<<15);
C->ls = _ls;
C->rs = _rs;
C->mx = _mx;
C->sum = _sum;
return C++;
}
}*rt[MAXN];
inline void update(seg *x) {
x->mx = x->sum = 0;
if(x->ls) x->mx = max(x->mx, x->ls->mx), x->sum += x->ls->sum;
if(x->rs) x->mx = max(x->mx, x->rs->mx), x->sum += x->rs->sum;
}
void insert(seg *&x, int l, int r, int pos, int val) {
if(!x) x = new(0x0, 0x0, 0, 0) seg;
if(l == r) {
x->sum = x->mx = val;
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) insert(x->ls, l, mid, pos, val);
else insert(x->rs, mid+1, r, pos, val);
update(x);
}
int querysum(seg *p, int l, int r, int x, int y) {
if(!p) return 0;
if(l == x && r == y) return p->sum;
int mid = (l + r) >> 1;
if(y <= mid) return querysum(p->ls, l, mid, x, y);
else if(x > mid) return querysum(p->rs, mid+1, r, x, y);
else return querysum(p->ls, l, mid, x, mid) + querysum(p->rs, mid+1, r, mid+1, y);
}
int querymx(seg *p, int l, int r, int x, int y) {
if(!p) return 0;
if(l == x && r == y) return p->mx;
int mid = (l + r) >> 1;
if(y <= mid) return querymx(p->ls, l, mid, x, y);
else if(x > mid) return querymx(p->rs, mid+1, r, x, y);
else return max(querymx(p->ls, l, mid, x, mid), querymx(p->rs, mid+1, r, mid+1, y));
}
}
inline int Sum(SegmentTree::seg *r, int x, int y) {
int res = 0;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
res += SegmentTree::querysum(r, 1, n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
res += SegmentTree::querysum(r, 1, n, dfn[y], dfn[x]);
return res;
}
inline int Max(SegmentTree::seg *r, int x, int y) {
int res = 0;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
res = max(res, SegmentTree::querymx(r, 1, n, dfn[top[x]], dfn[x]));
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
res = max(res, SegmentTree::querymx(r, 1, n, dfn[y], dfn[x]));
return res;
}
int c[MAXN], w[MAXN];
int main() {
memset(fir, -1, sizeof fir);
read(n), read(m);
for(int i = 1; i <= n; ++i) read(w[i]), read(c[i]);
for(int x, y, i = 1; i < n; ++i) {
read(x), read(y);
Add(x, y), Add(y, x);
}
dfs(1); dfs2(1, 1);
for(int i = 1; i <= n; ++i) SegmentTree::insert(SegmentTree::rt[c[i]], 1, n, dfn[i], w[i]);
int x, y; char s[2];
while(m--) {
while(!isalpha(s[0] = getchar())); s[1] = getchar();
read(x), read(y);
if(s[0] == 'C' && s[1] == 'W') SegmentTree::insert(SegmentTree::rt[c[x]], 1, n, dfn[x], w[x]=y);
else if(s[0] == 'C' && s[1] == 'C') SegmentTree::insert(SegmentTree::rt[c[x]], 1, n, dfn[x], 0), c[x] = y, SegmentTree::insert(SegmentTree::rt[c[x]], 1, n, dfn[x], w[x]);
else if(s[0] == 'Q' && s[1] == 'S') printf("%d\n", Sum(SegmentTree::rt[c[x]], x, y));
else printf("%d\n", Max(SegmentTree::rt[c[x]], x, y));
}
}
BZOJ 3531: [Sdoi2014]旅行 (树剖+动态开点线段树)的更多相关文章
- BZOJ3531 树剖 + 动态开点线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=3531 首先这题意要求树链上的最大值以及求和,其树链剖分的做法已经昭然若揭 问题在于这次的信息有宗教 ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...
- BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树
题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...
- [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...
- bzoj3531: [Sdoi2014]旅行 (树链剖分 && 动态开点线段树)
感觉动态开点线段树空间复杂度好优秀呀 树剖裸题 把每个宗教都开一颗线段树就可以了 但是我一直TLE 然后调了一个小时 为什么呢 因为我 #define max(x, y) (x > y ? x ...
- 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)
题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...
- bzoj 3531: [Sdoi2014]旅行
Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. ...
- NFLSOJ #917 -「lych_cys模拟题2018」橘子树(树剖+ODT+莫反统计贡献的思想+动态开点线段树)
题面传送门 sb 出题人不在题面里写 \(b_i=0\) 导致我挂成零蛋/fn/fn 首先考虑树链剖分将路径问题转化为序列上的问题,因此下文中简称"位置 \(i\)"表示 DFS ...
- [ZJOI2019]语言(树链剖分+动态开点线段树+启发式合并)
首先,对于从每个点出发的路径,答案一定是过这个点的路径所覆盖的点数.然后可以做树上差分,对每个点记录路径产生总贡献,然后做一个树剖维护,对每个点维护一个动态开点线段树.最后再从根节点开始做一遍dfs, ...
随机推荐
- pyhanlp 繁简转换之拼音转换与字符正则化
繁简转换 HanLP几乎实现了所有我们需要的繁简转换方式,并且已经封装到了HanLP中,使得我们可以轻松的使用,而分词器中已经默认支持多种繁简格式或者混合.这里我们不再做过多描述. ·说明 · Han ...
- web/服务器知识
一 PV 推到出 QPS 你想建设一个能承受500万PV/每天的网站吗? 500万PV是什么概念?服务器每秒要处理多少个请求才能应对?如果计算呢?? PV是什么:PV是page view的简写.PV是 ...
- Spring4学习回顾之路06- IOC容器中Bean的生命周期方法
SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行特定的任务! Spring IOC容器对Bean的生命周期进行管理的过程: -通过构造器或者工厂方法创建 ...
- 洛谷 P2018 消息传递 题解
题面 总体来说是一道从下往上的DP+贪心: 设f[i]表示将消息传给i,i的子树全部接收到所能消耗的最小时间: 那么对于i的所有亲儿子节点j,我们会贪心地先给f[j]大的人传递,然后次大..... 可 ...
- PAT A1005 Spell It Right (20)
书中AC代码 #include <cstdio> #include <cstring> #include <iostream> char num[10][10] = ...
- Python学习8——魔法方法、特性和迭代器
Python中很多名称比较古怪,开头和结尾都是两个下划线.这样的拼写表示名称有特殊意义,因此绝不要在程序中创建这样的名称.这样的名称中大部分都是魔法(方法)的名称.如果你的对象实现了这些方法,他们将在 ...
- Bean属性复制,字段名可不同,字段类型不同需要自行处理
@Setter @Getter public class SourceA { private String name; private String text; public SourceA(Stri ...
- WebFont技术使用之如何在app中使用自定义字体
参考 H5自定义字体解决方法(mark) 移动Web字体的使用 [原]移动web页面使用字体的思考 CSS @font-face规则 引用外部服务器字体
- python3.3.2中的关键字(转)
The following identifiers are used as reserved words, or keywords of the language, and cannot be use ...
- Spring中常用的设计模式之:代理模式
看了tom老师讲的深入分析spring源码,讲的挺好,做个小总结 代理模式的定义: 为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以 ...