답안 #112236

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
112236 2019-05-18T01:47:24 Z nxteru Construction of Highway (JOI18_construction) C++14
100 / 100
507 ms 89296 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<ll,ll> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
struct BIT{
	int n;
	ll bit[100005];
	void ini(int x){
		n=x;
		for(int i=0;i<=n;i++)bit[i]=0;
	}
	void add(int a,ll x){
		a++;
		while(a<=n){
			bit[a]+=x;
			a+=(a&-a);
		}
	}
	ll sum(int a){
		a++;
		ll res=0;
		while(a>0){
			res+=bit[a];
			a-=(a&-a);
		}
		return res;
	}
};
int n,k,id[100005],a[100005],b[100005],sz[100005],hd[100005],cn[100005];
vector<int>g[100005],cm;
stack<P>s[100005];
ll q[100005];
BIT bit;
void dfs1(int v){
	sz[v]=1;
	for(int i=0;i<g[v].size();i++){
		dfs1(g[v][i]);
		sz[v]+=sz[g[v][i]];
		if(sz[g[v][0]]<sz[g[v][i]])swap(g[v][0],g[v][i]);
	}
}
void dfs2(int v,int h){
	id[v]=k;
	hd[k]=h;
	cn[k]=cn[h];
	k++;
	if(g[v].size()!=0)dfs2(g[v][0],h);
	s[h].push(P(q[v],1LL));
	for(int i=1;i<g[v].size();i++){
		cn[k]=id[v];
		dfs2(g[v][i],k);
	}
}
int main(void){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
		scanf("%lld",q+i);
		cm.PB(q[i]);
	}
	sort(cm.begin(),cm.end());
	cm.erase(unique(cm.begin(),cm.end()),cm.end());
	bit.ini(cm.size());
	for(int i=0;i<n;i++)q[i]=lower_bound(cm.begin(),cm.end(),q[i])-cm.begin();
    for(int i=0;i<n-1;i++){
		scanf("%d%d",a+i,b+i);
		a[i]--,b[i]--;
		g[a[i]].PB(b[i]);
	}
	dfs1(0);
	dfs2(0,0);
	for(int i=0;i<n-1;i++){
		vector<P>r;
		stack<P>w;
		ll ans=0;
		int v=a[i],u=b[i];
		v=id[v];
		while(1){
			int h=hd[v],res=v-h+1;
			while(s[h].top().S<=res){
				w.push(s[h].top());
				res-=s[h].top().S;
				s[h].pop();
			}
			if(res>0){
				s[h].top().S-=res;
				w.push(P(s[h].top().F,res));
			}
			s[h].push(P(q[u],ll(v-h+1)));
			while(!w.empty()){
				r.PB(w.top());
				w.pop();
			}
			if(h==0)break;
			else v=cn[h];
		}
		for(int j=0;j<r.size();j++){
			ans+=r[j].S*bit.sum(r[j].F-1);
			bit.add(r[j].F,r[j].S);
		}
		for(int j=0;j<r.size();j++)bit.add(r[j].F,-r[j].S);
		printf("%lld\n",ans);
	}
}

Compilation message

construction.cpp: In function 'void dfs1(int)':
construction.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[v].size();i++){
              ~^~~~~~~~~~~~
construction.cpp: In function 'void dfs2(int, int)':
construction.cpp:55:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1;i<g[v].size();i++){
              ~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:102:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<r.size();j++){
               ~^~~~~~~~~
construction.cpp:106:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<r.size();j++)bit.add(r[j].F,-r[j].S);
               ~^~~~~~~~~
construction.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
construction.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld",q+i);
   ~~~~~^~~~~~~~~~~~
