답안 #58887

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
58887 2018-07-19T18:27:11 Z TadijaSebez Construction of Highway (JOI18_construction) C++11
100 / 100
500 ms 90284 KB
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
const int N=100050;
vector<int> E[N];
int c[N];
struct BIT
{
	int sum[N];
	vector<int> use;
	void init(){ for(int i=0;i<use.size();i++) sum[use[i]]=0;use.clear();}
	BIT(){}
	void Set(int i, int x){ for(;i<N;i+=i&-i){ if(!sum[i]) use.pb(i);sum[i]+=x;}}
	int Get(int i){ int ret=0;for(;i;i-=i&-i) ret+=sum[i];return ret;}
} Tree;
int sz[N],head[N],dep[N],par[N];
vector<pair<int,int> > all[N];
void DFS(int u, int p)
{
	dep[u]=dep[p]+1;
	par[u]=p;
	sz[u]=1;
	for(int i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p) DFS(v,u),sz[u]+=sz[v];
	}
}
void HLD(int u, int p)
{
	if(!p) head[u]=u;
	int HC=0,i;
	for(i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p && sz[v]>sz[HC]) HC=v;
	}
	for(i=0;i<E[u].size();i++)
	{
		int v=E[u][i];
		if(v!=p)
		{
			if(v==HC) head[v]=head[u];
			else head[v]=v;
			HLD(v,u);
		}
	}
}
ll Query(int u, int v)
{
	ll ans=0;
	int col=c[v];
	vector<pair<int,int> > work;
	while(u)
	{
		int len=dep[u]-dep[head[u]]+1;
		int chain=head[u];
		work.clear();
		while(all[chain].size() && len>=all[chain].back().first)
		{
			work.pb(all[chain].back());
			len-=all[chain].back().first;
			all[chain].pop_back();
		}
		if(all[chain].size() && len>0)
		{
			work.pb(mp(len,all[chain].back().second));
			all[chain][all[chain].size()-1].first-=len;
		}
		while(work.size())
		{
			ans+=(ll)work.back().first*Tree.Get(work.back().second-1);
			Tree.Set(work.back().second,work.back().first);
			//printf("del:%i %i\n",work.back().first,work.back().second);
			work.pop_back();
		}
		u=par[head[u]];
	}
	while(v)
	{
		int push=dep[v]-dep[head[v]]+1;
		all[head[v]].pb(mp(push,col));
		//printf("add:%i %i\n",push,col);
		//printf("%i %i %i %i\n",head[v],v,push,col);
		v=par[head[v]];
	}
	Tree.init();
	return ans;
}
vector<int> id;
int Find(int x){ return lower_bound(id.begin(),id.end(),x)-id.begin()+1;}
int a[N],b[N];
int main()
{
	int n,i,u,v;
	scanf("%i",&n);
	for(i=1;i<=n;i++) scanf("%i",&c[i]),id.pb(c[i]);
	sort(id.begin(),id.end());
	id.erase(unique(id.begin(),id.end()),id.end());
	for(i=1;i<=n;i++) c[i]=Find(c[i]);
	for(i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),a[i]=u,b[i]=v;
	DFS(1,0);
	HLD(1,0);
	all[1].pb(mp(1,c[1]));
	for(i=1;i<n;i++) printf("%lld\n",Query(a[i],b[i]));
	return 0;
}

Compilation message

construction.cpp: In member function 'void BIT::init()':
construction.cpp:15:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  void init(){ for(int i=0;i<use.size();i++) sum[use[i]]=0;use.clear();}
                           ~^~~~~~~~~~~
construction.cpp: In function 'void DFS(int, int)':
construction.cpp:27:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<E[u].size();i++)
              ~^~~~~~~~~~~~
construction.cpp: In function 'void HLD(int, int)':
construction.cpp:37:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0;i<E[u].size();i++)
          ~^~~~~~~~~~~~
construction.cpp:42:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0;i<E[u].size();i++)
          ~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:100:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
construction.cpp:101:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1;i<=n;i++) scanf("%i",&c[i]),id.pb(c[i]);
                    ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
