제출 #917425

#제출 시각아이디문제언어결과실행 시간메모리
917425hotboy2703Two Currencies (JOI23_currencies)C++14
100 / 100
2228 ms52416 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define MASK(i) (1LL<<(i))
#define BIT(mask,i) (((mask) >> (i))&1LL)
const ll MAXN = 1e5;
const ll MAXK = 17;
vector <ll> g[MAXN + 100];
ll in[MAXN+100],out[MAXN+100];
ll timeDFS;
ll sp[MAXN+100][MAXK];
ll depth[MAXN+100];
pll edge[MAXN+100];
pll station[MAXN + 100];
ll pl[MAXN+100],pr[MAXN+100],pans[MAXN+100],ans[MAXN+100];
struct query{
    ll s,t,x,y,l;
};
query all[MAXN + 100];
ll n,m,q;
namespace seg{
    pll operator + (pll x,pll y){
        return {x.fi+y.fi,x.se+y.se};
    }
    pll operator - (pll x,pll y){
        return {x.fi-y.fi,x.se-y.se};
    }
    pll tree[MAXN*4+100];
    void init(){
        for (ll i = 1;i <= n*4+10;i++)tree[i] = {0,0};
    }
    void add(ll id,pll delta){
        tree[id] = tree[id]+delta;
    }
    void lz(ll id){
        add(id<<1,tree[id]);
        add(id<<1|1,tree[id]);
        tree[id] = {0,0};
    }
    void update(ll id,ll l,ll r,ll l1,ll r1,pll delta){
        if (l > r1 || l1 > r)return;
        if (l1 <= l && r <= r1){
            add(id,delta);
            return;
        }
        lz(id);
        ll mid = (l + r) >> 1;
        update(id<<1,l,mid,l1,r1,delta);
        update(id<<1|1,mid+1,r,l1,r1,delta);
    }
    pll get(ll id,ll l,ll r,ll i){
        if (l == r){
            return tree[id];
        }
        lz(id);
        ll mid = (l + r) >> 1;
        if (mid >= i)return get(id<<1,l,mid,i);
        else return get(id<<1|1,mid+1,r,i);
    }
    void update(ll u,ll silver){
        update(1,1,n,in[u],out[u],{-1,silver});
    }
    pll get(ll i){
        return get(1,1,n,in[i]);
    }
    void gold(ll u){
        update(1,1,n,in[u],out[u],{+1,0});
    }
    pll eval(query x){
        return get(x.s)+get(x.t)-get(x.l)-get(x.l);
    }
}

ll lca(ll u,ll v){
    if (depth[u] > depth[v])swap(v,u);
    for (ll j = MAXK-1;j >= 0;j --){
        if (depth[v] - MASK(j) >= depth[u]){
            v = sp[v][j];
        }
    }
    if (u==v)return u;
    for (ll j = MAXK-1;j >= 0;j --){
        if (sp[u][j] != sp[v][j]){
            u = sp[u][j];
            v = sp[v][j];
        }
    }
    return sp[u][0];
}

void dfs(ll u,ll p){
    in[u] = ++timeDFS;
    depth[u] = depth[p]+1;
    sp[u][0] = p;
    for (ll i = 1;i < MAXK;i ++){
        sp[u][i] = sp[sp[u][i-1]][i-1];
    }
    for (auto v:g[u]){
        if (v==p)continue;
        dfs(v,u);
    }
    out[u] = timeDFS;
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
    cin>>n>>m>>q;
    for (ll i = 1;i < n;i ++){
        ll u,v;
        cin>>u>>v;
        edge[i] = {u,v};
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1,1);
    for (ll i = 1;i <= m;i ++){
        cin>>station[i].se>>station[i].fi;
        ll u = edge[station[i].se].fi, v = edge[station[i].se].se;
        if (depth[u] < depth[v])station[i].se = v;
        else station[i].se = u;
//        cout<<station[i].fi<<' '<<station[i].se<<'\n';
    }
    sort(station+1,station+1+m);
    for (ll i = 1;i <= q;i ++){
        cin>>all[i].s>>all[i].t>>all[i].x>>all[i].y;
        all[i].l = lca(all[i].s,all[i].t);
    }
    for (ll i = 1;i <= q;i ++){
        pl[i] = 0;
        pr[i] = m;
        pans[i] = -1;
    }
    while (1){
        vector <pll> ask;
        for (ll i = 1;i <= q;i ++){
            if (pl[i] <= pr[i]){
                 ask.emplace_back((pl[i]+pr[i])/2,i);
            }
        }
        if (ask.empty())break;
        sort(ask.begin(),ask.end());
        ll ptr = 0;
        seg::init();
        for (ll i = 1;i <= m;i ++){
            seg::gold(station[i].se);
        }
        for (ll i = 0;i <= m;i ++){
            if (i)seg::update(station[i].se,station[i].fi);
            while (ptr<sz(ask) && ask[ptr].fi == i){
                ll id = ask[ptr].se;
                pll tmp = seg::eval(all[id]);
                if (all[id].y >= tmp.se){
                    pans[id] = i;
                    pl[id] = i+1;
                    ans[id] = tmp.fi;
                }
                else{
                    pr[id] = i-1;
                }
                ptr++;
            }
        }
    }
    for (ll i = 1;i <= q;i ++){
//        cout<<pans[i]<<' ';
        if (ans[i] > all[i].x){cout<<-1;}
        else cout<<all[i].x-ans[i];
        cout<<'\n';
    }
    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...