답안 #728317

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
728317 2023-04-22T07:24:42 Z myrcella Construction of Highway (JOI18_construction) C++17
100 / 100
1257 ms 28468 KB
//by szh
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}

const int maxn = 1e5+10;

int n;
int tree[maxn*4];
int dfn[maxn];
vector <int> edge[maxn];
int w[maxn],ww[maxn];
int st[maxn][17];
pii q[maxn];
int tot = 1;
map <int,int> mp;
int bit[maxn];
int sz[maxn];
int id[maxn];

void dfs(int u) {
	dfn[u] = tot++;
	sz[u] = 1;
	rep(i,1,17) {
		if (st[u][i-1]==-1) break;
		st[u][i] = st[st[u][i-1]][i-1];
	}
	for (int v:edge[u]) dfs(v),sz[u] += sz[v];
}

void update(int c,int cl,int cr,int pos,int val) {
	if (cl==cr) tree[c] = val;
	else {
		int mid=cl+cr>>1;
		if (pos<=mid) update(c<<1,cl,mid,pos,val);
		else update(c<<1|1,mid+1,cr,pos,val);
		tree[c] = max(tree[c<<1],tree[c<<1|1]);
	}
	return;
}

void update(int pos,int val) {
	while (pos<maxn) {
		bit[pos] += val;
		pos += lowbit(pos);
	}
	return;
}

int query(int c,int cl,int cr,int l,int r) {
	if (l<=cl and cr<=r) return tree[c];
	int mid=cl+cr>>1;
	int ret = -1;
	if (l<=mid) ret = query(c<<1,cl,mid,l,r);
	if (r>mid) ret = max(ret,query(c<<1|1,mid+1,cr,l,r));
	return ret;
}

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

int main() {
//	freopen("input.txt","r",stdin);	
	std::ios::sync_with_stdio(false);cin.tie(0);
	memset(st,-1,sizeof(st));
	memset(tree,-1,sizeof(tree));
	cin>>n;
	rep(i,1,n+1) cin>>w[i],mp[w[i]]=1;
	for (auto &it:mp) it.se=tot++;
	tot = 1;
	rep(i,1,n+1) ww[i] = mp[w[i]];
	id[1] = tot++;
	rep(i,1,n) {
		int u,v;
		cin>>u>>v;
		id[v] = tot++;
		u = id[u];
		v = id[v];
//		debug(u),debug(v);
		q[i] = {u,v};
		edge[u].pb(v);
		st[v][0] = u;
	}
	rep(i,1,n+1) w[id[i]] = ww[i];
	tot = 1;
	dfs(1);
	update(1,1,n,dfn[1],1);
	rep(qaq,1,n) {
		int u=q[qaq].fi;
//		debug(u);
		int val=query(1,1,n,dfn[u],dfn[u]+sz[u]-1);
//		debug(val);
		vector<pii> tmp;
		while (1) {
			int cnt = 1;
			for (int j =16;j>=0;j--) if (st[u][j]!=-1 and query(1,1,n,dfn[st[u][j]],dfn[st[u][j]]+sz[st[u][j]]-1)==val) u = st[u][j],cnt+=(1<<j);
			tmp.pb({w[val],cnt});
//			debug(w[val]);debug(cnt);debug(u);debug(st[u][0]);
			if (st[u][0]==-1) break;
			u = st[u][0],val = query(1,1,n,dfn[u],dfn[u]+sz[u]-1);
		}
//		debug(SZ(tmp));
		ll ans = 0;
		if (SZ(tmp)>1) {
			int cnt = 0;
			for (int i=SZ(tmp)-1;i>=0;i--) {
				ans += 1ll*(cnt-query(tmp[i].fi))*tmp[i].se;
				cnt += tmp[i].se;
				update(tmp[i].fi,tmp[i].se);
			}
			for (auto it:tmp) update(it.fi,-it.se);
		}
		cout<<ans<<"\n";
		update(1,1,n,dfn[q[qaq].se],q[qaq].se);
	}
	return 0;
}

Compilation message

construction.cpp: In function 'void update(int, int, int, int, int)':
construction.cpp:52:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |   int mid=cl+cr>>1;
      |           ~~^~~
construction.cpp: In function 'int query(int, int, int, int, int)':
construction.cpp:70:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |  int mid=cl+cr>>1;
      |          ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10836 KB Output is correct
