Description

给你一片森林, 支持两个操作: 查询$x$到$y$的$K$大值,  连接两棵树中的两个点

Solution

对每个节点$x$动态开权值线段树, 表示从$x$到根节点路径上权值出现的次数。

查询时差分即可: $sum[x]+sum[y]-sum[lca]-sum[f[lca]]$

连边时需要启发式合并,将节点数小的接到节点数大的上去, 再遍历小的树, 并更新权值

我刚开始以为testcase是数据组数, TLE我好久,,

Code

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
using namespace std; const int N = 1e5; int lson[N * ], rson[N * ], sum[N * ], root[N];
int n, m, T, a[N], b[N], f[N][], head[N], tot, dep[N];
int father[N], num[N], cnt, nd_num;
int lastans, Case; struct edge {
int nxt, to;
}e[N << ]; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
} int find_anc(int x) {
return father[x] == x ? x : father[x] = find_anc(father[x]);
} int find_lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = ; ~i; --i) if(dep[f[x][i]] >= dep[y])
x = f[x][i];
if(x == y) return x;
for(int i = ; ~i; --i) if(f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][];
} int fd(int x) {
return lower_bound(b + , b + + cnt, x) - b;
} void ins(int &o, int now, int l, int r, int pos) {
o = ++nd_num;
sum[o] = sum[now] + ;
lson[o] = lson[now];
rson[o] = rson[now];
if(l == r) return;
int mid = (l + r) >> ;
if(pos <= mid) ins(lson[o], lson[now], l, mid, pos);
else ins(rson[o], rson[now], mid + , r, pos);
} int query(int x, int y, int lca, int flca, int l, int r, int k) {
if(l == r) return l;
int mid = (l + r) >> , tmp;
if((tmp = sum[lson[x]] + sum[lson[y]] - sum[lson[lca]] - sum[lson[flca]]) >= k) return query(lson[x], lson[y], lson[lca], lson[flca], l, mid, k);
else return query(rson[x], rson[y], rson[lca], rson[flca], mid + , r, k - tmp);
} void dfs(int u) {
dep[u] = dep[f[u][]] + ;
for(int i = ; i <= ; ++i)
f[u][i] = f[f[u][i - ]][i - ];
ins(root[u], root[f[u][]], , cnt, fd(a[u]));
for(int i = head[u]; i; i = e[i].nxt) {
int nt = e[i].to;
if(nt == f[u][]) continue;
f[nt][] = u;
dfs(nt);
}
} int work() {
lastans = ; tot = ;
nd_num = ;
/* memset(root, 0, sizeof(root));
memset(lson, 0, sizeof(lson));
memset(rson, 0, sizeof(rson));
memset(head, 0, sizeof(head));
memset(dep, 0, sizeof(dep));*/
n = rd; m = rd; T = rd;
for(int i = ; i <= n; ++i) b[i] = a[i] = rd;
sort(b + , b + + n);
cnt = unique(b + , b + + n) - b - ;
for(int i = ; i <= n; ++i) father[i] = i, num[i] = ;
for(int i = ; i <= m; ++i) {
int u = rd, v = rd;
int x = find_anc(u), y = find_anc(v);
father[y] = x;
num[x] += num[y];
add(u, v); add(v, u);
}
for(int i = ; i <= n; ++i) if(!dep[i]) dfs(i);
for(int i = ; i <= T; ++i) {
char c = getchar();
while(c != 'Q' && c != 'L') c = getchar();
int u = rd ^ lastans, v = rd ^ lastans;
if(c == 'Q') {
int lca = find_lca(u, v), flca = f[lca][], k = rd ^ lastans;
lastans = query(root[u], root[v], root[lca], root[flca], , cnt, k);
if(lastans > cnt || lastans < ) return printf("F**k,WA\n"), ;
lastans = b[lastans];
printf("%d\n", lastans);
}
else {
int x = find_anc(u), y = find_anc(v);
if(num[x] < num[y]) {
swap(x, y); swap(u, v);
}
father[y] = x;
num[x] += num[y];
f[v][] = u;
add(v, u); add(u, v);
dfs(v);
}
}
return ;
} int main()
{
Case = rd;
work();
}

