题面传送门


复出的第一道题.. md就遇到坑了..

简单来说就是可持久化线段树+启发式合并啊..

感觉启发式合并好神奇好想学

每一次建边就暴力合并,每一个节点维护从根到它的权值线段树

按照题面的话最省空间的做法就是垃圾回收,但是实在是太慢了..

而且这题有坑,题面说的是多组数据其实只有一组 而且是$T>1$的一组..

然后看给了512MB就不需要垃圾回收,而且很多预处理都tm不用了呢!wqnmdsy

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 80010;
const int lg = 18;
struct node {
int y, next;
}a[Maxn*2]; int first[Maxn], len;
void ins ( int x, int y ){
len ++;
a[len].y = y;
a[len].next = first[x]; first[x] = len;
}
int n, m, t;
int sum[Maxn*lg*lg], lc[Maxn*lg*lg], rc[Maxn*lg*lg], tot;
int rt[Maxn];
int na[Maxn], b[Maxn], bl;
int fa[Maxn][lg], dep[Maxn];
int faa[Maxn], gs[Maxn];
int ff ( int x ){
if ( faa[x] == x ) return x;
return faa[x] = ff (faa[x]);
}
void merge ( int &now, int fnow, int l, int r, int x ){
if ( !now ) now = ++tot;
sum[now] = sum[fnow]+1;
if ( l == r ) return;
int mid = ( l + r ) >> 1;
if ( x <= mid ) merge ( lc[now], lc[fnow], l, mid, x ), rc[now] = rc[fnow];
else merge ( rc[now], rc[fnow], mid+1, r, x ), lc[now] = lc[fnow];
}
void dfs1 ( int x, int f ){
rt[x] = 0;
merge ( rt[x], rt[fa[x][0]], 1, bl, na[x] );
for ( int i = 1; i <= 17; i ++ ) fa[x][i] = fa[fa[x][i-1]][i-1];
for ( int k = first[x]; k; k = a[k].next ){
int y = a[k].y;
if ( y == f ) continue;
fa[y][0] = x; dep[y] = dep[x]+1;
dfs1 ( y, x );
}
}
void change ( int &now, int l, int r, int x ){
if ( !now ) now = ++tot;
sum[now] ++;
if ( l == r ) return;
int mid = ( l + r ) >> 1;
if ( x <= mid ) change ( lc[now], l, mid, x );
else change ( rc[now], mid+1, r, x );
}
int getlca ( int x, int y ){
if ( dep[x] < dep[y] ) swap ( x, y );
for ( int i = 17; i >= 0; i -- ){
if ( dep[fa[x][i]] >= dep[y] ){
x = fa[x][i];
}
}
if ( x == y ) return x;
for ( int i = 17; i >= 0; i -- ){
if ( fa[x][i] != fa[y][i] ){
x = fa[x][i]; y = fa[y][i];
}
}
return fa[x][0];
}
int getrank ( int now1, int now2, int now3, int p, int l, int r, int k ){
if ( l == r ) return b[l];
int mid = ( l + r ) >> 1;
int ls = sum[lc[now1]]+sum[lc[now2]]-2*sum[lc[now3]];
if ( na[p] <= mid && na[p] >= l ) ls --;
if ( ls >= k ) return getrank ( lc[now1], lc[now2], lc[now3], p, l, mid, k );
else return getrank ( rc[now1], rc[now2], rc[now3], p, mid+1, r, k-ls );
}
void read ( int &x ){
char c = getchar ();
for ( ; c > '9' || c < '0'; c = getchar () );
x = 0;
for ( ; c <= '9' && c >= '0'; c = getchar () ) x = x*10+c-'0';
}
int main (){
int i, j, k, T;
read (T);
tot = 0;
while ( T -- ){
len = 0; memset ( first, 0, sizeof (first) );
read (n); read (m); read (t);
for ( i = 1; i <= n; i ++ ) rt[i] = 0, faa[i] = i, gs[i] = 1, dep[i] = 1;
for ( i = 1; i <= n; i ++ ){
read (na[i]);
b[i] = na[i];
}
sort ( b+1, b+n+1 );
bl = unique ( b+1, b+n+1 ) - (b+1);
for ( i = 1; i <= n; i ++ ){
na[i] = lower_bound ( b+1, b+bl+1, na[i] ) - b;
change ( rt[i], 1, bl, na[i] );
}
for ( i = 1; i <= m; i ++ ){
int x, y;
read (x); read (y);
int fx = ff (x), fy = ff (y);
if ( gs[fx] > gs[fy] ) swap ( x, y );
faa[fx] = fy; gs[fy] += gs[fx];
fa[x][0] = y; dep[x] = dep[y]+1;
dfs1 ( x, y );
ins ( x, y ); ins ( y, x );
}
char c;
int lastans = 0;
while ( t -- ){
c = getchar ();
for ( ; c > 'Z' || c < 'A'; c = getchar () );
if ( c == 'Q' ){
int x, y;
read (x); read (y); read (k);
x ^= lastans; y ^= lastans; k ^= lastans;
int lca = getlca ( x, y );
lastans = getrank ( rt[x], rt[y], rt[fa[lca][0]], lca, 1, bl, k );
printf ( "%d\n", lastans );
}
else {
int x, y;
read (x); read (y);
x ^= lastans; y ^= lastans;
int fx = ff (x), fy = ff (y);
if ( gs[fx] > gs[fy] ) swap ( x, y );
faa[fx] = fy; gs[fy] += gs[fx];
fa[x][0] = y; dep[x] = dep[y]+1;
dfs1 ( x, y );
ins ( x, y ); ins ( y, x );
}
}
break;
}
return 0;
}

  

