Submission #274824

# Submission time Handle Problem Language Result Execution time Memory
274824 2020-08-19T16:21:30 Z crackersamdjam Construction of Highway (JOI18_construction) C++17
100 / 100
1116 ms 35832 KB
#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#endif

#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;
	}
};

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

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);
	}
	
	int query(int l, int r, int val, int nl = 0, int nr = MM-1, int rt = 1){
		if(r < nl)
			return -1; //too far right, it works
		if(nr < l)
			return l; //too far left
		
		if(tree[rt].val.first == val and tree[rt].val.second == val)
			return -1;

		if(nl == nr) //doesn't work
			return nl+1;

		push(rt, nl, nr);
		int res = query(l, r, val, nm+1, nr, rc);
		if(res == -1) return query(l, r, val, nl, nm, lc);
		return res;
		//if doesn't work on right, get it
	}

	int get(int l, int nl = 0, int nr = MM-1, int rt = 1){
		if(l < nl || l > nr)
			return 0;
		if(nl == nr)
			return tree[rt].val.first;
		push(rt, nl, nr);
		return get(l, nl, nm, lc) | get(l, nm+1, nr, rc);
	}
#undef lc
#undef rc
#undef nm
} ST;

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;
	rpos[ptr] = cur; //reverse mapping
	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.get(pos[u]);

		u = ST.query(pos[head[u]], pos[u], tar);
		u = rpos[u];
		u = par[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 b, L v){
	int a = 0;
	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]);
	}
	init();
	update(pos[1], c[1]);
	
	for(int i = 1; i < n; i++){
		print(query(ina[i]));
		update(inb[i], c[inb[i]]);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 12 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 11 ms 14592 KB Output is correct
9 Correct 11 ms 14592 KB Output is correct
10 Correct 12 ms 14592 KB Output is correct
11 Correct 11 ms 14520 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 11 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 13 ms 14592 KB Output is correct
17 Correct 13 ms 14592 KB Output is correct
18 Correct 15 ms 14464 KB Output is correct
19 Correct 11 ms 14464 KB Output is correct
20 Correct 11 ms 14464 KB Output is correct
21 Correct 11 ms 14592 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 13 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 11 ms 14464 KB Output is correct
28 Correct 11 ms 14464 KB Output is correct
29 Correct 11 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 12 ms 14464 KB Output is correct
32 Correct 11 ms 14464 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14464 KB Output is correct
35 Correct 12 ms 14464 KB Output is correct
36 Correct 12 ms 14476 KB Output is correct
37 Correct 12 ms 14464 KB Output is correct
38 Correct 11 ms 14476 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 11 ms 14464 KB Output is correct
44 Correct 11 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 12 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 11 ms 14592 KB Output is correct
9 Correct 11 ms 14592 KB Output is correct
10 Correct 12 ms 14592 KB Output is correct
11 Correct 11 ms 14520 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 11 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 13 ms 14592 KB Output is correct
17 Correct 13 ms 14592 KB Output is correct
18 Correct 15 ms 14464 KB Output is correct
19 Correct 11 ms 14464 KB Output is correct
20 Correct 11 ms 14464 KB Output is correct
21 Correct 11 ms 14592 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 13 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 11 ms 14464 KB Output is correct
28 Correct 11 ms 14464 KB Output is correct
29 Correct 11 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 12 ms 14464 KB Output is correct
32 Correct 11 ms 14464 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14464 KB Output is correct
35 Correct 12 ms 14464 KB Output is correct
36 Correct 12 ms 14476 KB Output is correct
37 Correct 12 ms 14464 KB Output is correct
38 Correct 11 ms 14476 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 11 ms 14464 KB Output is correct
44 Correct 11 ms 14464 KB Output is correct
45 Correct 13 ms 14592 KB Output is correct
46 Correct 26 ms 14968 KB Output is correct
47 Correct 23 ms 14968 KB Output is correct
48 Correct 23 ms 14976 KB Output is correct
49 Correct 16 ms 15232 KB Output is correct
50 Correct 16 ms 15232 KB Output is correct
51 Correct 16 ms 15232 KB Output is correct
52 Correct 17 ms 15104 KB Output is correct
53 Correct 17 ms 15104 KB Output is correct
54 Correct 17 ms 15104 KB Output is correct
55 Correct 17 ms 15104 KB Output is correct
56 Correct 17 ms 15104 KB Output is correct
57 Correct 31 ms 14848 KB Output is correct
58 Correct 32 ms 14976 KB Output is correct
59 Correct 32 ms 14976 KB Output is correct
60 Correct 32 ms 14968 KB Output is correct
61 Correct 17 ms 15104 KB Output is correct
62 Correct 18 ms 15104 KB Output is correct
63 Correct 17 ms 15104 KB Output is correct
64 Correct 21 ms 14720 KB Output is correct
65 Correct 21 ms 14720 KB Output is correct
66 Correct 23 ms 14740 KB Output is correct
67 Correct 26 ms 14720 KB Output is correct
68 Correct 18 ms 15108 KB Output is correct
69 Correct 18 ms 15104 KB Output is correct
70 Correct 16 ms 14848 KB Output is correct
71 Correct 16 ms 14848 KB Output is correct
72 Correct 36 ms 14848 KB Output is correct
73 Correct 30 ms 14720 KB Output is correct
74 Correct 16 ms 14848 KB Output is correct
75 Correct 18 ms 14976 KB Output is correct
76 Correct 18 ms 14976 KB Output is correct
77 Correct 23 ms 14976 KB Output is correct
78 Correct 18 ms 14720 KB Output is correct
79 Correct 17 ms 14848 KB Output is correct
80 Correct 17 ms 14720 KB Output is correct
81 Correct 19 ms 14976 KB Output is correct
82 Correct 19 ms 14976 KB Output is correct
83 Correct 22 ms 14976 KB Output is correct
84 Correct 17 ms 14720 KB Output is correct
85 Correct 17 ms 14720 KB Output is correct
86 Correct 17 ms 14720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 11 ms 14464 KB Output is correct
3 Correct 13 ms 14464 KB Output is correct
4 Correct 12 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 11 ms 14592 KB Output is correct
9 Correct 11 ms 14592 KB Output is correct
10 Correct 12 ms 14592 KB Output is correct
11 Correct 11 ms 14520 KB Output is correct
12 Correct 12 ms 14464 KB Output is correct
13 Correct 13 ms 14464 KB Output is correct
14 Correct 11 ms 14464 KB Output is correct
15 Correct 12 ms 14464 KB Output is correct
16 Correct 13 ms 14592 KB Output is correct
17 Correct 13 ms 14592 KB Output is correct
18 Correct 15 ms 14464 KB Output is correct
19 Correct 11 ms 14464 KB Output is correct
20 Correct 11 ms 14464 KB Output is correct
21 Correct 11 ms 14592 KB Output is correct
22 Correct 12 ms 14464 KB Output is correct
23 Correct 12 ms 14464 KB Output is correct
24 Correct 13 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 11 ms 14464 KB Output is correct
28 Correct 11 ms 14464 KB Output is correct
29 Correct 11 ms 14464 KB Output is correct
30 Correct 13 ms 14464 KB Output is correct
31 Correct 12 ms 14464 KB Output is correct
32 Correct 11 ms 14464 KB Output is correct
33 Correct 12 ms 14464 KB Output is correct
34 Correct 12 ms 14464 KB Output is correct
35 Correct 12 ms 14464 KB Output is correct
36 Correct 12 ms 14476 KB Output is correct
37 Correct 12 ms 14464 KB Output is correct
38 Correct 11 ms 14476 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 11 ms 14464 KB Output is correct
44 Correct 11 ms 14464 KB Output is correct
45 Correct 13 ms 14592 KB Output is correct
46 Correct 26 ms 14968 KB Output is correct
47 Correct 23 ms 14968 KB Output is correct
48 Correct 23 ms 14976 KB Output is correct
49 Correct 16 ms 15232 KB Output is correct
50 Correct 16 ms 15232 KB Output is correct
51 Correct 16 ms 15232 KB Output is correct
52 Correct 17 ms 15104 KB Output is correct
53 Correct 17 ms 15104 KB Output is correct
54 Correct 17 ms 15104 KB Output is correct
55 Correct 17 ms 15104 KB Output is correct
56 Correct 17 ms 15104 KB Output is correct
57 Correct 31 ms 14848 KB Output is correct
58 Correct 32 ms 14976 KB Output is correct
59 Correct 32 ms 14976 KB Output is correct
60 Correct 32 ms 14968 KB Output is correct
61 Correct 17 ms 15104 KB Output is correct
62 Correct 18 ms 15104 KB Output is correct
63 Correct 17 ms 15104 KB Output is correct
64 Correct 21 ms 14720 KB Output is correct
65 Correct 21 ms 14720 KB Output is correct
66 Correct 23 ms 14740 KB Output is correct
67 Correct 26 ms 14720 KB Output is correct
68 Correct 18 ms 15108 KB Output is correct
69 Correct 18 ms 15104 KB Output is correct
70 Correct 16 ms 14848 KB Output is correct
71 Correct 16 ms 14848 KB Output is correct
72 Correct 36 ms 14848 KB Output is correct
73 Correct 30 ms 14720 KB Output is correct
74 Correct 16 ms 14848 KB Output is correct
75 Correct 18 ms 14976 KB Output is correct
76 Correct 18 ms 14976 KB Output is correct
77 Correct 23 ms 14976 KB Output is correct
78 Correct 18 ms 14720 KB Output is correct
79 Correct 17 ms 14848 KB Output is correct
80 Correct 17 ms 14720 KB Output is correct
81 Correct 19 ms 14976 KB Output is correct
82 Correct 19 ms 14976 KB Output is correct
83 Correct 22 ms 14976 KB Output is correct
84 Correct 17 ms 14720 KB Output is correct
85 Correct 17 ms 14720 KB Output is correct
86 Correct 17 ms 14720 KB Output is correct
87 Correct 49 ms 15616 KB Output is correct
88 Correct 136 ms 18168 KB Output is correct
89 Correct 585 ms 26636 KB Output is correct
90 Correct 637 ms 26616 KB Output is correct
91 Correct 642 ms 26564 KB Output is correct
92 Correct 189 ms 35708 KB Output is correct
93 Correct 197 ms 35704 KB Output is correct
94 Correct 190 ms 35832 KB Output is correct
95 Correct 234 ms 30708 KB Output is correct
96 Correct 226 ms 30832 KB Output is correct
97 Correct 225 ms 30964 KB Output is correct
98 Correct 227 ms 30832 KB Output is correct
99 Correct 220 ms 30456 KB Output is correct
100 Correct 980 ms 26872 KB Output is correct
101 Correct 1018 ms 27196 KB Output is correct
102 Correct 995 ms 27300 KB Output is correct
103 Correct 1116 ms 27056 KB Output is correct
104 Correct 320 ms 30584 KB Output is correct
105 Correct 300 ms 30584 KB Output is correct
106 Correct 290 ms 30800 KB Output is correct
107 Correct 458 ms 20984 KB Output is correct
108 Correct 488 ms 20968 KB Output is correct
109 Correct 567 ms 22688 KB Output is correct
110 Correct 129 ms 30328 KB Output is correct
111 Correct 225 ms 30708 KB Output is correct
112 Correct 163 ms 25460 KB Output is correct
113 Correct 163 ms 24952 KB Output is correct
114 Correct 950 ms 27000 KB Output is correct
115 Correct 917 ms 21652 KB Output is correct
116 Correct 197 ms 24824 KB Output is correct
117 Correct 248 ms 28152 KB Output is correct
118 Correct 250 ms 27384 KB Output is correct
119 Correct 257 ms 27000 KB Output is correct
120 Correct 199 ms 22776 KB Output is correct
121 Correct 204 ms 22264 KB Output is correct
122 Correct 192 ms 21880 KB Output is correct
123 Correct 330 ms 28152 KB Output is correct
124 Correct 372 ms 27384 KB Output is correct
125 Correct 381 ms 27128 KB Output is correct
126 Correct 253 ms 23032 KB Output is correct
127 Correct 247 ms 22264 KB Output is correct
128 Correct 234 ms 22008 KB Output is correct