BZOJ 3123 [SDOI2013] 森林 - 启发式合并 主席树的更多相关文章

  1. 【BZOJ3123】[SDOI2013] 森林(启发式合并主席树)

    点此看题面 大致题意: 给你一片森林,有两种操作:询问两点之间的第\(k\)小点权和在两棵树之间连一条边. 前置技能:树上主席树 做这道题目,我们首先要会树上主席树. 关于树上主席树,这有一道很好的例 ...

  2. bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)

    Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...

  3. BZOJ 2733 [HNOI2012]永无乡 - 启发式合并主席树

    Description 1: 查询一个集合内的K大值 2: 合并两个集合 Solution 启发式合并主席树板子 Code #include<cstdio> #include<cst ...

  4. BZOJ 3123: [Sdoi2013]森林 [主席树启发式合并]

    3123: [Sdoi2013]森林 题意:一个森林,加边,询问路径上k小值.保证任意时刻是森林 LCT没法搞,树上kth肯定要用树上主席树 加边?启发式合并就好了,小的树dfs重建一下 注意 测试点 ...

  5. Bzoj 3123: [Sdoi2013]森林(主席树+启发式合并)

    3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 第一行包含一个正整数testcase,表示当前 ...

  6. BZOJ3123: [Sdoi2013]森林(启发式合并&主席树)

    3123: [Sdoi2013]森林 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4813  Solved: 1420[Submit][Status ...

  7. bzoj 3123: [Sdoi2013]森林(45分暴力)

    3123: [Sdoi2013]森林 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4184  Solved: 1235[Submit][Status ...

  8. bzoj 3123 [Sdoi2013]森林(主席树,lca,启发式合并)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  9. bzoj 3123 [Sdoi2013]森林(主席树+启发式合并+LCA)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

随机推荐

  1. linux下安装Cmake和Sniffles

    -------------------------------------------------------------------cmake的安装------------------------- ...

  2. 【翻译】View Frustum Culling --2 Geometric Approach – Extracting the Planes

    在上一篇中,我们知道了视锥体的形状,并且也确定了我们进行裁剪时的步骤.那我们接下来要走的就是确定视锥体的六个平面: near, far, top, bottom, left and right 2.计 ...

  3. Numpy:索引与切片

    numpy基本的索引和切片 import numpy as np arr = np.array([1,2,3,555,666,888,10]) arr array([ 1, 2, 3, 555, 66 ...

  4. log4j最中意的配置

    log4j.rootLogger=DEBUG, Console, logfile log4j.appender.Console=org.apache.log4j.ConsoleAppender log ...

  5. 变态跳台阶(python)

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. # -*- coding:utf-8 -*- class Solution: ...

  6. 重建二叉树(python)

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  7. ucore-lab1-练习1report

    练习1 report 问题1:OS镜像文件ucore.img是如何一步一步生成的(需要比较详细地解释Makefile中的每一条相关命令和命令参数的含义,以及说明命令导致的结果)? GNU make是一 ...

  8. PHPlaravel中从数据库中选择数据是增加时间条件及各种条件

    注:附加条件后要加get函数. 1.public function getForDataTable($startTime,$endTime){ return $this->query() -&g ...

  9. VCSA 6.5, 初始化设置root密码失败can't set root password 或 安装时卡在80%

    是因为下载的非官方的包密码过期了,如果是Windows引导安装: 安装完虚拟机的时候就马上执行下面步骤,修改密码有效期即可. 重启vcsa,在引导界面(photon的图形界面)里按e编辑启动项 在co ...

  10. [剑指Offer]9-用两个栈实现队列

    题目链接 https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&t ...