답안 #529044

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529044 2022-02-22T03:10:22 Z Haruto810198 Construction of Highway (JOI18_construction) C++17
100 / 100
757 ms 39616 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define double long double

#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())

#define vi vector<int>
#define pii pair<int, int>

#define F first
#define S second

#define pb push_back
#define eb emplace_back
#define mkp make_pair

#define V st[cidx]
#define LC st[cidx*2]
#define RC st[cidx*2+1]

#define lsb(x) ((x)&(-(x)))

const int INF = INT_MAX;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

const int MAX = 100010;

// tree, query

int n;
int val[MAX];
int qv[MAX]; // 1 --> qv[i]
bool vis[MAX];
vi ch[MAX]; // child

// HLD

int par[MAX], sz[MAX];
int heavy[MAX], fr[MAX], dfn[MAX];
int ts;

void find_heavy(int v){
	
	sz[v] = 1;
	int Max = 0;
	heavy[v] = -1;
	par[v] = v;

	for(int i : ch[v]){
		find_heavy(i);
		if(sz[i] > Max){
			Max = sz[i];
			heavy[v] = i;
		}
		sz[v] += sz[i];
		par[i] = par[v];
	}
}

void HLD(int v, int frv){
	
	ts++;
	dfn[v] = ts;
	fr[v] = frv;
	if(heavy[v] != -1) HLD(heavy[v], frv);

	for(int i : ch[v]){
		if(i == heavy[v]) continue;
		HLD(i, i);
	}
}

// 1 --> v
vector<pii> find_segments(int v){
	vector<pii> ret;
	while(v > 1){
		ret.eb(dfn[fr[v]], dfn[v]);
		v = par[fr[v]];
	}

	if(ret.empty() or ret.back().F != 1) ret.eb(1, 1);
	reverse(ret.begin(), ret.end());
	return ret;
}

// segment tree

struct ST_Node{
	int sl, sr;
	int val, all;
	int tag;
};

struct SegTree{
	
	ST_Node st[4*MAX];
		
	void push(int cidx){
		if(V.tag == -1) return;

		V.val = V.tag;
		V.all = 1;
		if(V.sl < V.sr){
			LC.tag = V.tag;
			RC.tag = V.tag;
		}

		V.tag = -1;
	}

	void pull(int cidx){
		push(cidx);

		if(V.sl < V.sr){
			push(cidx*2);
			push(cidx*2+1);

			V.val = LC.val;
			V.all = (LC.val == RC.val and LC.all and RC.all);
		}
	}

	void build(int cidx, int cl, int cr){
		V.sl = cl;
		V.sr = cr;
		V.tag = -1;
		if(cl < cr){
			int mid = (cl + cr) / 2;
			build(cidx*2, cl, mid);
			build(cidx*2+1, mid+1, cr);
			pull(cidx);
		}
		else{
			V.val = val[cl];
			V.all = 1;
		}
	}

	void modify(int cidx, int ml, int mr, int mval){
		if(mr < V.sl or V.sr < ml) return;
		if(ml <= V.sl and V.sr <= mr){
			V.tag = mval;
			return;
		}

		modify(cidx*2, ml, mr, mval);
		modify(cidx*2+1, ml, mr, mval);
		pull(cidx);
	}

	void query(vector<pii>& arr, int cidx, int ql, int qr){
		if(qr < V.sl or V.sr < ql) return;

		pull(cidx);
		if(ql <= V.sl and V.sr <= qr and V.all){
			arr.eb(V.val, V.sr - V.sl + 1);
			return;
		}
		
		query(arr, cidx*2, ql, qr);
		query(arr, cidx*2+1, ql, qr);
	}
	
};

SegTree st;

// BIT

struct BIT{
	
	int Node[MAX];

	void modify(int pos, int val){
		while(pos < MAX){
			Node[pos] += val;
			pos += lsb(pos);
		}
	}

	int query(int pos){
		int ret = 0;
		while(pos > 0){
			ret += Node[pos];
			pos -= lsb(pos);
		}
		return ret;
	}

};

BIT bit;

