题意

题目链接

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. Tools - 浏览器Firefox

    简介 http://www.mozilla.org/ 中文官网:http://www.firefox.com.cn/ https://www.mozilla.org/zh-CN/firefox/ Mo ...

  2. 【disruptor】2、disruptor中生产者线程与消费者之间的协调

    由于ringbuffer是一个环形的队列,那么生产者和消费者在遍历这个队列的时候,如何制衡呢? 1.生产快,消费慢,数据丢失? 生产者速度过快,导致一个对象还没消费完,就循环生产了一个新的对象要加入r ...

  3. QQ gtk,bkn算法

    public long GetGTK(string sKey) { ; , len = sKey.Length; i < len; ++i) { hash += (hash << ) ...

  4. MVC3学习:将excel文件导入到sql server数据库

    思路: 1.将excel文件导入到服务器中. 2.读取excel文件,转换成dataset. 3.循环将dataset数据插入到数据库中. 本例子使用的表格为一个友情链接表F_Link(LinkId, ...

  5. (转)X-Frame-Options响应头缺失漏洞

    原文:https://blog.csdn.net/ljl890705/article/details/78071601 x-frame-options响应头缺失漏洞. 故名思意,就是返回的响应头信息中 ...

  6. Bash算术运算

    使用let命令 let let let let let let let 使用expr命令 - ` # + ` # \* ` # / ` # / ` # − \* ` # +` # + -*· # -* ...

  7. spring@Transactional注解事务不回滚不起作用无效的问题处理

    这几天在项目里面发现我使用@Transactional注解事务之后,抛了异常居然不回滚.后来终于找到了原因. 如果你也出现了这种情况,可以从下面开始排查. 一.特性先来了解一下@Transaction ...

  8. axios跨域post请求后台参数为null

    查了一个小时的资料,大部分都是说设置headers  可能是我查找的关键词不对吧,最后找到一篇文章,把问题解决了 前端解决方式 官方文档 后台解决方式 教程参考地址:http://blog.csdn. ...

  9. MyCat配置文件详解--server.xml

    server.xml包含mycat的系统配置信息,它有两个标签,分别是user和system,掌握system标签的各项配置属性是mycat调优的关键. <?xml version=" ...

  10. Eclipse 下Spring cloud项目集成开发插件Spring Tool Suite (STS) 安装

    安装eclipse插件 Help->Eclipse Marketplace-> Search中查找"Spring Tool Suite (STS) for Eclipse&quo ...