Submission #1113017

# Submission time Handle Problem Language Result Execution time Memory
1113017 2024-11-15T12:10:14 Z guagua0407 Fireworks (APIO16_fireworks) C++17
7 / 100
7 ms 16720 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);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

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

const int mxn=6e5+5;
vector<pair<int,int>> adj[mxn];
ll d[mxn];
ll val[mxn];
pair<ll,ll> pt[mxn];
int n,m;

void dfs(int v,int p=0){
    if(v>n){
        pt[v]={d[v],d[v]};
        val[v]=0;
        //cout<<v<<'\n';
    //cout<<pt[v].f<<' '<<pt[v].s<<' '<<val[v]<<'\n';
        return;
    }
    vector<ll> vec;
    for(auto u:adj[v]){
        d[u.f]=d[v]+u.s;
        dfs(u.f,v);
        vec.push_back(pt[u.f].f);
        vec.push_back(pt[u.f].s);
    }
    sort(all(vec));
    int sz=(int)vec.size();
    ll x=vec[(sz-1)/2];
    for(auto u:adj[v]){
        val[v]+=val[u.f];
        if(x<pt[u.f].f){
            val[v]+=pt[u.f].f-x;
        }
        else if(x>pt[u.f].s){
            val[v]+=x-pt[u.f].s;
        }
    }
    pt[v]={vec[(sz-1)/2],vec[(sz-1)/2+1]};
    //cout<<v<<'\n';
    //cout<<pt[v].f<<' '<<pt[v].s<<' '<<val[v]<<'\n';
}

int main() {_
    cin>>n>>m;
    for(int i=2;i<=n+m;i++){
        int p,c;
        cin>>p>>c;
        adj[p].push_back({i,c});
    }
    dfs(1);
    cout<<val[1]<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//diaoborz

Compilation message

fireworks.cpp: In function 'void setIO(std::string)':
fireworks.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fireworks.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 16720 KB Output is correct
2 Correct 4 ms 14672 KB Output is correct
3 Correct 4 ms 16720 KB Output is correct
4 Correct 6 ms 14672 KB Output is correct
5 Correct 7 ms 14672 KB Output is correct
6 Correct 7 ms 14672 KB Output is correct
7 Correct 6 ms 14672 KB Output is correct
8 Correct 6 ms 14672 KB Output is correct
9 Correct 5 ms 14672 KB Output is correct
10 Correct 5 ms 14672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14672 KB Output is correct
2 Correct 4 ms 14672 KB Output is correct
3 Incorrect 4 ms 14672 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 16720 KB Output is correct
2 Correct 4 ms 14672 KB Output is correct
3 Correct 4 ms 16720 KB Output is correct
4 Correct 6 ms 14672 KB Output is correct
5 Correct 7 ms 14672 KB Output is correct
6 Correct 7 ms 14672 KB Output is correct
7 Correct 6 ms 14672 KB Output is correct
8 Correct 6 ms 14672 KB Output is correct
9 Correct 5 ms 14672 KB Output is correct
10 Correct 5 ms 14672 KB Output is correct
11 Correct 5 ms 14672 KB Output is correct
12 Correct 4 ms 14672 KB Output is correct
13 Incorrect 4 ms 14672 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 16720 KB Output is correct
2 Correct 4 ms 14672 KB Output is correct
3 Correct 4 ms 16720 KB Output is correct
4 Correct 6 ms 14672 KB Output is correct
5 Correct 7 ms 14672 KB Output is correct
6 Correct 7 ms 14672 KB Output is correct
7 Correct 6 ms 14672 KB Output is correct
8 Correct 6 ms 14672 KB Output is correct
9 Correct 5 ms 14672 KB Output is correct
10 Correct 5 ms 14672 KB Output is correct
11 Correct 5 ms 14672 KB Output is correct
12 Correct 4 ms 14672 KB Output is correct
13 Incorrect 4 ms 14672 KB Output isn't correct
14 Halted 0 ms 0 KB -