답안 #129092

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129092 2019-07-11T15:30:52 Z ZwariowanyMarcin Construction of Highway (JOI18_construction) C++14
100 / 100
513 ms 17624 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define ss(x) (int) x.size()
#define FOR(i, n) for(int i = 1; i <= n; ++i) 
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl;
 
 
using namespace std;
using namespace __gnu_pbds;
 
// order_by_key
// find_by_order
// tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> siema;

const int nax = 100005, pod = (1 << 17);
int n, a, b;
vector <int> v[nax];
int val[nax];
int q[nax][2], h[nax];
int in[nax], fre = 1, out[nax];
int P[nax];

void dfs(int u, int p) {
	in[u] = fre++;
	for(auto it: v[u]) {
		if(it != p) {
			P[it] = u;
			h[it] = h[u] + 1;
			dfs(it, u);
		}
	}
	out[u] = fre;
}

int up[nax];

struct drzewo {
	pair<int, int> d[2 * pod];
	void init() {
		for(int i = 0; i < 2 * pod; ++i)
			d[i] = mp(0, 0);
	}
	void add(int pos, int u, int ile) {
		d[pos + pod] = mp(ile, u);
		pos += pod;
		pos /= 2;
		while(pos) {
			d[pos] = max(d[2 * pos], d[2 * pos + 1]);
			pos /= 2;
		}
	}
	pair<int, int> maxi(int l, int r) {
		pair <int, int> res = mp(0, 0);
		for(l += pod, r += pod; l < r; l /= 2, r /= 2) {
			if(l & 1)
				res = max(res, d[l++]);
			if(r & 1)
				res = max(res, d[--r]);
		}
		return res;
	}
};

struct fen {
	int d[nax];
	void init() {
		for(int i = 0; i < nax; ++i)
			d[i] = 0;
	}
	void add(int pos, int x) {
		for(; pos < nax; pos += pos & -pos) {
			d[pos] += x;
		}
	}
	int daj(int x) {
		int res = 0;
		for(; x > 0; x -= x & -x) {
			res += d[x];
		}
		return res;
	}
};

fen F;
drzewo D;
			
vector <pair<int, int>> mam;

vector <int> war;

ll akt(int u) {
	mam.clear();
	int ans = 0;
	while(u != 0 && D.maxi(in[u], out[u]).fi == 0) {
		ans += F.daj(val[u] - 1);
		F.add(val[u], 1);
		mam.pb(mp(val[u], -1));
		u = P[u];
	}
	
	while(u != 0) {
		int color = D.maxi(in[u], out[u]).se;
		int ile = h[u] - h[up[color]];
		ans += (ll) F.daj(val[color] - 1) * ile;
		F.add(val[color], ile);
		mam.pb(mp(val[color], -ile));
		int D = up[color];
		up[color] = u;
		u = D;
	}
	for(auto it: mam)
		F.add(it.fi, it.se);
	return ans;
}
		


int main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	
	F.init();
	D.init();	
	h[0] = -1;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &val[i]);
		war.pb(val[i]);
	}
	
	sort(war.begin(), war.end());
	for(int i = 1; i <= n; ++i)
		val[i] = lower_bound(war.begin(), war.end(), val[i]) - war.begin() + 1;
	
	
	for(int i = 1; i <= n - 1; ++i) {
		scanf("%d%d", &q[i][0], &q[i][1]);
		v[q[i][0]].pb(q[i][1]);
		v[q[i][1]].pb(q[i][0]);
	}
	
	dfs(1, 0);
	
	for(int i = 1; i < n; ++i) {
		printf("%lld\n", akt(q[i][0]));
		D.add(in[q[i][1]], q[i][1], i);
	}
	
	
	
	
	
	
	
	
	return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:133:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
construction.cpp:135:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &val[i]);
   ~~~~~^~~~~~~~~~~~~~~
