对于每种信仰维护一棵动态开点线段树就行了…

#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]旅行 (树剖+动态开点线段树)的更多相关文章

  1. BZOJ3531 树剖 + 动态开点线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=3531 首先这题意要求树链上的最大值以及求和,其树链剖分的做法已经昭然若揭 问题在于这次的信息有宗教 ...

  2. CF915E Physical Education Lessons 动态开点线段树

    题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...

  3. BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树

    题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...

  4. [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...

  5. bzoj3531: [Sdoi2014]旅行 (树链剖分 && 动态开点线段树)

    感觉动态开点线段树空间复杂度好优秀呀 树剖裸题 把每个宗教都开一颗线段树就可以了 但是我一直TLE 然后调了一个小时 为什么呢 因为我 #define max(x, y) (x > y ? x ...

  6. 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)

    题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...

  7. bzoj 3531: [Sdoi2014]旅行

    Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. ...

  8. NFLSOJ #917 -「lych_cys模拟题2018」橘子树(树剖+ODT+莫反统计贡献的思想+动态开点线段树)

    题面传送门 sb 出题人不在题面里写 \(b_i=0\) 导致我挂成零蛋/fn/fn 首先考虑树链剖分将路径问题转化为序列上的问题,因此下文中简称"位置 \(i\)"表示 DFS ...

  9. [ZJOI2019]语言(树链剖分+动态开点线段树+启发式合并)

    首先,对于从每个点出发的路径,答案一定是过这个点的路径所覆盖的点数.然后可以做树上差分,对每个点记录路径产生总贡献,然后做一个树剖维护,对每个点维护一个动态开点线段树.最后再从根节点开始做一遍dfs, ...

随机推荐

  1. java23种设计模式之四:建造者模式

    在软件开发过程中有时需要创建一个复杂的对象,这个复杂对象通常由多个子部件按一定的步骤组合而成.例如:在新招收一个员工时,对个人信息对象的创建,在不同的阶段,需要个人信息的内容也不一样,姓名.性别.年龄 ...

  2. 查看Oracle索引是否被使用或者有效

    第一步: explain plan for select * from T_USER where OPEN_ID='12345'; 第二步: select * from table(dbms_xpla ...

  3. idea快捷生成

    列模式: shift+alt+insert进入列选择模式,可选择一列或者多列 shift+alt+insert退出列选择模式 大小写转换: Ctrl + Shift + U 循环: iter  增强版 ...

  4. win32多线程: 线程创建与结束等待

    #include<Windows.h> #include<iostream> using namespace std; /*1.在启动一个线程之前,必须为线程编写一个全局的线程 ...

  5. C语言一行太长的换行处理

    转载:https://blog.csdn.net/baiqishijkh/article/details/79236202 在C语言程序编写中,我们有时会遇到一行代码太长而影响阅读或者出现与部分公司或 ...

  6. php修改替换数据库图片(文件)

    <?php extract($_POST); $date = date('Y-m-d'); $file_name = $_FILES['image']['name'];//获取缓存区图片,格式不 ...

  7. php通过session来实现登录验证

    1.概述 这几天在做一个内部统计管理系统,所有内容需要登录后才能查看.这就需要系统内部每个模块都有登录验证的功能.在网上找了一圈资料,决定用session来做. 2.系统概况 后端语言:php(用的是 ...

  8. 嵌套泛型参数IList<IList<Object>>如何传参

    在调用第三方库的时候,有这么一个泛型参数,如下图: 按照经验,使用两个List嵌套声明变量即可: IList<IList<AnnotatedPoint2D>>  outImag ...

  9. Ruby 参考教程

    Ruby 参考教程 https://www.ruby-lang.org/zh_cn/documentation/ http://ruby-doc.org/docs/ https://ruby-chin ...

  10. java代码实现图片内容转文字

    前言 现在的手机已经可以实现拍照转文字了.作为一名程序员,得使用java代码实现这一功能,虽然可能没啥用!!! pom.xml 添加依赖 <dependency> <groupId& ...