Submission #274727

# Submission time Handle Problem Language Result Execution time Memory
274727 2020-08-19T14:46:05 Z crackersamdjam Construction of Highway (JOI18_construction) C++17
16 / 100
2000 ms 37220 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}

using namespace std;
typedef long long ll;
typedef pair<int, int> T;
typedef int L;
const int MM = 1e5+5, LOG = 17;

struct node{
	T val;
	L lp;
	inline void apply(L v){
		val = {v, v};
		lp = v;
	}
};

struct segtree{
#define lc (rt<<1)
#define rc (rt<<1|1)
#define nm ((nl+nr)>>1)
	node tree[MM*4];
	const T DEF = {INT_MAX, INT_MIN};
	const L LDEF = 0;
	inline T merge(T va, T vb){ return {min(va.first, vb.first), max(va.second, vb.second)};}
	inline void pull(int rt){ tree[rt].val = merge(tree[lc].val, tree[rc].val);}
	
	// node with lazy val means yet to push to children (but updated itself)
	inline void push(int rt, int nl, int nr){
		L &val = tree[rt].lp;
		if(nl != nr and val){
			tree[lc].apply(val);
			tree[rc].apply(val);
		}
		val = LDEF;
	}
	
	void build(int l = 0, int r = MM-1, int rt = 1){
		int nl = l, nr = r;
		
		tree[rt].val = DEF;
		tree[rt].lp = LDEF;
		
		if(l == r)
			return;
		build(l, nm, lc);
		build(nm+1, r, rc);
		pull(rt);
	}
	
	void update(int l, int r, L val, int nl = 0, int nr = MM-1, int rt = 1){
		if(r < nl || l > nr)
			return;
		if(l <= nl && r >= nr){
			tree[rt].apply(val);
			return;
		}
		push(rt, nl, nr);
		update(l, r, val, nl, nm, lc);
		update(l, r, val, nm+1, nr, rc);
		pull(rt);
	}
	
	T query(int l, int r, int nl = 0, int nr = MM-1, int rt = 1){
		if(r < nl || l > nr)
			return DEF;
		if(l <= nl && r >= nr)
			return tree[rt].val;
		push(rt, nl, nr);
		return merge(query(l, r, nl, nm, lc), query(l, r, nm+1, nr, rc));
	}
#undef lc
#undef rc
#undef nm
} ST;

int n, c[MM], ina[MM], inb[MM];
vector<int> adj[MM];
int par[MM], dep[MM], heavy[MM], head[MM], pos[MM], ptr;
int bit[MM], sp[LOG][MM];
map<int, int> mp;
int X = 2;

inline int bitqu(int i){
	int ret = 0;
	for(; i; i -= i&-i)
		ret += bit[i];
	return ret;
}

inline void bitup(int i, int v){
	for(; i < X; i += i&-i)
		bit[i] += v;
}

int dfs(int cur, int pre){
	int size = 1, maxsz = 0;
	for(int u : adj[cur]){
		if(u == pre)
			continue;
		par[u] = cur, dep[u] = dep[cur]+1;
		sp[0][u] = cur;
		int szu = dfs(u, cur);
		size += szu;
		if(szu > maxsz)
			maxsz = szu, heavy[cur] = u;
	}
	return size;
}

void decompose(int cur, int id){
	head[cur] = id, pos[cur] = ++ptr;
	if(~heavy[cur])
		decompose(heavy[cur], id);
	for(int u: adj[cur]){
		if (u != par[cur] && u != heavy[cur])
			decompose(u, u);
	}
}

inline void init(){
	memset(sp, -1, sizeof sp);
	memset(heavy, -1, sizeof heavy);
	ptr = 0;
	adj[0].push_back(1);
	// adj[1].push_back(0);
	dfs(0, -1);
	decompose(1, 1);
	ST.build();
	for(int i = 1; i < LOG; i++){
		for(int j = 1; j <= n; j++){
			int u = sp[i-1][j];
			if(~u) sp[i][j] = sp[i-1][u];
		}
	}
}