construction.cpp:105:57: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1;i<n;i++) scanf("%i %i",&u,&v),E[u].pb(v),a[i]=u,b[i]=v;
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4988 KB Output is correct
2 Correct 8 ms 5216 KB Output is correct
3 Correct 7 ms 5392 KB Output is correct
4 Correct 8 ms 5392 KB Output is correct
5 Correct 7 ms 5392 KB Output is correct
6 Correct 7 ms 5484 KB Output is correct
7 Correct 8 ms 5484 KB Output is correct
8 Correct 8 ms 5772 KB Output is correct
9 Correct 8 ms 5772 KB Output is correct
10 Correct 7 ms 5772 KB Output is correct
11 Correct 7 ms 5772 KB Output is correct
12 Correct 8 ms 5780 KB Output is correct
13 Correct 8 ms 5912 KB Output is correct
14 Correct 8 ms 5912 KB Output is correct
15 Correct 9 ms 5912 KB Output is correct
16 Correct 9 ms 5912 KB Output is correct
17 Correct 8 ms 5928 KB Output is correct
18 Correct 8 ms 5944 KB Output is correct
19 Correct 7 ms 5952 KB Output is correct
20 Correct 9 ms 6092 KB Output is correct
21 Correct 7 ms 6092 KB Output is correct
22 Correct 9 ms 6092 KB Output is correct
23 Correct 8 ms 6092 KB Output is correct
24 Correct 9 ms 6092 KB Output is correct
25 Correct 9 ms 6092 KB Output is correct
26 Correct 7 ms 6092 KB Output is correct
27 Correct 8 ms 6092 KB Output is correct
28 Correct 8 ms 6092 KB Output is correct
29 Correct 9 ms 6092 KB Output is correct
30 Correct 9 ms 6092 KB Output is correct
31 Correct 8 ms 6092 KB Output is correct
32 Correct 8 ms 6092 KB Output is correct
33 Correct 8 ms 6096 KB Output is correct
34 Correct 8 ms 6096 KB Output is correct
35 Correct 8 ms 6140 KB Output is correct
36 Correct 8 ms 6140 KB Output is correct
37 Correct 8 ms 6172 KB Output is correct
38 Correct 9 ms 6172 KB Output is correct
39 Correct 8 ms 6172 KB Output is correct
40 Correct 8 ms 6172 KB Output is correct
41 Correct 8 ms 6172 KB Output is correct
42 Correct 11 ms 6172 KB Output is correct
43 Correct 8 ms 6196 KB Output is correct
44 Correct 8 ms 6196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4988 KB Output is correct
2 Correct 8 ms 5216 KB Output is correct
3 Correct 7 ms 5392 KB Output is correct
4 Correct 8 ms 5392 KB Output is correct
5 Correct 7 ms 5392 KB Output is correct
6 Correct 7 ms 5484 KB Output is correct
7 Correct 8 ms 5484 KB Output is correct
8 Correct 8 ms 5772 KB Output is correct
9 Correct 8 ms 5772 KB Output is correct
10 Correct 7 ms 5772 KB Output is correct
11 Correct 7 ms 5772 KB Output is correct
12 Correct 8 ms 5780 KB Output is correct
13 Correct 8 ms 5912 KB Output is correct
14 Correct 8 ms 5912 KB Output is correct
15 Correct 9 ms 5912 KB Output is correct
16 Correct 9 ms 5912 KB Output is correct
17 Correct 8 ms 5928 KB Output is correct
18 Correct 8 ms 5944 KB Output is correct
19 Correct 7 ms 5952 KB Output is correct
20 Correct 9 ms 6092 KB Output is correct
21 Correct 7 ms 6092 KB Output is correct
22 Correct 9 ms 6092 KB Output is correct
23 Correct 8 ms 6092 KB Output is correct
24 Correct 9 ms 6092 KB Output is correct
25 Correct 9 ms 6092 KB Output is correct
26 Correct 7 ms 6092 KB Output is correct
27 Correct 8 ms 6092 KB Output is correct
28 Correct 8 ms 6092 KB Output is correct
29 Correct 9 ms 6092 KB Output is correct
30 Correct 9 ms 6092 KB Output is correct
31 Correct 8 ms 6092 KB Output is correct
32 Correct 8 ms 6092 KB Output is correct
33 Correct 8 ms 6096 KB Output is correct
34 Correct 8 ms 6096 KB Output is correct
35 Correct 8 ms 6140 KB Output is correct
36 Correct 8 ms 6140 KB Output is correct
37 Correct 8 ms 6172 KB Output is correct
38 Correct 9 ms 6172 KB Output is correct
39 Correct 8 ms 6172 KB Output is correct
40 Correct 8 ms 6172 KB Output is correct
41 Correct 8 ms 6172 KB Output is correct
42 Correct 11 ms 6172 KB Output is correct
43 Correct 8 ms 6196 KB Output is correct
44 Correct 8 ms 6196 KB Output is correct
45 Correct 9 ms 6196 KB Output is correct
46 Correct 19 ms 6468 KB Output is correct
47 Correct 14 ms 6568 KB Output is correct
48 Correct 14 ms 6620 KB Output is correct
49 Correct 17 ms 7100 KB Output is correct
50 Correct 13 ms 7176 KB Output is correct
51 Correct 12 ms 7264 KB Output is correct
52 Correct 12 ms 7264 KB Output is correct
53 Correct 13 ms 7268 KB Output is correct
54 Correct 13 ms 7344 KB Output is correct
55 Correct 11 ms 7420 KB Output is correct
56 Correct 12 ms 7496 KB Output is correct
57 Correct 16 ms 7496 KB Output is correct
58 Correct 16 ms 7500 KB Output is correct
59 Correct 17 ms 7544 KB Output is correct
60 Correct 19 ms 7544 KB Output is correct
61 Correct 14 ms 7732 KB Output is correct
62 Correct 16 ms 7808 KB Output is correct
63 Correct 13 ms 7884 KB Output is correct
64 Correct 15 ms 7884 KB Output is correct
65 Correct 15 ms 7884 KB Output is correct
66 Correct 15 ms 7940 KB Output is correct
67 Correct 17 ms 7980 KB Output is correct
68 Correct 11 ms 8292 KB Output is correct
69 Correct 12 ms 8340 KB Output is correct
70 Correct 10 ms 8340 KB Output is correct
71 Correct 10 ms 8340 KB Output is correct
72 Correct 14 ms 8364 KB Output is correct
73 Correct 14 ms 8364 KB Output is correct
74 Correct 10 ms 8432 KB Output is correct
75 Correct 15 ms 8524 KB Output is correct
76 Correct 13 ms 8560 KB Output is correct
77 Correct 12 ms 8624 KB Output is correct
78 Correct 15 ms 8828 KB Output is correct
79 Correct 16 ms 8828 KB Output is correct
80 Correct 12 ms 8828 KB Output is correct
81 Correct 13 ms 8900 KB Output is correct
82 Correct 12 ms 8900 KB Output is correct
83 Correct 12 ms 8996 KB Output is correct
84 Correct 14 ms 9188 KB Output is correct
85 Correct 11 ms 9188 KB Output is correct
86 Correct 14 ms 9188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4988 KB Output is correct
2 Correct 8 ms 5216 KB Output is correct
3 Correct 7 ms 5392 KB Output is correct
4 Correct 8 ms 5392 KB Output is correct
5 Correct 7 ms 5392 KB Output is correct
6 Correct 7 ms 5484 KB Output is correct
7 Correct 8 ms 5484 KB Output is correct
8 Correct 8 ms 5772 KB Output is correct
9 Correct 8 ms 5772 KB Output is correct
10 Correct 7 ms 5772 KB Output is correct
11 Correct 7 ms 5772 KB Output is correct
12 Correct 8 ms 5780 KB Output is correct
13 Correct 8 ms 5912 KB Output is correct
14 Correct 8 ms 5912 KB Output is correct
15 Correct 9 ms 5912 KB Output is correct
16 Correct 9 ms 5912 KB Output is correct
17 Correct 8 ms 5928 KB Output is correct
18 Correct 8 ms 5944 KB Output is correct
19 Correct 7 ms 5952 KB Output is correct
20 Correct 9 ms 6092 KB Output is correct
21 Correct 7 ms 6092 KB Output is correct
22 Correct 9 ms 6092 KB Output is correct
23 Correct 8 ms 6092 KB Output is correct
24 Correct 9 ms 6092 KB Output is correct
25 Correct 9 ms 6092 KB Output is correct
26 Correct 7 ms 6092 KB Output is correct
27 Correct 8 ms 6092 KB Output is correct
28 Correct 8 ms 6092 KB Output is correct
29 Correct 9 ms 6092 KB Output is correct
30 Correct 9 ms 6092 KB Output is correct
31 Correct 8 ms 6092 KB Output is correct
32 Correct 8 ms 6092 KB Output is correct
33 Correct 8 ms 6096 KB Output is correct
34 Correct 8 ms 6096 KB Output is correct
35 Correct 8 ms 6140 KB Output is correct
36 Correct 8 ms 6140 KB Output is correct
37 Correct 8 ms 6172 KB Output is correct
38 Correct 9 ms 6172 KB Output is correct
39 Correct 8 ms 6172 KB Output is correct
40 Correct 8 ms 6172 KB Output is correct
41 Correct 8 ms 6172 KB Output is correct
42 Correct 11 ms 6172 KB Output is correct
43 Correct 8 ms 6196 KB Output is correct
44 Correct 8 ms 6196 KB Output is correct
45 Correct 9 ms 6196 KB Output is correct
46 Correct 19 ms 6468 KB Output is correct
47 Correct 14 ms 6568 KB Output is correct
48 Correct 14 ms 6620 KB Output is correct
49 Correct 17 ms 7100 KB Output is correct
50 Correct 13 ms 7176 KB Output is correct
51 Correct 12 ms 7264 KB Output is correct
52 Correct 12 ms 7264 KB Output is correct
53 Correct 13 ms 7268 KB Output is correct
54 Correct 13 ms 7344 KB Output is correct
55 Correct 11 ms 7420 KB Output is correct
56 Correct 12 ms 7496 KB Output is correct
57 Correct 16 ms 7496 KB Output is correct
58 Correct 16 ms 7500 KB Output is correct
59 Correct 17 ms 7544 KB Output is correct
60 Correct 19 ms 7544 KB Output is correct
61 Correct 14 ms 7732 KB Output is correct
62 Correct 16 ms 7808 KB Output is correct
63 Correct 13 ms 7884 KB Output is correct
64 Correct 15 ms 7884 KB Output is correct
65 Correct 15 ms 7884 KB Output is correct
66 Correct 15 ms 7940 KB Output is correct
67 Correct 17 ms 7980 KB Output is correct
68 Correct 11 ms 8292 KB Output is correct
69 Correct 12 ms 8340 KB Output is correct
70 Correct 10 ms 8340 KB Output is correct
71 Correct 10 ms 8340 KB Output is correct
72 Correct 14 ms 8364 KB Output is correct
73 Correct 14 ms 8364 KB Output is correct
74 Correct 10 ms 8432 KB Output is correct
75 Correct 15 ms 8524 KB Output is correct
76 Correct 13 ms 8560 KB Output is correct
77 Correct 12 ms 8624 KB Output is correct
78 Correct 15 ms 8828 KB Output is correct
79 Correct 16 ms 8828 KB Output is correct
80 Correct 12 ms 8828 KB Output is correct
81 Correct 13 ms 8900 KB Output is correct
82 Correct 12 ms 8900 KB Output is correct
83 Correct 12 ms 8996 KB Output is correct
84 Correct 14 ms 9188 KB Output is correct
85 Correct 11 ms 9188 KB Output is correct
86 Correct 14 ms 9188 KB Output is correct
87 Correct 33 ms 9720 KB Output is correct
88 Correct 79 ms 11684 KB Output is correct
89 Correct 321 ms 18760 KB Output is correct
90 Correct 345 ms 20844 KB Output is correct
91 Correct 331 ms 22992 KB Output is correct
92 Correct 193 ms 32616 KB Output is correct
93 Correct 268 ms 34764 KB Output is correct
94 Correct 179 ms 37116 KB Output is correct
95 Correct 191 ms 37116 KB Output is correct
96 Correct 195 ms 38384 KB Output is correct
97 Correct 194 ms 40536 KB Output is correct
98 Correct 183 ms 42652 KB Output is correct
99 Correct 191 ms 43508 KB Output is correct
100 Correct 390 ms 43508 KB Output is correct
101 Correct 425 ms 43508 KB Output is correct
102 Correct 500 ms 45592 KB Output is correct
103 Correct 442 ms 47720 KB Output is correct
104 Correct 178 ms 53484 KB Output is correct
105 Correct 198 ms 55708 KB Output is correct
106 Correct 202 ms 57900 KB Output is correct
107 Correct 262 ms 57900 KB Output is correct
108 Correct 280 ms 57900 KB Output is correct
109 Correct 321 ms 58204 KB Output is correct
110 Correct 155 ms 67204 KB Output is correct
111 Correct 161 ms 67204 KB Output is correct
112 Correct 155 ms 67640 KB Output is correct
113 Correct 163 ms 68192 KB Output is correct
114 Correct 435 ms 68192 KB Output is correct
115 Correct 441 ms 68192 KB Output is correct
116 Correct 223 ms 72888 KB Output is correct
117 Correct 209 ms 73180 KB Output is correct
118 Correct 210 ms 74384 KB Output is correct
119 Correct 191 ms 75772 KB Output is correct
120 Correct 171 ms 77980 KB Output is correct
121 Correct 167 ms 78808 KB Output is correct
122 Correct 178 ms 79972 KB Output is correct
123 Correct 225 ms 83252 KB Output is correct
124 Correct 226 ms 84392 KB Output is correct
125 Correct 241 ms 85944 KB Output is correct
126 Correct 242 ms 88192 KB Output is correct
127 Correct 200 ms 88900 KB Output is correct
128 Correct 210 ms 90284 KB Output is correct