답안 #56337

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
56337 2018-07-11T05:23:45 Z khsoo01(#2109) Construction of Highway (JOI18_construction) C++11
100 / 100
645 ms 82108 KB
#include <bits/stdc++.h>
using namespace std;

const int NMAX=100000;
const int BS=17;

int Par[NMAX],Dep[NMAX],X[NMAX],dep[NMAX],par[NMAX],S[NMAX];

int n;
vector<vector<int> > e;

void dfs(int i)
{
  S[i]=0;
  for(int x=0;x<e[i].size();x++){
    int j=e[i][x];
    dep[j]=dep[i]+1;
    par[j]=i;
    dfs(j);
    S[i]+=S[j];
  }
}

void HLD()
{
  dep[0]=0;
  par[0]=-1;
  dfs(0);
  int NX=0;
  Par[0]=0;
  Dep[0]=0;
  stack<int> ST;
  ST.push(0);
  while(!ST.empty()){
    int i=ST.top();
    ST.pop();
    X[i]=NX;
    NX++;
    int K=-1;
    for(int j=0;j<e[i].size();j++){
      int k=e[i][j];
      if(K==-1||S[K]<S[k]){
	K=k;
      }
    }
    if(K==-1)continue;
    Par[K]=Par[i];
    Dep[K]=Dep[i]+1;
    for(int j=0;j<e[i].size();j++){
      int k=e[i][j];
      if(k==K)continue;
      Par[k]=k;
      Dep[k]=0;
      ST.push(k);
    }
    ST.push(K);
  }
}

stack<pair<int,int> > St[NMAX];

long long bit[1<<BS];
void bitadd(int N,int i,long long v)
{
  int p=i+1;
  while(p<1<<N){
    bit[p]+=v;
    p+=p&-p;
  }
}
long long bitsum(int N,int i)
{
  int p=i+1;
  long long ret=0;
  while(p>0){
    ret+=bit[p];
    p-=p&-p;
  }
  return ret;
}

long long cal_inv(vector<pair<int,int> > &V)
{
  int n=V.size(),N=0;
  while((1<<N)<=n)N++;
  vector<int> X(n);
  for(int i=0;i<n;i++)X[i]=V[i].first;
  sort(X.begin(),X.end());
  for(int i=0;i<n;i++)V[i].first=lower_bound(X.begin(),X.end(),V[i].first)-X.begin();
  for(int i=0;i<(1<<N);i++)bit[i]=0;
  long long ret=0ll;
  for(int t=0;t<n;t++){
    ret+=bitsum(N,V[t].first-1)*V[t].second;
    bitadd(N,V[t].first,V[t].second);
  }
  return ret;
}

int main()
{
  scanf("%d",&n);
  vector<int> C(n);
  for(int i=0;i<n;i++){
    scanf("%d",&(C[i]));
  }
  vector<int> A(n-1),B(n-1);
  for(int i=0;i<n-1;i++){
    scanf("%d%d",&(A[i]),&(B[i]));
    A[i]--,B[i]--;
  }
  e=vector<vector<int> >(n);
  for(int i=0;i<n-1;i++){
    e[A[i]].push_back(B[i]);
  }
  HLD();
  for(int T=0;T<n-1;T++){
    int p=B[T],c=C[p];
    vector<pair<int,int> > V;
    while(p!=-1){
      int d=Dep[p]+1,t=0;
      p=Par[p];
      stack<pair<int,int> > &S=St[p];
      vector<pair<int,int> > V2;
      while(!S.empty()&&S.top().second<=d){
	pair<int,int> pr=S.top();
	S.pop();
	V2.push_back(make_pair(pr.first,pr.second-t));
	t=pr.second;
      }
      if(!S.empty()){
	V2.push_back(make_pair(S.top().first,d-t));
      }
      S.push(make_pair(c,d));
      for(int x=V2.size()-1;x>=0;x--)V.push_back(V2[x]);
      p=par[p];
    }
    printf("%lld\n",cal_inv(V));
  }
  return 0;
}

Compilation message

construction.cpp: In function 'void dfs(int)':
construction.cpp:15:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int x=0;x<e[i].size();x++){
               ~^~~~~~~~~~~~
construction.cpp: In function 'void HLD()':
construction.cpp:40:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<e[i].size();j++){
                 ~^~~~~~~~~~~~
construction.cpp:49:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j=0;j<e[i].size();j++){
                 ~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&n);
   ~~~~~^~~~~~~~~
construction.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&(C[i]));
     ~~~~~^~~~~~~~~~~~~~
