Submission #866809

# Submission time Handle Problem Language Result Execution time Memory
866809 2023-10-27T07:35:47 Z imarn Two Currencies (JOI23_currencies) C++14
28 / 100
362 ms 145996 KB
#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define vi vector<ll>
using namespace std;
const int N=1e5+5;
struct node{
    ll cnt,tt;
    node *l,*r;
    node (ll x=0,ll y=0) : cnt(x),tt(y),l(NULL),r(NULL){};
    node (node* le,node*re) : cnt(le->cnt+re->cnt),tt(le->tt+re->tt),l(le),r(re){};
};
vector<pair<int,vi>>g[N];
pii road[N];
vi w[N];
vi we;int M;
node *root[N];
int pr[N][20]{0};
int dep[N]{0};
node* build(int l,int r){
    if(l==r)return new node();
    int m=(l+r)>>1;
    return new node(build(l,m),build(m+1,r));
}
node* upd(node*t,int l,int r,int pos,ll val){
    if(l==r){
        return new node(t->cnt+1,t->tt+val);
    }int m=(l+r)>>1;
    if(m>=pos)return new node(upd(t->l,l,m,pos,val),t->r);
    else return new node(t->l,upd(t->r,m+1,r,pos,val));
}
void dfs(int u,int p,int l){
    pr[u][0]=p;
    dep[u]=l;
    for(auto v:g[u]){
        if(v.f==p)continue;
        root[v.f]=root[u];
        for(auto it : v.s)root[v.f]=upd(root[v.f],1,M,lower_bound(all(we),it)-we.begin()+1,it);
        dfs(v.f,u,l+1);
    }
}
int lc(int a,int b){
    if(dep[a]>dep[b])swap(a,b);
    for(int i=19;i>=0;i--)if(dep[b]-(1<<i)>=dep[a])b=pr[b][i];
    if(a==b)return a;
    for(int i=19;i>=0;i--)if(pr[a][i]!=pr[b][i])a=pr[a][i],b=pr[b][i];
    return pr[a][0];
}
int qr(node*tl,node*tr,node*ta,int l,int r,ll k){
    if(l==r){
        return min(k,tl->tt+tr->tt-2*ta->tt)/we[l-1];
    }
    int m=(l+r)>>1;
    ll lcnt=tl->tt+tr->tt-2*ta->tt;//cout<<lcnt<<" "<<tl->cnt+tr->cnt-2*ta->cnt<<"\n";
    if(lcnt>k)return qr(tl->l,tr->l,ta->l,l,m,k);
    else return tl->cnt+tr->cnt-2*ta->cnt+qr(tl->r,tr->r,ta->r,m+1,r,k-lcnt);
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,m,q;cin>>n>>m>>q;M=m;
    for(int i=1;i<=n-1;i++)cin>>road[i].f>>road[i].s;
    for(int i=0;i<m;i++){
        int p;ll c;cin>>p>>c;
        we.pb(c);w[p].pb(c);
    }sort(we.begin(),we.end());
    for(int i=1;i<=n-1;i++){
        g[road[i].f].pb({road[i].s,w[i]});
        g[road[i].s].pb({road[i].f,w[i]});
    }
    root[1]=build(1,m);
    dfs(1,1,0);
    for(int j=1;j<20;j++)for(int i=1;i<=n;i++)pr[i][j]=pr[pr[i][j-1]][j-1];
    while(q--){
        int u,v;ll x,y;cin>>u>>v>>x>>y;
        int lca=lc(u,v);
        int all = root[u]->cnt+root[v]->cnt-2*root[lca]->cnt;
        int cur = min(qr(root[u],root[v],root[lca],1,m,y),all);
        cout<<(x+cur-all>=0?x+cur-all:-1)<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Incorrect 5 ms 12124 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 5 ms 12308 KB Output is correct
3 Correct 6 ms 12380 KB Output is correct
4 Correct 5 ms 12380 KB Output is correct
5 Correct 229 ms 118900 KB Output is correct
6 Correct 227 ms 108532 KB Output is correct
7 Correct 217 ms 93632 KB Output is correct
8 Correct 180 ms 87244 KB Output is correct
9 Correct 202 ms 95176 KB Output is correct
10 Correct 290 ms 129784 KB Output is correct
11 Correct 326 ms 129772 KB Output is correct
12 Correct 288 ms 130176 KB Output is correct
13 Correct 301 ms 129740 KB Output is correct
14 Correct 295 ms 130040 KB Output is correct
15 Correct 362 ms 145180 KB Output is correct
16 Correct 362 ms 145996 KB Output is correct
17 Correct 337 ms 144460 KB Output is correct
18 Correct 331 ms 130220 KB Output is correct
19 Correct 321 ms 130264 KB Output is correct
20 Correct 349 ms 130660 KB Output is correct
21 Correct 243 ms 128484 KB Output is correct
22 Correct 218 ms 128592 KB Output is correct
23 Correct 249 ms 128576 KB Output is correct
24 Correct 222 ms 128444 KB Output is correct
25 Correct 242 ms 129020 KB Output is correct
26 Correct 251 ms 129100 KB Output is correct
27 Correct 246 ms 129080 KB Output is correct
28 Correct 208 ms 129648 KB Output is correct
29 Correct 206 ms 129732 KB Output is correct
30 Correct 219 ms 129972 KB Output is correct
31 Correct 216 ms 129988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Incorrect 6 ms 12380 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Incorrect 5 ms 12124 KB Output isn't correct
6 Halted 0 ms 0 KB -