BZOJ 3514: Codechef MARCH14 GERALD07加强版 (LCT维护最大生成树+主席树)
题意
给出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维护最大生成树+主席树)的更多相关文章
- BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT维护最大生成树 主席树
题面 考虑没有询问,直接给你一个图问联通块怎么做. 并查集是吧. 现在想要动态地做,那么应该要用LCT. 考虑新加进来一条边,想要让它能够减少一个联通块的条件就是现在边的两个端点还没有联通. 如果联通 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版( LCT + 主席树 )
从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版 [LCT 主席树 kruskal]
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1312 Solved: 501 ...
- [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)
[BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...
- BZOJ 3514: Codechef MARCH14 GERALD07加强版(LCT + 主席树)
题意 \(N\) 个点 \(M\) 条边的无向图,询问保留图中编号在 \([l,r]\) 的边的时候图中的联通块个数. \(K\) 次询问强制在线. \(1\le N,M,K \le 200,000\ ...
- 【刷题】BZOJ 3514 Codechef MARCH14 GERALD07加强版
Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来 ...
- BZOJ 3514 Codechef MARCH14 GERALD07加强版 Link-Cut-Tree+划分树
题目大意: 给定n个点m条边的无向图.求问当图中仅仅有[编号在[l,r]区间内]的边存在时图中的联通块个数 强制在线 注意联通块是指联通了就是同一块,不是Tarjan求的那种块 看到这题的那一刻我就想 ...
- BZOJ 3514 Codechef MARCH14 GERALD07加强版
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3514 题意:给出一个图m条边.每次询问只加入编号在区间[L,R]之内的边有多少连通 ...
- [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2177 Solved: 834 ...
随机推荐
- [CF306C]White, Black and White Again_排列组合
White, Black and White Again 题目链接:https://www.luogu.org/problem/CF306C 数据范围:略. 题解: 记得不要看错题,容易看成来回交替下 ...
- Kubernetes---网络通讯模式笔记
⒈kubernetes网络通讯模式 Kubernetes的网络模型假定了所有Pod都在一个可以直接连通的扁平的网络空间中,这在GCE(Google Compute Engine)里面是现成的网 ...
- 从用户在浏览器输入URL回车之后,浏览器都做了什么
在直接列出执行的步骤之前先来普及几个知识,相信了解完这些知识之后会对前后端的交互有更深入的理解. 1.TCP连接 TCP:Transmission Control Protocol, 传输控制协议,是 ...
- 下载HTMLTestRunner 地址
通过pip安装 HTMLTestRunne失败 则需要通过手动下载. 下载地址: http://tungwaiyip.info/software/HTMLTestRunner.html 下载后,把H ...
- selenium登录4399
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...
- golang数据类型
整数类型 Golang各整数类型分:有符号和无符号,int uint 的大小和系统有关. Golang查看一个变量的数据类型: package main import "fmt" ...
- 第十一章 ZYNQ-MIZ701 PS读写PL端BRAM
本篇文章目的是使用Block Memory进行PS和PL的数据交互或者数据共享,通过zynq PS端的Master GP0端口向BRAM写数据,然后再通过PS端的Mater GP1把数据读出来,将 ...
- WMIC命令的利用技巧
WMIC扩展WMI(Windows Management Instrumentation,Windows管理工具),提供了从命令行接口和批命令脚本执行系统管理的支持.在WMIC出现之前,如果要管理WM ...
- ORACLE通过JOB定时创建序列
http://blog.csdn.net/cuihaiyang/article/details/7872982 因为业务需要每月需要增加一个序列,想到了使用job定时创建,每次创建一年的.写此job的 ...
- luogu1018乘积最大--区间DP
题目链接 https://www.luogu.org/problemnew/show/P1018 分析 这道题套路跟山区建小学差不多,可以先去看看那篇题解 \(f[i][j]\)表示枚举到第\(i\) ...