题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效。问有多少种通行证可以使得1和N连通。

思路:和bzoj魔法森林有点像,LCT维护最小生成树。  开始和队友在想维护连通性,而不是维护树,这样好像会很麻烦。

队友yy了一个算法:用线段树模拟并查集维护连通性。(发现和标程有点像?

我的代码:LCT维护最小生成树。

...先给代码,后面补一下题解。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>e[];
int n,m,t[],q[],siz[],ans,efree,a[],cnt,l[],r[],u[],v[],f[];
template<class T>
inline void read(T&a){
char c=getchar();
for(a=;(c<''||c>'')&&c!='-';c=getchar());
bool f=;if(c=='-')f=,c=getchar();
for(;c>=''&&c<='';c=getchar())a=a*+c-'';
if(f)a=-a;
}
inline int gf(int x){return x==f[x]?f[x]:gf(f[x]);}
void ins(int k,int nl,int nr,int l,int r,int x){
if(nl==l&&nr==r)return (void)e[k].push_back(x);
int mid=(nl+nr)>>;
if(r<=mid)ins(k<<,nl,mid,l,r,x);
else if(l>mid)ins(k<<|,mid+,nr,l,r,x);
else ins(k<<,nl,mid,l,mid,x),ins(k<<|,mid+,nr,mid+,r,x);
}
void dfs(int k,int l,int r){
vector<int>lastf;
int m=e[k].size(),mid=(l+r)>>;
for(register int i=;i<m;i++){
int x=u[e[k][i]],y=v[e[k][i]];
x=gf(x),y=gf(y);
if(siz[x]>siz[y])swap(x,y);
lastf.push_back(x);f[x]=y;siz[y]+=siz[x];
}
if(gf()==gf(n))ans+=a[r+]-a[l];
else if(l<r)dfs(k<<,l,mid),dfs(k<<|,mid+,r);
m=lastf.size();
for(register int i=m-;i>=;i--)f[lastf[i]]=lastf[i];
lastf.clear();
}
int main(){
read(n),read(m);
for(int i=;i<=n;i++)f[i]=i,siz[i]=;
for(int i=;i<=m;i++){
read(u[i]),read(v[i]),read(l[i]),read(r[i]);
a[++cnt]=l[i];a[++cnt]=r[i]+;
}
sort(a+,a++cnt);cnt=unique(a+,a++cnt)-a-;
for(int i=;i<=m;i++)
ins(,,cnt-,lower_bound(a+,a++cnt,l[i])-a,lower_bound(a+,a++cnt,r[i]+)-a-,i);
dfs(,,cnt-);printf("%d\n",ans);
return ;
}

LCT代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=;
struct edge{
int x,y,a,b;
}e[maxn];
bool cmp (edge w,edge v){
return w.b>v.b;
}
void read(int &x){
char c=getchar(); x=;
for(;c>''||c<'';c=getchar());
for(;c<=''&&c>='';c=getchar()) x=(x<<)+(x<<)+c-'';
}
struct LCT
{
int Max[maxn],rev[maxn],ch[maxn][],fa[maxn],stc[maxn],top;
int isroot(int x){
return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;
}
int get(int x){
return ch[fa[x]][]==x;
}
void pushdown(int x)
{
if(!rev[x]||!x) return ;
swap(ch[x][],ch[x][]);
if(ch[x][]) rev[ch[x][]]^=;
if(ch[x][]) rev[ch[x][]]^=;
rev[x]=;
}
void pushup(int x)
{
Max[x]=x;
if(ch[x][]&&e[Max[ch[x][]]].a>e[Max[x]].a) Max[x]=Max[ch[x][]];
if(ch[x][]&&e[Max[ch[x][]]].a>e[Max[x]].a) Max[x]=Max[ch[x][]];
}
void rotate(int x)
{
int old=fa[x],fold=fa[old],opt=get(x);
if(!isroot(old)) ch[fold][get(old)]=x;
fa[x]=fold;
ch[old][opt]=ch[x][opt^]; fa[ch[old][opt]]=old;
ch[x][opt^]=old; fa[old]=x;
pushup(old); pushup(x);
}
void splay(int x)
{
int top=; stc[++top]=x;
for(int i=x;!isroot(i);i=fa[i]) stc[++top]=fa[i];
for(int i=top;i;i--) pushdown(stc[i]);
for(int f;!isroot(x);rotate(x)){
if(!isroot(f=fa[x]))
rotate(get(x)==get(f)?f:x);
}
}
void access(int x)
{
int rson=;
for(;x;rson=x,x=fa[x]){
splay(x);
ch[x][]=rson;
pushup(x);
}
}
int find(int x){ access(x); splay(x); while(ch[x][]) x=ch[x][]; return x;}
int query(int x,int y) { make_root(y); access(x); splay(x); return Max[x]; }
void make_root(int x) { access(x); splay(x); rev[x]^=; }
void link(int x,int y) { make_root(x); fa[x]=y; splay(x); }
void cut(int x,int y) { make_root(x); access(y); splay(y); fa[x]=ch[y][]=; }
}S;
pair<int,int>fcy[maxn]; int tot;
int main()
{
int N,M,i,ans=;
read(N); read(M);
for(i=;i<=M;i++){
read(e[i].x); read(e[i].y);
read(e[i].a); read(e[i].b);
}
sort(e+,e+M+,cmp);
for(i=;i<=M;i++){
if(S.find(M+e[i].x)!=S.find(M+e[i].y)) {
S.link(i,M+e[i].x);
S.link(i,M+e[i].y);
}
else{
int tmp=S.query(M+e[i].x,M+e[i].y);
if(e[tmp].a>e[i].a) {
S.cut(tmp,M+e[tmp].x); S.cut(tmp,M+e[tmp].y);
S.link(i,M+e[i].x); S.link(i,M+e[i].y);
}
}
if(S.find(M+)==S.find(M+N)){
int tmp=S.query(M+,M+N);
if(e[tmp].a<=e[i].b){
tot++;
fcy[tot].first=e[tmp].a; fcy[tot].second=e[i].b;
}
}
}
sort(fcy+,fcy+tot+);
for(i=;i<=tot;i++){
int j=i,Mx=fcy[i].second;
while(j+<=tot&&fcy[j+].first<=Mx) {
j++; Mx=max(Mx,fcy[j].second);
}
ans+=Mx-fcy[i].first+;
i=j;
}
printf("%d\n",ans);
return ;
}

2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)的更多相关文章

  1. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  2. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  3. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  4. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  5. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  6. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  7. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  8. 2019牛客暑期多校训练营(第二场)J-Subarray(思维)

    >传送门< 前言 这题我前前后后看了三遍,每次都是把网上相关的博客和通过代码认真看了再思考,然并卵,最后终于第三遍也就是现在终于看懂了,其实懂了之后发现其实没有那么难,但是的的确确需要思维 ...

  9. 2019牛客暑期多校训练营(第一场)-A (单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个长度均为n的数组a和b,求最大的p使得(a1,ap)和(b1,bp)等价,等价的定义为其任意 ...

随机推荐

  1. jQuery必知必会

    原文地址:https://my.oschina.net/u/218421/blog/37391 jQuery优势 轻量级    强大的选择器   出色的DOM操作的封装   可靠的事件处理机制   完 ...

  2. 在myecplice中关联svn

    1:下载插件 site-1.8.22 2:找到myecplic的安装目录 下的dropins 文件夹(例如:C:\Users\han\AppData\Local\MyEclipse Professio ...

  3. CentOS 7下JumpServer安装及配置

    环境 系统 # cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) # uname -r 3.10.0-693.21.1.el7. ...

  4. [转帖]/var/log/wtmp文件的作用

    /var/log/wtmp文件的作用 https://blog.51cto.com/oldyunwei/1658778   /var/log/wtmp是一个二进制文件,记录每个用户的登录次数和持续时间 ...

  5. List/Map 导出到表格(使用注解和反射)

    Java 的 POI 库可以用来创建和操作 Excel 表格,有时候我们只需要简单地将 List 或 Map 导出到表格,样板代码比较多,不够优雅.如果能像 Gson 那样,使用注解标记要导出的属性, ...

  6. (原创)C#操作MYSQL数据库

    应用程序对数据库的操作都是只有4个:增,删,改,查. 只有”查”的操作需要使用适配器来存储查询得到的数据.其它3个操作不需要用到适配器. 不同的数据库有共同操作方法:都要建立连接对象,连接对象要有连接 ...

  7. 服务发现框架选型: Consul、Zookeeper还是etcd ?

    背景 本文并不介绍服务发现的基本原理.除了一致性算法之外,其他并没有太多高深的算法,网上的资料很容易让大家明白上面是服务发现.想直接查看结论的同学,请直接跳到文末.目前,市面上有非常多的服务发现工具, ...

  8. Kafka分布式的消息顺序

    Kafka分布式的单位是partition,同一个partition用一个write ahead log组织,所以可以保证FIFO的顺序.不同partition之间不能保证顺序. 但是绝大多数用户都可 ...

  9. 2019 游族网络java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.游族网络等公司offer,岗位是Java后端开发,因为发展原因最终选择去了游族网络,入职一年时间了,也成为了面 ...

  10. Java自学-类和对象 包

    Java中的 包 包: package 把比较接近的类,规划在同一个包下 步骤 1 : 把比较接近的类,规划在同一个包下 Hero,ADHero 规划在一个包,叫做charactor(角色) Item ...