Submission #1214037

#TimeUsernameProblemLanguageResultExecution timeMemory
1214037trinm01Harbingers (CEOI09_harbingers)C++20
70 / 100
1096 ms21680 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define FOR(i, l, r) for (int i = (l); i <= (r); i++)
#define FOD(i, r, l) for (int i = (r); i >= (l); i--)
#define fi first
#define se second
#define pii pair<int, int>

const ll mod = 1e9 + 7;
const ll MAXN = 1e5 + 1;
const int oo = 1e18;
const int base = 320;

const int BUFFER_SIZE = 1 << 20;
char input_buffer[BUFFER_SIZE];
int input_pos = 0, input_len = 0;

inline char nextChar() {
    if (input_pos == input_len) {
        input_pos = 0;
        input_len = fread(input_buffer, 1, BUFFER_SIZE, stdin);
        if (input_len == 0) return EOF;
    }
    return input_buffer[input_pos++];
}

inline void fast_read(int &x) {
    x = 0;
    char c;
    bool neg = false;

    do {
        c = nextChar();
    } while (c != '-' && (c < '0' || c > '9'));

    if (c == '-') {
        neg = true;
        c = nextChar();
    }

    while (c >= '0' && c <= '9') {
        x = x * 10 + (c - '0');
        c = nextChar();
    }

    if (neg) x = -x;
}

int n, s[MAXN], p[MAXN], d[MAXN];
vector<pii> adj[MAXN];
int xoa[MAXN];
int f[MAXN];
vector<int> A, B, xoaA, xoaB;
bool bad(int l1, int l2, int l3){
    return (B[l3]-B[l1])*(A[l1]-A[l2])<(B[l2]-B[l1])*(A[l1]-A[l3]);
}
void add(int a, int b, int u){
    // if(u==7){
    //     for(auto x:A){
    //         cout << x << ' ';
    //     }
    //     cout << '\n';
    // }
    A.push_back(a);
    B.push_back(b);
    
    while((int)A.size()>=3 && bad(A.size()-3, A.size()-2, A.size()-1)){
        xoaA.push_back(A[A.size()-2]);
        xoaB.push_back(B[B.size()-2]);
        xoa[u]++;
        A.erase(A.end()-2);
        B.erase(B.end()-2);
    }
    // if(u==9){
    //     for(auto x:A){
    //         cout << x << ' ';
    //     }
    //     cout << '\n';
    //     for(auto x:B){
    //         cout << x << ' ';
    //     }
    //     cout << '\n';
    // }
}
void hoi(int a, int b, int u){
    A.pop_back();
    B.pop_back();
    int cnt=0;
    while(cnt<xoa[u]){
        A.push_back(xoaA.back());
        B.push_back(xoaB.back());
        xoaA.pop_back();
        xoaB.pop_back();
        cnt++;
    }
    xoa[u]=0;
    // if(u==7){
    //     for(auto x:A){
    //         cout << x << ' ';
    //     }
    //     cout << '\n';
    // }
}
int query(int x){
    int l=1, r=(int)A.size()-1, id=1;
    if(r==-1){
        return 0;
    }
    if(r==0){
        return A[0]*x+B[0];
    }
    int ans=oo;
    while(l<=r){
        int mid=(l+r)/2;
        if(A[mid]*x+B[mid]<A[mid-1]*x+B[mid-1]){
            id=mid;
            l=mid+1;
        }
        else{
            r=mid-1;
        }
    }
    // if(x==6)cout << id << '\n';
    ans=min({ans, A[id]*x+B[id], A[id-1]*x+B[id-1]});
    return ans;
}

void dfs2(int u, int last){
    if(u!=1){
        f[u]=query(p[u])+d[u]*p[u]+s[u];
        // if(u==4) cout << query(p[u]) << ' ';
    }
    add(-d[u], f[u], u);

    for(auto [v, c]:adj[u]){
        if(v==last) continue;
        d[v]=d[u]+c;
        dfs2(v, u);
    }

    hoi(-d[u], f[u], u);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    if (fopen(".INP", "r"))
    {
        freopen(".INP", "r", stdin);
        freopen(".OUT", "w", stdout);
    }       

    fast_read(n);
    FOR(i, 1, n-1){
        int u, v, c;
        fast_read(u);
        fast_read(v);
        fast_read(c);
        adj[u].push_back({v, c});
        adj[v].push_back({u, c});
    }
    FOR(i, 2, n){
        fast_read(s[i]);
        fast_read(p[i]); 
    }

    dfs2(1, 1);
    FOR(i, 2, n){
        cout << f[i] << ' ';
    }

    return 0;
}

Compilation message (stderr)

harbingers.cpp: In function 'int main()':
harbingers.cpp:154:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |         freopen(".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
harbingers.cpp:155:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  155 |         freopen(".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...