Submission #1043753

# Submission time Handle Problem Language Result Execution time Memory
1043753 2024-08-04T16:15:16 Z Tob Petrol stations (CEOI24_stations) C++14
100 / 100
589 ms 18508 KB
#include <bits/stdc++.h>

#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef long long ll;
typedef pair <ll, ll> pii;

const int N = 7e4 + 7;

int n, k;
int siz[N], gt[N], bio[N], dp[N], dp2[N], pa[N];
ll ps[N], res[N];
vector <pii> adj[N];
vector <int> v, u;

int ind(vector <ll>& v_, ll x) {return lower_bound(all(v_), x)-v_.begin();}

struct F {
	vector <int> fen;
	vector <ll> g;
	void init(vector <ll>& h) {
		fen.clear(); g.clear();
		sort(all(h)); h.erase(unique(all(h)), h.end());
		g = h;
		fen.resize(g.size(), 0);
	}
	void add(ll x, int val) {
		x = ind(g, x);
		for (x++; x <= g.size(); x += x & -x) fen[x-1] += val;
	}
	int get(int x) {
		int out = 0;
		for (x++; x; x -= x & -x) out += fen[x-1];
		return out;
	}
	int query(ll l, ll r) {
		l = ind(g, l)-1; r = ind(g, r+1)-1;
		return get(r)-get(l);
	}
} f;

int dfs(int x, int o = 0, int p = -1) {
	if (o) v.pb(x);
	u.pb(x);
	pa[x] = p;
	
	int lo = 0, hi = u.size()-1;
	while (lo < hi) {
		int mid = (lo + hi) / 2;
		if (ps[x]-ps[u[mid]] <= k) hi = mid;
		else lo = mid+1;
	}
	gt[x] = u[lo];
	dp[x] = 0;
	
	siz[x] = 1;
	for (auto y : adj[x]) if (y.F != p && !bio[y.F]) {
		ps[y.F] = ps[x] + y.S;
		siz[x] += dfs(y.F, o, x);
	}
	
	dp[u[lo]] += dp[x]+1;
	u.pop_back();
	return siz[x];
}

void dfs2(int x, int p = -1) {
	dp2[x] = f.query(ps[p], ps[x]-1);
	f.add(ps[p]+k, dp2[x]);
	for (auto y : adj[x]) if (y.F != p && !bio[y.F]) dfs2(y.F, x);
	f.add(ps[p]+k, -dp2[x]);
}

void vdfs(int x, int p = -1) {
	v.pb(x);
	for (auto y : adj[x]) if (y.F != p && !bio[y.F]) vdfs(y.F, x);
}

int cent(int x, int m, int p = -1) {
	for (auto y : adj[x]) if (y.F != p && !bio[y.F] && siz[y.F] > m/2) return cent(y.F, m, x);
	return x;
}

void rek(int x) {
	dfs(x);
	int m = siz[x];
	x = cent(x, m);
	ps[x] = 0;
	dfs(x);
	
	for (auto y : adj[x]) {
		if (bio[y.F]) continue;
		vdfs(y.F, x);
		vector <ll> g;
		for (auto z : v) {
			g.pb(k-ps[z]);
			g.pb(ps[pa[z]]+k);
		}
		f.init(g);
		for (auto z : v) {
			res[z] += (ll)dp[z]*(m-siz[y.F]);
			if (ps[z] <= k) f.add(k-ps[z], dp[z]+1);
		}
		dfs2(y.F, x);
		for (auto z : v) res[pa[z]] -= (ll)dp2[z]*siz[z];
		v.clear();
	}
	vector <ll> g;
	for (auto y : adj[x]) if (!bio[y.F]) vdfs(y.F, x);
	g.pb(k);
	for (auto y : v) {
		g.pb(k-ps[y]);
		g.pb(ps[pa[y]]+k);
	}
	f.init(g);
	f.add(k, 1);
	for (auto y : v) if (ps[y] <= k) f.add(k-ps[y], dp[y]+1);
	for (auto y : adj[x]) if (!bio[y.F]) dfs2(y.F, x);
	for (auto y : v) res[pa[y]] += (ll)dp2[y]*siz[y];
	v.clear();
	
	bio[x] = 1;
	for (auto y : adj[x]) if (!bio[y.F]) rek(y.F);
}

