답안 #309307

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
309307 2020-10-03T07:27:55 Z arnold518 Construction of Highway (JOI18_construction) C++14
100 / 100
1923 ms 19052 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

int N;
vector<int> adj[MAXN+10];
int A[MAXN+10], B[MAXN+10];

int par[MAXN+10], sz[MAXN+10];

void dfs(int now)
{
	sz[now]=1;
	for(int nxt : adj[now])
	{
		dfs(nxt);
		sz[now]+=sz[nxt];
	}
}

int head[MAXN+10], dfn[MAXN+10], idfn[MAXN+10], cnt;

void hld(int now)
{
	dfn[now]=++cnt;
	idfn[dfn[now]]=now;

	int heavy=0;
	for(int nxt : adj[now])
	{
		if(sz[heavy]<sz[nxt]) heavy=nxt;
	}
	if(heavy==0) return;

	head[heavy]=head[now];
	hld(heavy);

	for(int nxt : adj[now])
	{
		if(nxt==heavy) continue;
		head[nxt]=nxt;
		hld(nxt);
	}
}

struct Data
{
	int l, r, v;
};
vector<Data> V[MAXN+10];

vector<int> comp;

struct BIT
{
	int tree[MAXN+10];
	void init() { memset(tree, 0, sizeof(tree)); }
	void update(int i, int x) { for(; i<=N; i+=(i&-i)) tree[i]+=x; }
	int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
}bit;

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);

	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());
	for(int i=1; i<=N; i++) A[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1;

	for(int i=1; i<N; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		par[v]=u;
		B[i]=v;
	}

	dfs(1);
	head[1]=1;
	hld(1);

	//printf("DFN\n");
	//for(int i=1; i<=N; i++) printf("%d ", dfn[i]); printf("\n");

	V[1].push_back({1, 1, A[1]});
	for(int i=1; i<N; i++)
	{
		int u=par[B[i]], v=B[i];

		int now=v;
		vector<pii> V2;
		while(1)
		{
			vector<pii> VV;
			while(!V[head[now]].empty() && V[head[now]].back().r<=dfn[now])
			{
				VV.push_back({V[head[now]].back().v, V[head[now]].back().r-V[head[now]].back().l+1});
				V[head[now]].pop_back();
			}
			if(!V[head[now]].empty() && V[head[now]].back().l<=dfn[now])
			{
				VV.push_back({V[head[now]].back().v, dfn[now]-V[head[now]].back().l+1});
				V[head[now]].back().l=dfn[now]+1;
			}
			V[head[now]].push_back({dfn[head[now]], dfn[now], A[v]});
			reverse(VV.begin(), VV.end());
			for(auto it : VV) V2.push_back(it);

			if(head[now]==1) break;
			now=par[head[now]];
		}

		reverse(V2.begin(), V2.end());
		bit.init();

		ll ans=0;
		//printf("V2 of %d %d is\n", u, v);
		for(auto it : V2)
		{
			//printf("%d %d\n", it.first, it.second);
			ans+=(ll)it.second*(bit.query(N)-bit.query(it.first));
			bit.update(it.first, it.second);
		}
		printf("%lld\n", ans);
	}
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:95:7: warning: unused variable 'u' [-Wunused-variable]
   95 |   int u=par[B[i]], v=B[i];
      |       ^
construction.cpp:69:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   69 |  scanf("%d", &N);
      |  ~~~~~^~~~~~~~~~
construction.cpp:70:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |  for(int i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
      |                          ~~~~~^~~~~~~~~~~~~
construction.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   79 |   scanf("%d%d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5376 KB Output is correct
2 Correct 4 ms 5376 KB Output is correct
3 Correct 5 ms 5376 KB Output is correct
4 Correct 8 ms 5504 KB Output is correct
5 Correct 12 ms 5504 KB Output is correct
6 Correct 12 ms 5504 KB Output is correct
7 Correct 13 ms 5504 KB Output is correct
8 Correct 11 ms 5504 KB Output is correct
9 Correct 12 ms 5504 KB Output is correct
10 Correct 11 ms 5504 KB Output is correct
11 Correct 11 ms 5504 KB Output is correct
12 Correct 11 ms 5504 KB Output is correct
13 Correct 12 ms 5504 KB Output is correct
14 Correct 11 ms 5504 KB Output is correct
15 Correct 12 ms 5504 KB Output is correct
16 Correct 11 ms 5504 KB Output is correct
17 Correct 11 ms 5504 KB Output is correct
18 Correct 14 ms 5504 KB Output is correct
19 Correct 11 ms 5504 KB Output is correct
20 Correct 12 ms 5504 KB Output is correct
21 Correct 11 ms 5504 KB Output is correct
22 Correct 20 ms 5504 KB Output is correct
23 Correct 11 ms 5504 KB Output is correct
24 Correct 11 ms 5504 KB Output is correct
25 Correct 11 ms 5532 KB Output is correct
26 Correct 15 ms 5504 KB Output is correct
27 Correct 12 ms 5504 KB Output is correct
28 Correct 11 ms 5504 KB Output is correct
29 Correct 12 ms 5504 KB Output is correct
30 Correct 11 ms 5504 KB Output is correct
31 Correct 12 ms 5504 KB Output is correct
32 Correct 11 ms 5504 KB Output is correct
33 Correct 12 ms 5632 KB Output is correct
34 Correct 12 ms 5504 KB Output is correct
35 Correct 12 ms 5504 KB Output is correct
36 Correct 11 ms 5504 KB Output is correct
37 Correct 11 ms 5504 KB Output is correct
38 Correct 12 ms 5504 KB Output is correct
39 Correct 11 ms 5504 KB Output is correct
40 Correct 11 ms 5536 KB Output is correct
41 Correct 11 ms 5504 KB Output is correct
42 Correct 11 ms 5504 KB Output is correct
43 Correct 12 ms 5504 KB Output is correct
44 Correct 11 ms 5504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5376 KB Output is correct
2 Correct 4 ms 5376 KB Output is correct
3 Correct 5 ms 5376 KB Output is correct
4 Correct 8 ms 5504 KB Output is correct
5 Correct 12 ms 5504 KB Output is correct
6 Correct 12 ms 5504 KB Output is correct
7 Correct 13 ms 5504 KB Output is correct
8 Correct 11 ms 5504 KB Output is correct
9 Correct 12 ms 5504 KB Output is correct
10 Correct 11 ms 5504 KB Output is correct
11 Correct 11 ms 5504 KB Output is correct
12 Correct 11 ms 5504 KB Output is correct
13 Correct 12 ms 5504 KB Output is correct
14 Correct 11 ms 5504 KB Output is correct
15 Correct 12 ms 5504 KB Output is correct
16 Correct 11 ms 5504 KB Output is correct
17 Correct 11 ms 5504 KB Output is correct
18 Correct 14 ms 5504 KB Output is correct
19 Correct 11 ms 5504 KB Output is correct
20 Correct 12 ms 5504 KB Output is correct
21 Correct 11 ms 5504 KB Output is correct
22 Correct 20 ms 5504 KB Output is correct
23 Correct 11 ms 5504 KB Output is correct
24 Correct 11 ms 5504 KB Output is correct
25 Correct 11 ms 5532 KB Output is correct
26 Correct 15 ms 5504 KB Output is correct
27 Correct 12 ms 5504 KB Output is correct
28 Correct 11 ms 5504 KB Output is correct
29 Correct 12 ms 5504 KB Output is correct
30 Correct 11 ms 5504 KB Output is correct
31 Correct 12 ms 5504 KB Output is correct
32 Correct 11 ms 5504 KB Output is correct
33 Correct 12 ms 5632 KB Output is correct
34 Correct 12 ms 5504 KB Output is correct
35 Correct 12 ms 5504 KB Output is correct
36 Correct 11 ms 5504 KB Output is correct
37 Correct 11 ms 5504 KB Output is correct
38 Correct 12 ms 5504 KB Output is correct
39 Correct 11 ms 5504 KB Output is correct
40 Correct 11 ms 5536 KB Output is correct
41 Correct 11 ms 5504 KB Output is correct
42 Correct 11 ms 5504 KB Output is correct
43 Correct 12 ms 5504 KB Output is correct
44 Correct 11 ms 5504 KB Output is correct
45 Correct 21 ms 5504 KB Output is correct
46 Correct 69 ms 5760 KB Output is correct
47 Correct 65 ms 5760 KB Output is correct
48 Correct 64 ms 5760 KB Output is correct
49 Correct 66 ms 5996 KB Output is correct
50 Correct 66 ms 6008 KB Output is correct
51 Correct 62 ms 6008 KB Output is correct
52 Correct 65 ms 5880 KB Output is correct
53 Correct 67 ms 5880 KB Output is correct
54 Correct 66 ms 5880 KB Output is correct
55 Correct 66 ms 5880 KB Output is correct
56 Correct 61 ms 5880 KB Output is correct
57 Correct 76 ms 5752 KB Output is correct
58 Correct 72 ms 5760 KB Output is correct
59 Correct 74 ms 5760 KB Output is correct
60 Correct 72 ms 5760 KB Output is correct
61 Correct 62 ms 5880 KB Output is correct
62 Correct 62 ms 5880 KB Output is correct
63 Correct 63 ms 5880 KB Output is correct
64 Correct 68 ms 5752 KB Output is correct
65 Correct 64 ms 5752 KB Output is correct
66 Correct 68 ms 5752 KB Output is correct
67 Correct 70 ms 5760 KB Output is correct
68 Correct 70 ms 6008 KB Output is correct
69 Correct 64 ms 5956 KB Output is correct
70 Correct 60 ms 5880 KB Output is correct
71 Correct 61 ms 5880 KB Output is correct
72 Correct 71 ms 5760 KB Output is correct
73 Correct 74 ms 5752 KB Output is correct
74 Correct 65 ms 5880 KB Output is correct
75 Correct 70 ms 5880 KB Output is correct
76 Correct 70 ms 5760 KB Output is correct
77 Correct 63 ms 5760 KB Output is correct
78 Correct 60 ms 5760 KB Output is correct
79 Correct 69 ms 5880 KB Output is correct
80 Correct 62 ms 5752 KB Output is correct
81 Correct 62 ms 5760 KB Output is correct
82 Correct 61 ms 5760 KB Output is correct
83 Correct 69 ms 5760 KB Output is correct
84 Correct 68 ms 5760 KB Output is correct
85 Correct 67 ms 5752 KB Output is correct
86 Correct 68 ms 5880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5376 KB Output is correct
2 Correct 4 ms 5376 KB Output is correct
3 Correct 5 ms 5376 KB Output is correct
4 Correct 8 ms 5504 KB Output is correct
5 Correct 12 ms 5504 KB Output is correct
6 Correct 12 ms 5504 KB Output is correct
7 Correct 13 ms 5504 KB Output is correct
8 Correct 11 ms 5504 KB Output is correct
9 Correct 12 ms 5504 KB Output is correct
10 Correct 11 ms 5504 KB Output is correct
11 Correct 11 ms 5504 KB Output is correct
12 Correct 11 ms 5504 KB Output is correct
13 Correct 12 ms 5504 KB Output is correct
14 Correct 11 ms 5504 KB Output is correct
15 Correct 12 ms 5504 KB Output is correct
16 Correct 11 ms 5504 KB Output is correct
17 Correct 11 ms 5504 KB Output is correct
18 Correct 14 ms 5504 KB Output is correct
19 Correct 11 ms 5504 KB Output is correct
20 Correct 12 ms 5504 KB Output is correct
21 Correct 11 ms 5504 KB Output is correct
22 Correct 20 ms 5504 KB Output is correct
23 Correct 11 ms 5504 KB Output is correct
24 Correct 11 ms 5504 KB Output is correct
25 Correct 11 ms 5532 KB Output is correct
26 Correct 15 ms 5504 KB Output is correct
27 Correct 12 ms 5504 KB Output is correct
28 Correct 11 ms 5504 KB Output is correct
29 Correct 12 ms 5504 KB Output is correct
30 Correct 11 ms 5504 KB Output is correct
31 Correct 12 ms 5504 KB Output is correct
32 Correct 11 ms 5504 KB Output is correct
33 Correct 12 ms 5632 KB Output is correct
34 Correct 12 ms 5504 KB Output is correct
35 Correct 12 ms 5504 KB Output is correct
36 Correct 11 ms 5504 KB Output is correct
37 Correct 11 ms 5504 KB Output is correct
38 Correct 12 ms 5504 KB Output is correct
39 Correct 11 ms 5504 KB Output is correct
40 Correct 11 ms 5536 KB Output is correct
41 Correct 11 ms 5504 KB Output is correct
42 Correct 11 ms 5504 KB Output is correct
43 Correct 12 ms 5504 KB Output is correct
44 Correct 11 ms 5504 KB Output is correct
45 Correct 21 ms 5504 KB Output is correct
46 Correct 69 ms 5760 KB Output is correct
47 Correct 65 ms 5760 KB Output is correct
48 Correct 64 ms 5760 KB Output is correct
49 Correct 66 ms 5996 KB Output is correct
50 Correct 66 ms 6008 KB Output is correct
51 Correct 62 ms 6008 KB Output is correct
52 Correct 65 ms 5880 KB Output is correct
53 Correct 67 ms 5880 KB Output is correct
54 Correct 66 ms 5880 KB Output is correct
55 Correct 66 ms 5880 KB Output is correct
56 Correct 61 ms 5880 KB Output is correct
57 Correct 76 ms 5752 KB Output is correct
58 Correct 72 ms 5760 KB Output is correct
59 Correct 74 ms 5760 KB Output is correct
60 Correct 72 ms 5760 KB Output is correct
61 Correct 62 ms 5880 KB Output is correct
62 Correct 62 ms 5880 KB Output is correct
63 Correct 63 ms 5880 KB Output is correct
64 Correct 68 ms 5752 KB Output is correct
65 Correct 64 ms 5752 KB Output is correct
66 Correct 68 ms 5752 KB Output is correct
67 Correct 70 ms 5760 KB Output is correct
68 Correct 70 ms 6008 KB Output is correct
69 Correct 64 ms 5956 KB Output is correct
70 Correct 60 ms 5880 KB Output is correct
71 Correct 61 ms 5880 KB Output is correct
72 Correct 71 ms 5760 KB Output is correct
73 Correct 74 ms 5752 KB Output is correct
74 Correct 65 ms 5880 KB Output is correct
75 Correct 70 ms 5880 KB Output is correct
76 Correct 70 ms 5760 KB Output is correct
77 Correct 63 ms 5760 KB Output is correct
78 Correct 60 ms 5760 KB Output is correct
79 Correct 69 ms 5880 KB Output is correct
80 Correct 62 ms 5752 KB Output is correct
81 Correct 62 ms 5760 KB Output is correct
82 Correct 61 ms 5760 KB Output is correct
83 Correct 69 ms 5760 KB Output is correct
84 Correct 68 ms 5760 KB Output is correct
85 Correct 67 ms 5752 KB Output is correct
86 Correct 68 ms 5880 KB Output is correct
87 Correct 170 ms 6524 KB Output is correct
88 Correct 481 ms 8188 KB Output is correct
89 Correct 1671 ms 14708 KB Output is correct
90 Correct 1701 ms 14576 KB Output is correct
91 Correct 1694 ms 14572 KB Output is correct
92 Correct 1474 ms 19052 KB Output is correct
93 Correct 1458 ms 18928 KB Output is correct
94 Correct 1455 ms 18928 KB Output is correct
95 Correct 1507 ms 17776 KB Output is correct
96 Correct 1648 ms 18532 KB Output is correct
97 Correct 1521 ms 18360 KB Output is correct
98 Correct 1478 ms 18284 KB Output is correct
99 Correct 1485 ms 16624 KB Output is correct
100 Correct 1769 ms 14452 KB Output is correct
101 Correct 1844 ms 14616 KB Output is correct
102 Correct 1893 ms 14652 KB Output is correct
103 Correct 1750 ms 14728 KB Output is correct
104 Correct 1512 ms 16624 KB Output is correct
105 Correct 1510 ms 16624 KB Output is correct
106 Correct 1507 ms 16624 KB Output is correct
107 Correct 1791 ms 13548 KB Output is correct
108 Correct 1613 ms 13680 KB Output is correct
109 Correct 1634 ms 13936 KB Output is correct
110 Correct 1467 ms 18324 KB Output is correct
111 Correct 1574 ms 18020 KB Output is correct
112 Correct 1665 ms 17688 KB Output is correct
113 Correct 1546 ms 15980 KB Output is correct
114 Correct 1923 ms 14280 KB Output is correct
115 Correct 1826 ms 13936 KB Output is correct
116 Correct 1659 ms 15968 KB Output is correct
117 Correct 1500 ms 15216 KB Output is correct
118 Correct 1639 ms 14704 KB Output is correct
119 Correct 1501 ms 14756 KB Output is correct
120 Correct 1474 ms 14676 KB Output is correct
121 Correct 1523 ms 14444 KB Output is correct
122 Correct 1566 ms 14320 KB Output is correct
123 Correct 1529 ms 15392 KB Output is correct
124 Correct 1604 ms 14832 KB Output is correct
125 Correct 1543 ms 14600 KB Output is correct
126 Correct 1646 ms 14960 KB Output is correct
127 Correct 1577 ms 14444 KB Output is correct
128 Correct 1571 ms 14324 KB Output is correct