답안 #102630

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102630 2019-03-26T10:55:00 Z scanhex Construction of Highway (JOI18_construction) C++17
100 / 100
1240 ms 28780 KB
#include <vector>
#include <iostream>
#include <random>
#include <cassert>
#include <tuple>
#include <algorithm>

using namespace std;
using nagai = long long;
#define sz(x) int((x).size())

const int LG=17;
const int N=1<<LG;
struct v{
	int mn,mx,psh;
} t[2*N];
void push1(int u){
	if(t[u].psh){
		t[u*2].psh=t[u*2+1].psh=t[u].psh;
		t[u*2].mn=t[u*2+1].mn=t[u*2].mx=t[u*2+1].mx=t[u].psh;
		t[u].psh=0;
	}
}
void push(int u){
	for(int i=LG;i>0;--i)
		push1(u>>i);
}
void upd(int u){
	t[u].mn=min(t[u*2+1].mn,t[u*2].mn);
	t[u].mx=max(t[u*2].mx,t[u*2+1].mx);
}
void st(int u,int l,int r,int ql,int qr,int v){
	if(l>=qr||ql>=r)return;
	if(ql<=l&&r<=qr){
		t[u]={v,v,v};
		return;
	}
	push1(u);
	int m=(l+r)/2;
	st(u*2,l,m,ql,qr,v);
	st(u*2+1,m,r,ql,qr,v);
	upd(u);
}
int getr(int u,int l,int r,int ql,int v=-1){
	if(u==1){
		push(ql+N);
		v=t[ql+N].mn;
	}
	if(v<t[u].mn||v>t[u].mx)return l;
	if(t[u].mn==t[u].mx)
		return r;
	push1(u);
	int m=(l+r)/2;
	if(m<=ql)return getr(u*2+1,m,r,ql,v);
	int kek=getr(u*2,l,m,ql,v);
	if(kek==m){
		return getr(u*2+1,m,r,ql,v);
	}
	return kek;
}
int n;
int C[N];
vector<int>g[N];
vector<int>ch[N];
int sz[N];
void d1(int u,int p=-1){
	sz[u]=1;
	for(int v:g[u]){
		if(v==p)continue;
		d1(v,u);
		sz[u]+=sz[v];
		ch[u].push_back(v);
		if(sz[v]>sz[ch[u][0]])
			swap(ch[u].back(),ch[u].front());
	}
}
int tin[N];
int tot=0;
int top[N];
int par[N];
void d2(int u){
	tin[u]=tot++;
	for(int v:ch[u]){
		par[v]=u;
		top[v]=v==ch[u][0]?top[u]:v;
		d2(v);
	}
}
void st(int u,int col){
	while(true){
		int tp=top[u];
		st(1,0,N,tin[tp],tin[u]+1,col);
		if(tp==0)return;
		u=par[tp];
	}
}
vector<pair<int,int>>glob;
void gt1(int l,int r){
	while(true){
		int kek=getr(1,0,N,l);
		int v=t[N+l].mn;
		if(kek>=r){
			glob.emplace_back(r-l,v);
			return;
		}
		glob.emplace_back(kek-l,v);
		l=kek;
	}
}
void gt(int u){
	glob.clear();
	vector<pair<int,int>>p;
	while(true){
		int tp=top[u];
		p.emplace_back(tin[tp],tin[u]+1);
		if(tp==0)break;
		u=par[tp];
	}
	reverse(p.begin(),p.end());
	for(auto q:p)gt1(q.first,q.second);
}
struct it{
	int t[2*N];
	int get(int l,int r){
		l+=N;
		r+=N;
		int ans=0;
		while(l<r){
			 if(l&1)ans+=t[l++];
			 if(r&1)ans+=t[--r];
			 l/=2,r/=2;
		}
		return ans;
	}
	void inc(int x,int y){
		x+=N;
		t[x]+=y;
		while(x>1)t[x>>1]=t[x]+t[x^1],x>>=1;
	}
} myit;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	n=100000;
	cin>>n;
	for(int i=0;i<n;++i){
		C[i]=i;
		cin>>C[i];
	}
	vector<pair<int,int>>p;
	for(int i=0;i<n-1;++i){
		 int a=rand()%(i+1),b=i+1;
		 cin>>a>>b;
		 --a,--b;
		 p.emplace_back(a,b);
		 g[a].push_back(b);
		 g[b].push_back(a);
	}
	vector<int>cs(C,C+n);
	sort(cs.begin(),cs.end());
	for(int i=0;i<n;++i)
		C[i]=lower_bound(cs.begin(),cs.end(),C[i])-cs.begin()+1;
	d1(0);
	d2(0);
	st(0,C[0]);
	int s=0;
	for(auto q:p){
		int a,b;
		tie(a,b)=q;
		gt(a);
		auto&lol=glob;
		s+=lol.size();
		nagai ans=0;
		for(int i=0;i<lol.size();++i){
			ans+=(nagai)lol[i].first*myit.get(lol[i].second+1,N);
			myit.inc(lol[i].second,lol[i].first);
		}
		for(int i=0;i<lol.size();++i)
			myit.inc(lol[i].second,-lol[i].first);
		cout<<ans<<'\n';
		st(b,C[b]);
	}
	return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:176:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<lol.size();++i){
               ~^~~~~~~~~~~
construction.cpp:180:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<lol.size();++i)
               ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6672 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 7 ms 6628 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 10 ms 6656 KB Output is correct
