제출 #986233

#제출 시각아이디문제언어결과실행 시간메모리
986233GrindMachineReconstruction Project (JOI22_reconstruction)C++17
100 / 100
1327 ms100708 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

/*

refs:
some solutions

*/

const int MOD = 1e9 + 7;
const int N = 500 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int n;
    vector<T> tr;

    fenwick() {

    }

    fenwick(int n_) {
        n = n_;
        tr = vector<T>(n + 1);
    }

    int lsb(int x) {
        return x & -x;
    }

    void pupd(int i, T v) {
        for(; i <= n; i += lsb(i)){
            tr[i] += v;
        }
    }

    T sum(int i) {
        T res = 0;
        for(; i; i ^= lsb(i)){
            res += tr[i];
        }
        return res;
    }

    T query(int l, int r) {
        if (l > r) return 0;
        T res = sum(r) - sum(l - 1);
        return res;
    }

    int lower_bound(T s){
        // first pos with sum >= s
        if(sum(n) < s) return n+1;
        int i = 0;
        rev(bit,16,0){
            int j = i+(1<<bit);
            if(j > n) conts;
            if(tr[j] < s){
                s -= tr[j];
                i = j;
            }
        }

        return i+1;
    }

    int upper_bound(T s){
        return lower_bound(s+1);
    }
};

vector<pll> adj[N];

ll dfs1(ll u, ll p, ll des, ll mn){
    if(u == des){
        return mn;
    }

    ll ans = inf1;

    for(auto [v,id] : adj[u]){
        if(v == p) conts;
        amin(ans,dfs1(v,u,des,min(mn,id)));
    }

    return ans;
}

ll dfs2(ll u, ll p, ll des, ll mx){
    if(u == des){
        return mx;
    }

    ll ans = 0;

    for(auto [v,id] : adj[u]){
        if(v == p) conts;
        amax(ans,dfs2(v,u,des,max(mx,id)));
    }

    return ans;
}

void solve(int test_case)
{
    ll n,m; cin >> n >> m;
    vector<array<ll,3>> edges(m+5);

    rep1(i,m){
        ll u,v,w; cin >> u >> v >> w;
        edges[i] = {w,u,v};
    }

    sort(edges.begin()+1,edges.begin()+m+1);

    vector<ll> lx(m+5,1), rx(m+5,inf1);

    auto rem = [&](ll id){
        auto [w,u,v] = edges[id];
        adj[u].erase(find(all(adj[u]),make_pair(v,id)));
        adj[v].erase(find(all(adj[v]),make_pair(u,id)));
    };

    // find L[i]
    rep1(i,m){
        auto [w,u,v] = edges[i];
        ll j = dfs1(u,-1,v,inf1);
        if(j <= m){
            rem(j);
            lx[i] = (w+edges[j][0])/2+1;
        }
        adj[u].pb({v,i}), adj[v].pb({u,i});
    }

    // find R[i]
    rep1(i,n) adj[i].clear();

    rev(i,m,1){
        auto [w,u,v] = edges[i];
        ll j = dfs2(u,-1,v,0);
        if(j){
            rem(j);
            rx[i] = (w+edges[j][0])>>1;
        }
        adj[u].pb({v,i}), adj[v].pb({u,i});
    }

    vector<array<ll,3>> events;
    vector<ll> b;

    rep1(i,m){
        ll w = edges[i][0];
        events.pb({lx[i],1,w});
        events.pb({rx[i],3,w});
        b.pb(w);
    }

    ll q; cin >> q;
    rep1(id,q){
        ll x; cin >> x;
        events.pb({x,2,id});
        b.pb(x);
    }

    b.pb(0);
    sort(all(b));
    b.resize(unique(all(b))-b.begin());
    ll siz = sz(b)-1;
    fenwick<ll> fenw_cnt(siz+5), fenw_sum(siz+5);

    auto f = [&](ll x){
        return lower_bound(all(b),x)-b.begin();
    };

    sort(all(events));
    vector<ll> ans(q+5);

    for(auto [x,t,w] : events){
        if(t == 1){
            ll ind = f(w);
            fenw_cnt.pupd(ind,1);
            fenw_sum.pupd(ind,w);
        }
        else if(t == 2){
            ll ind = f(x);
            ll id = w;
            ll lc = fenw_cnt.query(1,ind-1), ls = fenw_sum.query(1,ind-1);
            ll rc = fenw_cnt.query(ind+1,siz), rs = fenw_sum.query(ind+1,siz);
            ans[id] = (x*lc-ls)+rs-(x*rc);
        }
        else{
            ll ind = f(w);
            fenw_cnt.pupd(ind,-1);
            fenw_sum.pupd(ind,-w);
        }
    }

    rep1(i,q) cout << ans[i] << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...