Submission #865228

# Submission time Handle Problem Language Result Execution time Memory
865228 2023-10-24T06:40:25 Z efedmrlr Harbingers (CEOI09_harbingers) C++17
70 / 100
1000 ms 28236 KB
//https://cses.fi/problemset/task/2087/

#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); } 
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const long double EPS = 0.000001;
const int INF = 1e17+500;
const int N = 1e5+5;
const int ALPH = 26;
const int LGN = 25;
const int MOD = 1e9+7;
int n,k;
vector<vector<array<int,2> > > adj;
vector<int> s,v;
vector<int> dp;
vector<int> p, pat;
vector<int> ints;
struct Line {
    int a,b;
    Line() {
        a = 0; b = 0;
    }
    Line(int a, int b) {
        this->a = a;
        this->b = b;
    }
    int eval(int x) {
        return a*x + b;
    }
    long double intersect(Line y) {
        return (long double)(b - y.b) / (long double)(y.a - a);
    }
    void print_line() {
        cout<<a<<"x + "<<b<<"\n";
    }
};

deque<Line> cht;
vector<vector<pair<Line, int> > > upds;

bool comp(int idx, int x) {
    return cht[idx].intersect(cht[idx + 1]) >= x;
}

    
void update(Line nw) {
    upds.pb(vector<pair<Line, int> >());
    while(cht.size() > 1 && cht.front().intersect(cht[1]) >= cht.front().intersect(nw)) {
        upds[upds.size() - 1].pb(MP(cht.front(), 0));
        cht.pop_front();
    }
    upds[upds.size() - 1].pb(MP(nw, 1));
    cht.push_front(nw);
}
void restore_last() {
    while(upds.back().size() > 0) {
        auto cur = upds.back().back();
        if(cur.second) {
            cht.pop_front();
        }  
        else {
            cht.push_front(cur.first);
        }
        upds.back().pop_back();
    }
    upds.pop_back();
}
int query(int x) {
    
    int idx = *lower_bound(ints.begin(), ints.begin() + cht.size() - 1, x, comp);
    return cht[idx].eval(x);
    
   /*
   int ret = INF;
   cout<<"x:"<<x<<"\n";
   REP(i,cht.size()) {
    cht[i].print_line();
   }
   for(auto c : cht) {
    ret = min(c.eval(x), ret);
   }
   return ret;
   */
}




void dfs(int node) {
    for(auto c : adj[node]) {
        if(c[0] == pat[node]) continue;
        pat[c[0]] = node;
        p[c[0]] = p[node] + c[1];
        dfs(c[0]);
    }
}

void dfs2(int node) {
    update(Line(-p[node], dp[node]));
    for(auto c : adj[node]) {
        
        if(c[0] == pat[node]) continue;
        dp[c[0]] = query(v[c[0]]) + v[c[0]] * p[c[0]] + s[c[0]];
        dfs2(c[0]);
    }
    restore_last();
}

inline void solve() {
    cin >> n;
    cht.clear(); upds.clear();
    adj.resize(n+1, vector<array<int,2> >());
    ints.resize(n);
    iota(ints.begin(), ints.end(), 0);
    for(int i = 0; i<n-1; i++) {
        int u,v,d;
        cin>>u>>v>>d;
        adj[u].pb({v,d});
        adj[v].pb({u,d});
    }
    s.resize(n+1);
    v.resize(n+1);
    for(int i = 2; i<=n; i++) {
        cin>>s[i]>>v[i];
    }
    dp.resize(n+1);
    dp[1] = 0;
    pat.resize(n+1); p.resize(n+1);
    pat[1] = 0;
    p[1] = 0;
    dfs(1);
    dfs2(1);
    for(int i = 2; i<=n; i++) {
        cout<<dp[i]<<" ";
    }
    cout<<"\n";
}
 
signed main() {
    fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}

Compilation message

harbingers.cpp: In function 'std::string to_string(std::string, long long int, long long int)':
harbingers.cpp:13:208: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 | template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
      |                                                                                                                                                                                                             ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 35 ms 11648 KB Output is correct
4 Correct 52 ms 17124 KB Output is correct
5 Correct 74 ms 22784 KB Output is correct
6 Correct 109 ms 28236 KB Output is correct
7 Correct 46 ms 13324 KB Output is correct
8 Execution timed out 1051 ms 20584 KB Time limit exceeded
9 Execution timed out 1099 ms 17904 KB Time limit exceeded
10 Execution timed out 1053 ms 21924 KB Time limit exceeded