Submission #736154

# Submission time Handle Problem Language Result Execution time Memory
736154 2023-05-05T09:08:49 Z marvinthang Janjetina (COCI21_janjetina) C++17
110 / 110
425 ms 16572 KB
/*************************************
*    author: marvinthang             *
*    created: 05.08.2022 16:14:49    *
*************************************/

#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                 div  ___div
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define             FULL(i)  (MASK(i) - 1)
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#define          file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }

template <class T>             scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class T>            print_op(vector <T>)  { out << '{'; for (size_t i = 0; i + 1 < u.size(); ++i) out << u[i] << ", "; if (!u.empty()) out << u.back(); return out << '}'; }
template <class U, class V>   scan_op(pair <U, V>)  { return in >> u.fi >> u.se; }
template <class U, class V>  print_op(pair <U, V>)  { return out << '(' << u.fi << ", " << u.se << ')'; }

const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const int dir[] = {1, 0, -1, 0, 1, 1, -1, -1, 1}; // {2, 1, -2, -1, -2, 1, 2, -1, 2};
const int MAX = 1e5 + 5;

// index from 0
template <class T>
    struct Fenwick {

        T *bit;
        int n = -1;

        Fenwick(int _n = 0) {
            resize(_n);
        }

        void reset(void) {
            fill(bit, bit + 1 + n, T(0));
        }

        void resize(int _n) {
            if (~n) delete bit;
            n = _n;
            bit = new T[n + 1];
            reset();
        }

        void update(int i, T val) {
            assert(0 <= i);
            ++i;
            for (; i <= n; i += i & -i) bit[i] += val;
        }

        void update(int l, int r, T val) {
            if (l > r) return;
            update(l, val);
            update(r + 1, -val);
        }

        T get(int i) {
            if (i < 0) return T(0);
            ++i;
            i = min(i, n);
            T res(0);
            for (; i > 0; i -= i & -i)
                res += bit[i];
            return res;
        }

        T get(int l, int r) {
            if (l > r) return T(0);
            return get(r) - get(l - 1);
        }

        int upper_bound(T val) {
            int res = 0;
            for (int i = __lg(n); i >= 0; --i) {
                if ((res | MASK(i)) <= n && val >= bit[res | MASK(i)]) {
                    res |= MASK(i);
                    val -= bit[res];
                }
            }
            return res;
        }

        int lower_bound(T val) {
            int res = 0;
            for (int i = __lg(n); i >= 0; --i) {
                if ((res | MASK(i)) <= n && val > bit[res | MASK(i)]) {
                    res |= MASK(i);
                    val -= bit[res];
                }
            }
            return res;
        }

    };


int N, K;
vector <pair <int, int>> adj[MAX];
Fenwick <int> bit;

void loadTree(void) {
	cin >> N >> K;
	for (int i = 1; i < N; ++i) {
		int u, v, w; cin >> u >> v >> w;
		adj[u].push_back(make_pair(w, v));
		adj[v].push_back(make_pair(w, u));
	}
}

bool used[MAX];
int treeSize[MAX];

int calcSize(int u, int par) {
	treeSize[u] = 1;
	for (auto [_, v]: adj[u]) if (!used[v] && v != par) treeSize[u] += calcSize(v, u);
	return treeSize[u];
}

int findCentroid(int u, int par, int n) {
	for (auto [_, v]: adj[u]) if (!used[v] && v != par && treeSize[v] > n / 2) return findCentroid(v, u, n);
	return u;
}


void dfs(int u, int par, int dep, int ma, vector <pair <int, int>> &values) {
	values.push_back(make_pair(ma, dep));
	for (auto [w, v]: adj[u]) if (!used[v] && v != par) 
		dfs(v, u, dep + 1, max(ma, w), values);
}

long long solve(int u) {
	int n = calcSize(u, 0);
	u = findCentroid(u, 0, n);

	vector <pair <int, int>> values;
	dfs(u, 0, 0, 0, values);
	sort(values.begin(), values.end());
	long long res = 0;
	for (auto [ma, dep]: values) {
		res += bit.get(ma - dep - K);
		bit.update(dep, 1);
	}
	for (auto [ma, dep]: values) bit.update(dep, -1);
	
	for (auto [w, v]: adj[u]) if (!used[v]) {
		values.clear();
		dfs(v, u, 1, w, values);
		sort(values.begin(), values.end());
		for (auto [ma, dep]: values) {
			res -= bit.get(ma - dep - K);
			bit.update(dep, 1);
		}
		for (auto [ma, dep]: values) bit.update(dep, -1);
	}

	used[u] = true;
	for (auto [_, v]: adj[u]) if (!used[v]) res += solve(v);
	return res;
}