2 Correct 6 ms 10916 KB Output is correct
3 Correct 6 ms 10912 KB Output is correct
4 Correct 5 ms 10964 KB Output is correct
5 Correct 6 ms 10924 KB Output is correct
6 Correct 6 ms 10928 KB Output is correct
7 Correct 6 ms 10928 KB Output is correct
8 Correct 6 ms 10964 KB Output is correct
9 Correct 5 ms 10928 KB Output is correct
10 Correct 5 ms 10964 KB Output is correct
11 Correct 6 ms 10964 KB Output is correct
12 Correct 6 ms 10964 KB Output is correct
13 Correct 6 ms 10940 KB Output is correct
14 Correct 6 ms 10964 KB Output is correct
15 Correct 7 ms 10964 KB Output is correct
16 Correct 6 ms 10964 KB Output is correct
17 Correct 6 ms 10900 KB Output is correct
18 Correct 7 ms 10940 KB Output is correct
19 Correct 6 ms 10928 KB Output is correct
20 Correct 6 ms 10964 KB Output is correct
21 Correct 6 ms 10964 KB Output is correct
22 Correct 6 ms 10964 KB Output is correct
23 Correct 6 ms 10924 KB Output is correct
24 Correct 6 ms 10928 KB Output is correct
25 Correct 6 ms 10964 KB Output is correct
26 Correct 5 ms 10924 KB Output is correct
27 Correct 6 ms 10964 KB Output is correct
28 Correct 6 ms 10928 KB Output is correct
29 Correct 6 ms 10928 KB Output is correct
30 Correct 6 ms 10920 KB Output is correct
31 Correct 6 ms 10964 KB Output is correct
32 Correct 6 ms 10920 KB Output is correct
33 Correct 6 ms 10964 KB Output is correct
34 Correct 6 ms 10964 KB Output is correct
35 Correct 6 ms 10924 KB Output is correct
36 Correct 6 ms 10964 KB Output is correct
37 Correct 6 ms 10964 KB Output is correct
38 Correct 6 ms 10984 KB Output is correct
39 Correct 6 ms 10928 KB Output is correct
40 Correct 7 ms 10924 KB Output is correct
41 Correct 6 ms 10964 KB Output is correct
42 Correct 6 ms 10964 KB Output is correct
43 Correct 6 ms 10968 KB Output is correct
44 Correct 6 ms 10924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10836 KB Output is correct
2 Correct 6 ms 10916 KB Output is correct
3 Correct 6 ms 10912 KB Output is correct
4 Correct 5 ms 10964 KB Output is correct
5 Correct 6 ms 10924 KB Output is correct
6 Correct 6 ms 10928 KB Output is correct
7 Correct 6 ms 10928 KB Output is correct
8 Correct 6 ms 10964 KB Output is correct
9 Correct 5 ms 10928 KB Output is correct
10 Correct 5 ms 10964 KB Output is correct
11 Correct 6 ms 10964 KB Output is correct
12 Correct 6 ms 10964 KB Output is correct
13 Correct 6 ms 10940 KB Output is correct
14 Correct 6 ms 10964 KB Output is correct
15 Correct 7 ms 10964 KB Output is correct
16 Correct 6 ms 10964 KB Output is correct
17 Correct 6 ms 10900 KB Output is correct
18 Correct 7 ms 10940 KB Output is correct
19 Correct 6 ms 10928 KB Output is correct
20 Correct 6 ms 10964 KB Output is correct
21 Correct 6 ms 10964 KB Output is correct
22 Correct 6 ms 10964 KB Output is correct
23 Correct 6 ms 10924 KB Output is correct
24 Correct 6 ms 10928 KB Output is correct
25 Correct 6 ms 10964 KB Output is correct
26 Correct 5 ms 10924 KB Output is correct
27 Correct 6 ms 10964 KB Output is correct
28 Correct 6 ms 10928 KB Output is correct
29 Correct 6 ms 10928 KB Output is correct
30 Correct 6 ms 10920 KB Output is correct
31 Correct 6 ms 10964 KB Output is correct
32 Correct 6 ms 10920 KB Output is correct
33 Correct 6 ms 10964 KB Output is correct
34 Correct 6 ms 10964 KB Output is correct
35 Correct 6 ms 10924 KB Output is correct
36 Correct 6 ms 10964 KB Output is correct
37 Correct 6 ms 10964 KB Output is correct
38 Correct 6 ms 10984 KB Output is correct
39 Correct 6 ms 10928 KB Output is correct
40 Correct 7 ms 10924 KB Output is correct
41 Correct 6 ms 10964 KB Output is correct
42 Correct 6 ms 10964 KB Output is correct
43 Correct 6 ms 10968 KB Output is correct
44 Correct 6 ms 10924 KB Output is correct
45 Correct 8 ms 11004 KB Output is correct
46 Correct 17 ms 11348 KB Output is correct
47 Correct 20 ms 11312 KB Output is correct
48 Correct 15 ms 11400 KB Output is correct
49 Correct 11 ms 11604 KB Output is correct
50 Correct 11 ms 11572 KB Output is correct
51 Correct 13 ms 11556 KB Output is correct
52 Correct 17 ms 11440 KB Output is correct
53 Correct 15 ms 11444 KB Output is correct
54 Correct 16 ms 11476 KB Output is correct
55 Correct 15 ms 11476 KB Output is correct
56 Correct 17 ms 11452 KB Output is correct
57 Correct 25 ms 11332 KB Output is correct
58 Correct 26 ms 11360 KB Output is correct
59 Correct 26 ms 11316 KB Output is correct
60 Correct 28 ms 11292 KB Output is correct
61 Correct 18 ms 11444 KB Output is correct
62 Correct 17 ms 11428 KB Output is correct
63 Correct 18 ms 11496 KB Output is correct
64 Correct 16 ms 11092 KB Output is correct
65 Correct 16 ms 11072 KB Output is correct
66 Correct 17 ms 11192 KB Output is correct
67 Correct 16 ms 11276 KB Output is correct
68 Correct 11 ms 11368 KB Output is correct
69 Correct 16 ms 11444 KB Output is correct
70 Correct 14 ms 11236 KB Output is correct
71 Correct 14 ms 11220 KB Output is correct
72 Correct 26 ms 11376 KB Output is correct
73 Correct 24 ms 11092 KB Output is correct
74 Correct 18 ms 11220 KB Output is correct
75 Correct 14 ms 11424 KB Output is correct
76 Correct 14 ms 11348 KB Output is correct
77 Correct 14 ms 11308 KB Output is correct
78 Correct 15 ms 11184 KB Output is correct
79 Correct 13 ms 11204 KB Output is correct
80 Correct 12 ms 11200 KB Output is correct
81 Correct 16 ms 11432 KB Output is correct
82 Correct 17 ms 11316 KB Output is correct
83 Correct 14 ms 11348 KB Output is correct
84 Correct 14 ms 11220 KB Output is correct
85 Correct 14 ms 11092 KB Output is correct
86 Correct 14 ms 11092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10836 KB Output is correct
2 Correct 6 ms 10916 KB Output is correct
3 Correct 6 ms 10912 KB Output is correct
4 Correct 5 ms 10964 KB Output is correct
5 Correct 6 ms 10924 KB Output is correct
6 Correct 6 ms 10928 KB Output is correct
7 Correct 6 ms 10928 KB Output is correct
8 Correct 6 ms 10964 KB Output is correct
9 Correct 5 ms 10928 KB Output is correct
10 Correct 5 ms 10964 KB Output is correct
11 Correct 6 ms 10964 KB Output is correct
12 Correct 6 ms 10964 KB Output is correct
13 Correct 6 ms 10940 KB Output is correct
14 Correct 6 ms 10964 KB Output is correct
15 Correct 7 ms 10964 KB Output is correct
16 Correct 6 ms 10964 KB Output is correct
17 Correct 6 ms 10900 KB Output is correct
18 Correct 7 ms 10940 KB Output is correct
19 Correct 6 ms 10928 KB Output is correct
20 Correct 6 ms 10964 KB Output is correct
21 Correct 6 ms 10964 KB Output is correct
22 Correct 6 ms 10964 KB Output is correct
23 Correct 6 ms 10924 KB Output is correct
24 Correct 6 ms 10928 KB Output is correct
25 Correct 6 ms 10964 KB Output is correct
26 Correct 5 ms 10924 KB Output is correct
27 Correct 6 ms 10964 KB Output is correct
28 Correct 6 ms 10928 KB Output is correct
29 Correct 6 ms 10928 KB Output is correct
30 Correct 6 ms 10920 KB Output is correct
31 Correct 6 ms 10964 KB Output is correct
32 Correct 6 ms 10920 KB Output is correct
33 Correct 6 ms 10964 KB Output is correct
34 Correct 6 ms 10964 KB Output is correct
35 Correct 6 ms 10924 KB Output is correct
36 Correct 6 ms 10964 KB Output is correct
37 Correct 6 ms 10964 KB Output is correct
38 Correct 6 ms 10984 KB Output is correct
39 Correct 6 ms 10928 KB Output is correct
40 Correct 7 ms 10924 KB Output is correct
41 Correct 6 ms 10964 KB Output is correct
42 Correct 6 ms 10964 KB Output is correct
43 Correct 6 ms 10968 KB Output is correct
44 Correct 6 ms 10924 KB Output is correct
45 Correct 8 ms 11004 KB Output is correct
46 Correct 17 ms 11348 KB Output is correct
47 Correct 20 ms 11312 KB Output is correct
48 Correct 15 ms 11400 KB Output is correct
49 Correct 11 ms 11604 KB Output is correct
50 Correct 11 ms 11572 KB Output is correct
51 Correct 13 ms 11556 KB Output is correct
52 Correct 17 ms 11440 KB Output is correct
53 Correct 15 ms 11444 KB Output is correct
54 Correct 16 ms 11476 KB Output is correct
55 Correct 15 ms 11476 KB Output is correct
56 Correct 17 ms 11452 KB Output is correct
57 Correct 25 ms 11332 KB Output is correct
58 Correct 26 ms 11360 KB Output is correct
59 Correct 26 ms 11316 KB Output is correct
60 Correct 28 ms 11292 KB Output is correct
61 Correct 18 ms 11444 KB Output is correct
62 Correct 17 ms 11428 KB Output is correct
63 Correct 18 ms 11496 KB Output is correct
64 Correct 16 ms 11092 KB Output is correct
65 Correct 16 ms 11072 KB Output is correct
66 Correct 17 ms 11192 KB Output is correct
67 Correct 16 ms 11276 KB Output is correct
68 Correct 11 ms 11368 KB Output is correct
69 Correct 16 ms 11444 KB Output is correct
70 Correct 14 ms 11236 KB Output is correct
71 Correct 14 ms 11220 KB Output is correct
72 Correct 26 ms 11376 KB Output is correct
73 Correct 24 ms 11092 KB Output is correct
74 Correct 18 ms 11220 KB Output is correct
75 Correct 14 ms 11424 KB Output is correct
76 Correct 14 ms 11348 KB Output is correct
77 Correct 14 ms 11308 KB Output is correct
78 Correct 15 ms 11184 KB Output is correct
79 Correct 13 ms 11204 KB Output is correct
80 Correct 12 ms 11200 KB Output is correct
81 Correct 16 ms 11432 KB Output is correct
82 Correct 17 ms 11316 KB Output is correct
83 Correct 14 ms 11348 KB Output is correct
84 Correct 14 ms 11220 KB Output is correct
85 Correct 14 ms 11092 KB Output is correct
86 Correct 14 ms 11092 KB Output is correct
87 Correct 40 ms 12112 KB Output is correct
88 Correct 134 ms 14436 KB Output is correct
89 Correct 546 ms 22644 KB Output is correct
90 Correct 507 ms 22720 KB Output is correct
91 Correct 570 ms 22728 KB Output is correct
92 Correct 200 ms 28452 KB Output is correct
93 Correct 198 ms 28332 KB Output is correct
94 Correct 223 ms 28468 KB Output is correct
95 Correct 477 ms 24800 KB Output is correct
96 Correct 461 ms 25356 KB Output is correct
97 Correct 492 ms 25444 KB Output is correct
98 Correct 484 ms 25416 KB Output is correct
99 Correct 539 ms 25096 KB Output is correct
100 Correct 1165 ms 22740 KB Output is correct
101 Correct 1171 ms 23192 KB Output is correct
102 Correct 1257 ms 23120 KB Output is correct
103 Correct 1138 ms 23160 KB Output is correct
104 Correct 605 ms 25036 KB Output is correct
105 Correct 572 ms 25072 KB Output is correct
106 Correct 604 ms 25084 KB Output is correct
107 Correct 520 ms 16848 KB Output is correct
108 Correct 510 ms 16972 KB Output is correct
109 Correct 684 ms 18776 KB Output is correct
110 Correct 161 ms 23056 KB Output is correct
111 Correct 448 ms 24852 KB Output is correct
112 Correct 394 ms 19920 KB Output is correct
113 Correct 398 ms 19404 KB Output is correct
114 Correct 1037 ms 23188 KB Output is correct
115 Correct 1053 ms 17584 KB Output is correct
116 Correct 468 ms 19312 KB Output is correct
117 Correct 434 ms 23316 KB Output is correct
118 Correct 404 ms 22840 KB Output is correct
119 Correct 378 ms 22704 KB Output is correct
120 Correct 374 ms 18108 KB Output is correct
121 Correct 358 ms 17832 KB Output is correct
122 Correct 334 ms 17724 KB Output is correct
123 Correct 542 ms 23460 KB Output is correct
124 Correct 467 ms 22992 KB Output is correct
125 Correct 521 ms 22720 KB Output is correct
126 Correct 455 ms 18144 KB Output is correct
127 Correct 412 ms 17816 KB Output is correct
128 Correct 442 ms 17632 KB Output is correct