Submission #100446

# Submission time Handle Problem Language Result Execution time Memory
100446 2019-03-11T10:22:43 Z JPN20 Construction of Highway (JOI18_construction) C++17
16 / 100
1367 ms 110764 KB
#include <bits/stdc++.h>
using namespace std;

int size_ = 1, rems = 1;
int va[1 << 18], vb[1 << 18], dat[1 << 18];
int V[1 << 17][88], VS[1 << 17];

void inits(int sz) {
	while (size_ <= sz) size_ *= 2;

	int cnt = 1;
	for (int i = size_; i >= 1; i /= 2) {
		for (int j = 0; j < size_; j += i) {
			va[cnt] = j;
			vb[cnt] = j + i;
			cnt++;
		}
	}
}
void update(int pos, int x) {
	pos += size_;
	dat[pos] = x;

	while (pos >= 2) {
		pos >>= 1;
		dat[pos] = max(dat[pos * 2], dat[pos * 2 + 1]);
	}
}

int query(int pos) {
	int ret = -(1 << 30);
	for (int i = 0; i < VS[pos]; i++) ret = max(ret, dat[V[pos][i]]);
	return ret;
}

void query_init(int pos, int l, int r) {
	// 非再帰 RMQ
	int vu[4], vp; vu[0] = 1; vp = 1;
	int cu[4], cp;

	while (vp >= 1) {
		cp = 0;
		for (int i = 0; i < vp; i++) {
			int vv = vu[i];
			int vl = va[vv], vr = vb[vv], vm = ((vl + vr) >> 1);

			// 左側
			if (l <= vl && vm <= r) { V[pos][VS[pos]] = (vv << 1); VS[pos]++; }
			else if (!(vm <= l || r <= vl)) { cu[cp] = vv * 2; cp++; }

			// 右側
			if (l <= vm && vr <= r) { V[pos][VS[pos]] = (vv << 1) + 1; VS[pos]++; }
			else if (!(vr <= l || r <= vm)) { cu[cp] = vv * 2 + 1; cp++; }
		}
		for (int i = 0; i < cp; i++) vu[i] = cu[i];
		vp = cp;
	}
}

class BIT {
public:
	vector<int>bit; int size1_;

	void init(int sz) {
		size1_ = sz + 2;
		bit.resize(size1_ + 1, 0);
	}
	void add(int pos, int x) {
		pos++;
		while (pos <= size1_) {
			bit[pos] += x; pos += (pos & -pos);
		}
	}
	int sum(int pos) {
		int s = 0; pos++;
		while (pos >= 1) {
			s += bit[pos]; pos -= (pos & -pos);
		}
		return s;
	}
};

long long check_inversions(vector<pair<int, int>> vec) {
	vector<int> A;
	for (int i = 0; i < (int)vec.size(); i++) A.push_back(vec[i].first);

	sort(A.begin(), A.end());
	A.erase(unique(A.begin(), A.end()), A.end());
	for (int i = 0; i < (int)vec.size(); i++) vec[i].first = lower_bound(A.begin(), A.end(), vec[i].first) - A.begin();

	BIT Z; Z.init(A.size() + 1);

	long long ans = 0;
	for (int i = 0; i < (int)vec.size(); i++) {
		ans += 1LL * vec[i].second * (Z.sum(A.size()) - Z.sum(vec[i].first));
		Z.add(vec[i].first, vec[i].second);
	}
	return ans;
}

int N, C[1 << 17], A[1 << 17], B[1 << 17], dist[1 << 17], dp[1 << 17][20];
int cl[1 << 17], cr[1 << 17], cnts;
vector<int> X[1 << 17];

void dfs(int pos, int dep) {
	cnts++; cl[pos] = cnts; dist[pos] = dep;
	for (int i = 0; i < (int)X[pos].size(); i++) {
		if (dist[X[pos][i]] != -1) continue;
		dp[X[pos][i]][0] = pos;
		dfs(X[pos][i], dep + 1);
	}
	cr[pos] = cnts;
}

int prevs(int pos, int x) {
	for (int i = 19; i >= 0; i--) {
		if (x >= (1 << i)) { x -= (1 << i); pos = dp[pos][i]; }
	}
	if (pos == 0) pos = 1;
	return pos;
}

vector<pair<int, int>> get_inverse(int pos) {
	vector<pair<int, int>> U;
	while (true) {
		// Binary Search.
		int L = 0, R = dist[pos] + 1, M, maxn = (1 << 30);
		int E = query(pos);

		for (int i = 0; i < 30; i++) {
			M = (L + R) / 2;
			int G = prevs(pos, M);
			int P = query(G);
			if (E != P) { maxn = min(maxn, M); R = M; }
			else { L = M; }
		}

		if (maxn == (1 << 30)) { U.push_back(make_pair(C[B[E]], dist[pos] + 1)); break; }
		U.push_back(make_pair(C[B[E]], maxn));
		pos = prevs(pos, maxn);

		rems++;
		assert(rems <= 500000);
	}
	reverse(U.begin(), U.end());
	return U;
}