6 Correct 11 ms 6656 KB Output is correct
7 Correct 10 ms 6700 KB Output is correct
8 Correct 10 ms 6756 KB Output is correct
9 Correct 10 ms 6784 KB Output is correct
10 Correct 7 ms 6784 KB Output is correct
11 Correct 8 ms 6656 KB Output is correct
12 Correct 10 ms 6656 KB Output is correct
13 Correct 9 ms 6656 KB Output is correct
14 Correct 11 ms 6784 KB Output is correct
15 Correct 11 ms 6656 KB Output is correct
16 Correct 11 ms 6656 KB Output is correct
17 Correct 11 ms 6656 KB Output is correct
18 Correct 10 ms 6656 KB Output is correct
19 Correct 9 ms 6656 KB Output is correct
20 Correct 9 ms 6756 KB Output is correct
21 Correct 10 ms 6784 KB Output is correct
22 Correct 9 ms 6656 KB Output is correct
23 Correct 9 ms 6656 KB Output is correct
24 Correct 10 ms 6656 KB Output is correct
25 Correct 12 ms 6656 KB Output is correct
26 Correct 9 ms 6656 KB Output is correct
27 Correct 9 ms 6796 KB Output is correct
28 Correct 8 ms 6656 KB Output is correct
29 Correct 10 ms 6784 KB Output is correct
30 Correct 12 ms 6656 KB Output is correct
31 Correct 11 ms 6656 KB Output is correct
32 Correct 13 ms 6784 KB Output is correct
33 Correct 9 ms 6656 KB Output is correct
34 Correct 10 ms 6704 KB Output is correct
35 Correct 11 ms 6656 KB Output is correct
36 Correct 10 ms 6656 KB Output is correct
37 Correct 10 ms 6784 KB Output is correct
38 Correct 9 ms 6656 KB Output is correct
39 Correct 10 ms 6656 KB Output is correct
40 Correct 10 ms 6656 KB Output is correct
41 Correct 10 ms 6656 KB Output is correct
42 Correct 10 ms 6656 KB Output is correct
43 Correct 10 ms 6656 KB Output is correct
44 Correct 11 ms 6656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6672 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 7 ms 6628 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 10 ms 6656 KB Output is correct
6 Correct 11 ms 6656 KB Output is correct
7 Correct 10 ms 6700 KB Output is correct
8 Correct 10 ms 6756 KB Output is correct
9 Correct 10 ms 6784 KB Output is correct
10 Correct 7 ms 6784 KB Output is correct
11 Correct 8 ms 6656 KB Output is correct
12 Correct 10 ms 6656 KB Output is correct
13 Correct 9 ms 6656 KB Output is correct
14 Correct 11 ms 6784 KB Output is correct
15 Correct 11 ms 6656 KB Output is correct
16 Correct 11 ms 6656 KB Output is correct
17 Correct 11 ms 6656 KB Output is correct
18 Correct 10 ms 6656 KB Output is correct
19 Correct 9 ms 6656 KB Output is correct
20 Correct 9 ms 6756 KB Output is correct
21 Correct 10 ms 6784 KB Output is correct
22 Correct 9 ms 6656 KB Output is correct
23 Correct 9 ms 6656 KB Output is correct
24 Correct 10 ms 6656 KB Output is correct
25 Correct 12 ms 6656 KB Output is correct
26 Correct 9 ms 6656 KB Output is correct
27 Correct 9 ms 6796 KB Output is correct
28 Correct 8 ms 6656 KB Output is correct
29 Correct 10 ms 6784 KB Output is correct
30 Correct 12 ms 6656 KB Output is correct
31 Correct 11 ms 6656 KB Output is correct
32 Correct 13 ms 6784 KB Output is correct
33 Correct 9 ms 6656 KB Output is correct
34 Correct 10 ms 6704 KB Output is correct
35 Correct 11 ms 6656 KB Output is correct
36 Correct 10 ms 6656 KB Output is correct
37 Correct 10 ms 6784 KB Output is correct
38 Correct 9 ms 6656 KB Output is correct
39 Correct 10 ms 6656 KB Output is correct
40 Correct 10 ms 6656 KB Output is correct
41 Correct 10 ms 6656 KB Output is correct
42 Correct 10 ms 6656 KB Output is correct
43 Correct 10 ms 6656 KB Output is correct
44 Correct 11 ms 6656 KB Output is correct
45 Correct 12 ms 6912 KB Output is correct
46 Correct 22 ms 7040 KB Output is correct
47 Correct 23 ms 7040 KB Output is correct
48 Correct 30 ms 7160 KB Output is correct
49 Correct 17 ms 7552 KB Output is correct
50 Correct 15 ms 7552 KB Output is correct
51 Correct 14 ms 7500 KB Output is correct
52 Correct 16 ms 7296 KB Output is correct
53 Correct 23 ms 7296 KB Output is correct
54 Correct 16 ms 7296 KB Output is correct
55 Correct 15 ms 7296 KB Output is correct
56 Correct 16 ms 7296 KB Output is correct
57 Correct 30 ms 7040 KB Output is correct
58 Correct 34 ms 7040 KB Output is correct
59 Correct 28 ms 7040 KB Output is correct
60 Correct 32 ms 7168 KB Output is correct
61 Correct 15 ms 7296 KB Output is correct
62 Correct 15 ms 7296 KB Output is correct
63 Correct 15 ms 7296 KB Output is correct
64 Correct 18 ms 7040 KB Output is correct
65 Correct 20 ms 7040 KB Output is correct
66 Correct 21 ms 7156 KB Output is correct
67 Correct 24 ms 7040 KB Output is correct
68 Correct 14 ms 7552 KB Output is correct
69 Correct 18 ms 7296 KB Output is correct
70 Correct 14 ms 7296 KB Output is correct
71 Correct 17 ms 7296 KB Output is correct
72 Correct 28 ms 7040 KB Output is correct
73 Correct 28 ms 7040 KB Output is correct
74 Correct 21 ms 7296 KB Output is correct
75 Correct 17 ms 7084 KB Output is correct
76 Correct 17 ms 7168 KB Output is correct
77 Correct 15 ms 7168 KB Output is correct
78 Correct 20 ms 7168 KB Output is correct
79 Correct 18 ms 7168 KB Output is correct
80 Correct 16 ms 7168 KB Output is correct
81 Correct 22 ms 7168 KB Output is correct
82 Correct 21 ms 7160 KB Output is correct
83 Correct 21 ms 7040 KB Output is correct
84 Correct 17 ms 7168 KB Output is correct
85 Correct 18 ms 7168 KB Output is correct
86 Correct 23 ms 7168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6672 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 7 ms 6628 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 10 ms 6656 KB Output is correct
6 Correct 11 ms 6656 KB Output is correct
7 Correct 10 ms 6700 KB Output is correct
8 Correct 10 ms 6756 KB Output is correct
9 Correct 10 ms 6784 KB Output is correct
10 Correct 7 ms 6784 KB Output is correct
11 Correct 8 ms 6656 KB Output is correct
12 Correct 10 ms 6656 KB Output is correct
13 Correct 9 ms 6656 KB Output is correct
14 Correct 11 ms 6784 KB Output is correct
15 Correct 11 ms 6656 KB Output is correct
16 Correct 11 ms 6656 KB Output is correct
17 Correct 11 ms 6656 KB Output is correct
18 Correct 10 ms 6656 KB Output is correct
19 Correct 9 ms 6656 KB Output is correct
20 Correct 9 ms 6756 KB Output is correct
21 Correct 10 ms 6784 KB Output is correct
22 Correct 9 ms 6656 KB Output is correct
23 Correct 9 ms 6656 KB Output is correct
24 Correct 10 ms 6656 KB Output is correct
25 Correct 12 ms 6656 KB Output is correct
26 Correct 9 ms 6656 KB Output is correct
27 Correct 9 ms 6796 KB Output is correct
28 Correct 8 ms 6656 KB Output is correct
29 Correct 10 ms 6784 KB Output is correct
30 Correct 12 ms 6656 KB Output is correct
31 Correct 11 ms 6656 KB Output is correct
32 Correct 13 ms 6784 KB Output is correct
33 Correct 9 ms 6656 KB Output is correct
34 Correct 10 ms 6704 KB Output is correct
35 Correct 11 ms 6656 KB Output is correct
36 Correct 10 ms 6656 KB Output is correct
37 Correct 10 ms 6784 KB Output is correct
38 Correct 9 ms 6656 KB Output is correct
39 Correct 10 ms 6656 KB Output is correct
40 Correct 10 ms 6656 KB Output is correct
41 Correct 10 ms 6656 KB Output is correct
42 Correct 10 ms 6656 KB Output is correct
43 Correct 10 ms 6656 KB Output is correct
44 Correct 11 ms 6656 KB Output is correct
45 Correct 12 ms 6912 KB Output is correct
46 Correct 22 ms 7040 KB Output is correct
47 Correct 23 ms 7040 KB Output is correct
48 Correct 30 ms 7160 KB Output is correct
49 Correct 17 ms 7552 KB Output is correct
50 Correct 15 ms 7552 KB Output is correct
51 Correct 14 ms 7500 KB Output is correct
52 Correct 16 ms 7296 KB Output is correct
53 Correct 23 ms 7296 KB Output is correct
54 Correct 16 ms 7296 KB Output is correct
55 Correct 15 ms 7296 KB Output is correct
56 Correct 16 ms 7296 KB Output is correct
57 Correct 30 ms 7040 KB Output is correct
58 Correct 34 ms 7040 KB Output is correct
59 Correct 28 ms 7040 KB Output is correct
60 Correct 32 ms 7168 KB Output is correct
61 Correct 15 ms 7296 KB Output is correct
62 Correct 15 ms 7296 KB Output is correct
63 Correct 15 ms 7296 KB Output is correct
64 Correct 18 ms 7040 KB Output is correct
65 Correct 20 ms 7040 KB Output is correct
66 Correct 21 ms 7156 KB Output is correct
67 Correct 24 ms 7040 KB Output is correct
68 Correct 14 ms 7552 KB Output is correct
69 Correct 18 ms 7296 KB Output is correct
70 Correct 14 ms 7296 KB Output is correct
71 Correct 17 ms 7296 KB Output is correct
72 Correct 28 ms 7040 KB Output is correct
73 Correct 28 ms 7040 KB Output is correct
74 Correct 21 ms 7296 KB Output is correct
75 Correct 17 ms 7084 KB Output is correct
76 Correct 17 ms 7168 KB Output is correct
77 Correct 15 ms 7168 KB Output is correct
78 Correct 20 ms 7168 KB Output is correct
79 Correct 18 ms 7168 KB Output is correct
80 Correct 16 ms 7168 KB Output is correct
81 Correct 22 ms 7168 KB Output is correct
82 Correct 21 ms 7160 KB Output is correct
83 Correct 21 ms 7040 KB Output is correct
84 Correct 17 ms 7168 KB Output is correct
85 Correct 18 ms 7168 KB Output is correct
86 Correct 23 ms 7168 KB Output is correct
87 Correct 48 ms 7808 KB Output is correct
88 Correct 176 ms 10068 KB Output is correct
89 Correct 757 ms 18164 KB Output is correct
90 Correct 757 ms 18152 KB Output is correct
91 Correct 780 ms 18244 KB Output is correct
92 Correct 249 ms 28776 KB Output is correct
93 Correct 242 ms 28780 KB Output is correct
94 Correct 272 ms 28780 KB Output is correct
95 Correct 271 ms 23460 KB Output is correct
96 Correct 318 ms 23404 KB Output is correct
97 Correct 322 ms 23376 KB Output is correct
98 Correct 309 ms 23404 KB Output is correct
99 Correct 332 ms 22724 KB Output is correct
100 Correct 1139 ms 18688 KB Output is correct
101 Correct 1153 ms 18664 KB Output is correct
102 Correct 1240 ms 18512 KB Output is correct
103 Correct 1171 ms 18540 KB Output is correct
104 Correct 358 ms 22636 KB Output is correct
105 Correct 374 ms 22584 KB Output is correct
106 Correct 355 ms 22632 KB Output is correct
107 Correct 571 ms 17476 KB Output is correct
108 Correct 722 ms 18152 KB Output is correct
109 Correct 705 ms 18152 KB Output is correct
110 Correct 245 ms 28780 KB Output is correct
111 Correct 339 ms 23408 KB Output is correct
112 Correct 277 ms 23316 KB Output is correct
113 Correct 276 ms 22620 KB Output is correct
114 Correct 1088 ms 18536 KB Output is correct
115 Correct 1185 ms 18408 KB Output is correct
116 Correct 366 ms 22632 KB Output is correct
117 Correct 352 ms 20296 KB Output is correct
118 Correct 396 ms 19436 KB Output is correct
119 Correct 337 ms 19056 KB Output is correct
120 Correct 373 ms 20196 KB Output is correct
121 Correct 331 ms 19560 KB Output is correct
122 Correct 312 ms 19200 KB Output is correct
123 Correct 358 ms 20332 KB Output is correct
124 Correct 500 ms 19436 KB Output is correct
125 Correct 433 ms 19156 KB Output is correct
126 Correct 382 ms 20340 KB Output is correct
127 Correct 407 ms 19560 KB Output is correct
128 Correct 436 ms 19312 KB Output is correct