답안 #490912

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
490912 2021-11-29T17:37:10 Z nafis_shifat Construction of Highway (JOI18_construction) C++17
100 / 100
288 ms 22168 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e5+5;
const int inf=1e9;
int n, c[mxn];
vector<int> adj[mxn];
int U[mxn], V[mxn];

int chainNo = 1;
int ptr = 1;

int parent[mxn];


int chainHead[mxn];
int chainInd[mxn];
int posInBase[mxn];
int subsize[mxn];
int chainLen[mxn] = {};

vector<pii> con[mxn];



struct BIT {
	int bit[mxn] = {};
	vector<pii> ups;


	void update(int p,int v, bool rem = true) {
		if(p <= 0) return;
		if(rem) ups.emplace_back(p, v);
		for(;p<mxn;p+=p&-p) bit[p] += v;
	}
    int query(int p) {
    	int r=0;
    	for(;p>0;p-=p&-p) r += bit[p];
        return r;
    }

    void clear() {
    	while(!ups.empty()) {
    		update(ups.back().first, -ups.back().second, false);
    		ups.pop_back();
    	}
    }
} bt;

void dfs(int u, int prev) {
	subsize[u] = 1;
	for(int v : adj[u]) {
		if(v == prev) continue;

		parent[v] = u;
		dfs(v, u);
		subsize[u] += subsize[v];
	}
}



void HLD(int curNode, int prev) {

	if(chainHead[chainNo] == -1) {
		chainHead[chainNo] = curNode; 

	}

	chainLen[chainNo]++;
	chainInd[curNode] = chainNo;
	posInBase[curNode] = ptr++; 
	int sc = -1;
	for(int i = 0; i < adj[curNode].size(); i++) if(adj[curNode][i] != prev){
		if(sc == -1 || subsize[sc] < subsize[adj[curNode][i]]) {
			sc = adj[curNode][i];
		}
	}

	if(sc != -1) {
		HLD(sc, curNode);
	}

	for(int i=0; i < adj[curNode].size(); i++) if(adj[curNode][i] != prev) {
		if(sc != adj[curNode][i]) {
			chainNo++;
			HLD(adj[curNode][i], curNode);
		}
	}
}



ll get(int u, int v, int x) {
	int dn = 0;

	int len = posInBase[v] - posInBase[u] + 1;

	int id = chainInd[u];
	ll ans = 0;
	vector<pii> prc;
	while(dn < len) {
		if(dn + con[id].back().first > len) {
			pii lst = con[id].back();
			con[id].pop_back();
			int dif = len - dn;

			prc.emplace_back(dif, lst.second);


			dn = len;

			con[id].push_back({lst.first - dif, lst.second});
			break;
		}

		assert(!con[id].empty());

		pii lst = con[id].back();
		con[id].pop_back();
		


		prc.emplace_back(lst.first, lst.second);

		dn += lst.first;
	}

	con[id].push_back({len, x});


	while(!prc.empty()) {
		ans += 1ll * prc.back().first * bt.query(prc.back().second - 1);
		bt.update(prc.back().second, prc.back().first);
		prc.pop_back();
	}

	return ans;
}


ll query_up(int u, int x) {
	if(u == 1) return 0; 
	int uchain, ans = 0;
	while(u > 0) {
		uchain = chainInd[u];
		if(uchain == 1) {
			ans += get(1, u, x);
			break;
		}

		ans += get(chainHead[uchain], u, x);
		
		u = chainHead[uchain]; 
		u = parent[u];
	}
	return ans;
}


int main() {

	memset(chainHead, -1, sizeof chainHead);
	cin >> n;

	vector<int> vv;

	for(int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
		vv.push_back(c[i]);
	}

	sort(vv.begin(), vv.end());
	vv.erase(unique(vv.begin(), vv.end()), vv.end());

	for(int i = 1; i <= n; i++) {
		c[i] = lower_bound(vv.begin(), vv.end(), c[i]) - vv.begin() + 1;
	}

	for(int i = 1; i < n; i++) {
		scanf("%d%d", &U[i], &V[i]);
		adj[U[i]].push_back(V[i]);
		adj[V[i]].push_back(U[i]);
	}

	dfs(1, -1);


	HLD(1, -1);

	for(int i = 1; i <= n && chainHead[i] != -1; i++) {
		con[i].emplace_back(chainLen[i], -1);
	}


	for(int i = 1; i < n; i++) {
		cout<<query_up(V[i], c[V[i]])<<"\n";
		bt.clear();
	}
	
}

Compilation message

