【题解】SDOI2014旅行
大概是一道树链剖分的裸题。可以看出如果不是查询相同宗教的这一点,就和普通的树链剖分毫无两样了。所以针对每一个宗教都单独开一棵线段树,变成单点修改+区间查询。只不过宗教数目很多,空间消耗太大所以只能开一棵总的再动态开点。
#include <bits/stdc++.h>
using namespace std;
#define maxn 200500
#define maxm 2000000
int n, q, cnp = , cnt, tot, root[maxn], name[maxn], head[maxn], w[maxn], c[maxn];
struct node
{
int id, size, fa, dep, hson, gra;
}P[maxn]; struct edge
{
int to, last;
}E[maxn]; struct tree
{
int sum, ext, lson, rson;
}T[maxm]; int read()
{
int x = , k = ;
char c;
c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void add(int x, int y)
{
E[cnp].to = y, E[cnp].last = head[x], head[x] = cnp ++;
} struct Segament_Tree
{
int query1(int p, int l, int r, int L, int R)
{
if(l <= L && r >= R) return T[p].sum;
if(l > R || r < L) return ;
int mid = (L + R) >> ;
return query1(T[p].lson, l, r, L, mid) + query1(T[p].rson, l, r, mid + , R);
} int query2(int p, int l, int r, int L, int R)
{
if(l <= L && r >= R) return T[p].ext;
if(l > R || r < L) return ;
int mid = (L + R) >> ;
return max(query2(T[p].lson, l, r, L, mid), query2(T[p].rson, l, r, mid + , R));
} void update(int &p, int L, int R, int x, int w)
{
if(!p) p = ++ cnt;
if(L == R)
{
T[p].sum = T[p].ext = w;
return;
}
int mid = (L + R) >> ;
if(x <= mid) update(T[p].lson, L, mid, x, w);
else update(T[p].rson, mid + , R, x, w);
T[p].sum = T[T[p].lson].sum + T[T[p].rson].sum;
T[p].ext = max(T[T[p].lson].ext, T[T[p].rson].ext);
} }ST[maxn]; struct Heavy_Light_Decomposition
{
int dfs(int u)
{
P[u].size = ;
for(int i = head[u]; i; i = E[i].last)
{
int v = E[i].to;
if(v == P[u].fa) continue;
P[v].dep = P[u].dep + , P[v].fa = u;
dfs(v);
P[u].size += P[v].size;
if(P[v].size > P[P[u].hson].size) P[u].hson = v;
}
} int dfs2(int u, int anc)
{
P[u].id = ++ tot, P[u].gra = anc, name[tot] = u;
if(P[u].hson) dfs2(P[u].hson, anc);
for(int i = head[u]; i; i = E[i].last)
{
int v = E[i].to;
if(v == P[u].hson || v == P[u].fa) continue;
dfs2(v, v);
}
} void update1(int x, int r)
{
ST[c[x]].update(root[c[x]], , tot, P[x].id, );
ST[r].update(root[r], , tot, P[x].id, w[x]);
c[x] = r;
} void update2(int x, int t)
{
ST[c[x]].update(root[c[x]], , tot, P[x].id, t);
w[x] = t;
} void query1(int x, int y)
{
int tx = P[x].gra, ty = P[y].gra;
int r = c[x];
int ans = ;
while(tx != ty)
{
if(P[tx].dep < P[ty].dep) swap(x, y), swap(tx, ty);
ans += ST[r].query1(root[r], P[tx].id, P[x].id, , tot);
x = P[tx].fa, tx = P[x].gra;
}
if(P[x].dep < P[y].dep) swap(x, y), swap(tx, ty);
ans += ST[r].query1(root[r], P[y].id, P[x].id, , tot);
printf("%d\n", ans);
} void query2(int x, int y)
{
int tx = P[x].gra, ty = P[y].gra;
int r = c[x];
int ans = ;
while(tx != ty)
{
if(P[tx].dep < P[ty].dep) swap(x, y), swap(tx, ty);
ans = max(ans, ST[r].query2(root[r], P[tx].id, P[x].id, , tot));
x = P[tx].fa, tx = P[x].gra;
}
if(P[x].dep < P[y].dep) swap(x, y), swap(tx, ty);
ans = max(ans, ST[r].query2(root[r], P[y].id, P[x].id, , tot));
printf("%d\n", ans);
} }HLD; int main()
{
n = read(), q = read();
for(int i = ; i <= n; i ++)
w[i] = read(), c[i] = read();
for(int i = ; i < n; i ++)
{
int x = read(), y = read();
add(x, y), add(y, x);
}
HLD.dfs(), HLD.dfs2(, );
for(int i = ; i < maxn; i ++) root[i] = ++ cnt;
for(int i = ; i <= n; i ++) ST[c[i]].update(root[c[i]], , tot, P[i].id, w[i]);
for(int i = ; i <= q; i ++)
{
string s;
cin >> s;
int x = read(), y = read();
if(s[] == 'C')
{
if(s[] == 'C') HLD.update1(x, y);
else HLD.update2(x, y);
}
else
{
if(s[] == 'S') HLD.query1(x, y);
else HLD.query2(x, y);
}
}
return ;
}
【题解】SDOI2014旅行的更多相关文章
- 【BZOJ3531】[Sdoi2014]旅行 树链剖分+动态开点线段树
[BZOJ3531][Sdoi2014]旅行 Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天 ...
- BZOJ 3531: [Sdoi2014]旅行 [树链剖分]
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1685 Solved: 751[Submit][Status] ...
- bzoj 3531 [Sdoi2014]旅行(树链剖分,线段树)
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 876 Solved: 446[Submit][Status][ ...
- [SDOI2014]旅行
洛谷 P3313 [SDOI2014]旅行 https://www.luogu.org/problem/show?pid=3313 题目描述 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接 ...
- B20J_3231_[SDOI2014]旅行_树链剖分+线段树
B20J_3231_[SDOI2014]旅行_树链剖分+线段树 题意: S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,城市信仰不同的宗教,为了方便,我们用不同的正整数代表各种宗教. S国 ...
- [luogu P3313] [SDOI2014]旅行
[luogu P3313] [SDOI2014]旅行 题目描述 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神 ...
- 洛谷 P3313 [SDOI2014]旅行 解题报告
P3313 [SDOI2014]旅行 题目描述 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教 ...
- bzoj 3531 [Sdoi2014]旅行 (树剖+线段树 动态开点)
3531: [Sdoi2014]旅行 Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2984 Solved: 1312[Submit][Status ...
- P3313 [SDOI2014]旅行
P3313 [SDOI2014]旅行 树链剖分+动态线段树(并不是lct) 显然的,我们对于每一个宗教都要维护一个线段树. (那么空间不是爆炸了吗) 在这里引入:动态开点线段树 就是需要的点开起来,不 ...
- 3531: [Sdoi2014]旅行
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 1731 Solved: 772 [Submit][Statu ...
随机推荐
- I/O流、ZIP文档
1) ZIP文档通常以压缩格式存储一个或多个文档.在Java中可以用ZipInputStream读入ZIP文档(即解压文件流),用ZipOutputStream写入ZIP文档(即压缩文件流),无论解压 ...
- Ruby字符串的一些方法
最近因为公司需求开始看ruby,先从ruby的基本数据类型开始看 看到ruby的字符串类型string,发现ruby中的字符串单双引号是不一样的,这点和Python有那么点不一样 主要是我们对字符串进 ...
- ruby Encoding
一. 查看ruby支持的编码 Encoding.name_list 二. 搜索编码 Encoding.find('US-ASCII') #=> US-ASCII,不存在则抛出异常 三. __EN ...
- 基于STM32F103的Max30100心率、血氧检测代码(转载)
MAX30100是能够读取心率.血氧的传感器,通信方式是通过IIC进行通信.其工作原理是通过红外led灯照射,能够得到心率的ADC值. MAX30100的寄存器可以分为五类,状态寄存器.F ...
- 51定时器控制4各led,使用回调函数机制
程序转载自51hei,经过自己的实际验证,多了一种编程的思路技能,回调函数的基本思想也是基于事件机制的,哪个事件来了, 就执行哪个事件. 程序中,最多四个子定时器,说明51的处理速度是不够的,在中断中 ...
- GIT LFS 使用笔记
一.背景 由于git上传文件大小受限,所以我们需要使用GIT LFS对大小超过一定上限的大文件进行处理. 二.安装 linux上安装参见 https://askubuntu.com/questions ...
- Java8新特性(三)——Optional类、接口方法与新时间日期API
一.Optional容器类 这是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象. 查看结构图可以看到有如下常用方法: of(T)—— ...
- Cyclone IV器件的逻辑单元和逻辑阵列快
1. 逻辑单元 (LE) 在 Cyclone IV 器件结构中是最小的逻辑单位.LE 紧密且有效的提供了高级功能的逻辑使用.每个 LE 有以下特性:一个四口输入的查找表 (LUT),以实现四种变量的任 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- 「日常训练」 Soldier and Number Game (CFR304D2D)
题意 (Codeforces 546D) 给定一个数x=a!b!" role="presentation">x=a!b!x=a!b!的形式,问其中有几个质因数. 分 ...