답안 #865893

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
865893 2023-10-25T02:18:17 Z guagua0407 Designated Cities (JOI19_designated_cities) C++17
100 / 100
1231 ms 116808 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=2e5+5;
vector<pair<ll,pair<ll,ll>>> adj[mxn];
ll n,q;
vector<ll> ans(mxn);
map<pair<int,int>,int> mp;
vector<pair<ll,ll>> segtree(mxn*4);
vector<ll> lazy(mxn*4);
int st[mxn],en[mxn];
int rev[mxn];
bool used[mxn];
int par[mxn];
pair<ll,ll> w[mxn];
int cnt=1;
ll dp[mxn];
ll dp1[mxn];
ll dp2[mxn];
ll dp3[mxn];
ll dp4[mxn];
ll inf=1e18;

void push(int v){
    segtree[v*2].f+=lazy[v];
    segtree[v*2+1].f+=lazy[v];
    lazy[v*2]+=lazy[v];
    lazy[v*2+1]+=lazy[v];
    lazy[v]=0;
}

void build(int l=1,int r=n,int v=1){
    if(l==r){
        segtree[v]={dp[rev[l]],rev[l]};
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,v*2);
    build(mid+1,r,v*2+1);
    segtree[v]=max(segtree[v*2],segtree[v*2+1]);
}

void update(int tl,int tr,ll val,int l=1,int r=n,int v=1){
    if(r<tl or tr<l){
        return;
    }
    if(tl<=l and r<=tr){
        segtree[v].f+=val;
        lazy[v]+=val;
        return;
    }
    push(v);
    int mid=(l+r)/2;
    update(tl,min(mid,tr),val,l,mid,v*2);
    update(max(mid+1,tl),tr,val,mid+1,r,v*2+1);
    segtree[v]=max(segtree[v*2],segtree[v*2+1]);
}

void dfs(int v,int p=-1,ll val=0){
    par[v]=p;
    dp[v]=val;
    st[v]=cnt++;
    rev[st[v]]=v;
    for(auto u:adj[v]){
        if(u.f==p) continue;
        w[u.f]={u.s.f,u.s.s};
        if(mp.count({u.f,v})) dfs(u.f,v,val+u.s.s);
        else dfs(u.f,v,val+u.s.f);
    }
    en[v]=cnt-1;
}

void dfs1(int v,int p=-1){
    for(auto u:adj[v]){
        if(u.f==p) continue;
        dfs1(u.f,v);
        dp1[v]+=dp1[u.f];
        if(mp.count({u.f,v})) dp1[v]+=u.s.f;
        else dp1[v]+=u.s.s;
    }
}

void dfs2(int v,int p=-1,ll val=0){
    dp2[v]=dp1[v]+val;
    for(auto u:adj[v]){
        if(u.f==p) continue;
        ll sum=dp2[v];
        if(mp.count({u.f,v})){
            sum-=u.s.f+dp1[u.f];
            sum+=u.s.s;
        }
        else{
            sum-=u.s.s+dp1[u.f];
            sum+=u.s.f;
        }
        dfs2(u.f,v,sum);
    }
}

void dfs3(int v,int p=-1){
    for(auto u:adj[v]){
        if(u.f==p) continue;
        dfs3(u.f,v);
        if(mp.count({u.f,v})){
            dp3[v]=max(dp3[v],dp3[u.f]+u.s.s);
        }
        else{
            dp3[v]=max(dp3[v],dp3[u.f]+u.s.f);
        }
    }
}

void dfs4(int v,int p=-1,ll val=0){
    dp4[v]=dp2[v]+max(dp3[v],val);
    multiset<ll> S;
    for(auto u:adj[v]){
        if(u.f==p) continue;
        if(mp.count({u.f,v})){
            S.insert(u.s.s+dp3[u.f]);
        }
        else{
            S.insert(u.s.f+dp3[u.f]);
        }
    }
    for(auto u:adj[v]){
        if(u.f==p) continue;
        if(mp.count({u.f,v})){
            S.erase(S.find(u.s.s+dp3[u.f]));
        }
        else{
            S.erase(S.find(u.s.f+dp3[u.f]));
        }
        ll mx=val;
        if(!S.empty()){
            mx=max(mx,*S.rbegin());
        }
        if(mp.count({u.f,v})){
            mx+=u.s.f;
        }
        else{
            mx+=u.s.s;
        }
        if(mp.count({u.f,v})){
            S.insert(u.s.s+dp3[u.f]);
        }
        else{
            S.insert(u.s.f+dp3[u.f]);
        }
        dfs4(u.f,v,mx);
    }
}


ll cal1(){
    dfs1(1);
    dfs2(1);
    return *max_element(dp2+1,dp2+n+1);
}