int main () {
	FIO;
	cin >> n >> k;
	
	for (int i = 0; i < n-1; i++) {
		int x, y, w; cin >> x >> y >> w;
		adj[x].pb({y, w});
		adj[y].pb({x, w});
	}
	
	rek(0);
	
	for (int i = 0; i < n; i++) cout << res[i] << "\n";

	return 0;
}

Compilation message

Main.cpp: In member function 'void first::add(ll, int)':
Main.cpp:35:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |   for (x++; x <= g.size(); x += x & -x) fen[x-1] += val;
      |             ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 2 ms 4700 KB Output is correct
4 Correct 2 ms 4700 KB Output is correct
5 Correct 2 ms 4700 KB Output is correct
6 Correct 5 ms 4700 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 3 ms 4700 KB Output is correct
10 Correct 3 ms 4700 KB Output is correct
11 Correct 3 ms 4700 KB Output is correct
12 Correct 3 ms 4700 KB Output is correct
13 Correct 3 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 548 ms 16584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 548 ms 16584 KB Output is correct
5 Correct 589 ms 18508 KB Output is correct
6 Correct 567 ms 18216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 336 ms 11768 KB Output is correct
4 Correct 567 ms 17324 KB Output is correct
5 Correct 279 ms 17768 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 408 ms 13120 KB Output is correct
8 Correct 435 ms 12948 KB Output is correct
9 Correct 437 ms 13352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 336 ms 11768 KB Output is correct
4 Correct 567 ms 17324 KB Output is correct
5 Correct 279 ms 17768 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 408 ms 13120 KB Output is correct
8 Correct 435 ms 12948 KB Output is correct
9 Correct 437 ms 13352 KB Output is correct
10 Correct 204 ms 13080 KB Output is correct
11 Correct 225 ms 12996 KB Output is correct
12 Correct 226 ms 13012 KB Output is correct
13 Correct 246 ms 12872 KB Output is correct
14 Correct 261 ms 12804 KB Output is correct
15 Correct 255 ms 13100 KB Output is correct
16 Correct 34 ms 12480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 2 ms 4700 KB Output is correct
4 Correct 2 ms 4700 KB Output is correct
5 Correct 2 ms 4700 KB Output is correct
6 Correct 5 ms 4700 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 3 ms 4700 KB Output is correct
10 Correct 3 ms 4700 KB Output is correct
11 Correct 3 ms 4700 KB Output is correct
12 Correct 3 ms 4700 KB Output is correct
13 Correct 3 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 548 ms 16584 KB Output is correct
17 Correct 589 ms 18508 KB Output is correct
18 Correct 567 ms 18216 KB Output is correct
19 Correct 336 ms 11768 KB Output is correct
20 Correct 567 ms 17324 KB Output is correct
21 Correct 279 ms 17768 KB Output is correct
22 Correct 1 ms 4700 KB Output is correct
23 Correct 408 ms 13120 KB Output is correct
24 Correct 435 ms 12948 KB Output is correct
25 Correct 437 ms 13352 KB Output is correct
26 Correct 204 ms 13080 KB Output is correct
27 Correct 225 ms 12996 KB Output is correct
28 Correct 226 ms 13012 KB Output is correct
29 Correct 246 ms 12872 KB Output is correct
30 Correct 261 ms 12804 KB Output is correct
31 Correct 255 ms 13100 KB Output is correct
32 Correct 34 ms 12480 KB Output is correct
33 Correct 468 ms 14180 KB Output is correct
34 Correct 231 ms 14380 KB Output is correct
35 Correct 339 ms 13748 KB Output is correct
36 Correct 346 ms 14312 KB Output is correct
37 Correct 331 ms 13696 KB Output is correct
38 Correct 335 ms 13944 KB Output is correct
39 Correct 324 ms 13972 KB Output is correct
40 Correct 50 ms 12996 KB Output is correct