탐색기 (2019 니안 가축 오프 더 학교 여덟 번째 필드 E + 제목 + 세그먼트 트리 취소 분리 된 세트)

주제 링크

문제의 의미

당신에게 무향 그래프, 각 에지 부여 \을 (u_i, V_I \) 의 범위에 무게 \ ([L_i, R_i] \) , 당신은 용량 원하는이 인연의 조건을 통과하기 위해 \를 ([L_i , R_i] \) , 당신이 지금 얼마나 많은 용량의 종류 당신이에서 얻을 수 있도록 요청해야합니다 \ (1 \) 갔다 합니다 (N- \) \를 .

생각

이어 거물 코드는 취소 해체-설정하고 보여 영업 부문 트리를 파도 좋은 음식 아 느낌을 배운다.

첫째, 우리는와 세그먼트 트리 노드 영역 값들의 대응 중량 범위의 내측을 유지하기 위해 이산 세트 사용 \ (벡터 \)의 노드 번호 (간격이 지나치게 크기 때문에, 우리는 이산 좌측 확대를 구축해야 할 유의 사항을 저장할 수를 바로 열린 세그먼트 트리). 쿼리 시간 아래로 모든 모서리의 엔드 포인트에 해당하는 저장 노드 번호를 통해 모든 방법 두 개의 분리 된 세트 라인이 노드에 도달 할 때 잎이 모든 잎 노드에 해당하는 설명을 포함에 연결을 유지하기 위해 부 에지가 분리 된 세트에 유지하고 새롭게 판정 \ (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;
}

추천

출처www.cnblogs.com/Dillonh/p/11334020.html