Submission #1068678

# Submission time Handle Problem Language Result Execution time Memory
1068678 2024-08-21T11:19:24 Z new_acc Truck Driver (IOI23_deliveries) C++17
100 / 100
907 ms 39988 KB
#include "deliveries.h"
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
const int N=1e5+10;
const int SS=1<<17;
const int INFi=1e9;
ll seg1[SS*2+10],seg2[SS*2+10],kr[N],sc[N],sum,vmn[N],sk[N],cres;
int dol[N],gora[N],pod[N],li,t[N],n,odw[N],oj[N];
bool ci[N];
pair<int,int> pre[N];
set<pair<ll,int>> s;
vector<pair<int,int>> graf[N];
void upd1(int a,ll b){
    for(a+=SS;a>0;a/=2) seg1[a]+=b;
}
void upd2(int a,ll b){
    for(a+=SS;a>0;a/=2) seg2[a]+=b;
}
ll qr1(int a,int b){
    ll res=0;
    for(a+=SS-1,b+=SS+1;a/2!=b/2;a/=2,b/=2){
        if(a%2==0) res+=seg1[a+1];
        if(b%2) res+=seg1[b-1];
    }
    return res;
}
ll qr2(int a,int b){
    ll res=0;
    for(a+=SS-1,b+=SS+1;a/2!=b/2;a/=2,b/=2){
        if(a%2==0) res+=seg2[a+1];
        if(b%2) res+=seg2[b-1];
    }
    return res;
}
void dfs(int v,int o,int val){
    oj[v]=o;
    kr[v]=val;
    pod[v]=1;
    for(auto [u,c]:graf[v]){
        if(u==o) continue;
        sk[u]=sk[v]+c;
        dfs(u,v,c);
        pod[v]+=pod[u];
    }
}
void dfs2(int v,int o){
    int g=-1,g2=0;
    pre[v].fi=++li;
    odw[li]=v;
    dol[v]=v;
    if(ci[v]) gora[v]=gora[o];
    else gora[v]=v;
    for(auto [u,c]:graf[v]){
        if(u==o) continue;
        if(g==-1 or pod[u]>pod[g]) g=u,g2=c;
    }
    if(g!=-1){
        ci[g]=1;
        sc[g]=sc[v]+g2;
        dfs2(g,v);
        dol[v]=dol[g];
    }
    for(auto [u,c]:graf[v]){
        if(u==o or u==g) continue;
        dfs2(u,v);
    }
    pre[v].se=li;
}
void build(int v){
    if(v>=SS){
        int id=v-SS;
        if(id<=n and id>=1) seg1[v]=t[odw[id]];
    }else{
        build(v*2),build(v*2+1);
        seg1[v]=seg1[v*2]+seg1[v*2+1];
    }
}
void zm(int a,int b){
    ll r=b-t[a];
    t[a]=b;
    sum+=r;
    cres+=r*sk[a];
    upd1(pre[a].fi,r);
    if(ci[a]) upd2(pre[a].fi,r*sc[a]);
    while(a!=1){
        if(!ci[a]){
            upd2(pre[oj[a]].fi,r*sc[oj[a]]);
            s.erase({vmn[a],-pre[a].fi});
            vmn[a]+=r;
            s.insert({vmn[a],-pre[a].fi}); 
            a=oj[a];
        }else a=gora[a];
    }
    s.erase({vmn[a],-pre[a].fi});
    vmn[a]+=r;
    s.insert({vmn[a],-pre[a].fi}); 
}
int zn(){
    auto it=s.lower_bound(make_pair(sum/2+1,-INFi));
    if(it==s.end()) return -1;
    int v=odw[-(*it).se];
    int pocz=pre[v].fi,kon=pre[dol[v]].fi,sr,res=0;
    while(pocz<=kon){
        sr=(pocz+kon)/2;
        if(qr1(pre[odw[sr]].fi,pre[odw[sr]].se)>=sum/2+1){
            res=odw[sr];
            pocz=sr+1;
        }else kon=sr-1;
    }
    return res;
}
ll ob(int a){
    ll res=0;
    while(a!=1){
        if(ci[a]){
            res+=qr1(pre[a].fi,pre[a].se)*sc[a];
            res+=qr2(pre[gora[a]].fi,pre[a].fi-1);
            a=gora[a];
        }else{
            res+=vmn[a]*kr[a];
            a=oj[a];
        }
    }
    return res;
}
void init(int nn, std::vector<int> U, std::vector<int> V, std::vector<int> T, std::vector<int> W) {
    n=nn;
    for(int i=0;i<n-1;i++){
        graf[U[i]+1].push_back({V[i]+1,T[i]});
        graf[V[i]+1].push_back({U[i]+1,T[i]});
    }
    for(int i=0;i<n;i++) t[i+1]=W[i];     
    t[1]++;
    for(int i=1;i<=n;i++) sum+=t[i];
    dfs(1,1,0),dfs2(1,1);
    build(1);
    for(int i=1;i<=n;i++){
        ll xd=qr1(pre[i].fi,pre[i].se);
        cres+=kr[i]*xd;
        if(!ci[i]){
            vmn[i]=xd;
            s.insert({xd,-pre[i].fi});
            upd2(pre[oj[i]].fi,xd*sc[oj[i]]);
        }else{
            upd2(pre[i].fi,(ll)t[i]*sc[i]);
        }
    }
	return;
}

