トピックリンク
問題の意味
あなたに無向グラフを付け、各エッジ\(u_i、V_I \)の範囲内の重み\は([L_iを、R_iと] \) 、あなたは容量の場合は、この縁の条件を通過する\([L_iをします、R_iと] \) 、あなたが今どのように多くの容量の種類のあなたから得ることができるようにあなたに依頼する必要が\(1 \)行ってきました(N- \)\を。
考え
続く大物コード取消互いに素セットを波と事業セグメントツリーを表示し、良い食べ物ああを感じて学びます。
まず、我々が有するセグメントツリーノードゾーン、値の対応する重量範囲の内側に維持するために、互いに素セットを使用\が(ベクトル\)ノード番号を保存することができる(間隔が大きすぎるので、我々は、離散左近いを構築する必要があることに注意を右オープンセグメントツリー)。クエリダウンタイムですべてのエッジの端点に対応する格納されたノード番号を介してすべての方法2つの互いに素設定ラインがノードに到達したときに葉が全てのリーフノードに対応する記述を含んで接続性を維持しますセクションエッジは、互いに素なセットで維持再度決定される\(1 \)と\(N- \)このセクションの接合を説明するためではない、回答に追加された長さとなり、ブロックと通信しています。セクションができないという点に対応する\(1 \)来る\(N- \)脱退互いに素なセットのセット時間をバックトラックする前に、合成動作を制御します。
コード
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 200000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
pii st[maxn*4];
int n, m, tot, tp, ans;
vector<int> vec[maxn*4];
int u[maxn], v[maxn], L[maxn], R[maxn];
int num[maxn*2], fa[maxn], sz[maxn];
int fi(int x) {
return fa[x] == x ? x : fi(fa[x]);
}
void merge(int u, int v) {
int x = fi(u), y = fi(v);
if(x == y) {
st[++tp] = {-1, -1}; //按照我的写法不能省略,因为我是根据结点对应的vector的size来撤消的,少了这个那么tp会减到负数导致re。
return;
}
if(sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y];
fa[y] = x;
st[++tp] = {x, y};
}
void cancel() {
int x = st[tp].first;
int y = st[tp--].second;
if(x == -1) return;
sz[x] -= sz[y];
fa[y] = y;
}
void update(int l, int r, int id, int rt, int L, int R) {
if(l <= L && R <= r) {
vec[rt].emplace_back(id);
return;
}
int mid = (L + R) >> 1;
if(r <= mid) update(l, r, id, lson);
else if(l > mid) update(l, r, id, rson);
else {
update(l, mid, id, lson);
update(mid + 1, r, id, rson);
}
}
void query(int l, int r, int rt) {
for(int i = 0; i < (int)vec[rt].size(); ++i) {
int id = vec[rt][i];
merge(u[id], v[id]);
}
if(l == r) {
int x = fi(1), y = fi(n);
if(x == y) {
ans += num[r+1] - num[l];
}
for(int i = 0; i < (int)vec[rt].size(); ++i) {
cancel();
}
return;
}
int mid = (l + r) >> 1;
query(l, mid, rt<<1);
query(mid + 1, r, rt<<1|1);
for(int i = 0; i < (int)vec[rt].size(); ++i) {
cancel();
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d%d", &u[i], &v[i], &L[i], &R[i]);
num[++tot] = L[i];
num[++tot] = R[i] + 1;
}
sort(num + 1, num + tot + 1);
tot = unique(num + 1, num + tot + 1) - num - 1;
for(int i = 1; i <= m; ++i) {
int l = lower_bound(num + 1, num + tot + 1, L[i]) - num;
int r = lower_bound(num + 1, num + tot + 1, R[i] + 1) - num;
update(l, r - 1, i, 1, 1, tot);
}
for(int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;
query(1, tot, 1);
printf("%d\n", ans);
return 0;
}