ll cal2(){
    dfs3(1);
    dfs4(1);
    return max_element(dp4+1,dp4+n+1)-dp4;
}

int main() {_
    cin>>n;
    ll sum=0;
    for(int i=0;i<n-1;i++){
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        adj[a].push_back({b,{c,d}});
        adj[b].push_back({a,{c,d}});
        mp[{a,b}]=1;
        sum+=c;
        sum+=d;
    }
    ans[1]=sum-cal1();
    int root=cal2();
    dfs(root);
    build();
    ll cur=dp2[root];
    //cout<<root<<'\n';
    for(int i=2;i<=n;i++){
        auto tmp=segtree[1];
        cur+=tmp.f;
        ans[i]=sum-cur;
        int v=tmp.s;
        //cout<<tmp.f<<' '<<tmp.s<<'\n';
        while(!used[v] and par[v]!=-1){
            used[v]=true;
            if(mp.count({v,par[v]})){
                update(st[v],en[v],-w[v].s);
            }
            else{
                update(st[v],en[v],-w[v].f);
            }
            v=par[v];
        }
    }
    cin>>q;
    for(int i=0;i<q;i++){
        int x;
        cin>>x;
        cout<<ans[x]<<'\n';
    }
    return 0;
}
//maybe its multiset not set

Compilation message

