#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 400005
#define maxm 200005
#define maxk 4000005
using namespace std; int n,m,k,type,ans,Ans,size,fa[maxn],son[maxn][],sm[maxn],Val[maxm],val[maxn],sum[maxk],root[maxm],lc[maxk],rc[maxk];
bool rev[maxn];
struct date{
int u,v;
}wi[maxm]; struct note{
bool which(int x){
return son[fa[x]][]==x;
}
bool isroot(int x){
return son[fa[x]][]!=x&&son[fa[x]][]!=x;
}
void update(int x){
sm[x]=val[x];
if (son[x][]) sm[x]=min(sm[x],sm[son[x][]]);
if (son[x][]) sm[x]=min(sm[x],sm[son[x][]]);
}
void pushdown(int x){
if (!rev[x]) return;
rev[x]^=,swap(son[x][],son[x][]);
if (son[x][]) rev[son[x][]]^=;
if (son[x][]) rev[son[x][]]^=;
}
void relax(int x){
if (!isroot(x)) relax(fa[x]);
pushdown(x);
}
void rotata(int x){
int y=fa[x],d=which(x),dd=which(y);
if (!isroot(y)) son[fa[y]][dd]=x; fa[x]=fa[y];
fa[son[x][d^]]=y,son[y][d]=son[x][d^];
fa[y]=x,son[x][d^]=y;
update(y);
}
void splay(int x){
relax(x);
while (!isroot(x)){
if (isroot(fa[x])) rotata(x);
else if (which(x)==which(fa[x])) rotata(fa[x]),rotata(x);
else rotata(x),rotata(x);
}
update(x);
}
void access(int x){
for (int p=;x;x=fa[x]){
splay(x);
son[x][]=p;
p=x;
update(x);
}
}
void make_root(int x){
access(x);
splay(x);
rev[x]^=;
}
void link(int x,int y){
make_root(x);
fa[x]=y;
}
void cut(int x,int y){
make_root(x);
access(y);
splay(y);
son[y][]=fa[x]=;
update(y);
}
void split(int x,int y){
make_root(x);
access(y);
splay(y);
}
int find_root(int x){
access(x);
splay(x);
while (son[x][]) x=son[x][];
return x;
}
int find(int x,int y){
split(x,y);
return sm[y];
}
}lct; void insert(int &k,int p,int l,int r,int x){
k=++size,sum[k]=sum[p]+;
int mid=(l+r)/;
if (l==r) return;
if (x<=mid) rc[k]=rc[p],insert(lc[k],lc[p],l,mid,x);
else lc[k]=lc[p],insert(rc[k],rc[p],mid+,r,x);
} void insert(int id,int x){
insert(root[id],root[id-],,m+,x);
} void Query(int k1,int k2,int l,int r,int x,int y){
if (!k1&&!k2) return;
if (l>=x&&r<=y){
Ans+=(sum[k2]-sum[k1]);
return;
}
int mid=(l+r)/;
if (x<=mid) Query(lc[k1],lc[k2],l,mid,x,y);
if (y>mid) Query(rc[k1],rc[k2],mid+,r,x,y);
} int query(int x,int y){
Ans=;
int u=root[x-],v=root[y],l=,r=m+,mid;
Query(u,v,l,r,,x-);
return Ans;
} int main(){
int u,v,t1,t2,temp;
scanf("%d%d%d%d",&n,&m,&k,&type);
memset(sum,,sizeof(sum));
memset(Val,,sizeof(Val));
memset(fa,,sizeof(fa));
memset(son,,sizeof(son));
memset(rev,,sizeof(rev));
for (int i=;i<=m;i++) scanf("%d%d",&wi[i].u,&wi[i].v);
for (int i=;i<=n;i++) val[i]=sm[i]=m+;
for (int i=;i<=m;i++) val[n+i]=sm[n+i]=i;
for (int i=;i<=m;i++){
u=wi[i].u,v=wi[i].v;
if (u==v) Val[i]=m+;
if (u==v) continue;
if (lct.find_root(u)!=lct.find_root(v)){
Val[i]=;
lct.link(n+i,u),lct.link(n+i,v);
}else{
temp=lct.find(u,v);
Val[i]=temp,temp+=n;
lct.cut(temp,wi[temp-n].u),lct.cut(temp,wi[temp-n].v);
lct.link(n+i,u),lct.link(n+i,v);
}
}
memset(root,,sizeof(root));
for (int i=;i<=m;i++){
insert(i,Val[i]);
}
ans=;
for (int i=;i<=k;i++){
scanf("%d%d",&u,&v);
if (type==) u^=ans,v^=ans;
if (u>v) swap(u,v);
ans=n-query(u,v);
printf("%d\n",ans);
}
return ;
}

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3514