construction.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",a+i,b+i);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 70008 KB Output is correct
2 Correct 64 ms 70112 KB Output is correct
3 Correct 64 ms 70008 KB Output is correct
4 Correct 67 ms 70008 KB Output is correct
5 Correct 68 ms 70136 KB Output is correct
6 Correct 66 ms 70000 KB Output is correct
7 Correct 66 ms 70008 KB Output is correct
8 Correct 67 ms 70136 KB Output is correct
9 Correct 67 ms 70136 KB Output is correct
10 Correct 74 ms 70136 KB Output is correct
11 Correct 75 ms 70136 KB Output is correct
12 Correct 66 ms 70160 KB Output is correct
13 Correct 64 ms 70008 KB Output is correct
14 Correct 64 ms 70140 KB Output is correct
15 Correct 67 ms 70008 KB Output is correct
16 Correct 74 ms 70008 KB Output is correct
17 Correct 75 ms 70136 KB Output is correct
18 Correct 64 ms 70136 KB Output is correct
19 Correct 76 ms 70140 KB Output is correct
20 Correct 64 ms 70008 KB Output is correct
21 Correct 73 ms 70136 KB Output is correct
22 Correct 73 ms 70084 KB Output is correct
23 Correct 65 ms 70008 KB Output is correct
24 Correct 64 ms 69980 KB Output is correct
25 Correct 66 ms 70136 KB Output is correct
26 Correct 65 ms 70108 KB Output is correct
27 Correct 66 ms 70136 KB Output is correct
28 Correct 68 ms 70136 KB Output is correct
29 Correct 69 ms 70008 KB Output is correct
30 Correct 67 ms 70112 KB Output is correct
31 Correct 67 ms 70264 KB Output is correct
32 Correct 64 ms 70116 KB Output is correct
33 Correct 75 ms 70136 KB Output is correct
34 Correct 63 ms 70008 KB Output is correct
35 Correct 62 ms 70136 KB Output is correct
36 Correct 75 ms 70064 KB Output is correct
37 Correct 74 ms 70140 KB Output is correct
38 Correct 65 ms 70008 KB Output is correct
39 Correct 67 ms 70008 KB Output is correct
40 Correct 71 ms 70136 KB Output is correct
41 Correct 74 ms 70136 KB Output is correct
42 Correct 68 ms 70136 KB Output is correct
43 Correct 68 ms 70136 KB Output is correct
44 Correct 63 ms 70136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 70008 KB Output is correct
2 Correct 64 ms 70112 KB Output is correct
3 Correct 64 ms 70008 KB Output is correct
4 Correct 67 ms 70008 KB Output is correct
5 Correct 68 ms 70136 KB Output is correct
6 Correct 66 ms 70000 KB Output is correct
7 Correct 66 ms 70008 KB Output is correct
8 Correct 67 ms 70136 KB Output is correct
9 Correct 67 ms 70136 KB Output is correct
10 Correct 74 ms 70136 KB Output is correct
11 Correct 75 ms 70136 KB Output is correct
12 Correct 66 ms 70160 KB Output is correct
13 Correct 64 ms 70008 KB Output is correct
14 Correct 64 ms 70140 KB Output is correct
15 Correct 67 ms 70008 KB Output is correct
16 Correct 74 ms 70008 KB Output is correct
17 Correct 75 ms 70136 KB Output is correct
18 Correct 64 ms 70136 KB Output is correct
19 Correct 76 ms 70140 KB Output is correct
20 Correct 64 ms 70008 KB Output is correct
21 Correct 73 ms 70136 KB Output is correct
22 Correct 73 ms 70084 KB Output is correct
23 Correct 65 ms 70008 KB Output is correct
24 Correct 64 ms 69980 KB Output is correct
25 Correct 66 ms 70136 KB Output is correct
26 Correct 65 ms 70108 KB Output is correct
27 Correct 66 ms 70136 KB Output is correct
28 Correct 68 ms 70136 KB Output is correct
29 Correct 69 ms 70008 KB Output is correct
30 Correct 67 ms 70112 KB Output is correct
31 Correct 67 ms 70264 KB Output is correct
32 Correct 64 ms 70116 KB Output is correct
33 Correct 75 ms 70136 KB Output is correct
34 Correct 63 ms 70008 KB Output is correct
35 Correct 62 ms 70136 KB Output is correct
36 Correct 75 ms 70064 KB Output is correct
37 Correct 74 ms 70140 KB Output is correct
38 Correct 65 ms 70008 KB Output is correct
39 Correct 67 ms 70008 KB Output is correct
40 Correct 71 ms 70136 KB Output is correct
41 Correct 74 ms 70136 KB Output is correct
42 Correct 68 ms 70136 KB Output is correct
43 Correct 68 ms 70136 KB Output is correct
44 Correct 63 ms 70136 KB Output is correct
45 Correct 64 ms 70052 KB Output is correct
46 Correct 68 ms 70392 KB Output is correct
47 Correct 81 ms 70392 KB Output is correct
48 Correct 69 ms 70384 KB Output is correct
49 Correct 68 ms 70776 KB Output is correct
50 Correct 68 ms 70904 KB Output is correct
51 Correct 68 ms 70876 KB Output is correct
52 Correct 78 ms 70652 KB Output is correct
53 Correct 81 ms 70648 KB Output is correct
54 Correct 71 ms 70648 KB Output is correct
55 Correct 70 ms 70648 KB Output is correct
56 Correct 70 ms 70568 KB Output is correct
57 Correct 77 ms 70392 KB Output is correct
58 Correct 73 ms 70392 KB Output is correct
59 Correct 76 ms 70392 KB Output is correct
60 Correct 72 ms 70392 KB Output is correct
61 Correct 74 ms 70520 KB Output is correct
62 Correct 82 ms 70492 KB Output is correct
63 Correct 69 ms 70520 KB Output is correct
64 Correct 68 ms 70360 KB Output is correct
65 Correct 71 ms 70264 KB Output is correct
66 Correct 68 ms 70264 KB Output is correct
67 Correct 76 ms 70264 KB Output is correct
68 Correct 68 ms 70648 KB Output is correct
69 Correct 76 ms 70524 KB Output is correct
70 Correct 73 ms 70564 KB Output is correct
71 Correct 80 ms 70524 KB Output is correct
72 Correct 70 ms 70392 KB Output is correct
73 Correct 70 ms 70392 KB Output is correct
74 Correct 68 ms 70520 KB Output is correct
75 Correct 80 ms 70520 KB Output is correct
76 Correct 68 ms 70392 KB Output is correct
77 Correct 68 ms 70392 KB Output is correct
78 Correct 80 ms 70392 KB Output is correct
79 Correct 66 ms 70392 KB Output is correct
80 Correct 68 ms 70392 KB Output is correct
81 Correct 72 ms 70520 KB Output is correct
82 Correct 72 ms 70496 KB Output is correct
83 Correct 71 ms 70484 KB Output is correct
84 Correct 67 ms 70392 KB Output is correct
85 Correct 69 ms 70392 KB Output is correct
86 Correct 71 ms 70392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 70008 KB Output is correct
2 Correct 64 ms 70112 KB Output is correct
3 Correct 64 ms 70008 KB Output is correct
4 Correct 67 ms 70008 KB Output is correct
5 Correct 68 ms 70136 KB Output is correct
6 Correct 66 ms 70000 KB Output is correct
7 Correct 66 ms 70008 KB Output is correct
8 Correct 67 ms 70136 KB Output is correct
9 Correct 67 ms 70136 KB Output is correct
10 Correct 74 ms 70136 KB Output is correct
11 Correct 75 ms 70136 KB Output is correct
12 Correct 66 ms 70160 KB Output is correct
13 Correct 64 ms 70008 KB Output is correct
14 Correct 64 ms 70140 KB Output is correct
15 Correct 67 ms 70008 KB Output is correct
16 Correct 74 ms 70008 KB Output is correct
17 Correct 75 ms 70136 KB Output is correct
18 Correct 64 ms 70136 KB Output is correct
19 Correct 76 ms 70140 KB Output is correct
20 Correct 64 ms 70008 KB Output is correct
21 Correct 73 ms 70136 KB Output is correct
22 Correct 73 ms 70084 KB Output is correct
23 Correct 65 ms 70008 KB Output is correct
24 Correct 64 ms 69980 KB Output is correct
25 Correct 66 ms 70136 KB Output is correct
26 Correct 65 ms 70108 KB Output is correct
27 Correct 66 ms 70136 KB Output is correct
28 Correct 68 ms 70136 KB Output is correct
29 Correct 69 ms 70008 KB Output is correct
30 Correct 67 ms 70112 KB Output is correct
31 Correct 67 ms 70264 KB Output is correct
32 Correct 64 ms 70116 KB Output is correct
33 Correct 75 ms 70136 KB Output is correct
34 Correct 63 ms 70008 KB Output is correct
35 Correct 62 ms 70136 KB Output is correct
36 Correct 75 ms 70064 KB Output is correct
37 Correct 74 ms 70140 KB Output is correct
38 Correct 65 ms 70008 KB Output is correct
39 Correct 67 ms 70008 KB Output is correct
40 Correct 71 ms 70136 KB Output is correct
41 Correct 74 ms 70136 KB Output is correct
42 Correct 68 ms 70136 KB Output is correct
43 Correct 68 ms 70136 KB Output is correct
44 Correct 63 ms 70136 KB Output is correct
45 Correct 64 ms 70052 KB Output is correct
46 Correct 68 ms 70392 KB Output is correct
47 Correct 81 ms 70392 KB Output is correct
48 Correct 69 ms 70384 KB Output is correct
49 Correct 68 ms 70776 KB Output is correct
50 Correct 68 ms 70904 KB Output is correct
51 Correct 68 ms 70876 KB Output is correct
52 Correct 78 ms 70652 KB Output is correct
53 Correct 81 ms 70648 KB Output is correct
54 Correct 71 ms 70648 KB Output is correct
55 Correct 70 ms 70648 KB Output is correct
56 Correct 70 ms 70568 KB Output is correct
57 Correct 77 ms 70392 KB Output is correct
58 Correct 73 ms 70392 KB Output is correct
59 Correct 76 ms 70392 KB Output is correct
60 Correct 72 ms 70392 KB Output is correct
61 Correct 74 ms 70520 KB Output is correct
62 Correct 82 ms 70492 KB Output is correct
63 Correct 69 ms 70520 KB Output is correct
64 Correct 68 ms 70360 KB Output is correct
65 Correct 71 ms 70264 KB Output is correct
66 Correct 68 ms 70264 KB Output is correct
67 Correct 76 ms 70264 KB Output is correct
68 Correct 68 ms 70648 KB Output is correct
69 Correct 76 ms 70524 KB Output is correct
70 Correct 73 ms 70564 KB Output is correct
71 Correct 80 ms 70524 KB Output is correct
72 Correct 70 ms 70392 KB Output is correct
73 Correct 70 ms 70392 KB Output is correct
74 Correct 68 ms 70520 KB Output is correct
75 Correct 80 ms 70520 KB Output is correct
76 Correct 68 ms 70392 KB Output is correct
77 Correct 68 ms 70392 KB Output is correct
78 Correct 80 ms 70392 KB Output is correct
79 Correct 66 ms 70392 KB Output is correct
80 Correct 68 ms 70392 KB Output is correct
81 Correct 72 ms 70520 KB Output is correct
82 Correct 72 ms 70496 KB Output is correct
83 Correct 71 ms 70484 KB Output is correct
84 Correct 67 ms 70392 KB Output is correct
85 Correct 69 ms 70392 KB Output is correct
86 Correct 71 ms 70392 KB Output is correct
87 Correct 92 ms 70852 KB Output is correct
88 Correct 132 ms 72512 KB Output is correct
89 Correct 352 ms 78364 KB Output is correct
90 Correct 367 ms 78304 KB Output is correct
91 Correct 366 ms 78324 KB Output is correct
92 Correct 239 ms 89204 KB Output is correct
93 Correct 224 ms 89204 KB Output is correct
94 Correct 229 ms 89296 KB Output is correct
95 Correct 213 ms 83712 KB Output is correct
96 Correct 228 ms 84140 KB Output is correct
97 Correct 220 ms 84276 KB Output is correct
98 Correct 217 ms 84288 KB Output is correct
99 Correct 224 ms 83076 KB Output is correct
100 Correct 507 ms 78576 KB Output is correct
101 Correct 474 ms 78788 KB Output is correct
102 Correct 470 ms 78960 KB Output is correct
103 Correct 504 ms 78924 KB Output is correct
104 Correct 236 ms 83064 KB Output is correct
105 Correct 241 ms 83164 KB Output is correct
106 Correct 238 ms 83128 KB Output is correct
107 Correct 298 ms 76660 KB Output is correct
108 Correct 315 ms 76916 KB Output is correct
109 Correct 327 ms 77524 KB Output is correct
110 Correct 192 ms 87924 KB Output is correct
111 Correct 213 ms 83924 KB Output is correct
112 Correct 189 ms 82820 KB Output is correct
113 Correct 202 ms 81624 KB Output is correct
114 Correct 396 ms 78612 KB Output is correct
115 Correct 367 ms 77548 KB Output is correct
116 Correct 231 ms 81652 KB Output is correct
117 Correct 243 ms 80892 KB Output is correct
118 Correct 224 ms 80116 KB Output is correct
119 Correct 215 ms 79812 KB Output is correct
120 Correct 190 ms 79604 KB Output is correct
121 Correct 209 ms 79084 KB Output is correct
122 Correct 194 ms 78836 KB Output is correct
123 Correct 257 ms 80884 KB Output is correct
124 Correct 276 ms 80116 KB Output is correct
125 Correct 236 ms 79832 KB Output is correct
126 Correct 205 ms 79824 KB Output is correct
127 Correct 202 ms 79192 KB Output is correct
128 Correct 225 ms 78808 KB Output is correct