3514: Codechef MARCH14 GERALD07加强版

Time Limit: 60 Sec  Memory Limit: 256 MB
Submit: 2177  Solved: 834
[Submit][Status][Discuss]

Description

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

Input

第一行四个整数N、M、K、type,代表点数、边数、询问数以及询问是否加密。
接下来M行,代表图中的每条边。
接下来K行,每行两个整数L、R代表一组询问。对于type=0的测试点,读入的L和R即为询问的L、R;对于type=1的测试点,每组询问的L、R应为L xor lastans和R xor lastans。

Output

K行每行一个整数代表该组询问的联通块个数。

Sample Input

3 5 4 0
1 3
1 2
2 1
3 2
2 2
2 3
1 5
5 5
1 2

Sample Output

2
1
3
1

HINT

对于100%的数据,1≤N、M、K≤200,000。

2016.2.26提高时限至60s

Source

用LCT求出NTR数组,然后主席树在线查询即可,比较简洁巧妙。

http://hzwer.com/4358.html

不过是两个高级数据结构合在一起,而且不是嵌套,理论上很好写。

实际上犯了很多低级错误,而且非常难调,以后一定要慢点写代码。

 #include<cstdio>
#include<algorithm>
#define lc ch[x][0]
#define rc ch[x][1]
#define rep(i,l,r) for (int i=l; i<=r; i++)
using namespace std; const int N=,K=,M=,inf=;
int n,m,Q,op,l,r,x,u,v,nd,ans,fa[N],ntr[K],root[K];
struct E{ int u,v; }e[K];
int find(int x){ return (fa[x]==x) ? x : fa[x]=find(fa[x]); } struct LCT{
int v[N],mn[N],s[N],ch[N][],f[N],tag[N];
bool isroot(int x){ return (!f[x]) || ((ch[f[x]][]!=x) && (ch[f[x]][]!=x)); }
void rev(int x){ swap(ch[x][],ch[x][]); tag[x]^=; }
void push(int x){ if (tag[x]) rev(lc),rev(rc),tag[x]=; }
void pd(int x){ if (!isroot(x)) pd(f[x]); push(x); } void upd(int x){
mn[x]=x;
if (v[mn[lc]]<v[mn[x]]) mn[x]=mn[lc];
if (v[mn[rc]]<v[mn[x]]) mn[x]=mn[rc];
} void rot(int x){
int y=f[x],z=f[y],w=ch[y][]==x;
if (!isroot(y)) ch[z][ch[z][]==y]=x;
f[x]=z; f[y]=x; f[ch[x][w^]]=y;
ch[y][w]=ch[x][w^]; ch[x][w^]=y; upd(y);
} void splay(int x){
pd(x);
while (!isroot(x)){
int y=f[x],z=f[y];
if (!isroot(y)){ if ((ch[z][]==y)^(ch[y][]==x)) rot(x); else rot(y); }
rot(x);
}
upd(x);
} void access(int x){ for (int y=; x; y=x,x=f[x]) splay(x),ch[x][]=y,upd(x); }
void mkroot(int x){ access(x); splay(x); rev(x);}
void link(int x,int y){ mkroot(x); f[x]=y; }
void cut(int x,int y){ mkroot(x); access(y); splay(y); ch[y][]=f[x]=; upd(y); }
int que(int x,int y){ mkroot(x); access(y); splay(y); return mn[y]; }
}T; struct S{
int ls[M],rs[M],sm[M]; void ins(int y,int &x,int L,int R,int pos){
x=++nd; ls[x]=ls[y]; rs[x]=rs[y]; sm[x]=sm[y]+;
if (L==R) return; int mid=(L+R)>>;
if (pos<=mid) ins(ls[y],ls[x],L,mid,pos); else ins(rs[y],rs[x],mid+,R,pos);
} int que(int x,int y,int L,int R,int k){
if (R==k){ return sm[y]-sm[x]; }
int mid=(L+R)>>;
if (k<=mid) return que(ls[x],ls[y],L,mid,k);
else return sm[ls[y]]-sm[ls[x]]+que(rs[x],rs[y],mid+,R,k);
}
}S; void Kruskal(){
int tot=n;
rep(i,,n) fa[i]=i;
rep(i,,m){
int u=e[i].u,v=e[i].v,x=find(u),y=find(v);
if (u==v) { ntr[i]=i; continue; }
if (x==y){
int t=T.que(u,v),k=T.v[t];
ntr[i]=k; T.cut(e[k].u,t); T.cut(e[k].v,t);
}else fa[x]=y;
tot++; T.mn[tot]=tot; T.v[tot]=i;
T.link(u,tot); T.link(v,tot);
}
rep(i,,m) S.ins(root[i-],root[i],,m,ntr[i]);
} void solve(){
rep(i,,Q){
scanf("%d%d",&l,&r);
if (op) l^=ans,r^=ans;
printf("%d\n",ans=n-S.que(root[l-],root[r],,m,l-));
}
} int main(){
freopen("bzoj3514.in","r",stdin);
freopen("bzoj3514.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&Q,&op);
T.v[]=inf; rep(i,,n) T.mn[i]=i,T.v[i]=inf;
rep(i,,m) scanf("%d%d",&e[i].u,&e[i].v);
Kruskal(); solve();
return ;
}

[BZOJ3514]CodeChef MARCH14 GERALD07加强版(LCT+主席树)的更多相关文章

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

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

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

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

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

    题解: 还是比较简单的 首先我们的思路是 确定起点 然后之后贪心的选择边(也就是越靠前越希望选) 我们发现我们只需要将起点从后向前枚举 然后用lct维护连通性 因为强制在线,所以用主席树记录状态就可以 ...

  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. 【CodeForces】889 C. Maximum Element 排列组合+动态规划

    [题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...

  2. Linux内存高,触发oom-killer问题解决

    最近遇到两起Linux的内存问题,其一是触发了oom-killer导致系统挂 1. 首先确认该系统的版本是32位 ? #uname -a Linux alarm 2.6.9-67.ELsmp #1 S ...

  3. shell 监控磁盘使用率【转】

    方案一: disks=(`df |sed 1d | awk '{print $1,$5}'|tr -d %`) len=${#disks[@]} ;i<=$len;i=i+));do ];the ...

  4. memcached安装【转】

    1.安装依赖软件 # yum -y install libevent libevent-devel perl-Test-Harness perl-Time-HiRes perl-TermReadKey ...

  5. bootstrap table 双击可编辑,添加、删除行

    html: <table class="table table-bordered" id="para_table"> <tr> < ...

  6. .NET连接Oracle的方法

    .NET连接Oracle的方法 方式1:直接利用.NET的oracle驱动连接 引用System.data.oracleclient; using System.data.oracleclient; ...

  7. 《跟老齐学Python Django实战》读后感

    1.说一下这本书,讲解的很细致,内容选取足够入门Django. 2.在学习这本书要注意的几点: <1>如果你想跟着敲这本书的代码必须要安装:Django版本1.10.1(当然也可以玩玩新版 ...

  8. 转:google测试分享-测试经理

    原文: http://blog.sina.com.cn/s/blog_6cf812be0102vode.html 前言:这个系列分享的内容大部分都是出自于<google是如何测试的>的书, ...

  9. [Linux: vim]vim自动生成html代码

    如果直接将vim编辑的文字复制粘贴到一些blog的编辑器中,这些代码将会是死板的白纸黑字.如果能加入关键字高亮功能就好了,这样代码阅读起来会很方便.一些blog的编辑器提供了这项功能,一些没有,一些支 ...

  10. Effective C++笔记(二):构造/析构/赋值运算

    参考:http://www.cnblogs.com/ronny/p/3740926.html 条款05:了解C++默默编写并调用哪些函数 如果自定义一个空类的话,会自动生成默认构造函数.拷贝构造函数. ...