void process(void) {
	bit.resize(MAX);
	cout << solve(1) * 2 << '\n';
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
	file("coci2021_r4_janjetina");
	loadTree();
	process();
	// cerr << "Time elapsed: " << TIME << " s.\n";
	return (0^0);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:22:69: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define          file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                             ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:178:2: note: in expansion of macro 'file'
  178 |  file("coci2021_r4_janjetina");
      |  ^~~~
Main.cpp:22:103: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define          file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
      |                                                                                               ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:178:2: note: in expansion of macro 'file'
  178 |  file("coci2021_r4_janjetina");
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 4 ms 3156 KB Output is correct
5 Correct 4 ms 3156 KB Output is correct
6 Correct 4 ms 3204 KB Output is correct
7 Correct 4 ms 3156 KB Output is correct
8 Correct 4 ms 3200 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
10 Correct 3 ms 3156 KB Output is correct
11 Correct 3 ms 3156 KB Output is correct
12 Correct 4 ms 3068 KB Output is correct
13 Correct 4 ms 3028 KB Output is correct
14 Correct 4 ms 3156 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 4 ms 3068 KB Output is correct
17 Correct 4 ms 3072 KB Output is correct
18 Correct 4 ms 3072 KB Output is correct
19 Correct 4 ms 3156 KB Output is correct
20 Correct 4 ms 3028 KB Output is correct
21 Correct 4 ms 3076 KB Output is correct
22 Correct 4 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3056 KB Output is correct
2 Correct 2 ms 3060 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 4 ms 3156 KB Output is correct
5 Correct 31 ms 4368 KB Output is correct
6 Correct 180 ms 9820 KB Output is correct
7 Correct 339 ms 16072 KB Output is correct
8 Correct 407 ms 16496 KB Output is correct
9 Correct 359 ms 16124 KB Output is correct
10 Correct 425 ms 16372 KB Output is correct
11 Correct 370 ms 15988 KB Output is correct
12 Correct 391 ms 16428 KB Output is correct
13 Correct 363 ms 16044 KB Output is correct
14 Correct 393 ms 16288 KB Output is correct
15 Correct 391 ms 16216 KB Output is correct
16 Correct 387 ms 16564 KB Output is correct
17 Correct 396 ms 16316 KB Output is correct
18 Correct 398 ms 16192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 4 ms 3156 KB Output is correct
5 Correct 4 ms 3156 KB Output is correct
6 Correct 4 ms 3204 KB Output is correct
7 Correct 4 ms 3156 KB Output is correct
8 Correct 4 ms 3200 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
10 Correct 3 ms 3156 KB Output is correct
11 Correct 3 ms 3156 KB Output is correct
12 Correct 4 ms 3068 KB Output is correct
13 Correct 4 ms 3028 KB Output is correct
14 Correct 4 ms 3156 KB Output is correct
15 Correct 3 ms 3028 KB Output is correct
16 Correct 4 ms 3068 KB Output is correct
17 Correct 4 ms 3072 KB Output is correct
18 Correct 4 ms 3072 KB Output is correct
19 Correct 4 ms 3156 KB Output is correct
20 Correct 4 ms 3028 KB Output is correct
21 Correct 4 ms 3076 KB Output is correct
22 Correct 4 ms 3072 KB Output is correct
23 Correct 2 ms 3056 KB Output is correct
24 Correct 2 ms 3060 KB Output is correct
25 Correct 2 ms 3028 KB Output is correct
26 Correct 4 ms 3156 KB Output is correct
27 Correct 31 ms 4368 KB Output is correct
28 Correct 180 ms 9820 KB Output is correct
29 Correct 339 ms 16072 KB Output is correct
30 Correct 407 ms 16496 KB Output is correct
31 Correct 359 ms 16124 KB Output is correct
32 Correct 425 ms 16372 KB Output is correct
33 Correct 370 ms 15988 KB Output is correct
34 Correct 391 ms 16428 KB Output is correct
35 Correct 363 ms 16044 KB Output is correct
36 Correct 393 ms 16288 KB Output is correct
37 Correct 391 ms 16216 KB Output is correct
38 Correct 387 ms 16564 KB Output is correct
39 Correct 396 ms 16316 KB Output is correct
40 Correct 398 ms 16192 KB Output is correct
41 Correct 2 ms 3028 KB Output is correct
42 Correct 347 ms 15952 KB Output is correct
43 Correct 390 ms 16476 KB Output is correct
44 Correct 356 ms 16100 KB Output is correct
45 Correct 420 ms 16332 KB Output is correct
46 Correct 340 ms 16024 KB Output is correct
47 Correct 398 ms 16572 KB Output is correct
48 Correct 362 ms 16020 KB Output is correct
49 Correct 401 ms 16444 KB Output is correct
50 Correct 400 ms 16256 KB Output is correct
51 Correct 395 ms 16296 KB Output is correct
52 Correct 126 ms 10180 KB Output is correct
53 Correct 147 ms 10400 KB Output is correct
54 Correct 115 ms 10052 KB Output is correct
55 Correct 153 ms 10360 KB Output is correct
56 Correct 153 ms 10452 KB Output is correct
57 Correct 313 ms 10404 KB Output is correct
58 Correct 312 ms 10616 KB Output is correct
59 Correct 296 ms 10812 KB Output is correct
60 Correct 319 ms 10720 KB Output is correct
61 Correct 388 ms 10696 KB Output is correct
62 Correct 272 ms 10244 KB Output is correct
63 Correct 274 ms 10596 KB Output is correct
64 Correct 328 ms 10772 KB Output is correct
65 Correct 12 ms 3412 KB Output is correct
66 Correct 2 ms 3028 KB Output is correct