题意

题目链接

Sol

树链剖分板子 + 动态开节点线段树板子

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int ch(int x, int l, int r) {
return x >= l && x <= r;
}
int N, Q, C[MAXN], W[MAXN], dep[MAXN], fa[MAXN], son[MAXN], siz[MAXN], top[MAXN], id[MAXN], cnt;
vector<int> v[MAXN];
void dfs1(int x, int _fa) {
dep[x] = dep[_fa] + 1; siz[x] = 1; fa[x] = _fa;
for(auto &to : v[x]) {
if(to == _fa) continue;
dfs1(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void dfs2(int x, int topf) {
top[x] = topf; id[x] = ++cnt;
if(!son[x]) return ;
dfs2(son[x], topf);
for(auto &to : v[x]) {
if(top[to]) continue;
dfs2(to, to);
}
}
const int SS = (3e6 + 10);
int root[MAXN], ls[SS], rs[SS], mx[SS], sum[SS], tot;
void update(int k) {
mx[k] = max(mx[ls[k]], mx[rs[k]]);
sum[k] = sum[ls[k]] + sum[rs[k]];
}
void Modify(int &k, int l, int r, int p, int v) {
if(!k) k = ++tot;
if(l == r) {sum[k] = v, mx[k] = v; return ;}
int mid = l + r >> 1;
if(p <= mid) Modify(ls[k], l, mid, p, v);
if(p > mid) Modify(rs[k], mid + 1, r, p, v);
update(k);
}
int Query(int k, int l, int r, int ql, int qr, int opt) {
if(ql <= l && r <= qr) return opt == 0 ? mx[k] : sum[k];
int mid = l + r >> 1;
if(ql > mid) return Query(rs[k], mid + 1, r, ql, qr, opt);
else if(qr <= mid) return Query(ls[k], l, mid, ql, qr, opt);
else return opt == 0 ? max(Query(ls[k], l, mid, ql, qr, opt), Query(rs[k], mid + 1, r, ql, qr, opt))
: Query(ls[k], l, mid, ql, qr, opt) + Query(rs[k], mid + 1, r, ql, qr, opt);
}
int TreeMax(int x, int y) {
int ans = -INF, p = x;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
chmax(ans, Query(root[C[p]], 1, N, id[top[x]], id[x], 0));
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
chmax(ans, Query(root[C[p]], 1, N, id[y], id[x], 0));
return ans;
}
int TreeSum(int x, int y) {
int ans = 0, p = x;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
ans += Query(root[C[p]], 1, N, id[top[x]], id[x], 1);
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
ans += Query(root[C[p]], 1, N, id[y], id[x], 1);
return ans;
}
signed main() {
N = read(); Q = read();
for(int i = 1; i <= N; i++) W[i] = read(), C[i] = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
dfs1(1, 0);
dfs2(1, 1);
for(int i = 1; i <= N; i++)
Modify(root[C[i]], 1, N, id[i], W[i]);
while(Q--) {
char s[4];
scanf("%s", s);
int x = read(), c = read();
if(s[0] == 'C' && s[1] == 'C') {
Modify(root[C[x]], 1, N, id[x], 0);
Modify(root[c], 1, N, id[x], W[x]);
C[x] = c;
}
if(s[0] == 'C' && s[1] == 'W') {
Modify(root[C[x]], 1, N, id[x], c);
W[x] = c;
}
if(s[0] == 'Q' && s[1] == 'M') {
printf("%d\n", TreeMax(x, c));
}
if(s[0] == 'Q' && s[1] == 'S') {
printf("%d\n", TreeSum(x, c));
}
}
return 0;
}
/*
5 6
3 1
2 3
1 2
3 3
5 1
1 2
1 3
3 4
3 5
QS 1 5
CC 3 1
QS 1 5
CW 3 3
QS 1 5
QM 2 4
*/

洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)的更多相关文章

  1. 【bzoj4999】This Problem Is Too Simple! 树链剖分+动态开点线段树

    题目描述 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x<2^31) ...

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

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

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

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

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

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

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

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

  6. 【BZOJ3531】[Sdoi2014]旅行 树链剖分+动态开点线段树

    [BZOJ3531][Sdoi2014]旅行 Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天 ...

  7. bzoj3531——树链剖分+动态开点线段树

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MB Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连 ...

  8. BZOJ4999: This Problem Is Too Simple!树链剖分+动态开点线段树

    题目大意:将某个节点的颜色变为x,查询i,j路径上多少个颜色为x的点... 其实最开始一看就是主席树+树状数组+DFS序...但是过不去...MLE+TLE BY FCWWW 其实树剖裸的一批...只 ...

  9. 洛谷P4632 [APIO2018] New Home 新家(动态开节点线段树 二分答案 扫描线 set)

    题意 题目链接 Sol 这题没有想象中的那么难,但也绝对不简单. 首先把所有的询问离线,按照出现的顺序.维护时间轴来处理每个询问 对于每个询问\((x_i, y_i)\),可以二分答案\(mid\). ...

随机推荐

  1. karma 启动提示PhantomJS not found on PATH

    Karma 介绍:是由AngularJS团队开发的测试执行过程管理实用工具,帮助开发人员在不同的浏览器中执行测试. 一般搭配PhantomJS作为浏览器启动器.PhantomJS是一个非主流的Webk ...

  2. POJ 2606

    #include<iostream> #include<set> #include<stdio.h> #include<math.h> #include ...

  3. rabbitmq系列五 之主题交换机

    1.主题 在前面的例子中,我们对日志系统进行了改进.使用了direct交换机代替了fanout交换机,从只能盲目的广播消息改进为有可能选择性的接收日志. 尽管直接交换机能够改善我们的日志系统,但是它也 ...

  4. 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)——2.Programming Assignments: Building your Deep Neural Network: Step by Step

    Building your Deep Neural Network: Step by Step Welcome to your third programming exercise of the de ...

  5. String、StringBuffer与StringBuilder之间区别 (转载)

    最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringBuilder的东西,现在整理一下. 关于这三个类在字符串处理中的位置不言而喻,那 ...

  6. 关于C++11中的std::move和std::forward

    std::move是一个用于提示优化的函数,过去的c++98中,由于无法将作为右值的临时变量从左值当中区别出来,所以程序运行时有大量临时变量白白的创建后又立刻销毁,其中又尤其是返回字符串std::st ...

  7. 监控 Redis 服务方案

    RedisLive easy_install pip wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate python g ...

  8. jenkins持续集成的步骤

    项目的持续集成分享 源代码管理 项目仓库 配置仓库 发布仓库 ci/cd相关 gitlab,管理版本,测试流水线 jenkins,对项目进行持续集成 各模块的关系 graph TD a(jenkins ...

  9. 利用:before和:after伪类制作CSS3 圆形按钮 含demo

    要求 必备知识 基本了解CSS语法,初步了解CSS3语法知识. 开发环境 Adobe Dreamweaver CS6 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 制作步骤: 一 ...

  10. 高可用Hadoop平台-探索

    1.概述 上篇<高可用Hadoop平台-启航>博客已经让我们初步了解了Hadoop平台:接下来,我们对Hadoop做进一步的探索,一步一步的揭开Hadoop的神秘面纱.下面,我们开始赘述今 ...