Submission #526714

# Submission time Handle Problem Language Result Execution time Memory
526714 2022-02-16T02:46:04 Z kaxzert Harbingers (CEOI09_harbingers) C++17
100 / 100
104 ms 21956 KB
/**
      ⚡⚡  ⚡⚡      ⚡⚡⚡     ⚡⚡  ⚡⚡  ⚡⚡⚡⚡⚡⚡   ⚡⚡⚡⚡⚡  ⚡⚡⚡⚡⚡⚡  ⚡⚡⚡⚡⚡⚡
      ⚡⚡ ⚡⚡      ⚡⚡⚡⚡     ⚡⚡⚡⚡      ⚡⚡      ⚡⚡       ⚡⚡    ⚡⚡  ⚡⚡⚡⚡⚡⚡
      ⚡⚡⚡⚡      ⚡⚡  ⚡⚡      ⚡⚡      ⚡⚡       ⚡⚡⚡⚡⚡   ⚡⚡⚡⚡⚡⚡     ⚡⚡
      ⚡⚡ ⚡⚡    ⚡⚡⚡⚡⚡⚡     ⚡⚡     ⚡⚡         ⚡⚡       ⚡⚡  ⚡⚡       ⚡⚡
      ⚡⚡  ⚡⚡  ⚡⚡     ⚡⚡  ⚡⚡ ⚡⚡  ⚡⚡⚡⚡⚡⚡  ⚡⚡⚡⚡⚡    ⚡⚡   ⚡⚡     ⚡⚡
**/

#include<bits/stdc++.h>

using namespace std;

#define fto(i, a, b) for(int i = a; i <= b; ++i)
#define fdto(i, a, b) for(int i = a; i >= b; --i)
#define bugarr(a, i, j) cout << #a << "{" << i << "..." << j << "}:"; fto(k, i, j-1) cout << a[k] << ", "; cout << a[j] << endl;
#define ll long long
#define db double
#define ldb long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define vt vector
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(i, a) for(auto &i : a)
#define sz(a) (int)a.size()
#define pi(a, b) pair<a, b>
#define fast ios::sync_with_stdio(false); cin.tie(0)

void setIO(string s) {
    if (sz(s) != 0) {
        freopen((s+".inp").c_str(),"r",stdin);
        freopen((s+".out").c_str(),"w",stdout);
    }
}

void setIOusaco(string s) {
    if (sz(s) != 0) {
        freopen((s+".in").c_str(),"r",stdin);
        freopen((s+".out").c_str(),"w",stdout);
    }
}

template<typename T, typename V>
bool ckmin(T &a, V b) {return (b < a)? a = b, true : false;}
template<typename T, typename V>
bool ckmax(T &a, V b) {return (b > a)? a = b, true : false;}

void print(int x) {cout << x;}
void print(long long x) {cout << x;}
void print(unsigned x) {cout << x;}
void print(unsigned long long x) {cout << x;}
void print(double x) {cout << fixed << x;}
void print(long double x) {cout << fixed << x;}
void print(char x) {cout << "'" << x << "'";}
void print(string x) {cout << '"' << x << '"';}
void print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void print(const pair<T, V> &x) {cout << '{'; print(x.ff); cout << ", "; print(x.ss); cout << '}';}
template<typename T>
void print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? ", " : ""), print(i); cout << "}";}
void _print() {cout << "]" << endl;}
template <typename T, typename... V>
void _print(T t, V... v) {print(t); if (sizeof...(v)) cout << ", "; _print(v...);}

#ifdef TAP
#define bug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define bug(x...) 
#endif

//        a     x      b      c      c    c
// f[i] = -d[j]*v[i] + f[j] + s[i] + d[i]*v[i]

const int maxN = 100008;
ll s[maxN], v[maxN], f[maxN];
vt<pair<int, ll> > ke[maxN];

struct line {
	ll a, b;
	line() {
		a = 0;
		b = 0;
	}

	line(ll a, ll b):a(a), b(b){}

	line operator =(line o) {
		a = o.a;
		b = o.b;
		return *this;
	}

	ll intersect(line o) {
		ll x = o.b - b;
		ll y = a - o.a;
		return (x+y-1)/y;
	}

	ll val(ll x) {
		return a*x + b;
	}
};
line lines[maxN];
int N;

ll query2(ll x) {
	int res = 1;
	int l = 1, r = N-1;
	while(l <= r) {
		int m = (l+r) >> 1;
		if (lines[m].intersect(lines[m+1]) <= x) {
			res = m+1;
			l = m+1;
		} else r = m-1;
	}

	return lines[res].val(x);
}

int getpos(line newline) {
	if (N == 1 || lines[N-1].intersect(lines[N]) < lines[N].intersect(newline)) return N+1;

	int l = 2, r = N;
	int res = 1;
	while(l <= r) {
		int m = (l+r) >> 1;
		if (lines[m].intersect(lines[m-1]) >= lines[m].intersect(newline)) {
			res = m;
			r = m-1;
		} else l = m+1;
	}

	return res;
}

/*
[u] = [2]
0 0
[u] = [3]
0 0
-1718 134071
[u] = [4]
0 0
-3532 247280
[u] = [5]
0 0
-3532 247280
-4612 334774
[u] = [8]
0 0
-6330 430486
[u] = [6]
0 0
-3532 247280
-4612 334774
[u] = [7]
0 0
-1718 134071
[u] = [10]
0 0
-1718 134071
-3908 348734
[u] = [9]
0 0
-1718 134071
*/

void dfs(int u, int p = 0, ll d = 0) {
	int poscu;
	line linecu;
	int Ncu;
	if (u != 1) {
		f[u] = query2(v[u]) + s[u] + d*v[u];
		// bug(u);
		// fto(i, 1, N) {
		// 	cout << lines[i].a << " " << lines[i].b << '\n';
		// }
		line newline(-d, f[u]);
		Ncu = N;
		poscu = N = getpos(newline);
		//bug(poscu);
		//bug(Ncu);
		linecu = lines[poscu];
		lines[poscu] = newline;
	}

	for(auto [v, w] : ke[u]) {
		if (v == p) continue;
		dfs(v, u, d+w);
	}

	if (u != 1) {
		lines[poscu] = linecu;
		N = Ncu;
	}
}

int main() {
    #ifndef TAP 
    setIO("");
    //setIOusaco("harbingers");
    #endif

    fast;
    int n;
    cin >> n;
    fto(i, 1, n-1) {
    	int u, v;
    	ll w;
    	cin >> u >> v >> w;
    	ke[u].eb(v, w);
    	ke[v].eb(u, w);
    }

    ++N;
    lines[N] = {0, 0};
    fto(i, 2, n) {
    	cin >> s[i] >> v[i];
    }

    f[1] = 0;
    dfs(1);

    fto(i, 2, n) cout << f[i] << " \n"[i==n];

    return 0;
}

Compilation message

harbingers.cpp: In function 'void setIO(std::string)':
harbingers.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         freopen((s+".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen((s+".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp: In function 'void setIOusaco(std::string)':
harbingers.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen((s+".in").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen((s+".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4172 KB Output is correct
2 Correct 4 ms 4684 KB Output is correct
3 Correct 40 ms 11596 KB Output is correct
4 Correct 56 ms 15172 KB Output is correct
5 Correct 75 ms 18628 KB Output is correct
6 Correct 88 ms 21956 KB Output is correct
7 Correct 47 ms 12428 KB Output is correct
8 Correct 96 ms 16844 KB Output is correct
9 Correct 104 ms 18396 KB Output is correct
10 Correct 84 ms 17120 KB Output is correct