construction.cpp:145:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &q[i][0], &q[i][1]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 8 ms 5240 KB Output is correct
6 Correct 8 ms 5240 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
11 Correct 8 ms 5240 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 7 ms 5112 KB Output is correct
14 Correct 7 ms 5240 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 7 ms 5240 KB Output is correct
18 Correct 7 ms 5240 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 7 ms 5180 KB Output is correct
21 Correct 7 ms 5112 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 9 ms 5212 KB Output is correct
24 Correct 7 ms 5160 KB Output is correct
25 Correct 7 ms 5240 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 7 ms 5240 KB Output is correct
28 Correct 7 ms 5112 KB Output is correct
29 Correct 7 ms 5240 KB Output is correct
30 Correct 7 ms 5240 KB Output is correct
31 Correct 7 ms 5240 KB Output is correct
32 Correct 7 ms 5112 KB Output is correct
33 Correct 7 ms 5240 KB Output is correct
34 Correct 7 ms 5240 KB Output is correct
35 Correct 7 ms 5244 KB Output is correct
36 Correct 7 ms 5112 KB Output is correct
37 Correct 7 ms 5240 KB Output is correct
38 Correct 7 ms 5240 KB Output is correct
39 Correct 7 ms 5112 KB Output is correct
40 Correct 7 ms 5240 KB Output is correct
41 Correct 7 ms 5112 KB Output is correct
42 Correct 7 ms 5112 KB Output is correct
43 Correct 7 ms 5112 KB Output is correct
44 Correct 7 ms 5240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 8 ms 5240 KB Output is correct
6 Correct 8 ms 5240 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
11 Correct 8 ms 5240 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 7 ms 5112 KB Output is correct
14 Correct 7 ms 5240 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 7 ms 5240 KB Output is correct
18 Correct 7 ms 5240 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 7 ms 5180 KB Output is correct
21 Correct 7 ms 5112 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 9 ms 5212 KB Output is correct
24 Correct 7 ms 5160 KB Output is correct
25 Correct 7 ms 5240 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 7 ms 5240 KB Output is correct
28 Correct 7 ms 5112 KB Output is correct
29 Correct 7 ms 5240 KB Output is correct
30 Correct 7 ms 5240 KB Output is correct
31 Correct 7 ms 5240 KB Output is correct
32 Correct 7 ms 5112 KB Output is correct
33 Correct 7 ms 5240 KB Output is correct
34 Correct 7 ms 5240 KB Output is correct
35 Correct 7 ms 5244 KB Output is correct
36 Correct 7 ms 5112 KB Output is correct
37 Correct 7 ms 5240 KB Output is correct
38 Correct 7 ms 5240 KB Output is correct
39 Correct 7 ms 5112 KB Output is correct
40 Correct 7 ms 5240 KB Output is correct
41 Correct 7 ms 5112 KB Output is correct
42 Correct 7 ms 5112 KB Output is correct
43 Correct 7 ms 5112 KB Output is correct
44 Correct 7 ms 5240 KB Output is correct
45 Correct 8 ms 5240 KB Output is correct
46 Correct 14 ms 5496 KB Output is correct
47 Correct 14 ms 5496 KB Output is correct
48 Correct 13 ms 5496 KB Output is correct
49 Correct 12 ms 5624 KB Output is correct
50 Correct 12 ms 5624 KB Output is correct
51 Correct 11 ms 5624 KB Output is correct
52 Correct 11 ms 5496 KB Output is correct
53 Correct 11 ms 5624 KB Output is correct
54 Correct 12 ms 5624 KB Output is correct
55 Correct 12 ms 5620 KB Output is correct
56 Correct 12 ms 5628 KB Output is correct
57 Correct 16 ms 5496 KB Output is correct
58 Correct 17 ms 5624 KB Output is correct
59 Correct 17 ms 5496 KB Output is correct
60 Correct 17 ms 5624 KB Output is correct
61 Correct 12 ms 5496 KB Output is correct
62 Correct 12 ms 5624 KB Output is correct
63 Correct 12 ms 5624 KB Output is correct
64 Correct 12 ms 5496 KB Output is correct
65 Correct 13 ms 5496 KB Output is correct
66 Correct 13 ms 5496 KB Output is correct
67 Correct 14 ms 5496 KB Output is correct
68 Correct 11 ms 5624 KB Output is correct
69 Correct 11 ms 5496 KB Output is correct
70 Correct 11 ms 5500 KB Output is correct
71 Correct 11 ms 5496 KB Output is correct
72 Correct 16 ms 5496 KB Output is correct
73 Correct 17 ms 5496 KB Output is correct
74 Correct 12 ms 5496 KB Output is correct
75 Correct 12 ms 5436 KB Output is correct
76 Correct 11 ms 5496 KB Output is correct
77 Correct 11 ms 5496 KB Output is correct
78 Correct 11 ms 5468 KB Output is correct
79 Correct 11 ms 5496 KB Output is correct
80 Correct 11 ms 5496 KB Output is correct
81 Correct 12 ms 5496 KB Output is correct
82 Correct 12 ms 5496 KB Output is correct
83 Correct 12 ms 5496 KB Output is correct
84 Correct 12 ms 5496 KB Output is correct
85 Correct 14 ms 5496 KB Output is correct
86 Correct 12 ms 5496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 8 ms 5240 KB Output is correct
6 Correct 8 ms 5240 KB Output is correct
7 Correct 8 ms 5112 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5240 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
11 Correct 8 ms 5240 KB Output is correct
12 Correct 8 ms 5240 KB Output is correct
13 Correct 7 ms 5112 KB Output is correct
14 Correct 7 ms 5240 KB Output is correct
15 Correct 9 ms 5240 KB Output is correct
16 Correct 8 ms 5240 KB Output is correct
17 Correct 7 ms 5240 KB Output is correct
18 Correct 7 ms 5240 KB Output is correct
19 Correct 7 ms 5112 KB Output is correct
20 Correct 7 ms 5180 KB Output is correct
21 Correct 7 ms 5112 KB Output is correct
22 Correct 7 ms 5240 KB Output is correct
23 Correct 9 ms 5212 KB Output is correct
24 Correct 7 ms 5160 KB Output is correct
25 Correct 7 ms 5240 KB Output is correct
26 Correct 7 ms 5240 KB Output is correct
27 Correct 7 ms 5240 KB Output is correct
28 Correct 7 ms 5112 KB Output is correct
29 Correct 7 ms 5240 KB Output is correct
30 Correct 7 ms 5240 KB Output is correct
31 Correct 7 ms 5240 KB Output is correct
32 Correct 7 ms 5112 KB Output is correct
33 Correct 7 ms 5240 KB Output is correct
34 Correct 7 ms 5240 KB Output is correct
35 Correct 7 ms 5244 KB Output is correct
36 Correct 7 ms 5112 KB Output is correct
37 Correct 7 ms 5240 KB Output is correct
38 Correct 7 ms 5240 KB Output is correct
39 Correct 7 ms 5112 KB Output is correct
40 Correct 7 ms 5240 KB Output is correct
41 Correct 7 ms 5112 KB Output is correct
42 Correct 7 ms 5112 KB Output is correct
43 Correct 7 ms 5112 KB Output is correct
44 Correct 7 ms 5240 KB Output is correct
45 Correct 8 ms 5240 KB Output is correct
46 Correct 14 ms 5496 KB Output is correct
47 Correct 14 ms 5496 KB Output is correct
48 Correct 13 ms 5496 KB Output is correct
49 Correct 12 ms 5624 KB Output is correct
50 Correct 12 ms 5624 KB Output is correct
51 Correct 11 ms 5624 KB Output is correct
52 Correct 11 ms 5496 KB Output is correct
53 Correct 11 ms 5624 KB Output is correct
54 Correct 12 ms 5624 KB Output is correct
55 Correct 12 ms 5620 KB Output is correct
56 Correct 12 ms 5628 KB Output is correct
57 Correct 16 ms 5496 KB Output is correct
58 Correct 17 ms 5624 KB Output is correct
59 Correct 17 ms 5496 KB Output is correct
60 Correct 17 ms 5624 KB Output is correct
61 Correct 12 ms 5496 KB Output is correct
62 Correct 12 ms 5624 KB Output is correct
63 Correct 12 ms 5624 KB Output is correct
64 Correct 12 ms 5496 KB Output is correct
65 Correct 13 ms 5496 KB Output is correct
66 Correct 13 ms 5496 KB Output is correct
67 Correct 14 ms 5496 KB Output is correct
68 Correct 11 ms 5624 KB Output is correct
69 Correct 11 ms 5496 KB Output is correct
70 Correct 11 ms 5500 KB Output is correct
71 Correct 11 ms 5496 KB Output is correct
72 Correct 16 ms 5496 KB Output is correct
73 Correct 17 ms 5496 KB Output is correct
74 Correct 12 ms 5496 KB Output is correct
75 Correct 12 ms 5436 KB Output is correct
76 Correct 11 ms 5496 KB Output is correct
77 Correct 11 ms 5496 KB Output is correct
78 Correct 11 ms 5468 KB Output is correct
79 Correct 11 ms 5496 KB Output is correct
80 Correct 11 ms 5496 KB Output is correct
81 Correct 12 ms 5496 KB Output is correct
82 Correct 12 ms 5496 KB Output is correct
83 Correct 12 ms 5496 KB Output is correct
84 Correct 12 ms 5496 KB Output is correct
85 Correct 14 ms 5496 KB Output is correct
86 Correct 12 ms 5496 KB Output is correct
87 Correct 28 ms 6008 KB Output is correct
88 Correct 76 ms 7928 KB Output is correct
89 Correct 299 ms 14320 KB Output is correct
90 Correct 300 ms 14448 KB Output is correct
91 Correct 293 ms 14320 KB Output is correct
92 Correct 168 ms 17624 KB Output is correct
93 Correct 173 ms 17620 KB Output is correct
94 Correct 171 ms 17520 KB Output is correct
95 Correct 174 ms 16112 KB Output is correct
96 Correct 197 ms 16752 KB Output is correct
97 Correct 186 ms 16596 KB Output is correct
98 Correct 181 ms 16624 KB Output is correct
99 Correct 186 ms 15984 KB Output is correct
100 Correct 476 ms 13940 KB Output is correct
101 Correct 512 ms 14192 KB Output is correct
102 Correct 513 ms 14192 KB Output is correct
103 Correct 512 ms 14480 KB Output is correct
104 Correct 206 ms 15856 KB Output is correct
105 Correct 220 ms 15984 KB Output is correct
106 Correct 213 ms 16000 KB Output is correct
107 Correct 281 ms 13556 KB Output is correct
108 Correct 282 ms 13712 KB Output is correct
109 Correct 324 ms 13932 KB Output is correct
110 Correct 159 ms 16880 KB Output is correct
111 Correct 177 ms 16240 KB Output is correct
112 Correct 172 ms 15904 KB Output is correct
113 Correct 163 ms 15348 KB Output is correct
114 Correct 468 ms 13912 KB Output is correct
115 Correct 496 ms 13680 KB Output is correct
116 Correct 186 ms 15344 KB Output is correct
117 Correct 178 ms 14812 KB Output is correct
118 Correct 170 ms 14484 KB Output is correct
119 Correct 170 ms 14320 KB Output is correct
120 Correct 170 ms 14448 KB Output is correct
121 Correct 166 ms 14192 KB Output is correct
122 Correct 171 ms 14040 KB Output is correct
123 Correct 205 ms 14832 KB Output is correct
124 Correct 209 ms 14576 KB Output is correct
125 Correct 210 ms 14508 KB Output is correct
126 Correct 189 ms 14448 KB Output is correct
127 Correct 196 ms 14288 KB Output is correct
128 Correct 205 ms 14048 KB Output is correct