// solve
int solve(int v){
	
	int pv = par[v];

	// 1 --> pv : [  ][  ][  ] ...
	vector<pii> segs_HLD = find_segments(pv); // [l, r]
	vector<pii> segs_ST; // <val, cnt>
	for(pii p : segs_HLD) st.query(segs_ST, 1, p.F, p.S);
	/*	
	cerr<<"solve "<<v<<" : "<<endl;

	cerr<<"HLD : ";
	for(pii p : segs_HLD){
		cerr<<"["<<p.F<<", "<<p.S<<"] ";
	}
	cerr<<endl;
	
	cerr<<"arr : ";
	for(pii p : segs_ST){
		FOR(i, 1, p.S, 1){
			cerr<<p.F;
		}
		cerr<<" ";
	}
	cerr<<endl;
	*/
	// find ans.
	int ret = 0;
	for(pii p : segs_ST){
		ret += p.S * (bit.query(MAX-1) - bit.query(p.F)); // [p.F+1, ...)
		bit.modify(p.F, p.S);
	}

	// init. BIT
	for(pii p : segs_ST){
		bit.modify(p.F, -p.S);
	}
	
	// modify ST
	for(pii p : segs_HLD){
		st.modify(1, p.F, p.S, val[v]);
	}
	st.modify(1, dfn[v], dfn[v], val[v]);
	
	//cerr<<"ret = "<<ret<<endl<<endl;

	return ret;
}

signed main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	// in
	cin>>n;
	FOR(i, 1, n, 1){
		cin>>val[i];
	}
	
	vis[1] = 1;
	FOR(i, 1, n-1, 1){
		int u, v;
		cin>>u>>v;
		if(vis[v]) swap(u, v);
		qv[i] = v;
		vis[v] = 1;
		ch[u].pb(v);
	}
	
	// val -> [1, n]
	map<int, int> mp;
	vi tmp;
	FOR(i, 1, n, 1){
		tmp.pb(val[i]);
	}
	sort(tmp.begin(), tmp.end());

	for(int i : tmp){
		if(mp.find(i) == mp.end()){
			int sz = szof(mp);
			mp[i] = sz+1;
		}
	}

	FOR(i, 1, n, 1){
		val[i] = mp[val[i]];
	}

	// HLD
	find_heavy(1);
	HLD(1, 1);
	/*	
	cerr<<"dfn : ";
	FOR(i, 1, n, 1){
		cerr<<dfn[i]<<" ";
	}
	cerr<<endl;
	*/
	// build segment tree
	st.build(1, 1, n);
	
	// solve
	FOR(i, 1, n-1, 1){
		//solve(qv[i]);
		cout<<solve(qv[i])<<'\n';
	}
	
	return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 2 ms 2892 KB Output is correct
