Submission #955370

# Submission time Handle Problem Language Result Execution time Memory
955370 2024-03-30T09:36:36 Z Unforgettablepl Harbingers (CEOI09_harbingers) C++17
0 / 100
6 ms 9072 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 1e15;

struct line{
    int m,n;
    line(int m,int n):m(m),n(n){}
    int operator()(int x) const{
        return m*x+n;
    }
};

struct node{
    node *left,*right;
    int L,R;
    line lin;
    node(int L,int R): L(L), R(R), left(nullptr), right(nullptr), lin(0, INF){}
};

int query(node* curr,int x){
    if(curr==nullptr)return INF;
    int mid = (curr->L+curr->R)/2;
    if(mid==x)return curr->lin(x);
    if(mid<x)return min(curr->lin(x), query(curr->right, x));
    return min(curr->lin(x), query(curr->left, x));
}

void update(node* curr,line x, vector<pair<node*,node>> &updates){
    int mid = (curr->L+curr->R)/2;
    bool betterleft = curr->lin(curr->L) > x(curr->L);
    bool betterright = curr->lin(curr->R) > x(curr->R);
    bool bettermid = curr->lin(mid) > x(mid);
    if(!(betterleft or bettermid or betterright))return;
    if(bettermid and betterright and betterleft) {
        updates.emplace_back(curr,*curr);
        swap(x, curr->lin);
    }
    else if(betterleft and !bettermid){
        if(curr->left==nullptr)curr->left = new node(curr->L,mid-1);
        update(curr->left,x,updates);
    } else if(!bettermid){
        if(curr->right==nullptr)curr->right = new node(mid+1,curr->R);
        update(curr->right,x,updates);
    } else if(betterleft){
        updates.emplace_back(curr,*curr);
        swap(curr->lin,x);
        if(curr->right==nullptr)curr->right = new node(mid+1,curr->R);
        update(curr->right,x,updates);
    } else {
        updates.emplace_back(curr,*curr);
        swap(curr->lin, x);
        if(curr->left==nullptr)curr->left = new node(curr->L,mid-1);
        update(curr->left,x,updates);
    }
}



int DP[100001];
int S[100001];
int V[100001];
vector<pair<int,int>> adj[100001];
node* head = new node(1,1e9);

void dfs(int x,int p,int depth){
    DP[x] = S[x]+V[x]*depth+ query(head,V[x]);
    vector<pair<node*,node>> updates;
    update(head,line(-depth,DP[x]),updates);
    for(auto&i:adj[x])if(i.first!=p){
        dfs(i.first,x,depth+i.second);
    }
    for(auto&i:updates)*i.first=i.second;
}

int32_t main(){
    freopen("harbringers.in","r",stdin);
    freopen("harbringers.out","w",stdout);
    int n;
    cin >> n;
    for(int i=1;i<n;i++){
        int u,v,d;cin>>u>>v>>d;
        adj[u].emplace_back(v,d);
        adj[v].emplace_back(u,d);
    }
    for(int i=2;i<=n;i++)cin>>S[i]>>V[i];
    vector<pair<node*,node>> h;
    update(head,line(0,0),h);
    dfs(1,0,0);
    for(int i=2;i<=n;i++)cout<<DP[i]<<' ';
    cout << '\n';
}

Compilation message

harbingers.cpp: In constructor 'node::node(long long int, long long int)':
harbingers.cpp:18:11: warning: 'node::R' will be initialized after [-Wreorder]
   18 |     int L,R;
      |           ^
harbingers.cpp:17:11: warning:   'node* node::left' [-Wreorder]
   17 |     node *left,*right;
      |           ^~~~
harbingers.cpp:20:5: warning:   when initialized here [-Wreorder]
   20 |     node(int L,int R): L(L), R(R), left(nullptr), right(nullptr), lin(0, INF){}
      |     ^~~~
harbingers.cpp: In function 'int32_t main()':
harbingers.cpp:79:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |     freopen("harbringers.in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:80:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     freopen("harbringers.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 9048 KB Execution killed with signal 11
2 Runtime error 5 ms 9052 KB Execution killed with signal 11
3 Runtime error 5 ms 9052 KB Execution killed with signal 11
4 Runtime error 5 ms 9052 KB Execution killed with signal 11
5 Runtime error 6 ms 8992 KB Execution killed with signal 11
6 Runtime error 5 ms 9052 KB Execution killed with signal 11
7 Runtime error 5 ms 9052 KB Execution killed with signal 11
8 Runtime error 5 ms 9052 KB Execution killed with signal 11
9 Runtime error 5 ms 9072 KB Execution killed with signal 11
10 Runtime error 5 ms 9052 KB Execution killed with signal 11