题解:

还是比较简单的

首先我们的思路是 确定起点 然后之后贪心的选择边(也就是越靠前越希望选)

我们发现我们只需要将起点从后向前枚举

然后用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+主席树的更多相关文章

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

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

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

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

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

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

  4. bzoj3514 Codechef MARCH14 GERALD07加强版 lct预处理+主席树

    Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1951  Solved: 746[Submi ...

  5. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

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

  6. BZOJ3514: Codechef MARCH14 GERALD07加强版(LCT,主席树)

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

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

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

  8. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树

    自己独自想出来并切掉还是很开心的~ Code: #include <bits/stdc++.h> #define N 400005 #define inf 1000000000 #defi ...

  9. BZOJ3514 Codechef MARCH14 GERALD07加强版 LCT

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3514 题意概括 N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. N ...

随机推荐

  1. ADO读写DateTime方式

    // 读取日期 var = m_pResultSet->GetCollect(_variant_t("Birth_Time")); DATE dt = var.date; C ...

  2. 题解-POI2014 FAR-FarmCraft

    Problem bzoj权限题,洛谷上可提交 洛谷上的奇葩翻译不要看,很多条件缺漏 题意简述:给定一棵树,每条边权为1,给定所有点点权,每条边仅能走两次,求以一定顺序遍历整棵树后,使所有点中的到达时间 ...

  3. MYSQL添加远程用户或允许远程访问

    1.用root用户登陆 格式:grant 权限 on 数据库教程名.表名 to 用户@登录主机 identified by "用户密码";             @ 后面是访问M ...

  4. html5 - history 历史管理

    参考文章: w3c     :  http://www.w3.org/html/ig/zh/wiki/HTML5/history 张鑫旭  : http://www.zhangxinxu.com/wo ...

  5. lua 中 socket 通信示例

    server.lua #!/usr/bin/lua local socket = require("socket") host, port = "127.0.0.1&qu ...

  6. webservice:com.sun.xml.internal.ws.server.ServerRtException: [failed to localize]

    发布webservice发生了错误,一直没有能够解决,错误如下: Exception in thread "main" com.sun.xml.internal.ws.server ...

  7. python old six day

    今天主要内容: . is 和== 的区别 . 编程的问题 一.       is和==的区别! is  比较的是内存地址 ==  比较的是值 记住结果就好 ⑴id 通过id() 我们查看到一个变量表示 ...

  8. shiro使用

    web.xml配置 <filter> <filter-name>shiroFilter</filter-name> <filter-class>org. ...

  9. VBS计时器2

    打开计时器,如果点击暂停,会显示你刚才事物所用的时间(以分钟为单位) dim c //控制循环 c= vbyes while c<>vbno dim a a= 60*hour(now)+m ...

  10. iPhone内存溢出——黑白苹果

    一旦iPhone内存长期占用过大,一关机即再难启动,如果你Home键加power键等都不行,那么可以要刷机了. 方式: 1.电脑下载ITunes,这个过程比较漫长,一系列逐渐大概3G,下载完成打开. ...