题意

给出nnn个点,mmm条边.多次询问,求编号在[l,r][l,r][l,r]内的边形成的联通块的数量,强制在线.

分析

  • LCTLCTLCT维护动态最大生成树,先将每条边依次加进去,若形成环就断掉最早加进去(编号最小)的边,然后记录early[]early[]early[]数组,表示第i条边弹掉了哪条边,若没有弹出边,early[i]=0early[i]=0early[i]=0
  • 然后每个询问的答案就是用n减掉[l,r]区间内early值小于l的边的数量,可以用主席树来维护
  • 正确性证明:因为是维护的最大生成树,而early[i]early[i]early[i]又是最大生成树中的最小边,那么(early[i],i)(early[i],i)(early[i],i)这些边一定不能让iii的两个端点联通.所以说对于r>=ir>=ir>=i且l>early[i]l>early[i]l>early[i]的询问,如果按编号从小到大加入边,那么在iii之前它的两个端点一定还不联通,加入iii这条边让它们合并成为一个联通块,答案就减一.所以说只要求出所有earlyearlyearly在主席树上维护就行了.
  • 注意有自环(良心样例有自环)

CODE

#include <cctype>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; for(;!isdigit(ch=getc()););
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0');
}
const int MAXN = 200005;
const int MAXM = MAXN*20;
int n;
namespace LCT {
const int N = 400005;
#define ls ch[x][0]
#define rs ch[x][1]
int ch[N][2], fa[N], mn[N]; bool rev[N];
inline bool isr(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
inline bool get(int x) { return ch[fa[x]][1] == x; }
inline int Max(int x, int y) { if(x <= n) return y; if(y <= n) return x; return min(x, y); }
inline void upd(int x) { mn[x] = Max(x, Max(mn[ls], mn[rs]));}
inline void mt(int x) { if(rev[x]) rev[x]^=1, rev[ls]^=1, rev[rs]^=1, swap(ls, rs); }
void mtpath(int x) { if(!isr(x)) mtpath(fa[x]); mt(x); }
inline void rot(int x) {
int y = fa[x], z = fa[y];
bool l = get(x), r = l^1;
if(!isr(y)) ch[z][get(y)] = x;
fa[ch[x][r]] = y, fa[y] = x, fa[x] = z;
ch[y][l] = ch[x][r], ch[x][r] = y;
upd(y), upd(x);
}
inline void splay(int x) {
mtpath(x);
for(; !isr(x); rot(x))
if(!isr(fa[x])) rot(get(x) == get(fa[x]) ? fa[x] : x);
}
inline int access(int x) { int y = 0;
for(; x; x = fa[y=x]) splay(x), ch[x][1] = y, upd(x);
return y;
}
inline void bert(int x) { access(x), splay(x), rev[x]^=1; }
inline int sert(int x) { access(x), splay(x); for(; ls; x=ls); return x; }
inline bool judge(int x, int y) { bert(x); return sert(y) == x; }
inline void link(int x, int y) { bert(x); fa[x] = y; }
inline void split(int x, int y) { bert(x); access(y), splay(y); }
inline void cut(int x, int y) { split(x, y); ch[y][0] = fa[x] = 0; upd(y); }
inline int findmn(int x, int y) { split(x, y); return mn[y]; }
#undef ls
#undef rs
}
int m, q, type, pre[MAXN], u[MAXN], v[MAXN];
int rt[MAXN], sz, sum[MAXM], ch[MAXM][2];
void modify(int &i, int p, int l, int r, int x) {
if(!i) i = ++sz;
sum[i] = sum[p] + 1;
if(l == r) return;
int mid = (l + r) >> 1;
if(x <= mid) ch[i][1] = ch[p][1], modify(ch[i][0], ch[p][0], l, mid, x);
else ch[i][0] = ch[p][0], modify(ch[i][1], ch[p][1], mid+1, r, x);
}
int query(int i, int j, int l, int r, int ql, int qr) {
if(l == ql && r == qr) return sum[j] - sum[i];
int mid = (l + r) >> 1;
if(qr <= mid) return query(ch[i][0], ch[j][0], l, mid, ql, qr);
else if(ql > mid) return query(ch[i][1], ch[j][1], mid+1, r, ql, qr);
return query(ch[i][0], ch[j][0], l, mid, ql, mid) + query(ch[i][1], ch[j][1], mid+1, r, mid+1, qr);
}
int main () {
read(n), read(m), read(q), read(type);
for(int i = 1; i <= m; ++i) {
read(u[i]), read(v[i]);
if(u[i] == v[i]) rt[i] = rt[i-1];
else {
if(LCT::judge(u[i], v[i])) {
int p = LCT::findmn(u[i], v[i]);
pre[i] = p-n;
LCT::cut(u[pre[i]], p);
LCT::cut(v[pre[i]], p);
}
LCT::link(u[i], n+i);
LCT::link(v[i], n+i);
modify(rt[i], rt[i-1], 0, m, pre[i]);
}
}
int lastans = 0, x, y;
while(q--) {
read(x), read(y);
if(type) x ^= lastans, y ^= lastans;
printf("%d\n", lastans = (n - query(rt[x-1], rt[y], 0, m, 0, x-1)));
}
}

BZOJ 3514: Codechef MARCH14 GERALD07加强版 (LCT维护最大生成树+主席树)的更多相关文章

