Codeforces 293E Close Vertices 点分治 + 树状数组

点分治之后用树状数组维护个数。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int n, l, w;
vector<PII> G[N];
bool ban[N];
int son[N];
LL ans;

struct Bit {
    int a[N];
    inline void modify(int x, int v) {
        for(int i = x; i < N; i += i & -i)
            a[i] += v;
    }
    inline int sum(int x) {
        int ans = 0;
        for(int i = x; i; i -= i & -i)
            ans += a[i];
        return ans;
    }
    inline int query(int L, int R) {
        if(L > R) return 0;
        return sum(R) - sum(L - 1);
    }
} bit;

int getSubTreeSize(int u, int fa) {
    son[u] = 1;
    for(auto &e : G[u]) {
        if(e.se == fa || ban[e.se]) continue;
        son[u] += getSubTreeSize(e.se, u);
    }
    return son[u];
}

PII getSubTreeCenter(int u, int fa, int all) {
    PII res = mk(inf, -1);
    int s = 1, m = 0;
    for(auto &e : G[u]) {
        if(e.se == fa || ban[e.se]) continue;
        res = min(res, getSubTreeCenter(e.se, u, all));
        m = max(m, son[e.se]);
        s += son[e.se];
    }
    m = max(m, all - s);
    res = min(res, mk(m, u));
    return res;
}

void getDis(int u, int fa, int ld, int wd, vector<PII> &ds) {
    if(ld <= l && wd <= w) ans++;
    ds.push_back(mk(wd, ld));
    for(auto& e : G[u]) {
        if(e.se == fa || ban[e.se]) continue;
        getDis(e.se, u, ld + 1, wd + e.fi, ds);
    }
}

LL calc(vector<PII> &ds) {
    LL ans = 0;
    int n = ds.size();
    if(n <= 1) return 0;
    sort(ds.begin(), ds.end());
    for(auto &t : ds) bit.modify(t.se, 1);
    for(int i = 0, j = n - 1; i < n && i <= j; i++) {
        while(ds[i].fi + ds[j].fi > w && i <= j) {
            bit.modify(ds[j].se, -1);
            j--;
        }
        if(i > j) break;
        bit.modify(ds[i].se, -1);
        if(ds[i].se <= l) ans += bit.sum(l - ds[i].se);
    }
    return ans;
}

void solveSubPro(int u) {
    getSubTreeSize(u, 0);
    int s = getSubTreeCenter(u, 0, son[u]).se;
    ban[s] = true;
    vector<PII> ds, tds;
    for(auto &e : G[s]) {
        if(ban[e.se]) continue;
        tds.clear();
        getDis(e.se, s, 1, e.fi, tds);
        ans -= calc(tds);
        ds.insert(ds.end(), tds.begin(), tds.end());
    }
    ans += calc(ds);
    for(auto &e : G[s]) {
        if(ban[e.se]) continue;
        solveSubPro(e.se);
    }
}

int main() {
    scanf("%d%d%d", &n, &l, &w);
    for(int i = 1; i < n; i++) {
        int p, w;
        scanf("%d%d", &p, &w);
        G[p].push_back(mk(w, i + 1));
        G[i + 1].push_back(mk(w, p));
    }
    solveSubPro(1);
    printf("%lld\n", ans);
    return 0;
}

/*
*/

 

posted @ 2019-06-07 00:42  NotNight  阅读(151)  评论(0编辑  收藏  举报