【BZOJ3514】Codechef MARCH14 GERALD07加强版 LCT+主席树
题解:
还是比较简单的
首先我们的思路是 确定起点 然后之后贪心的选择边(也就是越靠前越希望选)
我们发现我们只需要将起点从后向前枚举
然后用lct维护连通性
因为强制在线,所以用主席树记录状态就可以了
*数组开小查了很久
代码:
#include <bits/stdc++.h>
using namespace std;
#define IL inline
#define rint register int
#define rep(i,h,t) for (rint i=h;i<=t;i++)
#define dep(i,t,h) for (rint i=t;i>=h;i--)
const int INF=1e9;
const int N=4e5+;
char ss[<<],*A=ss,*B=ss;
IL char gc()
{
return A==B&&(B=(A=ss)+fread(ss,,<<,stdin),A==B)?EOF:*A++;
}
template<class T> void read(T &x)
{
rint f=,c; while (c=gc(),c<||c>) if (c=='-') f=-; x=c^;
while (c=gc(),<c&&c<) x=(x<<)+(x<<)+(c^); x*=f;
}
int data[N],ls[N],rs[N],fa[N],v[N];
bool rev[N];
IL void updata(int x)
{
data[x]=min(min(data[ls[x]],data[rs[x]]),v[x]);
}
IL void down(int x)
{
if (!rev[x]) return;
rev[ls[x]]^=; rev[rs[x]]^=;
swap(ls[x],rs[x]);
rev[x]=;
}
IL bool pd(int x)
{
int fa1=fa[x];
if (ls[fa1]!=x&&rs[fa1]!=x) return();
else return();
}
void rotate(int x,int y)
{
int fa1=fa[x];
if (y==)
{
rs[fa1]=ls[x];
if (ls[x]) fa[ls[x]]=fa1;
} else
{
ls[fa1]=rs[x];
if (rs[x]) fa[rs[x]]=fa1;
}
fa[x]=fa[fa1];
if (pd(fa1))
{
if (ls[fa[fa1]]==fa1) ls[fa[fa1]]=x; else rs[fa[fa1]]=x;
}
fa[fa1]=x;
if (y==) ls[x]=fa1; else rs[x]=fa1;
updata(fa1); updata(x);
}
void dfs(int x)
{
if (pd(x)) dfs(fa[x]);
down(x);
}
void splay(int x)
{
dfs(x);
int fa1=fa[x];
while (pd(x))
{
if (!pd(fa1))
{
if (x==ls[fa1]) rotate(x,); else rotate(x,);
} else
if (ls[fa[fa1]]==fa1)
if (ls[fa1]==x)
rotate(fa1,),rotate(x,);
else
rotate(x,),rotate(x,);
else
if (rs[fa1]==x)
rotate(fa1,),rotate(x,);
else
rotate(x,),rotate(x,);
fa1=fa[x];
}
}
void access(int x)
{
for (int y=;x;y=x,x=fa[x])
{
splay(x); rs[x]=y; updata(x);
}
}
int fdr(int x)
{
access(x);
splay(x);
while (ls[x]) x=ls[x];
return(x);
}
void mkr(int x)
{
access(x);
splay(x);
rev[x]^=;
}
void split(int x,int y)
{
mkr(x);
access(y);
splay(y);
}
void link(int x,int y)
{
mkr(x);
access(y);
splay(y);
fa[x]=y;
}
void cut(int x,int y)
{
mkr(x);
access(y);
splay(y);
ls[y]=fa[x]=;
updata(y);
}
#define mid ((h+t)/2)
int root[N],jl1[N],jl2[N];
struct segtment{
int p[N*],ls[N*],rs[N*],cnt;
segtment() { cnt=; }
void updata(int x)
{
p[x]=p[ls[x]]+p[rs[x]];
}
void change(int last,int &x,int h,int t,int pos,int k)
{
x=++cnt; ls[x]=ls[last]; rs[x]=rs[last];
p[x]=p[last]+k;
if (h==t) return;
if (pos<=mid) change(ls[last],ls[x],h,mid,pos,k);
else change(rs[last],rs[x],mid+,t,pos,k);
updata(x);
}
int query(int x,int h,int t,int h1,int t1)
{
if (h1<=h&&t<=t1) return(p[x]);
int ans=;
if (h1<=mid) ans+=query(ls[x],h,mid,h1,t1);
if (mid<t1) ans+=query(rs[x],mid+,t,h1,t1);
return(ans);
}
}S;
int n,m,k,type;
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
read(n); read(m); read(k); read(type);
int ans=;
data[]=v[]=INF;
rep(i,,n) v[i]=INF,v[i+n]=i;
rep(i,,m)v[i+n]=i;
int p=,maxn=;
rep(i,,m)
{
// cout<<i<<endl;
int x,y;
root[i]=root[i-];
read(x); read(y);
if (x==y) continue;
jl1[i]=x;jl2[i]=y;
mkr(y);
if (fdr(x)==y)
{
split(x,y);
int x1=data[y];
cut(x1+n,jl1[x1]);
cut(x1+n,jl2[x1]);
S.change(root[i],root[i],,m,x1,-);
p--;
}
p++;
link(i+n,x);
link(i+n,y);
S.change(root[i],root[i],,m,i,);
if (p>n) return ;
maxn=max(maxn,p);
}
rep(i,,k)
{
int x,y;
read(x); read(y);
if (type) x^=ans,y^=ans;
printf("%d\n",ans=n-S.query(root[y],,m,x,y));
}
return ;
}
【BZOJ3514】Codechef MARCH14 GERALD07加强版 LCT+主席树的更多相关文章
- [BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 2177 Solved: 834 ...
- 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加强版( LCT + 主席树 )
从左到右加边, 假如+的边e形成环, 那么记下这个环上最早加入的边_e, 当且仅当询问区间的左端点> _e加入的时间, e对答案有贡献(脑补一下). 然后一开始是N个连通块, 假如有x条边有贡献 ...
- bzoj3514 Codechef MARCH14 GERALD07加强版 lct预处理+主席树
Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1951 Solved: 746[Submi ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- BZOJ3514: Codechef MARCH14 GERALD07加强版(LCT,主席树)
Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. Input 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密.接下来M ...
- BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT维护最大生成树 主席树
题面 考虑没有询问,直接给你一个图问联通块怎么做. 并查集是吧. 现在想要动态地做,那么应该要用LCT. 考虑新加进来一条边,想要让它能够减少一个联通块的条件就是现在边的两个端点还没有联通. 如果联通 ...
- BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树
自己独自想出来并切掉还是很开心的~ Code: #include <bits/stdc++.h> #define N 400005 #define inf 1000000000 #defi ...
- BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3514 题意概括 N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. N ...
随机推荐
- 使用Boost Regex 的regex_search进行遍历搜索
在regex_search函数中,会将找到的第一个匹配结果保存到一个smatch类中. 然而如果搜索字符串中有多个匹配结果,则需要自己实现了. 在smatch中,有两个成员,官方文档如下: itera ...
- oracle 会话 解锁
背景 这是当年第一次记录博客,当初记录的原因是感觉有些问题很少碰到,碰到有网上寻找一遍,文章很少是正好对症的,折腾半天终于解决了,但是没有记录过程,结果下次碰到又要从来一次.有的问题还极其不好找,or ...
- gnutls-3.5.18 static building for windows
gnutls-3.5.18 static building for windows Required libraries:1. libnettle 2. gmplib Optional librari ...
- 【原创】大叔经验分享(38)beeline连接hiveserver2报错impersonate
beeline连接hiveserver2报错 Error: Could not open client transport with JDBC Uri: jdbc:hive2://localhost: ...
- Jmeter之csv参数化
创建数据源csv文件 在线程组中添加CSV Data Set Config 1.添加CSV Data Set Config 添加CSV Data Set Config 2.配置CSV Data Set ...
- 06 元祖 字典 集合set
元组 定义: ti=() print(ti,type(ti)) 参数:for可以循环的对象(可迭代对象) t2=tuple(") # ('1', '2', '3') <class 't ...
- swift 学习- 20 -- 错误处理
// 错误处理 是响应错误以及 从错误中恢复的过程, Swift 提供了在运行时对 可恢复错误的 抛出, 捕获, 传递 和 操作的支持 // 某些操作无法保证总是执行完所有代码 或总是生层有用结果, ...
- -Dmaven.multiModuleProjectDirectory system property is not set.
1.配置 maven 环境变量 新建系统变量 -> 变量名(N): M2_HOME 变量值(V): D:\apache-maven-3.5.4(改为自己的maven目录) -> 添加 pa ...
- SQL Server2008从入门到精通pdf
下载地址:网盘下载 内容介绍 编辑 <SQL Server 从入门到精通>从初学者的角度出发,通过通俗易懂的语言.丰富多彩的实例,详细地介绍了SQLServer2008开发应该掌握的各方面 ...
- Confluence 6 开始使用
欢迎来到 Confluence 的开始使用指南文档.在这个稳定中,你将会找到有关对 Confluence 进行评估的指南和其他的一些有用的内容.当你开始使用 Confluence 的时候,这些信息能够 ...