construction.cpp: In function 'void HLD(int, int)':
construction.cpp:75:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |  for(int i = 0; i < adj[curNode].size(); i++) if(adj[curNode][i] != prev){
      |                 ~~^~~~~~~~~~~~~~~~~~~~~
construction.cpp:85:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |  for(int i=0; i < adj[curNode].size(); i++) if(adj[curNode][i] != prev) {
      |               ~~^~~~~~~~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:170:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |   scanf("%d", &c[i]);
      |   ~~~~~^~~~~~~~~~~~~
construction.cpp:182:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |   scanf("%d%d", &U[i], &V[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5792 KB Output is correct
3 Correct 3 ms 5764 KB Output is correct
4 Correct 3 ms 5836 KB Output is correct
5 Correct 5 ms 5836 KB Output is correct
6 Correct 3 ms 5796 KB Output is correct
7 Correct 4 ms 5836 KB Output is correct
8 Correct 3 ms 5836 KB Output is correct
9 Correct 4 ms 5836 KB Output is correct
10 Correct 4 ms 5836 KB Output is correct
11 Correct 4 ms 5836 KB Output is correct
12 Correct 3 ms 5836 KB Output is correct
13 Correct 3 ms 5836 KB Output is correct
14 Correct 4 ms 5800 KB Output is correct
15 Correct 3 ms 5836 KB Output is correct
16 Correct 4 ms 5836 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 4 ms 5836 KB Output is correct
19 Correct 4 ms 5836 KB Output is correct
20 Correct 3 ms 5836 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 3 ms 5836 KB Output is correct
23 Correct 4 ms 5836 KB Output is correct
24 Correct 5 ms 5780 KB Output is correct
25 Correct 3 ms 5836 KB Output is correct
26 Correct 4 ms 5792 KB Output is correct
27 Correct 4 ms 5836 KB Output is correct
28 Correct 3 ms 5836 KB Output is correct
29 Correct 4 ms 5836 KB Output is correct
30 Correct 4 ms 5836 KB Output is correct
31 Correct 4 ms 5836 KB Output is correct
32 Correct 3 ms 5836 KB Output is correct
33 Correct 3 ms 5836 KB Output is correct
34 Correct 3 ms 5836 KB Output is correct
35 Correct 3 ms 5836 KB Output is correct
36 Correct 4 ms 5836 KB Output is correct
37 Correct 4 ms 5836 KB Output is correct
38 Correct 4 ms 5836 KB Output is correct
39 Correct 4 ms 5800 KB Output is correct
40 Correct 4 ms 5852 KB Output is correct
41 Correct 3 ms 5836 KB Output is correct
42 Correct 4 ms 5836 KB Output is correct
43 Correct 4 ms 5836 KB Output is correct
44 Correct 3 ms 5836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5792 KB Output is correct
3 Correct 3 ms 5764 KB Output is correct
4 Correct 3 ms 5836 KB Output is correct
5 Correct 5 ms 5836 KB Output is correct
6 Correct 3 ms 5796 KB Output is correct
7 Correct 4 ms 5836 KB Output is correct
8 Correct 3 ms 5836 KB Output is correct
9 Correct 4 ms 5836 KB Output is correct
10 Correct 4 ms 5836 KB Output is correct
11 Correct 4 ms 5836 KB Output is correct
12 Correct 3 ms 5836 KB Output is correct
13 Correct 3 ms 5836 KB Output is correct
14 Correct 4 ms 5800 KB Output is correct
15 Correct 3 ms 5836 KB Output is correct
16 Correct 4 ms 5836 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 4 ms 5836 KB Output is correct
19 Correct 4 ms 5836 KB Output is correct
20 Correct 3 ms 5836 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 3 ms 5836 KB Output is correct
23 Correct 4 ms 5836 KB Output is correct
24 Correct 5 ms 5780 KB Output is correct
25 Correct 3 ms 5836 KB Output is correct
26 Correct 4 ms 5792 KB Output is correct
27 Correct 4 ms 5836 KB Output is correct
28 Correct 3 ms 5836 KB Output is correct
29 Correct 4 ms 5836 KB Output is correct
30 Correct 4 ms 5836 KB Output is correct
31 Correct 4 ms 5836 KB Output is correct
32 Correct 3 ms 5836 KB Output is correct
33 Correct 3 ms 5836 KB Output is correct
34 Correct 3 ms 5836 KB Output is correct
35 Correct 3 ms 5836 KB Output is correct
36 Correct 4 ms 5836 KB Output is correct
37 Correct 4 ms 5836 KB Output is correct
38 Correct 4 ms 5836 KB Output is correct
39 Correct 4 ms 5800 KB Output is correct
40 Correct 4 ms 5852 KB Output is correct
41 Correct 3 ms 5836 KB Output is correct
42 Correct 4 ms 5836 KB Output is correct
43 Correct 4 ms 5836 KB Output is correct
44 Correct 3 ms 5836 KB Output is correct
45 Correct 4 ms 5804 KB Output is correct
46 Correct 8 ms 6220 KB Output is correct
47 Correct 8 ms 6220 KB Output is correct
48 Correct 8 ms 6220 KB Output is correct
49 Correct 6 ms 6348 KB Output is correct
50 Correct 7 ms 6348 KB Output is correct
51 Correct 7 ms 6476 KB Output is correct
52 Correct 7 ms 6348 KB Output is correct
53 Correct 8 ms 6348 KB Output is correct
54 Correct 6 ms 6316 KB Output is correct
55 Correct 6 ms 6380 KB Output is correct
56 Correct 7 ms 6348 KB Output is correct
57 Correct 10 ms 6220 KB Output is correct
58 Correct 10 ms 6220 KB Output is correct
59 Correct 10 ms 6252 KB Output is correct
60 Correct 11 ms 6220 KB Output is correct
61 Correct 7 ms 6300 KB Output is correct
62 Correct 7 ms 6348 KB Output is correct
63 Correct 8 ms 6316 KB Output is correct
64 Correct 8 ms 6188 KB Output is correct
65 Correct 7 ms 6220 KB Output is correct
66 Correct 8 ms 6196 KB Output is correct
67 Correct 9 ms 6212 KB Output is correct
68 Correct 8 ms 6332 KB Output is correct
69 Correct 6 ms 6348 KB Output is correct
70 Correct 6 ms 6348 KB Output is correct
71 Correct 8 ms 6312 KB Output is correct
72 Correct 12 ms 6236 KB Output is correct
73 Correct 9 ms 6224 KB Output is correct
74 Correct 6 ms 6316 KB Output is correct
75 Correct 8 ms 6220 KB Output is correct
76 Correct 8 ms 6296 KB Output is correct
77 Correct 9 ms 6248 KB Output is correct
78 Correct 6 ms 6192 KB Output is correct
79 Correct 7 ms 6220 KB Output is correct
80 Correct 6 ms 6220 KB Output is correct
81 Correct 6 ms 6220 KB Output is correct
82 Correct 6 ms 6220 KB Output is correct
83 Correct 7 ms 6220 KB Output is correct
84 Correct 6 ms 6220 KB Output is correct
85 Correct 6 ms 6220 KB Output is correct
86 Correct 6 ms 6220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5792 KB Output is correct
3 Correct 3 ms 5764 KB Output is correct
4 Correct 3 ms 5836 KB Output is correct
5 Correct 5 ms 5836 KB Output is correct
6 Correct 3 ms 5796 KB Output is correct
7 Correct 4 ms 5836 KB Output is correct
8 Correct 3 ms 5836 KB Output is correct
9 Correct 4 ms 5836 KB Output is correct
10 Correct 4 ms 5836 KB Output is correct
11 Correct 4 ms 5836 KB Output is correct
12 Correct 3 ms 5836 KB Output is correct
13 Correct 3 ms 5836 KB Output is correct
14 Correct 4 ms 5800 KB Output is correct
15 Correct 3 ms 5836 KB Output is correct
16 Correct 4 ms 5836 KB Output is correct
17 Correct 4 ms 5836 KB Output is correct
18 Correct 4 ms 5836 KB Output is correct
19 Correct 4 ms 5836 KB Output is correct
20 Correct 3 ms 5836 KB Output is correct
21 Correct 4 ms 5836 KB Output is correct
22 Correct 3 ms 5836 KB Output is correct
23 Correct 4 ms 5836 KB Output is correct
24 Correct 5 ms 5780 KB Output is correct
25 Correct 3 ms 5836 KB Output is correct
26 Correct 4 ms 5792 KB Output is correct
27 Correct 4 ms 5836 KB Output is correct
28 Correct 3 ms 5836 KB Output is correct
29 Correct 4 ms 5836 KB Output is correct
30 Correct 4 ms 5836 KB Output is correct
31 Correct 4 ms 5836 KB Output is correct
32 Correct 3 ms 5836 KB Output is correct
33 Correct 3 ms 5836 KB Output is correct
34 Correct 3 ms 5836 KB Output is correct
35 Correct 3 ms 5836 KB Output is correct
36 Correct 4 ms 5836 KB Output is correct
37 Correct 4 ms 5836 KB Output is correct
38 Correct 4 ms 5836 KB Output is correct
39 Correct 4 ms 5800 KB Output is correct
40 Correct 4 ms 5852 KB Output is correct
41 Correct 3 ms 5836 KB Output is correct
42 Correct 4 ms 5836 KB Output is correct
43 Correct 4 ms 5836 KB Output is correct
44 Correct 3 ms 5836 KB Output is correct
45 Correct 4 ms 5804 KB Output is correct
46 Correct 8 ms 6220 KB Output is correct
47 Correct 8 ms 6220 KB Output is correct
48 Correct 8 ms 6220 KB Output is correct
49 Correct 6 ms 6348 KB Output is correct
50 Correct 7 ms 6348 KB Output is correct
51 Correct 7 ms 6476 KB Output is correct
52 Correct 7 ms 6348 KB Output is correct
53 Correct 8 ms 6348 KB Output is correct
54 Correct 6 ms 6316 KB Output is correct
55 Correct 6 ms 6380 KB Output is correct
56 Correct 7 ms 6348 KB Output is correct
57 Correct 10 ms 6220 KB Output is correct
58 Correct 10 ms 6220 KB Output is correct
59 Correct 10 ms 6252 KB Output is correct
60 Correct 11 ms 6220 KB Output is correct
61 Correct 7 ms 6300 KB Output is correct
62 Correct 7 ms 6348 KB Output is correct
63 Correct 8 ms 6316 KB Output is correct
64 Correct 8 ms 6188 KB Output is correct
65 Correct 7 ms 6220 KB Output is correct
66 Correct 8 ms 6196 KB Output is correct
67 Correct 9 ms 6212 KB Output is correct
68 Correct 8 ms 6332 KB Output is correct
69 Correct 6 ms 6348 KB Output is correct
70 Correct 6 ms 6348 KB Output is correct
71 Correct 8 ms 6312 KB Output is correct
72 Correct 12 ms 6236 KB Output is correct
73 Correct 9 ms 6224 KB Output is correct
74 Correct 6 ms 6316 KB Output is correct
75 Correct 8 ms 6220 KB Output is correct
76 Correct 8 ms 6296 KB Output is correct
77 Correct 9 ms 6248 KB Output is correct
78 Correct 6 ms 6192 KB Output is correct
79 Correct 7 ms 6220 KB Output is correct
80 Correct 6 ms 6220 KB Output is correct
81 Correct 6 ms 6220 KB Output is correct
82 Correct 6 ms 6220 KB Output is correct
83 Correct 7 ms 6220 KB Output is correct
84 Correct 6 ms 6220 KB Output is correct
85 Correct 6 ms 6220 KB Output is correct
86 Correct 6 ms 6220 KB Output is correct
87 Correct 21 ms 6828 KB Output is correct
88 Correct 60 ms 8944 KB Output is correct
89 Correct 199 ms 16628 KB Output is correct
90 Correct 186 ms 16652 KB Output is correct
91 Correct 201 ms 16640 KB Output is correct
92 Correct 108 ms 22168 KB Output is correct
93 Correct 98 ms 22108 KB Output is correct
94 Correct 119 ms 22164 KB Output is correct
95 Correct 103 ms 21120 KB Output is correct
96 Correct 107 ms 21520 KB Output is correct
97 Correct 105 ms 21568 KB Output is correct
98 Correct 109 ms 21516 KB Output is correct
99 Correct 107 ms 20160 KB Output is correct
100 Correct 285 ms 15956 KB Output is correct
101 Correct 269 ms 16288 KB Output is correct
102 Correct 277 ms 16328 KB Output is correct
103 Correct 288 ms 16316 KB Output is correct
104 Correct 115 ms 20028 KB Output is correct
105 Correct 113 ms 20028 KB Output is correct
106 Correct 124 ms 20060 KB Output is correct
107 Correct 174 ms 15748 KB Output is correct
108 Correct 166 ms 15992 KB Output is correct
109 Correct 174 ms 16196 KB Output is correct
110 Correct 91 ms 21536 KB Output is correct
111 Correct 123 ms 21196 KB Output is correct
112 Correct 94 ms 20908 KB Output is correct
113 Correct 93 ms 19488 KB Output is correct
114 Correct 236 ms 15976 KB Output is correct
115 Correct 260 ms 15668 KB Output is correct
116 Correct 102 ms 19492 KB Output is correct
117 Correct 119 ms 17940 KB Output is correct
118 Correct 108 ms 17172 KB Output is correct
119 Correct 109 ms 16780 KB Output is correct
120 Correct 96 ms 17376 KB Output is correct
121 Correct 97 ms 16796 KB Output is correct
122 Correct 96 ms 16552 KB Output is correct
123 Correct 142 ms 17804 KB Output is correct
124 Correct 132 ms 17280 KB Output is correct
125 Correct 122 ms 16924 KB Output is correct
126 Correct 132 ms 17484 KB Output is correct
127 Correct 111 ms 16944 KB Output is correct
128 Correct 133 ms 16572 KB Output is correct