题目大意:N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数。

做法:如果要求整个图中联通块的个数,记加入该边后不形成环的边数记为x,则答案为n-x,这题问的是加入编号为[l~r]中的边后联通块的个数,答案便稍微变化了一下,记能使联通块数目-1的边集为{S},答案为n-|S|,问题便成为了:有多少条边属于{S},仔细想想,可以先预处理一个数组val[i],怎么预处理呢?可以按编号依次加入边,有两种情况:*1.如果加入的这条边不形成环,则这条边的权值赋值为0,并加入这条边;*2.加入这条边形成环,记环上编号最小的边的编号为y,这加入的这条边权值为y,并删去编号最小的那条边,加入该边。这个过程用lct模拟即可,比较基础的操作。

得出每条边的权值val[i]后,对于一个询问[L~R],如果一条边的val<l,则该边属于{S},问题便简化为一个数组val,问在区间L~R中权值在0~L-1的个数,显而易见,可持久化线段树轻松搞定。问题得以圆满解决。(Lct+可持久化线段树)

bzoj3514Codechef MARCH14 GERALD07加强版的更多相关文章

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

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

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

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

  3. 【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版

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

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

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

  5. BZOJ_3514_Codechef MARCH14 GERALD07加强版_主席树+LCT

    BZOJ_3514_Codechef MARCH14 GERALD07加强版_主席树+LCT Description N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. I ...

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

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

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

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

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

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

  9. BZOJ3514 : Codechef MARCH14 GERALD07加强版

    以边编号为权值 用Link-cut Tree维护最大生成树 对于新加的第i条边(u,v) a[i]表示当a[i]这条边加入后连通块个数会减少 若u==v则a[i]=m 若u与v不连通则连上,a[i]= ...

随机推荐

  1. 浅析jQuery删除节点的三个方法

    jQuery提供了三种删除节点的方法,即remove(),detach()和empty().测试所用HTML代码:[html] view plaincopy<p title="选择你最 ...

  2. [Azure] 使用 Visual Studio 2013 管理中国版 Azure 订阅

    比较关心微软平台技术的朋友应该都知道,微软云服务(Microsoft Azure)以下简称Azure分为全球版和中国版,由于政府法规问题中国版的服务是由二十一世纪互联运营,整体来看中国版Azure和全 ...

  3. 阿里云Centos 6.3 64位 安全加固版 升级 Php 中的 Curl 7.19 到 7.35

    *注意是使用阿里云一键安装包的升级,升级前快照备份哟,小伙伴! 1.SSH远程到root下下载新版本curl 网址地址:http://curl.haxx.se/download.html 完成curl ...

  4. 设置apache https服务

    配置http.conf,所在位置d:\wamp\bin\apache\apache2.4.9\conf\http.conf   LoadModule socache_shmcb_module modu ...

  5. Cordova - 常用的插件汇总(附插件的安装、查询、更新、删除等命令)

    Hybrid应用比web应用强大之处在于可以使运行在容器中的web内容访问 native APIs.Cordova 提供了许多插件用于调用移动设备上的API. 一,插件相关常用命令   1,查看所有已 ...

  6. Linux execve函数簇用法

    exec函数簇实现的功能都是用一个新程序替换原来的程序,替换的内容包括堆栈段,代码段,进程控制器PCD,但是原进程的PID保持不变 int execl(const char *path, const ...

  7. 某站出品2016织梦CMS进阶教程共12课(附文档+工具)

    此为广告商内容使用最新版的dede cms建站 V5.7 sp1,经常注意后台的升级信息哦!一.安装DEDE的时候数据库的表前缀,最好改一下,不用dedecms默认的前缀dede_,随便一个名称即可. ...

  8. JavaScript的理解记录(4)

    客户端JavaScript:客户端就是Web浏览器; 一. 前奏: Web文档(document):一些呈现静态信息的页面,虽然有的页面是会动的,但信息本身还是静态! Web应用:可以动态载入信息,相 ...

  9. html+js实现图片预览

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. SVN常见图标的含义

    项目视图   The Package Explorer view - 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽 ...