Submission #825379

# Submission time Handle Problem Language Result Execution time Memory
825379 2023-08-14T19:05:47 Z winter0101 Designated Cities (JOI19_designated_cities) C++14
100 / 100
441 ms 63856 KB
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define eb emplace_back
#define pli pair<long long,int>
//#define int long long
pli cmp(pli &p, pli &q){
if (p.fi>q.fi)return p;
return q;
}
struct IT{
vector<pli>st;
vector<long long>lazy;
void resz(int n){
st.resize(4*n+9);
lazy.resize(4*n+9);
}
void push(int id){
if (lazy[id]){
    lazy[id*2]+=lazy[id];
    lazy[id*2+1]+=lazy[id];
    st[id*2].fi+=lazy[id];
    st[id*2+1].fi+=lazy[id];
    lazy[id]=0;
}
}
void update(int id,int l,int r,int u,int v,long long val){
if (l>v||r<u||u>v)return;
if (u<=l&&r<=v){
    st[id].fi+=val;
    lazy[id]+=val;
    if (l==r)st[id].se=l;
    return;
}
push(id);
int mid=(l+r)/2;
update(id*2,l,mid,u,v,val);
update(id*2+1,mid+1,r,u,v,val);
st[id]=cmp(st[id*2],st[id*2+1]);
}
};
const int maxn=2e5+9;
vector<pil>a[maxn];
long long f[maxn];//subtree
long long g[maxn];//g[i] if i root
pli dfs1(int u,int par){
pli test={0,u};
for (auto v:a[u]){
    if (v.fi==par)continue;
    auto tmp=dfs1(v.fi,u);
    tmp.fi+=v.se;
    test=cmp(test,tmp);
    f[u]+=v.se+f[v.fi];
}
return test;
}
void dfs2(int u,int par){
    for (auto v:a[u]){
        if (v.fi==par){
            g[u]+=v.se;
        }
    }
    for (auto v:a[u]){
        if (v.fi==par)continue;
        g[v.fi]=g[u]-v.se;
        dfs2(v.fi,u);
    }
}
long long dep[maxn],ans[maxn],w[maxn];
int in[maxn],out[maxn],rev[maxn],tme=0,p[maxn];
bool vis[maxn];
void dfs3(int u,int par){
    in[u]=++tme;
    rev[tme]=u;
    for (auto v:a[u]){
        if (v.fi==par)continue;
        dep[v.fi]=dep[u]+v.se;
        w[v.fi]=v.se;
        p[v.fi]=u;
        dfs3(v.fi,u);
    }
    out[u]=tme;
}
IT st;
int n;
void del(int u){
    while (u){
        if (vis[u])return;
        st.update(1,1,n,in[u],out[u],-w[u]);
        vis[u]=1;
        u=p[u];
    }
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("temp.INP","r",stdin);
    //freopen("temp.OUT","w",stdout);
    cin>>n;
    for1(i,1,n-1){
    int u,v;
    long long x,y;
    cin>>u>>v>>x>>y;
    a[u].pb({v,x});
    a[v].pb({u,y});
    }
    dfs1(1,0);
    g[1]=f[1];
    dfs2(1,0);
    int r1=min_element(g+1,g+1+n)-g;
    ans[1]=g[r1];
    int root=dfs1(r1,0).se;
    vis[root]=1;
    dfs3(root,0);
    st.resz(n);
    for1(i,1,n){
    st.update(1,1,n,in[i],in[i],dep[i]);
    }
    long long sum=g[root];
    for1(i,2,n){
    if (st.st[1].fi==0){
        ans[i]=sum;
        continue;
    }
    sum-=st.st[1].fi;
    ans[i]=sum;
    del(rev[st.st[1].se]);
    }
    int q;
    cin>>q;
    for1(i,1,q){
    int x;
    cin>>x;
    cout<<ans[x]<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 358 ms 45044 KB Output is correct
3 Correct 389 ms 60628 KB Output is correct
4 Correct 311 ms 45192 KB Output is correct
5 Correct 347 ms 45084 KB Output is correct
6 Correct 411 ms 47444 KB Output is correct
7 Correct 316 ms 44572 KB Output is correct
8 Correct 438 ms 61624 KB Output is correct
9 Correct 298 ms 42808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 352 ms 45068 KB Output is correct
3 Correct 361 ms 63856 KB Output is correct
4 Correct 291 ms 45068 KB Output is correct
5 Correct 386 ms 45236 KB Output is correct
6 Correct 353 ms 48228 KB Output is correct
7 Correct 297 ms 44296 KB Output is correct
8 Correct 417 ms 55232 KB Output is correct
9 Correct 300 ms 42824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 5 ms 5460 KB Output is correct
14 Correct 4 ms 5588 KB Output is correct
15 Correct 4 ms 5460 KB Output is correct
16 Correct 4 ms 5460 KB Output is correct
17 Correct 4 ms 5460 KB Output is correct
18 Correct 4 ms 5460 KB Output is correct
19 Correct 4 ms 5460 KB Output is correct
20 Correct 4 ms 5460 KB Output is correct
21 Correct 5 ms 5460 KB Output is correct
22 Correct 4 ms 5416 KB Output is correct
23 Correct 4 ms 5392 KB Output is correct
24 Correct 4 ms 5460 KB Output is correct
25 Correct 4 ms 5588 KB Output is correct
26 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 358 ms 45044 KB Output is correct
3 Correct 389 ms 60628 KB Output is correct
4 Correct 311 ms 45192 KB Output is correct
5 Correct 347 ms 45084 KB Output is correct
6 Correct 411 ms 47444 KB Output is correct
7 Correct 316 ms 44572 KB Output is correct
8 Correct 438 ms 61624 KB Output is correct
9 Correct 298 ms 42808 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 352 ms 45068 KB Output is correct
12 Correct 361 ms 63856 KB Output is correct
13 Correct 291 ms 45068 KB Output is correct
14 Correct 386 ms 45236 KB Output is correct
15 Correct 353 ms 48228 KB Output is correct
16 Correct 297 ms 44296 KB Output is correct
17 Correct 417 ms 55232 KB Output is correct
18 Correct 300 ms 42824 KB Output is correct
19 Correct 2 ms 5076 KB Output is correct
20 Correct 352 ms 45144 KB Output is correct
21 Correct 369 ms 63308 KB Output is correct
22 Correct 349 ms 45096 KB Output is correct
23 Correct 355 ms 45368 KB Output is correct
24 Correct 400 ms 45460 KB Output is correct
25 Correct 337 ms 45496 KB Output is correct
26 Correct 385 ms 45504 KB Output is correct
27 Correct 345 ms 45056 KB Output is correct
28 Correct 387 ms 47332 KB Output is correct
29 Correct 344 ms 45720 KB Output is correct
30 Correct 350 ms 44980 KB Output is correct
31 Correct 314 ms 44488 KB Output is correct
32 Correct 441 ms 55996 KB Output is correct
33 Correct 279 ms 42768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 358 ms 45044 KB Output is correct
14 Correct 389 ms 60628 KB Output is correct
15 Correct 311 ms 45192 KB Output is correct
16 Correct 347 ms 45084 KB Output is correct
17 Correct 411 ms 47444 KB Output is correct
18 Correct 316 ms 44572 KB Output is correct
19 Correct 438 ms 61624 KB Output is correct
20 Correct 298 ms 42808 KB Output is correct
21 Correct 2 ms 5076 KB Output is correct
22 Correct 352 ms 45068 KB Output is correct
23 Correct 361 ms 63856 KB Output is correct
24 Correct 291 ms 45068 KB Output is correct
25 Correct 386 ms 45236 KB Output is correct
26 Correct 353 ms 48228 KB Output is correct
27 Correct 297 ms 44296 KB Output is correct
28 Correct 417 ms 55232 KB Output is correct
29 Correct 300 ms 42824 KB Output is correct
30 Correct 3 ms 5076 KB Output is correct
31 Correct 5 ms 5460 KB Output is correct
32 Correct 4 ms 5588 KB Output is correct
33 Correct 4 ms 5460 KB Output is correct
34 Correct 4 ms 5460 KB Output is correct
35 Correct 4 ms 5460 KB Output is correct
36 Correct 4 ms 5460 KB Output is correct
37 Correct 4 ms 5460 KB Output is correct
38 Correct 4 ms 5460 KB Output is correct
39 Correct 5 ms 5460 KB Output is correct
40 Correct 4 ms 5416 KB Output is correct
41 Correct 4 ms 5392 KB Output is correct
42 Correct 4 ms 5460 KB Output is correct
43 Correct 4 ms 5588 KB Output is correct
44 Correct 4 ms 5460 KB Output is correct
45 Correct 2 ms 5076 KB Output is correct
46 Correct 352 ms 45144 KB Output is correct
47 Correct 369 ms 63308 KB Output is correct
48 Correct 349 ms 45096 KB Output is correct
49 Correct 355 ms 45368 KB Output is correct
50 Correct 400 ms 45460 KB Output is correct
51 Correct 337 ms 45496 KB Output is correct
52 Correct 385 ms 45504 KB Output is correct
53 Correct 345 ms 45056 KB Output is correct
54 Correct 387 ms 47332 KB Output is correct
55 Correct 344 ms 45720 KB Output is correct
56 Correct 350 ms 44980 KB Output is correct
57 Correct 314 ms 44488 KB Output is correct
58 Correct 441 ms 55996 KB Output is correct
59 Correct 279 ms 42768 KB Output is correct
60 Correct 2 ms 5076 KB Output is correct
61 Correct 380 ms 46468 KB Output is correct
62 Correct 376 ms 63436 KB Output is correct
63 Correct 331 ms 46544 KB Output is correct
64 Correct 367 ms 46852 KB Output is correct
65 Correct 382 ms 46560 KB Output is correct
66 Correct 400 ms 46828 KB Output is correct
67 Correct 417 ms 46580 KB Output is correct
68 Correct 367 ms 46656 KB Output is correct
69 Correct 399 ms 49204 KB Output is correct
70 Correct 371 ms 47148 KB Output is correct
71 Correct 348 ms 46576 KB Output is correct
72 Correct 334 ms 46616 KB Output is correct
73 Correct 425 ms 58668 KB Output is correct
74 Correct 354 ms 45680 KB Output is correct