int main() {
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) scanf("%d", &C[i]);
	for (int i = 1; i <= N - 1; i++) {
		scanf("%d%d", &A[i], &B[i]);
		X[A[i]].push_back(B[i]);
		X[B[i]].push_back(A[i]);
	}

	for (int i = 1; i <= N; i++) dist[i] = -1;
	dfs(1, 0);

	for (int i = 0; i < 19; i++) {
		for (int j = 1; j <= N; j++) dp[j][i + 1] = dp[dp[j][i]][i];
	}

	inits(N + 2);
	for (int i = 1; i <= N; i++) query_init(i, cl[i], cr[i] + 1);
	
	update(cl[1], 0);
	for (int i = 1; i <= N - 1; i++) {
		vector<pair<int, int>> T = get_inverse(A[i]);

		printf("%lld\n", check_inversions(T));
		update(cl[B[i]], i);
	}
	return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:150:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
construction.cpp:151:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 1; i <= N; i++) scanf("%d", &C[i]);
                               ~~~~~^~~~~~~~~~~~~
construction.cpp:153:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &A[i], &B[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 7 ms 3584 KB Output is correct
4 Correct 7 ms 3712 KB Output is correct
5 Correct 10 ms 3712 KB Output is correct
6 Correct 9 ms 3712 KB Output is correct
7 Correct 9 ms 3840 KB Output is correct
8 Correct 7 ms 3712 KB Output is correct
9 Correct 7 ms 3712 KB Output is correct
10 Correct 6 ms 3712 KB Output is correct
11 Correct 6 ms 3712 KB Output is correct
12 Correct 7 ms 3840 KB Output is correct
13 Correct 7 ms 3712 KB Output is correct
14 Correct 8 ms 3712 KB Output is correct
15 Correct 10 ms 3712 KB Output is correct
16 Correct 9 ms 3712 KB Output is correct
17 Correct 9 ms 3712 KB Output is correct
18 Correct 9 ms 3840 KB Output is correct
19 Correct 7 ms 3712 KB Output is correct
20 Correct 7 ms 3712 KB Output is correct
21 Correct 7 ms 3712 KB Output is correct
22 Correct 7 ms 3712 KB Output is correct
23 Correct 10 ms 3712 KB Output is correct
24 Correct 7 ms 3712 KB Output is correct
25 Correct 8 ms 3712 KB Output is correct
26 Correct 8 ms 3712 KB Output is correct
27 Correct 6 ms 3712 KB Output is correct
28 Correct 7 ms 3712 KB Output is correct
29 Correct 7 ms 3712 KB Output is correct
30 Correct 10 ms 3712 KB Output is correct
31 Correct 9 ms 3712 KB Output is correct
32 Correct 6 ms 3712 KB Output is correct
33 Correct 7 ms 3712 KB Output is correct
34 Correct 6 ms 3712 KB Output is correct
35 Correct 8 ms 3712 KB Output is correct
36 Correct 8 ms 3712 KB Output is correct
37 Correct 7 ms 3712 KB Output is correct
38 Correct 5 ms 3712 KB Output is correct
39 Correct 7 ms 3712 KB Output is correct
40 Correct 8 ms 3712 KB Output is correct
41 Correct 7 ms 3712 KB Output is correct
42 Correct 7 ms 3712 KB Output is correct
43 Correct 7 ms 3712 KB Output is correct
44 Correct 9 ms 3712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 7 ms 3584 KB Output is correct
4 Correct 7 ms 3712 KB Output is correct
5 Correct 10 ms 3712 KB Output is correct
6 Correct 9 ms 3712 KB Output is correct
7 Correct 9 ms 3840 KB Output is correct
8 Correct 7 ms 3712 KB Output is correct
9 Correct 7 ms 3712 KB Output is correct
10 Correct 6 ms 3712 KB Output is correct
11 Correct 6 ms 3712 KB Output is correct
12 Correct 7 ms 3840 KB Output is correct
13 Correct 7 ms 3712 KB Output is correct
14 Correct 8 ms 3712 KB Output is correct
15 Correct 10 ms 3712 KB Output is correct
16 Correct 9 ms 3712 KB Output is correct
17 Correct 9 ms 3712 KB Output is correct
18 Correct 9 ms 3840 KB Output is correct
19 Correct 7 ms 3712 KB Output is correct
20 Correct 7 ms 3712 KB Output is correct
21 Correct 7 ms 3712 KB Output is correct
22 Correct 7 ms 3712 KB Output is correct
23 Correct 10 ms 3712 KB Output is correct
24 Correct 7 ms 3712 KB Output is correct
25 Correct 8 ms 3712 KB Output is correct
26 Correct 8 ms 3712 KB Output is correct
27 Correct 6 ms 3712 KB Output is correct
28 Correct 7 ms 3712 KB Output is correct
29 Correct 7 ms 3712 KB Output is correct
30 Correct 10 ms 3712 KB Output is correct
31 Correct 9 ms 3712 KB Output is correct
32 Correct 6 ms 3712 KB Output is correct
33 Correct 7 ms 3712 KB Output is correct
34 Correct 6 ms 3712 KB Output is correct
35 Correct 8 ms 3712 KB Output is correct
36 Correct 8 ms 3712 KB Output is correct
37 Correct 7 ms 3712 KB Output is correct
38 Correct 5 ms 3712 KB Output is correct
39 Correct 7 ms 3712 KB Output is correct
40 Correct 8 ms 3712 KB Output is correct
41 Correct 7 ms 3712 KB Output is correct
42 Correct 7 ms 3712 KB Output is correct
43 Correct 7 ms 3712 KB Output is correct
44 Correct 9 ms 3712 KB Output is correct
45 Correct 10 ms 3968 KB Output is correct
46 Correct 34 ms 5504 KB Output is correct
47 Correct 34 ms 5632 KB Output is correct
48 Correct 43 ms 5504 KB Output is correct
49 Correct 20 ms 5632 KB Output is correct
50 Correct 20 ms 5708 KB Output is correct
51 Correct 19 ms 5632 KB Output is correct
52 Correct 21 ms 5632 KB Output is correct
53 Correct 23 ms 5632 KB Output is correct
54 Correct 24 ms 5632 KB Output is correct
55 Correct 21 ms 5632 KB Output is correct
56 Correct 20 ms 5504 KB Output is correct
57 Correct 57 ms 5632 KB Output is correct
58 Correct 62 ms 5504 KB Output is correct
59 Correct 73 ms 5504 KB Output is correct
60 Correct 65 ms 5504 KB Output is correct
61 Correct 25 ms 5632 KB Output is correct
62 Correct 31 ms 5676 KB Output is correct
63 Correct 24 ms 5632 KB Output is correct
64 Correct 34 ms 5504 KB Output is correct
65 Correct 48 ms 5504 KB Output is correct
66 Correct 33 ms 5504 KB Output is correct
67 Correct 32 ms 5544 KB Output is correct
68 Correct 20 ms 5632 KB Output is correct
69 Correct 29 ms 5632 KB Output is correct
70 Correct 27 ms 5748 KB Output is correct
71 Correct 22 ms 5632 KB Output is correct
72 Correct 68 ms 5724 KB Output is correct
73 Correct 58 ms 5504 KB Output is correct
74 Correct 22 ms 5632 KB Output is correct
75 Correct 25 ms 5504 KB Output is correct
76 Correct 21 ms 5504 KB Output is correct
77 Correct 20 ms 5504 KB Output is correct
78 Correct 21 ms 5504 KB Output is correct
79 Correct 31 ms 5504 KB Output is correct
80 Correct 22 ms 5504 KB Output is correct
81 Correct 30 ms 5504 KB Output is correct
82 Correct 26 ms 5628 KB Output is correct
83 Correct 23 ms 5504 KB Output is correct
84 Correct 23 ms 5504 KB Output is correct
85 Correct 23 ms 5504 KB Output is correct
86 Correct 21 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3456 KB Output is correct
2 Correct 6 ms 3456 KB Output is correct
3 Correct 7 ms 3584 KB Output is correct
4 Correct 7 ms 3712 KB Output is correct
5 Correct 10 ms 3712 KB Output is correct
6 Correct 9 ms 3712 KB Output is correct
7 Correct 9 ms 3840 KB Output is correct
8 Correct 7 ms 3712 KB Output is correct
9 Correct 7 ms 3712 KB Output is correct
10 Correct 6 ms 3712 KB Output is correct
11 Correct 6 ms 3712 KB Output is correct
12 Correct 7 ms 3840 KB Output is correct
13 Correct 7 ms 3712 KB Output is correct
14 Correct 8 ms 3712 KB Output is correct
15 Correct 10 ms 3712 KB Output is correct
16 Correct 9 ms 3712 KB Output is correct
17 Correct 9 ms 3712 KB Output is correct
18 Correct 9 ms 3840 KB Output is correct
19 Correct 7 ms 3712 KB Output is correct
20 Correct 7 ms 3712 KB Output is correct
21 Correct 7 ms 3712 KB Output is correct
22 Correct 7 ms 3712 KB Output is correct
23 Correct 10 ms 3712 KB Output is correct
24 Correct 7 ms 3712 KB Output is correct
25 Correct 8 ms 3712 KB Output is correct
26 Correct 8 ms 3712 KB Output is correct
27 Correct 6 ms 3712 KB Output is correct
28 Correct 7 ms 3712 KB Output is correct
29 Correct 7 ms 3712 KB Output is correct
30 Correct 10 ms 3712 KB Output is correct
31 Correct 9 ms 3712 KB Output is correct
32 Correct 6 ms 3712 KB Output is correct
33 Correct 7 ms 3712 KB Output is correct
34 Correct 6 ms 3712 KB Output is correct
35 Correct 8 ms 3712 KB Output is correct
36 Correct 8 ms 3712 KB Output is correct
37 Correct 7 ms 3712 KB Output is correct
38 Correct 5 ms 3712 KB Output is correct
39 Correct 7 ms 3712 KB Output is correct
40 Correct 8 ms 3712 KB Output is correct
41 Correct 7 ms 3712 KB Output is correct
42 Correct 7 ms 3712 KB Output is correct
43 Correct 7 ms 3712 KB Output is correct
44 Correct 9 ms 3712 KB Output is correct
45 Correct 10 ms 3968 KB Output is correct
46 Correct 34 ms 5504 KB Output is correct
47 Correct 34 ms 5632 KB Output is correct
48 Correct 43 ms 5504 KB Output is correct
49 Correct 20 ms 5632 KB Output is correct
50 Correct 20 ms 5708 KB Output is correct
51 Correct 19 ms 5632 KB Output is correct
52 Correct 21 ms 5632 KB Output is correct
53 Correct 23 ms 5632 KB Output is correct
54 Correct 24 ms 5632 KB Output is correct
55 Correct 21 ms 5632 KB Output is correct
56 Correct 20 ms 5504 KB Output is correct
57 Correct 57 ms 5632 KB Output is correct
58 Correct 62 ms 5504 KB Output is correct
59 Correct 73 ms 5504 KB Output is correct
60 Correct 65 ms 5504 KB Output is correct
61 Correct 25 ms 5632 KB Output is correct
62 Correct 31 ms 5676 KB Output is correct
63 Correct 24 ms 5632 KB Output is correct
64 Correct 34 ms 5504 KB Output is correct
65 Correct 48 ms 5504 KB Output is correct
66 Correct 33 ms 5504 KB Output is correct
67 Correct 32 ms 5544 KB Output is correct
68 Correct 20 ms 5632 KB Output is correct
69 Correct 29 ms 5632 KB Output is correct
70 Correct 27 ms 5748 KB Output is correct
71 Correct 22 ms 5632 KB Output is correct
72 Correct 68 ms 5724 KB Output is correct
73 Correct 58 ms 5504 KB Output is correct
74 Correct 22 ms 5632 KB Output is correct
75 Correct 25 ms 5504 KB Output is correct
76 Correct 21 ms 5504 KB Output is correct
77 Correct 20 ms 5504 KB Output is correct
78 Correct 21 ms 5504 KB Output is correct
79 Correct 31 ms 5504 KB Output is correct
80 Correct 22 ms 5504 KB Output is correct
81 Correct 30 ms 5504 KB Output is correct
82 Correct 26 ms 5628 KB Output is correct
83 Correct 23 ms 5504 KB Output is correct
84 Correct 23 ms 5504 KB Output is correct
85 Correct 23 ms 5504 KB Output is correct
86 Correct 21 ms 5504 KB Output is correct
87 Correct 107 ms 8748 KB Output is correct
88 Correct 338 ms 18928 KB Output is correct
89 Correct 1367 ms 55160 KB Output is correct
90 Correct 1273 ms 57180 KB Output is correct
91 Correct 1333 ms 57208 KB Output is correct
92 Correct 656 ms 61620 KB Output is correct
93 Correct 694 ms 61568 KB Output is correct
94 Correct 770 ms 61516 KB Output is correct
95 Correct 857 ms 60100 KB Output is correct
96 Correct 852 ms 60536 KB Output is correct
97 Correct 863 ms 60600 KB Output is correct
98 Correct 862 ms 60440 KB Output is correct
99 Correct 948 ms 59296 KB Output is correct
100 Runtime error 1154 ms 110764 KB Execution killed with signal 11 (could be triggered by violating memory limits)
101 Halted 0 ms 0 KB -