[Luogu] U18430 萌萌的大河
https://www.luogu.org/problemnew/show/U18430
思路比较好想
树链剖分
对于1操作
只需将以该点为根的子树打标记,将所有数存入数组排序
每次进行1操作时,判断若该点已经被标记过,则操作无效
对于操作2
在原树中求出lca,若lca被标记过,lca就是询问的两个点中
编号较小的那个,lca值不改变
对于操作3
若该点打过标记,输出该点与根的这条链上被打过标记的
深度最小节点编号a与该节点编号的差+a的深度
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath> using namespace std;
const int N = 1e6 + ; #define yxy getchar()
#define lson jd << 1
#define rson jd << 1 | 1 int n, Q, now = , tim, w1, w2, w3;
int head[N], Num[N], deep[N], fa[N], lnum[N], rnum[N], son[N], topp[N], siz[N], tree[N], g_min[N << ];
struct Node{int u, v, nxt;} G[N << ]; inline int read(){
int x = ; char c = yxy;
while(c < '' || c > '') c = yxy;
while(c >= '' && c <= '') x = x * + c - '', c = yxy;
return x;
} inline void add(int u, int v){
G[now].u = u; G[now].v = v; G[now].nxt = head[u]; head[u] = now ++;
} void dfs_1(int u, int father, int dep){
fa[u] = father;
deep[u] = dep;
siz[u] = ;
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != father){
dfs_1(v, u, dep + );
siz[u] += siz[v];
if(siz[v] > siz[son[u]]) son[u] = v;
}
}
} void dfs_2(int u, int tp){
topp[u] = tp;
lnum[u] = ++ tim;
tree[u] = tim;
Num[tim] = u;
if(!son[u]) {rnum[u] = tim; return ;}
dfs_2(son[u], tp);
for(int i = head[u]; ~ i; i = G[i].nxt){
int v = G[i].v;
if(v != fa[u] && v != son[u]) dfs_2(v, v);
}
rnum[u] = tim;
} void Poi_A(int l, int r, int jd, int x){
if(l == r) {
if(g_min[jd]) w1 = ;
return ;
}
if(g_min[jd]) {
g_min[lson] = g_min[jd];
g_min[rson] = g_min[jd];
}
int mid = (l + r) >> ;
if(x <= mid) Poi_A(l, mid, lson, x);
else Poi_A(mid + , r, rson, x);
} void Sec_G(int l, int r, int jd, int x, int y, int yg){
if(x <= l && r <= y){
g_min[jd] = yg;
return ;
}
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, yg);
if(y > mid) Sec_G(mid + , r, rson, x, y, yg);
} int calc_lca(int x, int y){
int tp1 = topp[x], tp2 = topp[y];
while(tp1 != tp2){
if(deep[tp1] < deep[tp2]) swap(tp1, tp2), swap(x, y);
x = fa[tp1];
tp1 = topp[x];
}
return deep[x] > deep[y] ? y : x;
} void Sec_A(int l, int r, int jd, int x, int y){
if(x <= l && r <= y){
w2 = g_min[jd];
return ;
}
if(g_min[jd]) {
g_min[lson] = g_min[jd];
g_min[rson] = g_min[jd];
}
int mid = (l + r) >> ;
if(x <= mid) Sec_A(l, mid, lson, x, y);
if(y > mid) Sec_A(mid + , r, rson, x, y);
} int get_answer(int l, int r, int x){
int L = l, R = r, ret;
while(L <= R){
int mid = (L + R) >> ;
if(Num[mid] <= x) L = mid + , ret = mid;
else R = mid - ;
}
return ret - l;
} int main()
{
n = read(); Q = read();
for(int i = ; i <= n; i ++) head[i] = -;
for(int i = ; i < n; i ++) {
int u = read(), v = read();
add(u, v); add(v, u);
}
dfs_1(, , );
dfs_2(, );
while(Q --){
int opt = read();
if(opt == ){
int x = read();
if(!son[x]) continue ;
w1 = ;
Poi_A(, n, , tree[x]);
if(w1) continue ;
Sec_G(, n, , lnum[x], rnum[x], x);
sort(Num + lnum[x] + , Num + rnum[x] + );
}
else if(opt == ){
int x = read(), y = read();
int Lca_now = calc_lca(x, y);
w1 = ;
Poi_A(, n, , tree[Lca_now]);
if(w1) printf("%d\n", min(x, y));
else printf("%d\n", Lca_now);
}
else {
int x = read();
w1 = ;
Poi_A(, n, , tree[x]);
if(!w1) printf("%d\n", deep[x]);
else {
Sec_A(, n, , lnum[x], rnum[x]);
printf("%d\n", get_answer(lnum[w2], rnum[w2], x) + deep[w2]);
}
}
}
return ;
}
[Luogu] U18430 萌萌的大河的更多相关文章
- Luogu P4144 大河的序列 贪心+脑子
首先向颜神犇致敬...还是自己太菜,又不仔细思考,上来就翻题解$qwq$ 首先有一种贪心方法:即,$ans=2*max(dirty_i)$ 证明:若现在的答案为$ans$,考虑一个新的数$x$对答案的 ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- [luogu P2647] 最大收益(贪心+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2647 题目描述 现在你面前有n个物品,编号分别为1,2,3,--,n.你可以在这当中任意选择任意多个物品. ...
- Luogu 考前模拟Round. 1
A.情书 题目:http://www.luogu.org/problem/show?pid=2264 赛中:sb题,直接暴力匹配就行了,注意一下读入和最后一句话的分句 赛后:卧槽 怎么只有40 B.小 ...
- luogu P2580 于是他错误的点名开始了
luogu P2580 于是他错误的点名开始了 https://www.luogu.org/problem/show?pid=2580 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边 ...
- html打造动画【系列1】- 萌萌的大白
每个人心中都有一个暖暖的大白,blingbling的大眼睛~软软的肚子~宽厚的肩膀~善良的心肠~如果可以,我愿意沦陷在大白的肚子里永远不出来,哈哈~毛球要失宠咯~ 哈哈哈 每个人都是独立的个体,大白也 ...
- CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)
CJOJ 1331 [HNOI2011]数学作业 / Luogu 3216 [HNOI2011]数学作业 / HYSBZ 2326 数学作业(递推,矩阵) Description 小 C 数学成绩优异 ...
随机推荐
- (一)CXF之发布第一个WebService服务
一.CXF入门 1.1 前提 Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS .这些 ...
- (二)Struts.xml文件详解
一.Struts.xml文件 Struts.xml文件构成 如图,<Struts>标签内共有5个子标签. 1.1 struts-default.xml 查看Struts的内容可知,Stru ...
- 关于NavigationBar的笔记
1常用几个方法 全局 //设置navigationBar 的类型 ,ps: status bar的状态受navigationbar控制(当用navigationcontroller时,通过设置此属性改 ...
- centos安装netcat工具及测试
netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...
- 抓包分析工具web版——capanalysis
1.下载安装 官网上,安装在Ubuntu上 2.使用教程 https://blog.51cto.com/chenguang/1325742
- selenium小结
1.selenium基本使用 https://www.cnblogs.com/andy9468/p/8976930.html 2.url发生跳转的处理 https://www.cnblogs.com/ ...
- 【python】python3连接mysql数据库
一.安装pymysql 详见http://www.runoob.com/python3/python3-mysql.html 二.连接mysql数据库 db = pymysql.connect( #连 ...
- Centos超详细安装步骤
Linux中三大主流操作系统 Ubuntu 优点:用户界面友好.工具完善 缺点:vps(虚拟服务器)成本较高.不具备商业化服务器操作系统 Centos--目前常用centos6.x,centos7.x ...
- Linux学习笔记(十三)磁盘管理(一):磁盘分区
一.查看磁盘分区使用状况 df [选项] 其中,-l是默认选项 -a 会显示出包括0字节分区在内的所有文件系统分区 -t 后须跟一个参数,这个参数为文件系统的名字,用来在系统中查找属于该文件系统的分区 ...
- C 全局变量 本地变量