long long max_time(int S, int X) {
    if(S==0) X++;
    zm(S+1,X);
    int a=zn();
    if(a==-1) return cres*2LL;
    ll pom=ob(a);
    ll dwa=0;
    int xdd=a;
    while(xdd!=1){
        dwa+=qr1(pre[xdd].fi,pre[xdd].se)*kr[xdd];
        xdd=oj[xdd];
    }
    ll ans=cres-pom*2+sum*sk[a];
	return ans*2LL;
}
# Verdict Execution time Memory Grader output
1 Correct 91 ms 18256 KB Output is correct
2 Correct 106 ms 17876 KB Output is correct
3 Correct 93 ms 18256 KB Output is correct
4 Correct 89 ms 18000 KB Output is correct
5 Correct 108 ms 18256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 3 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 3 ms 10840 KB Output is correct
6 Correct 3 ms 10840 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 3 ms 11044 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 10844 KB Output is correct
11 Correct 3 ms 11000 KB Output is correct
12 Correct 4 ms 10840 KB Output is correct
13 Correct 3 ms 10840 KB Output is correct
14 Correct 3 ms 10956 KB Output is correct
15 Correct 4 ms 10840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 18256 KB Output is correct
2 Correct 106 ms 17876 KB Output is correct
3 Correct 93 ms 18256 KB Output is correct
4 Correct 89 ms 18000 KB Output is correct
5 Correct 108 ms 18256 KB Output is correct
6 Correct 4 ms 10844 KB Output is correct
7 Correct 7 ms 11096 KB Output is correct
8 Correct 64 ms 13140 KB Output is correct
9 Correct 907 ms 34132 KB Output is correct
10 Correct 846 ms 34260 KB Output is correct
11 Correct 825 ms 34080 KB Output is correct
12 Correct 877 ms 36656 KB Output is correct
13 Correct 835 ms 36652 KB Output is correct
14 Correct 851 ms 36520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 18256 KB Output is correct
2 Correct 106 ms 17876 KB Output is correct
3 Correct 93 ms 18256 KB Output is correct
4 Correct 89 ms 18000 KB Output is correct
5 Correct 108 ms 18256 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 4 ms 11100 KB Output is correct
8 Correct 27 ms 13084 KB Output is correct
9 Correct 358 ms 33272 KB Output is correct
10 Correct 365 ms 33324 KB Output is correct
11 Correct 368 ms 33332 KB Output is correct
12 Correct 301 ms 34952 KB Output is correct
13 Correct 328 ms 35116 KB Output is correct
14 Correct 276 ms 33328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 18256 KB Output is correct
2 Correct 106 ms 17876 KB Output is correct
3 Correct 93 ms 18256 KB Output is correct
4 Correct 89 ms 18000 KB Output is correct
5 Correct 108 ms 18256 KB Output is correct
6 Correct 2 ms 10840 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 3 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 3 ms 10840 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 11044 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 11000 KB Output is correct
17 Correct 4 ms 10840 KB Output is correct
18 Correct 3 ms 10840 KB Output is correct
19 Correct 3 ms 10956 KB Output is correct
20 Correct 4 ms 10840 KB Output is correct
21 Correct 4 ms 10844 KB Output is correct
22 Correct 7 ms 11096 KB Output is correct
23 Correct 64 ms 13140 KB Output is correct
24 Correct 907 ms 34132 KB Output is correct
25 Correct 846 ms 34260 KB Output is correct
26 Correct 825 ms 34080 KB Output is correct
27 Correct 877 ms 36656 KB Output is correct
28 Correct 835 ms 36652 KB Output is correct
29 Correct 851 ms 36520 KB Output is correct
30 Correct 2 ms 10844 KB Output is correct
31 Correct 4 ms 11100 KB Output is correct
32 Correct 27 ms 13084 KB Output is correct
33 Correct 358 ms 33272 KB Output is correct
34 Correct 365 ms 33324 KB Output is correct
35 Correct 368 ms 33332 KB Output is correct
36 Correct 301 ms 34952 KB Output is correct
37 Correct 328 ms 35116 KB Output is correct
38 Correct 276 ms 33328 KB Output is correct
39 Correct 3 ms 10840 KB Output is correct
40 Correct 5 ms 11100 KB Output is correct
41 Correct 44 ms 13568 KB Output is correct
42 Correct 545 ms 37164 KB Output is correct
43 Correct 587 ms 37248 KB Output is correct
44 Correct 510 ms 37704 KB Output is correct
45 Correct 540 ms 37836 KB Output is correct
46 Correct 545 ms 37936 KB Output is correct
47 Correct 452 ms 39212 KB Output is correct
48 Correct 499 ms 39496 KB Output is correct
49 Correct 415 ms 39956 KB Output is correct
50 Correct 425 ms 39984 KB Output is correct
51 Correct 350 ms 39988 KB Output is correct
52 Correct 667 ms 36328 KB Output is correct
53 Correct 682 ms 36316 KB Output is correct
54 Correct 653 ms 36304 KB Output is correct
55 Correct 309 ms 35884 KB Output is correct
56 Correct 352 ms 37508 KB Output is correct
57 Correct 388 ms 37424 KB Output is correct