designated_cities.cpp: In function 'void setIO(std::string)':
designated_cities.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33368 KB Output is correct
2 Correct 6 ms 33368 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33476 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33484 KB Output is correct
9 Correct 6 ms 33372 KB Output is correct
10 Correct 6 ms 33412 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33624 KB Output is correct
2 Correct 1109 ms 69004 KB Output is correct
3 Correct 1144 ms 106896 KB Output is correct
4 Correct 1045 ms 68176 KB Output is correct
5 Correct 1101 ms 70340 KB Output is correct
6 Correct 1136 ms 75860 KB Output is correct
7 Correct 1092 ms 72964 KB Output is correct
8 Correct 1229 ms 108652 KB Output is correct
9 Correct 1043 ms 75516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 33372 KB Output is correct
2 Correct 1118 ms 69088 KB Output is correct
3 Correct 1173 ms 113236 KB Output is correct
4 Correct 1046 ms 68716 KB Output is correct
5 Correct 1113 ms 70264 KB Output is correct
6 Correct 1134 ms 76412 KB Output is correct
7 Correct 1084 ms 76560 KB Output is correct
8 Correct 1196 ms 95272 KB Output is correct
9 Correct 1046 ms 74444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33368 KB Output is correct
2 Correct 6 ms 33368 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33476 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33484 KB Output is correct
9 Correct 6 ms 33372 KB Output is correct
10 Correct 6 ms 33412 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 6 ms 33372 KB Output is correct
13 Correct 9 ms 33916 KB Output is correct
14 Correct 10 ms 33972 KB Output is correct
15 Correct 9 ms 33820 KB Output is correct
16 Correct 10 ms 33892 KB Output is correct
17 Correct 10 ms 33884 KB Output is correct
18 Correct 9 ms 33828 KB Output is correct
19 Correct 10 ms 33884 KB Output is correct
20 Correct 10 ms 33884 KB Output is correct
21 Correct 10 ms 33752 KB Output is correct
22 Correct 10 ms 33884 KB Output is correct
23 Correct 10 ms 33884 KB Output is correct
24 Correct 9 ms 33884 KB Output is correct
25 Correct 10 ms 34164 KB Output is correct
26 Correct 10 ms 34136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33624 KB Output is correct
2 Correct 1109 ms 69004 KB Output is correct
3 Correct 1144 ms 106896 KB Output is correct
4 Correct 1045 ms 68176 KB Output is correct
5 Correct 1101 ms 70340 KB Output is correct
6 Correct 1136 ms 75860 KB Output is correct
7 Correct 1092 ms 72964 KB Output is correct
8 Correct 1229 ms 108652 KB Output is correct
9 Correct 1043 ms 75516 KB Output is correct
10 Correct 7 ms 33372 KB Output is correct
11 Correct 1118 ms 69088 KB Output is correct
12 Correct 1173 ms 113236 KB Output is correct
13 Correct 1046 ms 68716 KB Output is correct
14 Correct 1113 ms 70264 KB Output is correct
15 Correct 1134 ms 76412 KB Output is correct
16 Correct 1084 ms 76560 KB Output is correct
17 Correct 1196 ms 95272 KB Output is correct
18 Correct 1046 ms 74444 KB Output is correct
19 Correct 6 ms 33372 KB Output is correct
20 Correct 1128 ms 69072 KB Output is correct
21 Correct 1174 ms 116808 KB Output is correct
22 Correct 1077 ms 70500 KB Output is correct
23 Correct 1097 ms 72024 KB Output is correct
24 Correct 1112 ms 71252 KB Output is correct
25 Correct 1093 ms 72240 KB Output is correct
26 Correct 1100 ms 70904 KB Output is correct
27 Correct 1174 ms 73156 KB Output is correct
28 Correct 1146 ms 78436 KB Output is correct
29 Correct 1072 ms 72788 KB Output is correct
30 Correct 1100 ms 72048 KB Output is correct
31 Correct 1126 ms 76368 KB Output is correct
32 Correct 1226 ms 100432 KB Output is correct
33 Correct 1123 ms 77560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 33368 KB Output is correct
2 Correct 6 ms 33368 KB Output is correct
3 Correct 6 ms 33372 KB Output is correct
4 Correct 6 ms 33372 KB Output is correct
5 Correct 6 ms 33372 KB Output is correct
6 Correct 6 ms 33476 KB Output is correct
7 Correct 6 ms 33372 KB Output is correct
8 Correct 6 ms 33484 KB Output is correct
9 Correct 6 ms 33372 KB Output is correct
10 Correct 6 ms 33412 KB Output is correct
11 Correct 6 ms 33372 KB Output is correct
12 Correct 6 ms 33624 KB Output is correct
13 Correct 1109 ms 69004 KB Output is correct
14 Correct 1144 ms 106896 KB Output is correct
15 Correct 1045 ms 68176 KB Output is correct
16 Correct 1101 ms 70340 KB Output is correct
17 Correct 1136 ms 75860 KB Output is correct
18 Correct 1092 ms 72964 KB Output is correct
19 Correct 1229 ms 108652 KB Output is correct
20 Correct 1043 ms 75516 KB Output is correct
21 Correct 7 ms 33372 KB Output is correct
22 Correct 1118 ms 69088 KB Output is correct
23 Correct 1173 ms 113236 KB Output is correct
24 Correct 1046 ms 68716 KB Output is correct
25 Correct 1113 ms 70264 KB Output is correct
26 Correct 1134 ms 76412 KB Output is correct
27 Correct 1084 ms 76560 KB Output is correct
28 Correct 1196 ms 95272 KB Output is correct
29 Correct 1046 ms 74444 KB Output is correct
30 Correct 6 ms 33372 KB Output is correct
31 Correct 9 ms 33916 KB Output is correct
32 Correct 10 ms 33972 KB Output is correct
33 Correct 9 ms 33820 KB Output is correct
34 Correct 10 ms 33892 KB Output is correct
35 Correct 10 ms 33884 KB Output is correct
36 Correct 9 ms 33828 KB Output is correct
37 Correct 10 ms 33884 KB Output is correct
38 Correct 10 ms 33884 KB Output is correct
39 Correct 10 ms 33752 KB Output is correct
40 Correct 10 ms 33884 KB Output is correct
41 Correct 10 ms 33884 KB Output is correct
42 Correct 9 ms 33884 KB Output is correct
43 Correct 10 ms 34164 KB Output is correct
44 Correct 10 ms 34136 KB Output is correct
45 Correct 6 ms 33372 KB Output is correct
46 Correct 1128 ms 69072 KB Output is correct
47 Correct 1174 ms 116808 KB Output is correct
48 Correct 1077 ms 70500 KB Output is correct
49 Correct 1097 ms 72024 KB Output is correct
50 Correct 1112 ms 71252 KB Output is correct
51 Correct 1093 ms 72240 KB Output is correct
52 Correct 1100 ms 70904 KB Output is correct
53 Correct 1174 ms 73156 KB Output is correct
54 Correct 1146 ms 78436 KB Output is correct
55 Correct 1072 ms 72788 KB Output is correct
56 Correct 1100 ms 72048 KB Output is correct
57 Correct 1126 ms 76368 KB Output is correct
58 Correct 1226 ms 100432 KB Output is correct
59 Correct 1123 ms 77560 KB Output is correct
60 Correct 6 ms 33368 KB Output is correct
61 Correct 1181 ms 74312 KB Output is correct
62 Correct 1177 ms 112100 KB Output is correct
63 Correct 1072 ms 73116 KB Output is correct
64 Correct 1129 ms 74644 KB Output is correct
65 Correct 1119 ms 73292 KB Output is correct
66 Correct 1183 ms 74736 KB Output is correct
67 Correct 1164 ms 73520 KB Output is correct
68 Correct 1153 ms 75704 KB Output is correct
69 Correct 1158 ms 80212 KB Output is correct
70 Correct 1078 ms 75508 KB Output is correct
71 Correct 1093 ms 75036 KB Output is correct
72 Correct 1112 ms 79380 KB Output is correct
73 Correct 1231 ms 98156 KB Output is correct
74 Correct 1125 ms 81640 KB Output is correct