답안 #368105

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
368105 2021-02-19T14:18:20 Z egekabas Janjetina (COCI21_janjetina) C++14
0 / 110
23 ms 24044 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll n, k;
struct fenwick{
    vector<ll> bit;
    vector<ll> vals;
    void init(){
        sort(all(vals));
        vals.resize(unique(all(vals))-vals.begin());
        bit.resize(vals.size()+1);
    }
    void upd(ll idx, ll val){
        idx = lower_bound(all(vals), idx)-vals.begin();
        for(++idx; idx < bit.size(); idx += idx&(-idx))
            bit[idx] += val;
    }
    ll get(ll idx, ll smaller = 0){
        idx = lower_bound(all(vals), idx)-vals.begin();
        idx -= smaller;
        if(idx == vals.size()) --idx;
        ll ret = 0;
        for(++idx; idx > 0; idx -= idx&(-idx))
            ret += bit[idx];
        return ret;
    }
};
vector<pii> g[100009];
ll subsz[100009];
ll block[100009];
void getsz(ll v, ll prt){
    subsz[v] = 1;
    for(auto u : g[v])
        if(u.ff != prt && !block[u.ff]){
            getsz(u.ff, v);
            subsz[v] += subsz[u.ff];
        }
}
ll getcentroid(ll v, ll prt, ll tot){
    for(auto u : g[v])
        if(u.ff != prt && !block[u.ff] && subsz[u.ff]*2 >= tot)
            return getcentroid(u.ff, v, tot);
    return v;
}
vector<pll> child[100009];
void dfs(ll v, ll prt,ll d, ll curmax, ll top){
    child[top].pb({curmax, d});
    for(auto u : g[v])
        if(u.ff != prt && !block[u.ff])
            dfs(u.ff, v, d+1, max(curmax, u.ss), top);
}
vector<ll> rel;
fenwick seg[400009];
void add(ll v, ll tl, ll tr, ll idx, ll val){
    rel.pb(v);
    seg[v].vals.pb(val);
    if(tl == tr) return;
    if(idx <= (tl+tr)/2)
        add(2*v, tl, (tl+tr)/2, idx, val);
    else
        add(2*v+1, (tl+tr)/2+1, tr, idx, val);
}
void upd(ll v, ll tl, ll tr, ll idx, ll val, ll change){
    seg[v].upd(val, change);
    if(tl == tr) return;
    if(idx <= (tl+tr)/2)
        upd(2*v, tl, (tl+tr)/2, idx, val, change);
    else
        upd(2*v+1, (tl+tr)/2+1, tr, idx, val, change);
}
ll mx = 1e5;
ll ans;
ll doubleans;
void get(ll v, ll tl, ll tr, ll l, ll r, ll val){
    if(tr < l || r < tl)
        return;
    if(l <= tl && tr <= r){
        ll small = seg[v].get(val, 1);
        /*if(small < 0){
            cout << tl << ' ' << tr << ' ' << val << '\n';
            for(auto u : seg[v].vals)
                cout << u << ' ';
            cout << '\n';
        }*/
        ans += small;
        doubleans += seg[v].get(val)-small;
        
    }
    else{
        get(2*v, tl, (tl+tr)/2, l, r, val);
        get(2*v+1, (tl+tr)/2+1, tr, l, r, val);        
    }
}
void getans(ll v){
    getsz(v, 0);
    v = getcentroid(v, 0, subsz[v]);
    for(auto u : g[v]){
        if(block[u.ff]) continue;
        dfs(u.ff, v, 1, u.ss, u.ff);
        for(auto u : child[u.ff]){
            if(u.ff-u.ss-k >= 0){
                ++ans;
            }
            add(1, 0, mx, u.ss, u.ff);
        }
    }
    for(auto u : rel)
        if(seg[u].bit.empty())
            seg[u].init();
    for(auto u : g[v]){
        if(block[u.ff]) continue;
        for(auto u : child[u.ff]){
            upd(1, 0, mx, u.ss, u.ff, 1);
        }
    }
    for(auto u : g[v]){
        if(block[u.ff]) continue;
        for(auto u : child[u.ff]){
            upd(1, 0, mx, u.ss, u.ff, -1);
        }
        for(auto u : child[u.ff]){
            get(1, 0, mx, 0, u.ff-u.ss-k, u.ff);
        }
        for(auto u : child[u.ff]){
            upd(1, 0, mx, u.ss, u.ff, 1);
        }
    }
    for(auto u : rel){
        seg[u].bit.clear();
        seg[u].vals.clear();
    }
    for(auto u : g[v])
        child[u.ff].clear();
    
    block[v] = 1;
    for(auto u : g[v])
        if(!block[u.ff])
            getans(u.ff);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    cin >> n >> k;
    for(ll i = 0; i < n-1; ++i){
        ll x, y, z;
        cin >> x >> y >> z;
        g[x].pb({y, z});
        g[y].pb({x, z});
    }
    getans(1);
    cout << ans*2+doubleans << '\n';
}

Compilation message

Main.cpp: In member function 'void fenwick::upd(ll, ll)':
Main.cpp:26:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |         for(++idx; idx < bit.size(); idx += idx&(-idx))
      |                    ~~~~^~~~~~~~~~~~
Main.cpp: In member function 'll fenwick::get(ll, ll)':
Main.cpp:32:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |         if(idx == vals.size()) --idx;
      |            ~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Incorrect 19 ms 24044 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Incorrect 23 ms 24044 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23916 KB Output is correct
2 Correct 16 ms 23788 KB Output is correct
3 Incorrect 19 ms 24044 KB Output isn't correct
4 Halted 0 ms 0 KB -