construction.cpp:108:10: 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 77 ms 67704 KB Output is correct
2 Correct 73 ms 67704 KB Output is correct
3 Correct 71 ms 67728 KB Output is correct
4 Correct 72 ms 67824 KB Output is correct
5 Correct 81 ms 67924 KB Output is correct
6 Correct 65 ms 68000 KB Output is correct
7 Correct 72 ms 68156 KB Output is correct
8 Correct 80 ms 68156 KB Output is correct
9 Correct 64 ms 68156 KB Output is correct
10 Correct 65 ms 68156 KB Output is correct
11 Correct 70 ms 68156 KB Output is correct
12 Correct 65 ms 68156 KB Output is correct
13 Correct 80 ms 68156 KB Output is correct
14 Correct 80 ms 68156 KB Output is correct
15 Correct 68 ms 68156 KB Output is correct
16 Correct 78 ms 68156 KB Output is correct
17 Correct 89 ms 68156 KB Output is correct
18 Correct 73 ms 68156 KB Output is correct
19 Correct 71 ms 68156 KB Output is correct
20 Correct 71 ms 68156 KB Output is correct
21 Correct 70 ms 68156 KB Output is correct
22 Correct 74 ms 68156 KB Output is correct
23 Correct 108 ms 68156 KB Output is correct
24 Correct 73 ms 68156 KB Output is correct
25 Correct 62 ms 68156 KB Output is correct
26 Correct 60 ms 68156 KB Output is correct
27 Correct 70 ms 68156 KB Output is correct
28 Correct 70 ms 68156 KB Output is correct
29 Correct 70 ms 68156 KB Output is correct
30 Correct 93 ms 68156 KB Output is correct
31 Correct 80 ms 68156 KB Output is correct
32 Correct 68 ms 68156 KB Output is correct
33 Correct 65 ms 68156 KB Output is correct
34 Correct 63 ms 68156 KB Output is correct
35 Correct 62 ms 68156 KB Output is correct
36 Correct 73 ms 68156 KB Output is correct
37 Correct 62 ms 68156 KB Output is correct
38 Correct 75 ms 68156 KB Output is correct
39 Correct 70 ms 68156 KB Output is correct
40 Correct 73 ms 68156 KB Output is correct
41 Correct 69 ms 68156 KB Output is correct
42 Correct 76 ms 68156 KB Output is correct
43 Correct 74 ms 68156 KB Output is correct
44 Correct 87 ms 68156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 67704 KB Output is correct
2 Correct 73 ms 67704 KB Output is correct
3 Correct 71 ms 67728 KB Output is correct
4 Correct 72 ms 67824 KB Output is correct
5 Correct 81 ms 67924 KB Output is correct
6 Correct 65 ms 68000 KB Output is correct
7 Correct 72 ms 68156 KB Output is correct
8 Correct 80 ms 68156 KB Output is correct
9 Correct 64 ms 68156 KB Output is correct
10 Correct 65 ms 68156 KB Output is correct
11 Correct 70 ms 68156 KB Output is correct
12 Correct 65 ms 68156 KB Output is correct
13 Correct 80 ms 68156 KB Output is correct
14 Correct 80 ms 68156 KB Output is correct
15 Correct 68 ms 68156 KB Output is correct
16 Correct 78 ms 68156 KB Output is correct
17 Correct 89 ms 68156 KB Output is correct
18 Correct 73 ms 68156 KB Output is correct
19 Correct 71 ms 68156 KB Output is correct
20 Correct 71 ms 68156 KB Output is correct
21 Correct 70 ms 68156 KB Output is correct
22 Correct 74 ms 68156 KB Output is correct
23 Correct 108 ms 68156 KB Output is correct
24 Correct 73 ms 68156 KB Output is correct
25 Correct 62 ms 68156 KB Output is correct
26 Correct 60 ms 68156 KB Output is correct
27 Correct 70 ms 68156 KB Output is correct
28 Correct 70 ms 68156 KB Output is correct
29 Correct 70 ms 68156 KB Output is correct
30 Correct 93 ms 68156 KB Output is correct
31 Correct 80 ms 68156 KB Output is correct
32 Correct 68 ms 68156 KB Output is correct
33 Correct 65 ms 68156 KB Output is correct
34 Correct 63 ms 68156 KB Output is correct
35 Correct 62 ms 68156 KB Output is correct
36 Correct 73 ms 68156 KB Output is correct
37 Correct 62 ms 68156 KB Output is correct
38 Correct 75 ms 68156 KB Output is correct
39 Correct 70 ms 68156 KB Output is correct
40 Correct 73 ms 68156 KB Output is correct
41 Correct 69 ms 68156 KB Output is correct
42 Correct 76 ms 68156 KB Output is correct
43 Correct 74 ms 68156 KB Output is correct
44 Correct 87 ms 68156 KB Output is correct
45 Correct 77 ms 68156 KB Output is correct
46 Correct 80 ms 68220 KB Output is correct
47 Correct 106 ms 68340 KB Output is correct
48 Correct 88 ms 68340 KB Output is correct
49 Correct 72 ms 68508 KB Output is correct
50 Correct 70 ms 68508 KB Output is correct
51 Correct 109 ms 68524 KB Output is correct
52 Correct 81 ms 68524 KB Output is correct
53 Correct 92 ms 68540 KB Output is correct
54 Correct 81 ms 68540 KB Output is correct
55 Correct 80 ms 68540 KB Output is correct
56 Correct 80 ms 68540 KB Output is correct
57 Correct 116 ms 68540 KB Output is correct
58 Correct 89 ms 68540 KB Output is correct
59 Correct 134 ms 68540 KB Output is correct
60 Correct 77 ms 68540 KB Output is correct
61 Correct 71 ms 68540 KB Output is correct
62 Correct 72 ms 68540 KB Output is correct
63 Correct 107 ms 68540 KB Output is correct
64 Correct 83 ms 68540 KB Output is correct
65 Correct 75 ms 68540 KB Output is correct
66 Correct 85 ms 68540 KB Output is correct
67 Correct 83 ms 68540 KB Output is correct
68 Correct 83 ms 68540 KB Output is correct
69 Correct 83 ms 68540 KB Output is correct
70 Correct 103 ms 68540 KB Output is correct
71 Correct 80 ms 68540 KB Output is correct
72 Correct 80 ms 68540 KB Output is correct
73 Correct 82 ms 68540 KB Output is correct
74 Correct 83 ms 68540 KB Output is correct
75 Correct 83 ms 68540 KB Output is correct
76 Correct 95 ms 68540 KB Output is correct
77 Correct 70 ms 68540 KB Output is correct
78 Correct 72 ms 68540 KB Output is correct
79 Correct 85 ms 68540 KB Output is correct
80 Correct 99 ms 68540 KB Output is correct
81 Correct 79 ms 68540 KB Output is correct
82 Correct 81 ms 68540 KB Output is correct
83 Correct 78 ms 68540 KB Output is correct
84 Correct 81 ms 68540 KB Output is correct
85 Correct 85 ms 68540 KB Output is correct
86 Correct 87 ms 68540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 67704 KB Output is correct
2 Correct 73 ms 67704 KB Output is correct
3 Correct 71 ms 67728 KB Output is correct
4 Correct 72 ms 67824 KB Output is correct
5 Correct 81 ms 67924 KB Output is correct
6 Correct 65 ms 68000 KB Output is correct
7 Correct 72 ms 68156 KB Output is correct
8 Correct 80 ms 68156 KB Output is correct
9 Correct 64 ms 68156 KB Output is correct
10 Correct 65 ms 68156 KB Output is correct
11 Correct 70 ms 68156 KB Output is correct
12 Correct 65 ms 68156 KB Output is correct
13 Correct 80 ms 68156 KB Output is correct
14 Correct 80 ms 68156 KB Output is correct
15 Correct 68 ms 68156 KB Output is correct
16 Correct 78 ms 68156 KB Output is correct
17 Correct 89 ms 68156 KB Output is correct
18 Correct 73 ms 68156 KB Output is correct
19 Correct 71 ms 68156 KB Output is correct
20 Correct 71 ms 68156 KB Output is correct
21 Correct 70 ms 68156 KB Output is correct
22 Correct 74 ms 68156 KB Output is correct
23 Correct 108 ms 68156 KB Output is correct
24 Correct 73 ms 68156 KB Output is correct
25 Correct 62 ms 68156 KB Output is correct
26 Correct 60 ms 68156 KB Output is correct
27 Correct 70 ms 68156 KB Output is correct
28 Correct 70 ms 68156 KB Output is correct
29 Correct 70 ms 68156 KB Output is correct
30 Correct 93 ms 68156 KB Output is correct
31 Correct 80 ms 68156 KB Output is correct
32 Correct 68 ms 68156 KB Output is correct
33 Correct 65 ms 68156 KB Output is correct
34 Correct 63 ms 68156 KB Output is correct
35 Correct 62 ms 68156 KB Output is correct
36 Correct 73 ms 68156 KB Output is correct
37 Correct 62 ms 68156 KB Output is correct
38 Correct 75 ms 68156 KB Output is correct
39 Correct 70 ms 68156 KB Output is correct
40 Correct 73 ms 68156 KB Output is correct
41 Correct 69 ms 68156 KB Output is correct
42 Correct 76 ms 68156 KB Output is correct
43 Correct 74 ms 68156 KB Output is correct
44 Correct 87 ms 68156 KB Output is correct
45 Correct 77 ms 68156 KB Output is correct
46 Correct 80 ms 68220 KB Output is correct
47 Correct 106 ms 68340 KB Output is correct
48 Correct 88 ms 68340 KB Output is correct
49 Correct 72 ms 68508 KB Output is correct
50 Correct 70 ms 68508 KB Output is correct
51 Correct 109 ms 68524 KB Output is correct
52 Correct 81 ms 68524 KB Output is correct
53 Correct 92 ms 68540 KB Output is correct
54 Correct 81 ms 68540 KB Output is correct
55 Correct 80 ms 68540 KB Output is correct
56 Correct 80 ms 68540 KB Output is correct
57 Correct 116 ms 68540 KB Output is correct
58 Correct 89 ms 68540 KB Output is correct
59 Correct 134 ms 68540 KB Output is correct
60 Correct 77 ms 68540 KB Output is correct
61 Correct 71 ms 68540 KB Output is correct
62 Correct 72 ms 68540 KB Output is correct
63 Correct 107 ms 68540 KB Output is correct
64 Correct 83 ms 68540 KB Output is correct
65 Correct 75 ms 68540 KB Output is correct
66 Correct 85 ms 68540 KB Output is correct
67 Correct 83 ms 68540 KB Output is correct
68 Correct 83 ms 68540 KB Output is correct
69 Correct 83 ms 68540 KB Output is correct
70 Correct 103 ms 68540 KB Output is correct
71 Correct 80 ms 68540 KB Output is correct
72 Correct 80 ms 68540 KB Output is correct
73 Correct 82 ms 68540 KB Output is correct
74 Correct 83 ms 68540 KB Output is correct
75 Correct 83 ms 68540 KB Output is correct
76 Correct 95 ms 68540 KB Output is correct
77 Correct 70 ms 68540 KB Output is correct
78 Correct 72 ms 68540 KB Output is correct
79 Correct 85 ms 68540 KB Output is correct
80 Correct 99 ms 68540 KB Output is correct
81 Correct 79 ms 68540 KB Output is correct
82 Correct 81 ms 68540 KB Output is correct
83 Correct 78 ms 68540 KB Output is correct
84 Correct 81 ms 68540 KB Output is correct
85 Correct 85 ms 68540 KB Output is correct
86 Correct 87 ms 68540 KB Output is correct
87 Correct 90 ms 68776 KB Output is correct
88 Correct 149 ms 70340 KB Output is correct
89 Correct 590 ms 75720 KB Output is correct
90 Correct 542 ms 75720 KB Output is correct
91 Correct 446 ms 75720 KB Output is correct
92 Correct 249 ms 82108 KB Output is correct
93 Correct 254 ms 82108 KB Output is correct
94 Correct 218 ms 82108 KB Output is correct
95 Correct 328 ms 82108 KB Output is correct
96 Correct 241 ms 82108 KB Output is correct
97 Correct 228 ms 82108 KB Output is correct
98 Correct 209 ms 82108 KB Output is correct
99 Correct 262 ms 82108 KB Output is correct
100 Correct 571 ms 82108 KB Output is correct
101 Correct 645 ms 82108 KB Output is correct
102 Correct 629 ms 82108 KB Output is correct
103 Correct 591 ms 82108 KB Output is correct
104 Correct 182 ms 82108 KB Output is correct
105 Correct 334 ms 82108 KB Output is correct
106 Correct 238 ms 82108 KB Output is correct
107 Correct 471 ms 82108 KB Output is correct
108 Correct 571 ms 82108 KB Output is correct
109 Correct 506 ms 82108 KB Output is correct
110 Correct 196 ms 82108 KB Output is correct
111 Correct 267 ms 82108 KB Output is correct
112 Correct 238 ms 82108 KB Output is correct
113 Correct 194 ms 82108 KB Output is correct
114 Correct 433 ms 82108 KB Output is correct
115 Correct 483 ms 82108 KB Output is correct
116 Correct 233 ms 82108 KB Output is correct
117 Correct 233 ms 82108 KB Output is correct
118 Correct 190 ms 82108 KB Output is correct
119 Correct 190 ms 82108 KB Output is correct
120 Correct 196 ms 82108 KB Output is correct
121 Correct 213 ms 82108 KB Output is correct
122 Correct 236 ms 82108 KB Output is correct
123 Correct 306 ms 82108 KB Output is correct
124 Correct 221 ms 82108 KB Output is correct
125 Correct 238 ms 82108 KB Output is correct
126 Correct 333 ms 82108 KB Output is correct
127 Correct 242 ms 82108 KB Output is correct
128 Correct 283 ms 82108 KB Output is correct