bzoj3123: [Sdoi2013]森林的更多相关文章

  1. [bzoj3123][Sdoi2013]森林_主席树_启发式合并

    森林 bzoj-3123 Sdoi-2013 题目大意:给定一片共n个点的森林,T个操作,支持:连接两个不在一棵树上的两个点:查询一棵树上路径k小值. 注释:$1\le n,T \le 8\cdot ...

  2. [BZOJ3123][Sdoi2013]森林 主席树+启发式合并

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

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

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

  4. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

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

  5. 【主席树 启发式合并】bzoj3123: [Sdoi2013]森林

    小细节磕磕碰碰浪费了半个多小时的时间 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M ...

  6. BZOJ3123[Sdoi2013]森林——主席树+LCA+启发式合并

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

  7. bzoj千题计划258:bzoj3123: [Sdoi2013]森林

    http://www.lydsy.com/JudgeOnline/problem.php?id=3123 启发式合并主席树 #include<cmath> #include<cstd ...

  8. [bzoj3123] [SDOI2013]森林 主席树+启发式合并+LCT

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

  9. bzoj3123 [Sdoi2013]森林 树上主席树+启发式合并

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3123 题解 如果是静态的查询操作,那么就是直接树上主席树的板子. 但是我们现在有了一个连接两棵 ...

随机推荐

  1. webpack那些事儿

    webpack那些事儿01-webpack到底是什么 webpack那些事儿02-从零开始 webpack那些事儿03-热插拔 hot webpack那些事儿04-spa项目实战分析 webpack那 ...

  2. oracle 视图的创建,游标,left join

    视图的创建: create or replace view dmv_mat_contract_stock_in_bill as select csib.*, sib.STOCK_IO_, sib.CO ...

  3. Django Restful Framework (二): ModelSerializer

    时常,你需要对django model 的实例进行序列化.ModelSerializer 类提供了一个捷径让你可以根据 Model 来创建 Serializer. ModelSerializer 类和 ...

  4. PYTHON 内置函数

    bin() #二进制 r = bin(11) print(r) # 0b1011 oct() #八进制 r = oct(14) print(r) #0o16 int() #十进制 r = int(10 ...

  5. C++中的vector 用法解析

         一.概述     vector 是C++标准模板库的部分内容,他是一个多功能的,能够操作多种 数据结构和算法 的模板类和函数库.     vector 是一个容器,它能够存放各种类型的对象, ...

  6. VS2015编译boost1.62

    VS2015编译boost1.62 Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有 ...

  7. Android 通用流行框架

    原文出处: http://android.jobbole.com/83028/ 1. 缓存 名称 描述 DiskLruCache Java实现基于LRU的磁盘缓存 2.图片加载 名称 描述 Andro ...

  8. NodeJS+Express下构建后端MVC文件结构

    关于MVC的结构大体上有两种方式,其一按照层级进行文件夹分类,其二是按照业务进行文件夹分类.关于这个demo相关的业务简单,所以暂采用第一种的方式,当然实际当中很恨复杂的项目可以采用两种方式相结合的方 ...

  9. linux无法挂载u盘

    一般插入u盘都会自动挂载,但有时挂载不了,错误提示:can't find /dev/sdb in /etc/fstab:这时可能是U盘坏了,我们当然不希望是这样.也有可能是U盘使用的接口不对应导致系统 ...

  10. android初练二

    android 之 Activity的启动方式 1.android的显示启动 显示启动一般用于在用自己的活动时进行页面跳转时常常使用到 public class MainActivity extend ...