链接: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;
}