2019牛客暑期多校训练营(第八场) E Explorer(线段树+模拟并查集)

链接:https://ac.nowcoder.com/acm/contest/888/E
来源:牛客网
 

题目描述

Gromah and LZR have entered the fifth level. Unlike the first four levels, they should do some moves in this level.

 

There are nn_{}n​ vertices and mm_{}m​ bidirectional roads in this level, each road is in format (u,v,l,r)(u, v, l, r)_{}(u,v,l,r)​, which means that vertex uu_{}u​ and vv_{}v​ are connected by this road, but the sizes of passers should be in interval [l,r][l, r]_{}[l,r]​. Since passers with small size are likely to be attacked by other animals and passers with large size may be blocked by some narrow roads.

 

Moreover, vertex 11_{}1​ is the starting point and vertex nn_{}n​ is the destination. Gromah and LZR should go from vertex 11_{}1​ to vertex nn_{}n​ to enter the next level.

 

At the beginning of their exploration, they may drink a magic potion to set their sizes to a fixed positive integer. They want to know the number of positive integer sizes that make it possible for them to go from 11_{}1​ to nn_{}n​.

 

Please help them to find the number of valid sizes.
 

输入描述:

 

The first line contains two positive integers n,mn,m_{}n,m​, denoting the number of vertices and roads.

 

Following m lines each contains four positive integers u,v,l,ru, v, l, r_{}u,v,l,r​, denoting a bidirectional road (u,v,l,r)(u, v, l, r)_{}(u,v,l,r)​.

 

 

1≤n,m≤105,1≤u<v≤n,1≤l≤r≤1091 \le n,m \le 10^5, 1 \le u < v \le n, 1 \le l \le r \le 10^91≤n,m≤105,1≤u<v≤n,1≤l≤r≤109

输出描述:

Print a non-negative integer in a single line, denoting the number of valid sizes.

示例1

输入

复制

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

输出

复制

2

说明

 

There are 2 valid sizes : 2 and 3.

 

For size 2, there exists a path 1→2→3→51 \rightarrow 2 \rightarrow 3 \rightarrow 51→2→3→5.

 

For size 3, there exists a path 1→2→4→51 \rightarrow 2 \rightarrow 4 \rightarrow 51→2→4→5.

#include<bits/stdc++.h>
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
vector<int>G[800010];
int n, m, dep[100010], ans, a[200010], cnt, l[100010], r[100010], u[100010], v[100010], f[100010];
inline int gf(int x) { return x == f[x] ? f[x] : gf(f[x]); }
inline void update(int rt, int L, int R, int l, int r, int x) {
	if (l <= L && R <= r) {
		G[rt].push_back(x);
		return;
	}
	int mid = (L + R) >> 1;
	if (l <= mid)update(ls, L, mid, l, r, x);
	if (r > mid)update(rs, mid + 1, R, l, r, x);
}
inline void dfs(int rt, int l, int r) {
	vector<int>lastf;
	int len = G[rt].size(), mid = (l + r) >> 1;
	for (int i = 0; i < len; i++) {
		int x = u[G[rt][i]], y = v[G[rt][i]];
		int fx = gf(x), fy = gf(y);
		if (dep[fx] > dep[fy])lastf.push_back(fy), f[fy] = fx;//这里dep指的深度
		else {
			lastf.push_back(fx), f[fx] = fy;
			if (dep[fx] == dep[fy])dep[fy]++;
		}
	}
	if (gf(1) == gf(n))ans += a[r + 1] - a[l];
	else if (l < r)dfs(ls, l, mid), dfs(rs, mid + 1, r);
	len = lastf.size();
	for (int i = len - 1; i >= 0; i--) {
		f[lastf[i]] = lastf[i];
	}
	lastf.clear();
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)f[i] = i, dep[i] = 1;
	for (int i = 1; i <= m; i++) {
		scanf("%d%d%d%d", &u[i], &v[i], &l[i], &r[i]);
		a[++cnt] = l[i]; a[++cnt] = r[i] + 1;
	}
	sort(a + 1, a + 1 + cnt); cnt = unique(a + 1, a + 1 + cnt) - a - 1;
	for (int i = 1; i <= m; i++)
		update(1, 1, cnt - 1, lower_bound(a + 1, a + 1 + cnt, l[i]) - a, lower_bound(a + 1, a + 1 + cnt, r[i] + 1) - a - 1, i);
	dfs(1, 1, cnt - 1); printf("%d\n", ans);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值