  1. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT维护最大生成树 主席树

    题面 考虑没有询问,直接给你一个图问联通块怎么做. 并查集是吧. 现在想要动态地做,那么应该要用LCT. 考虑新加进来一条边,想要让它能够减少一个联通块的条件就是现在边的两个端点还没有联通. 如果联通 ...

  2. BZOJ 3514: Codechef MARCH14 GERALD07加强版( LCT + 主席树 )

    从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...

  3. BZOJ 3514: Codechef MARCH14 GERALD07加强版 [LCT 主席树 kruskal]

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1312  Solved: 501 ...

  4. [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)

    [BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...

  5. BZOJ 3514: Codechef MARCH14 GERALD07加强版(LCT + 主席树)

    题意 \(N\) 个点 \(M\) 条边的无向图,询问保留图中编号在 \([l,r]\) 的边的时候图中的联通块个数. \(K\) 次询问强制在线. \(1\le N,M,K \le 200,000\ ...

  6. 【刷题】BZOJ 3514 Codechef MARCH14 GERALD07加强版

    Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来 ...

  7. BZOJ 3514 Codechef MARCH14 GERALD07加强版 Link-Cut-Tree+划分树

    题目大意: 给定n个点m条边的无向图.求问当图中仅仅有[编号在[l,r]区间内]的边存在时图中的联通块个数 强制在线 注意联通块是指联通了就是同一块,不是Tarjan求的那种块 看到这题的那一刻我就想 ...

  8. BZOJ 3514 Codechef MARCH14 GERALD07加强版

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3514 题意:给出一个图m条边.每次询问只加入编号在区间[L,R]之内的边有多少连通 ...

  9. [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 2177  Solved: 834 ...

随机推荐

  1. lua 元表Metatable (六)

    元表理解起来比较抽象,但这是lua设置的一种数据结构而已, 假设有table_A.table_B 这2个table,如果table_A要操作table_B,显然是不可能的 因为者都之间是没有关系的,如 ...

  2. *#【Python】【基础知识】【模块】【datetime】【使用datetime模块 】

    datetime模块 的介绍: 用于调取时间.日期 datetime包含的 方法: >>> dir(datetime) ['MAXYEAR', 'MINYEAR', '__built ...

  3. 编写shell脚本实现对虚拟机cpu、内存、磁盘监控机制

    一.安装Vmware,并通过镜像安装centos7. 二.安装xshell(可以不装,可以直接在虚拟机中直接进行以下步骤) 三.安装mail 一般Linux发送报警邮件通过本地邮箱或外部邮箱服务器,这 ...

  4. Redis 常用命令学习一:通用的基本命令

    1-链接,如果需要的 Redis 部署在远程机器上,可以通过以下命令链接,其中-h后面跟着主机名,-p后面跟端口名 redis-cli -h 233.2.2.4 -p 666 2-PING 命令用来测 ...

  5. Scala当中什么是Transformation和 Action,以及它们俩的区别是什么?

    [学习笔记] 一个完整的RDD任务由两部分组成:Transformation和 Action.Transformation用于对RDD的创建,还可以把老的RDD通过Transformation来生成新 ...

  6. Linux 安装MySQL流程

    1. yum -y install wegt 2. yum -y install vim 3. 下载MySQL的repo源 1. wget http://repo.mysql.com/mysql-co ...

  7. VS2017的一些调试方法技巧

    一.基本的操作. 1.启动调试. 可以通过VS的调试(Debug)菜单启动调试.点击调试菜单下的“启动调试”或者按F5键启动.如果你已经在代码中加入了断点,那么执行会自动开始. 注:退出调试快捷键sh ...

  8. Go语言之依赖管理

    Go语言之依赖管理 Go语言的依赖管理随着版本的更迭正逐渐完善起来. 依赖管理 为什么需要依赖管理 最早的时候,Go所依赖的所有的第三方库都放在GOPATH这个目录下面.这就导致了同一个库只能保存一个 ...

  9. Algorithm negotiation failed

    #用pycharm工具ssh client 报 algorithm negotiation failed#导致原因:是ssh升级后,为了安全,默认不再采用原来一些加密算法,我们手工添加进去即可#目前出 ...

  10. 怎样设置Cookie

    因为 Cookie 是服务器保存在浏览器中的一小段信息, 因此这个设置应当是服务器发起的, 设置方法是在Response Header中添加: Set-Cookie字段, 值是多个键值对. 如下: / ...