9 Correct 2 ms 2892 KB Output is correct
10 Correct 2 ms 2892 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 3 ms 2764 KB Output is correct
16 Correct 3 ms 2764 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 3 ms 2764 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 3 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2764 KB Output is correct
25 Correct 2 ms 2724 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 3 ms 2764 KB Output is correct
29 Correct 4 ms 2764 KB Output is correct
30 Correct 4 ms 2764 KB Output is correct
31 Correct 3 ms 2764 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
36 Correct 2 ms 2764 KB Output is correct
37 Correct 3 ms 2764 KB Output is correct
38 Correct 3 ms 2764 KB Output is correct
39 Correct 3 ms 2800 KB Output is correct
40 Correct 2 ms 2764 KB Output is correct
41 Correct 2 ms 2764 KB Output is correct
42 Correct 2 ms 2764 KB Output is correct
43 Correct 3 ms 2764 KB Output is correct
44 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 2 ms 2892 KB Output is correct
9 Correct 2 ms 2892 KB Output is correct
10 Correct 2 ms 2892 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 3 ms 2764 KB Output is correct
16 Correct 3 ms 2764 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 3 ms 2764 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 3 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2764 KB Output is correct
25 Correct 2 ms 2724 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 3 ms 2764 KB Output is correct
29 Correct 4 ms 2764 KB Output is correct
30 Correct 4 ms 2764 KB Output is correct
31 Correct 3 ms 2764 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
36 Correct 2 ms 2764 KB Output is correct
37 Correct 3 ms 2764 KB Output is correct
38 Correct 3 ms 2764 KB Output is correct
39 Correct 3 ms 2800 KB Output is correct
40 Correct 2 ms 2764 KB Output is correct
41 Correct 2 ms 2764 KB Output is correct
42 Correct 2 ms 2764 KB Output is correct
43 Correct 3 ms 2764 KB Output is correct
44 Correct 2 ms 2764 KB Output is correct
45 Correct 4 ms 2892 KB Output is correct
46 Correct 12 ms 3688 KB Output is correct
47 Correct 12 ms 3680 KB Output is correct
48 Correct 15 ms 3680 KB Output is correct
49 Correct 9 ms 4036 KB Output is correct
50 Correct 10 ms 4032 KB Output is correct
51 Correct 8 ms 3916 KB Output is correct
52 Correct 9 ms 3788 KB Output is correct
53 Correct 8 ms 3816 KB Output is correct
54 Correct 9 ms 3788 KB Output is correct
55 Correct 8 ms 3816 KB Output is correct
56 Correct 8 ms 3788 KB Output is correct
57 Correct 15 ms 3660 KB Output is correct
58 Correct 14 ms 3664 KB Output is correct
59 Correct 17 ms 3660 KB Output is correct
60 Correct 15 ms 3660 KB Output is correct
61 Correct 11 ms 3788 KB Output is correct
62 Correct 11 ms 3744 KB Output is correct
63 Correct 15 ms 3792 KB Output is correct
64 Correct 17 ms 3416 KB Output is correct
65 Correct 15 ms 3404 KB Output is correct
66 Correct 15 ms 3440 KB Output is correct
67 Correct 11 ms 3532 KB Output is correct
68 Correct 8 ms 3728 KB Output is correct
69 Correct 8 ms 3788 KB Output is correct
70 Correct 10 ms 3576 KB Output is correct
71 Correct 7 ms 3576 KB Output is correct
72 Correct 17 ms 3660 KB Output is correct
73 Correct 15 ms 3404 KB Output is correct
74 Correct 9 ms 3576 KB Output is correct
75 Correct 11 ms 3660 KB Output is correct
76 Correct 10 ms 3660 KB Output is correct
77 Correct 12 ms 3696 KB Output is correct
78 Correct 14 ms 3532 KB Output is correct
79 Correct 10 ms 3472 KB Output is correct
80 Correct 10 ms 3460 KB Output is correct
81 Correct 14 ms 3660 KB Output is correct
82 Correct 11 ms 3708 KB Output is correct
83 Correct 16 ms 3700 KB Output is correct
84 Correct 14 ms 3500 KB Output is correct
85 Correct 10 ms 3492 KB Output is correct
86 Correct 12 ms 3464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 2 ms 2892 KB Output is correct
9 Correct 2 ms 2892 KB Output is correct
10 Correct 2 ms 2892 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 3 ms 2764 KB Output is correct
16 Correct 3 ms 2764 KB Output is correct
17 Correct 3 ms 2764 KB Output is correct
18 Correct 3 ms 2764 KB Output is correct
19 Correct 2 ms 2764 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 3 ms 2764 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 2 ms 2764 KB Output is correct
24 Correct 2 ms 2764 KB Output is correct
25 Correct 2 ms 2724 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 2 ms 2764 KB Output is correct
28 Correct 3 ms 2764 KB Output is correct
29 Correct 4 ms 2764 KB Output is correct
30 Correct 4 ms 2764 KB Output is correct
31 Correct 3 ms 2764 KB Output is correct
32 Correct 3 ms 2764 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
36 Correct 2 ms 2764 KB Output is correct
37 Correct 3 ms 2764 KB Output is correct
38 Correct 3 ms 2764 KB Output is correct
39 Correct 3 ms 2800 KB Output is correct
40 Correct 2 ms 2764 KB Output is correct
41 Correct 2 ms 2764 KB Output is correct
42 Correct 2 ms 2764 KB Output is correct
43 Correct 3 ms 2764 KB Output is correct
44 Correct 2 ms 2764 KB Output is correct
45 Correct 4 ms 2892 KB Output is correct
46 Correct 12 ms 3688 KB Output is correct
47 Correct 12 ms 3680 KB Output is correct
48 Correct 15 ms 3680 KB Output is correct
49 Correct 9 ms 4036 KB Output is correct
50 Correct 10 ms 4032 KB Output is correct
51 Correct 8 ms 3916 KB Output is correct
52 Correct 9 ms 3788 KB Output is correct
53 Correct 8 ms 3816 KB Output is correct
54 Correct 9 ms 3788 KB Output is correct
55 Correct 8 ms 3816 KB Output is correct
56 Correct 8 ms 3788 KB Output is correct
57 Correct 15 ms 3660 KB Output is correct
58 Correct 14 ms 3664 KB Output is correct
59 Correct 17 ms 3660 KB Output is correct
60 Correct 15 ms 3660 KB Output is correct
61 Correct 11 ms 3788 KB Output is correct
62 Correct 11 ms 3744 KB Output is correct
63 Correct 15 ms 3792 KB Output is correct
64 Correct 17 ms 3416 KB Output is correct
65 Correct 15 ms 3404 KB Output is correct
66 Correct 15 ms 3440 KB Output is correct
67 Correct 11 ms 3532 KB Output is correct
68 Correct 8 ms 3728 KB Output is correct
69 Correct 8 ms 3788 KB Output is correct
70 Correct 10 ms 3576 KB Output is correct
71 Correct 7 ms 3576 KB Output is correct
72 Correct 17 ms 3660 KB Output is correct
73 Correct 15 ms 3404 KB Output is correct
74 Correct 9 ms 3576 KB Output is correct
75 Correct 11 ms 3660 KB Output is correct
76 Correct 10 ms 3660 KB Output is correct
77 Correct 12 ms 3696 KB Output is correct
78 Correct 14 ms 3532 KB Output is correct
79 Correct 10 ms 3472 KB Output is correct
80 Correct 10 ms 3460 KB Output is correct
81 Correct 14 ms 3660 KB Output is correct
82 Correct 11 ms 3708 KB Output is correct
83 Correct 16 ms 3700 KB Output is correct
84 Correct 14 ms 3500 KB Output is correct
85 Correct 10 ms 3492 KB Output is correct
86 Correct 12 ms 3464 KB Output is correct
87 Correct 32 ms 5580 KB Output is correct
88 Correct 127 ms 9880 KB Output is correct
89 Correct 545 ms 28480 KB Output is correct
90 Correct 550 ms 30664 KB Output is correct
91 Correct 558 ms 30660 KB Output is correct
92 Correct 244 ms 39520 KB Output is correct
93 Correct 280 ms 39616 KB Output is correct
94 Correct 278 ms 39540 KB Output is correct
95 Correct 236 ms 35176 KB Output is correct
96 Correct 308 ms 35876 KB Output is correct
97 Correct 249 ms 35668 KB Output is correct
98 Correct 258 ms 35552 KB Output is correct
99 Correct 244 ms 34236 KB Output is correct
100 Correct 682 ms 30400 KB Output is correct
101 Correct 741 ms 30972 KB Output is correct
102 Correct 743 ms 30880 KB Output is correct
103 Correct 751 ms 30776 KB Output is correct
104 Correct 339 ms 34160 KB Output is correct
105 Correct 333 ms 34360 KB Output is correct
106 Correct 337 ms 34272 KB Output is correct
107 Correct 504 ms 22876 KB Output is correct
108 Correct 468 ms 23100 KB Output is correct
109 Correct 480 ms 25152 KB Output is correct
110 Correct 195 ms 32144 KB Output is correct
111 Correct 251 ms 35300 KB Output is correct
112 Correct 214 ms 27960 KB Output is correct
113 Correct 192 ms 26760 KB Output is correct
114 Correct 757 ms 30376 KB Output is correct
115 Correct 653 ms 23288 KB Output is correct
116 Correct 342 ms 26772 KB Output is correct
117 Correct 301 ms 31548 KB Output is correct
118 Correct 297 ms 30928 KB Output is correct
119 Correct 321 ms 30656 KB Output is correct
120 Correct 283 ms 24640 KB Output is correct
121 Correct 238 ms 24088 KB Output is correct
122 Correct 394 ms 23776 KB Output is correct
123 Correct 389 ms 31644 KB Output is correct
124 Correct 389 ms 31044 KB Output is correct
125 Correct 400 ms 30800 KB Output is correct
126 Correct 376 ms 24796 KB Output is correct
127 Correct 347 ms 24252 KB Output is correct
128 Correct 390 ms 23948 KB Output is correct