Submission #101042

# Submission time Handle Problem Language Result Execution time Memory
101042 2019-03-16T06:37:24 Z ecasdqina Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 5096 KB
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;

struct SegmentTree {
	std::vector<i64> seg, lazy;
	int sz = 1;
	
	SegmentTree(int n) {
		while(sz < n) sz <<= 1;
		seg.assign(sz * 2, 0);
	}
	void add(int k, int x = 1) {
		k += sz;
		seg[k] += x;
		
		while(k >>= 1) seg[k] = seg[2 * k + 0] + seg[2 * k + 1];
	}
	i64 get(int a, int b) {
		i64 L = 0, R = 0;
		for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if(a & 1) L = L + seg[a++];
			if(b & 1) R = seg[--b] + R;
		}
		return L + R;
	}
	i64 operator[](int k) {
		return seg[k + sz];
	}
};

int main() {
	int n; scanf("%d", &n);
	std::vector<int> c(n), a(n - 1), b(n - 1), latte;
	std::vector<int> number(n, -1);
	for(int i = 0; i < n; i++) {
		scanf("%d", &c[i]);
		
		latte.push_back(c[i]);
	}
	for(int i = 0; i < n - 1; i++) {
		scanf("%d%d", &a[i], &b[i]);
		a[i]--; b[i]--;
		
		number[b[i]] = i;
	}
	sort(begin(latte), end(latte)); latte.erase(unique(begin(latte), end(latte)), end(latte));
	
	std::function<int (int)> getIndex = [&](int v) {
		return lower_bound(begin(latte), end(latte), v) - begin(latte);
	};
	
//	assert(n <= 4000);
	
	std::vector<int> parent(n, -1);
	for(int i = 0; i < n - 1; i++) {
		i64 ans = 0;
		int now = a[i];
		SegmentTree seg(latte.size());
		while(now != -1) {
			int k = getIndex(c[now]);
			ans += seg.get(0, k);
			seg.add(k);
			
			c[now] = c[b[i]];
			now = parent[now];
		}
		parent[b[i]] = a[i];
	
		printf("%lld\n", ans);
	//	for(auto v: c) cout << v << " "; cout << endl;
	}
	
	return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n; scanf("%d", &n);
         ~~~~~^~~~~~~~~~
construction.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
construction.cpp:45: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 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 10 ms 388 KB Output is correct
9 Correct 8 ms 384 KB Output is correct
10 Correct 8 ms 384 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
12 Correct 9 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 4 ms 256 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 6 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 384 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 4 ms 256 KB Output is correct
33 Correct 4 ms 384 KB Output is correct
34 Correct 4 ms 384 KB Output is correct
35 Correct 3 ms 384 KB Output is correct
36 Correct 4 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 5 ms 384 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 384 KB Output is correct
44 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 10 ms 388 KB Output is correct
9 Correct 8 ms 384 KB Output is correct
10 Correct 8 ms 384 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
12 Correct 9 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 4 ms 256 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 6 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 384 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 4 ms 256 KB Output is correct
33 Correct 4 ms 384 KB Output is correct
34 Correct 4 ms 384 KB Output is correct
35 Correct 3 ms 384 KB Output is correct
36 Correct 4 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 5 ms 384 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 384 KB Output is correct
44 Correct 0 ms 256 KB Output is correct
45 Correct 5 ms 384 KB Output is correct
46 Correct 34 ms 512 KB Output is correct
47 Correct 25 ms 512 KB Output is correct
48 Correct 31 ms 512 KB Output is correct
49 Correct 405 ms 632 KB Output is correct
50 Correct 435 ms 632 KB Output is correct
51 Correct 436 ms 632 KB Output is correct
52 Correct 256 ms 636 KB Output is correct
53 Correct 229 ms 512 KB Output is correct
54 Correct 230 ms 632 KB Output is correct
55 Correct 228 ms 632 KB Output is correct
56 Correct 250 ms 632 KB Output is correct
57 Correct 34 ms 632 KB Output is correct
58 Correct 26 ms 512 KB Output is correct
59 Correct 30 ms 512 KB Output is correct
60 Correct 28 ms 512 KB Output is correct
61 Correct 220 ms 524 KB Output is correct
62 Correct 208 ms 632 KB Output is correct
63 Correct 252 ms 512 KB Output is correct
64 Correct 6 ms 384 KB Output is correct
65 Correct 7 ms 384 KB Output is correct
66 Correct 11 ms 384 KB Output is correct
67 Correct 17 ms 384 KB Output is correct
68 Correct 213 ms 588 KB Output is correct
69 Correct 236 ms 632 KB Output is correct
70 Correct 104 ms 508 KB Output is correct
71 Correct 114 ms 492 KB Output is correct
72 Correct 28 ms 512 KB Output is correct
73 Correct 8 ms 512 KB Output is correct
74 Correct 103 ms 384 KB Output is correct
75 Correct 113 ms 512 KB Output is correct
76 Correct 99 ms 512 KB Output is correct
77 Correct 75 ms 512 KB Output is correct
78 Correct 58 ms 384 KB Output is correct
79 Correct 58 ms 384 KB Output is correct
80 Correct 32 ms 384 KB Output is correct
81 Correct 164 ms 512 KB Output is correct
82 Correct 97 ms 512 KB Output is correct
83 Correct 73 ms 512 KB Output is correct
84 Correct 54 ms 384 KB Output is correct
85 Correct 37 ms 384 KB Output is correct
86 Correct 29 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 10 ms 388 KB Output is correct
9 Correct 8 ms 384 KB Output is correct
10 Correct 8 ms 384 KB Output is correct
11 Correct 6 ms 384 KB Output is correct
12 Correct 9 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 4 ms 256 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 6 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 384 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 4 ms 256 KB Output is correct
33 Correct 4 ms 384 KB Output is correct
34 Correct 4 ms 384 KB Output is correct
35 Correct 3 ms 384 KB Output is correct
36 Correct 4 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 5 ms 384 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 384 KB Output is correct
44 Correct 0 ms 256 KB Output is correct
45 Correct 5 ms 384 KB Output is correct
46 Correct 34 ms 512 KB Output is correct
47 Correct 25 ms 512 KB Output is correct
48 Correct 31 ms 512 KB Output is correct
49 Correct 405 ms 632 KB Output is correct
50 Correct 435 ms 632 KB Output is correct
51 Correct 436 ms 632 KB Output is correct
52 Correct 256 ms 636 KB Output is correct
53 Correct 229 ms 512 KB Output is correct
54 Correct 230 ms 632 KB Output is correct
55 Correct 228 ms 632 KB Output is correct
56 Correct 250 ms 632 KB Output is correct
57 Correct 34 ms 632 KB Output is correct
58 Correct 26 ms 512 KB Output is correct
59 Correct 30 ms 512 KB Output is correct
60 Correct 28 ms 512 KB Output is correct
61 Correct 220 ms 524 KB Output is correct
62 Correct 208 ms 632 KB Output is correct
63 Correct 252 ms 512 KB Output is correct
64 Correct 6 ms 384 KB Output is correct
65 Correct 7 ms 384 KB Output is correct
66 Correct 11 ms 384 KB Output is correct
67 Correct 17 ms 384 KB Output is correct
68 Correct 213 ms 588 KB Output is correct
69 Correct 236 ms 632 KB Output is correct
70 Correct 104 ms 508 KB Output is correct
71 Correct 114 ms 492 KB Output is correct
72 Correct 28 ms 512 KB Output is correct
73 Correct 8 ms 512 KB Output is correct
74 Correct 103 ms 384 KB Output is correct
75 Correct 113 ms 512 KB Output is correct
76 Correct 99 ms 512 KB Output is correct
77 Correct 75 ms 512 KB Output is correct
78 Correct 58 ms 384 KB Output is correct
79 Correct 58 ms 384 KB Output is correct
80 Correct 32 ms 384 KB Output is correct
81 Correct 164 ms 512 KB Output is correct
82 Correct 97 ms 512 KB Output is correct
83 Correct 73 ms 512 KB Output is correct
84 Correct 54 ms 384 KB Output is correct
85 Correct 37 ms 384 KB Output is correct
86 Correct 29 ms 384 KB Output is correct
87 Correct 193 ms 896 KB Output is correct
88 Correct 1232 ms 1856 KB Output is correct
89 Execution timed out 2058 ms 5096 KB Time limit exceeded
90 Halted 0 ms 0 KB -