vector<pair<int, int>> undo;

inline ll query(int u){
	ll res = 0;
	while(u > 0){
		int last = u, tar = ST.query(pos[last], pos[last]).first; //not par[last]?
		for(int i = __lg(dep[u]); i >= 0; i--){
			if(sp[i][u] > 0 and ST.query(pos[sp[i][u]], pos[u]) == make_pair(tar, tar)){ // min/max query to check all same
				//same colour, jump
				u = sp[i][u];
			}
		}
		u = sp[0][u]; //jump to diff one
		int cnt = dep[last]-(u == -1 ? -1 : dep[u]);
		res += (ll)cnt*bitqu(tar-1);

		bitup(tar, cnt);
		undo.emplace_back(tar, cnt);
	}
	for(auto i: undo)
		bitup(i.first, -i.second);
	undo.clear();

	return res;
}

inline void update(int a, int b, L v){
	for(; head[a] != head[b]; b = par[head[b]]){
		if(dep[head[a]] > dep[head[b]])
			swap(a, b);
		int l = pos[head[b]], r = pos[b];
		ST.update(l, r, v);
	}
	if(a != b){
		if(dep[a] > dep[b])
			swap(a, b);
		ST.update(pos[a]+1, pos[b], v);
	}
}

int main(){
	scan(n);
	for(int i = 1; i <= n; i++){
		scan(c[i]);
		mp[c[i]] = 0;
	}
	for(auto &i: mp)
		i.second = X++;
	for(int i = 1; i <= n; i++){
		c[i] = mp[c[i]];
	}
	for(int i = 1; i < n; i++){
		scan(ina[i], inb[i]);
		adj[ina[i]].emplace_back(inb[i]);
		// adj[inb[i]].emplace_back(ina[i]);
	}
	init();
	update(pos[par[1]], pos[1], c[1]);
	
	for(int i = 1; i < n; i++){
		print(query(ina[i]));
		update(0, inb[i], c[inb[i]]);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 12 ms 14504 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 12 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14592 KB Output is correct
9 Correct 14 ms 14592 KB Output is correct
10 Correct 14 ms 14592 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 13 ms 14464 KB Output is correct
16 Correct 13 ms 14464 KB Output is correct
17 Correct 13 ms 14464 KB Output is correct
18 Correct 14 ms 14592 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 12 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 11 ms 14464 KB Output is correct
27 Correct 12 ms 14464 KB Output is correct
28 Correct 13 ms 14464 KB Output is correct
29 Correct 12 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 13 ms 14464 KB Output is correct
32 Correct 12 ms 14592 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14464 KB Output is correct
35 Correct 13 ms 14592 KB Output is correct
36 Correct 12 ms 14464 KB Output is correct
37 Correct 12 ms 14464 KB Output is correct
38 Correct 12 ms 14464 KB Output is correct
39 Correct 12 ms 14464 KB Output is correct
40 Correct 12 ms 14464 KB Output is correct
41 Correct 12 ms 14464 KB Output is correct
42 Correct 12 ms 14464 KB Output is correct
43 Correct 12 ms 14464 KB Output is correct
44 Correct 12 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 12 ms 14504 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 12 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14592 KB Output is correct
9 Correct 14 ms 14592 KB Output is correct
10 Correct 14 ms 14592 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 13 ms 14464 KB Output is correct
16 Correct 13 ms 14464 KB Output is correct
17 Correct 13 ms 14464 KB Output is correct
18 Correct 14 ms 14592 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 12 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 11 ms 14464 KB Output is correct
27 Correct 12 ms 14464 KB Output is correct
28 Correct 13 ms 14464 KB Output is correct
29 Correct 12 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 13 ms 14464 KB Output is correct
32 Correct 12 ms 14592 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14464 KB Output is correct
35 Correct 13 ms 14592 KB Output is correct
36 Correct 12 ms 14464 KB Output is correct
37 Correct 12 ms 14464 KB Output is correct
38 Correct 12 ms 14464 KB Output is correct
39 Correct 12 ms 14464 KB Output is correct
40 Correct 12 ms 14464 KB Output is correct
41 Correct 12 ms 14464 KB Output is correct
42 Correct 12 ms 14464 KB Output is correct
43 Correct 12 ms 14464 KB Output is correct
44 Correct 12 ms 14464 KB Output is correct
45 Correct 15 ms 14592 KB Output is correct
46 Correct 29 ms 14968 KB Output is correct
47 Correct 31 ms 14848 KB Output is correct
48 Correct 29 ms 14848 KB Output is correct
49 Correct 21 ms 15360 KB Output is correct
50 Correct 22 ms 15360 KB Output is correct
51 Correct 23 ms 15360 KB Output is correct
52 Correct 25 ms 15104 KB Output is correct
53 Correct 25 ms 15104 KB Output is correct
54 Correct 25 ms 15104 KB Output is correct
55 Correct 25 ms 15104 KB Output is correct
56 Correct 26 ms 15104 KB Output is correct
57 Correct 61 ms 14924 KB Output is correct
58 Correct 49 ms 14968 KB Output is correct
59 Correct 49 ms 14968 KB Output is correct
60 Correct 49 ms 14848 KB Output is correct
61 Correct 29 ms 15104 KB Output is correct
62 Correct 27 ms 15224 KB Output is correct
63 Correct 27 ms 15104 KB Output is correct
64 Correct 27 ms 14720 KB Output is correct
65 Correct 30 ms 14720 KB Output is correct
66 Correct 30 ms 14848 KB Output is correct
67 Correct 31 ms 14912 KB Output is correct
68 Correct 21 ms 15116 KB Output is correct
69 Correct 25 ms 15104 KB Output is correct
70 Correct 25 ms 14940 KB Output is correct
71 Correct 28 ms 14848 KB Output is correct
72 Correct 49 ms 14924 KB Output is correct
73 Correct 55 ms 14720 KB Output is correct
74 Correct 28 ms 14848 KB Output is correct
75 Correct 28 ms 14976 KB Output is correct
76 Correct 31 ms 14976 KB Output is correct
77 Correct 26 ms 14988 KB Output is correct
78 Correct 26 ms 14720 KB Output is correct
79 Correct 25 ms 14720 KB Output is correct
80 Correct 26 ms 14720 KB Output is correct
81 Correct 32 ms 14976 KB Output is correct
82 Correct 32 ms 14976 KB Output is correct
83 Correct 34 ms 14976 KB Output is correct
84 Correct 26 ms 14808 KB Output is correct
85 Correct 29 ms 14720 KB Output is correct
86 Correct 27 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 12 ms 14504 KB Output is correct
4 Correct 13 ms 14464 KB Output is correct
5 Correct 12 ms 14464 KB Output is correct
6 Correct 12 ms 14464 KB Output is correct
7 Correct 13 ms 14464 KB Output is correct
8 Correct 13 ms 14592 KB Output is correct
9 Correct 14 ms 14592 KB Output is correct
10 Correct 14 ms 14592 KB Output is correct
11 Correct 13 ms 14464 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 12 ms 14464 KB Output is correct
15 Correct 13 ms 14464 KB Output is correct
16 Correct 13 ms 14464 KB Output is correct
17 Correct 13 ms 14464 KB Output is correct
18 Correct 14 ms 14592 KB Output is correct
19 Correct 12 ms 14464 KB Output is correct
20 Correct 12 ms 14464 KB Output is correct
21 Correct 12 ms 14464 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 12 ms 14464 KB Output is correct
25 Correct 12 ms 14464 KB Output is correct
26 Correct 11 ms 14464 KB Output is correct
27 Correct 12 ms 14464 KB Output is correct
28 Correct 13 ms 14464 KB Output is correct
29 Correct 12 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 13 ms 14464 KB Output is correct
32 Correct 12 ms 14592 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14464 KB Output is correct
35 Correct 13 ms 14592 KB Output is correct
36 Correct 12 ms 14464 KB Output is correct
37 Correct 12 ms 14464 KB Output is correct
38 Correct 12 ms 14464 KB Output is correct
39 Correct 12 ms 14464 KB Output is correct
40 Correct 12 ms 14464 KB Output is correct
41 Correct 12 ms 14464 KB Output is correct
42 Correct 12 ms 14464 KB Output is correct
43 Correct 12 ms 14464 KB Output is correct
44 Correct 12 ms 14464 KB Output is correct
45 Correct 15 ms 14592 KB Output is correct
46 Correct 29 ms 14968 KB Output is correct
47 Correct 31 ms 14848 KB Output is correct
48 Correct 29 ms 14848 KB Output is correct
49 Correct 21 ms 15360 KB Output is correct
50 Correct 22 ms 15360 KB Output is correct
51 Correct 23 ms 15360 KB Output is correct
52 Correct 25 ms 15104 KB Output is correct
53 Correct 25 ms 15104 KB Output is correct
54 Correct 25 ms 15104 KB Output is correct
55 Correct 25 ms 15104 KB Output is correct
56 Correct 26 ms 15104 KB Output is correct
57 Correct 61 ms 14924 KB Output is correct
58 Correct 49 ms 14968 KB Output is correct
59 Correct 49 ms 14968 KB Output is correct
60 Correct 49 ms 14848 KB Output is correct
61 Correct 29 ms 15104 KB Output is correct
62 Correct 27 ms 15224 KB Output is correct
63 Correct 27 ms 15104 KB Output is correct
64 Correct 27 ms 14720 KB Output is correct
65 Correct 30 ms 14720 KB Output is correct
66 Correct 30 ms 14848 KB Output is correct
67 Correct 31 ms 14912 KB Output is correct
68 Correct 21 ms 15116 KB Output is correct
69 Correct 25 ms 15104 KB Output is correct
70 Correct 25 ms 14940 KB Output is correct
71 Correct 28 ms 14848 KB Output is correct
72 Correct 49 ms 14924 KB Output is correct
73 Correct 55 ms 14720 KB Output is correct
74 Correct 28 ms 14848 KB Output is correct
75 Correct 28 ms 14976 KB Output is correct
76 Correct 31 ms 14976 KB Output is correct
77 Correct 26 ms 14988 KB Output is correct
78 Correct 26 ms 14720 KB Output is correct
79 Correct 25 ms 14720 KB Output is correct
80 Correct 26 ms 14720 KB Output is correct
81 Correct 32 ms 14976 KB Output is correct
82 Correct 32 ms 14976 KB Output is correct
83 Correct 34 ms 14976 KB Output is correct
84 Correct 26 ms 14808 KB Output is correct
85 Correct 29 ms 14720 KB Output is correct
86 Correct 27 ms 14720 KB Output is correct
87 Correct 80 ms 15632 KB Output is correct
88 Correct 226 ms 17920 KB Output is correct
89 Correct 1113 ms 26576 KB Output is correct
90 Correct 983 ms 26360 KB Output is correct
91 Correct 1059 ms 26360 KB Output is correct
92 Correct 467 ms 37112 KB Output is correct
93 Correct 456 ms 37220 KB Output is correct
94 Correct 478 ms 37112 KB Output is correct
95 Correct 731 ms 31224 KB Output is correct
96 Correct 734 ms 31552 KB Output is correct
97 Correct 775 ms 31476 KB Output is correct
98 Correct 748 ms 31428 KB Output is correct
99 Correct 834 ms 30976 KB Output is correct
100 Execution timed out 2092 ms 26452 KB Time limit exceeded
101 Halted 0 ms 0 KB -