답안 #540210

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
540210 2022-03-19T16:33:28 Z inluminas Construction of Highway (JOI18_construction) C++14
100 / 100
328 ms 21300 KB
#include"bits/stdc++.h"
using namespace std;

#define ll long long
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false)
#define inf LLONG_MAX

const int lmt=1e5+10;
int c[lmt],chld[lmt],par[lmt],head[lmt],ind=1,len[lmt],cind[lmt],pos[lmt],ps;
ll tree[lmt],ans;
vector<int>adj[lmt];
vector<pair<int,int>>path[lmt],a;



void dfs(int u,int p){
  chld[u]=1,par[u]=p;
  for(int v:adj[u]){
    if(v==p) continue;
    dfs(v,u);
    chld[u]+=chld[v];
  }
}

void hld(int u,int p){
  if(!head[ind]) head[ind]=u;

  len[ind]++;
  cind[u]=ind;
  pos[u]=++ps;

  int to=0;
  for(int v:adj[u]){
    if(v==p) continue;
    if(!to || chld[to]<chld[v]) to=v;
  }

  if(to) hld(to,u);

  for(int v:adj[u]){
    if(v==p || v==to) continue;
    ind++;
    hld(v,u);
  }
}

void f(int hi,int lo,int nw){
  int Len=pos[lo]-pos[hi]+1,till=0;
  int id=cind[hi];
  vector<pair<int,int>>b;
  while(till<Len){
    if(till+ path[id].back().first>Len){
      pair<int,int>x=path[id].back();
      path[id].pop_back();
      int dif=Len-till;
      b.emplace_back(dif,x.second);
      till=Len;
      path[id].emplace_back(x.first-dif,x.second);
      break;
    }else{
      pair<int,int>x=path[id].back();
      path[id].pop_back();
      b.push_back(x);
      till+=x.first;
    }
  }
  path[id].push_back({Len,nw});

  reverse(b.begin(),b.end());

  a.insert(a.end(),b.begin(),b.end());
}

ll query(int idx){
  ll sum=0;
  while(idx){
    sum+=tree[idx];
    idx-=(idx&-idx);
  }
  return sum;
}

void update(int idx,ll val,int sz){
  while(idx>0 && idx<=sz){
    tree[idx]+=val,idx+=(idx&-idx);
  }
}

int main(){
  fastio;

  int n;
  cin>>n;
  for(int i=1;i<=n;i++) cin>>c[i];

  vector<pair<int,int>>e;

  for(int i=1;i<n;i++){
    int u,v;
    cin>>u>>v;
    e.push_back({u,v});
    adj[u].push_back(v);
  }

  dfs(1,0);
  hld(1,0);

  for(int i=1; head[i];i++){
    path[i].push_back({len[i],0});
  }


  for(auto [U,V]:e){
    int u=V,v=c[V];
    while(u){
      int id=cind[u];
      f(head[id],u,v);
      u=par[head[id]];
    }
    a[0].first--;

    vector<int>q;
    for(auto [x,y]:a){
      if(x==0) continue;
      q.push_back(y);
    }

    sort(q.begin(),q.end());
    q.erase(unique(q.begin(),q.end()),q.end());

    map<int,int>ID;
    int o=0;
    for(int x:q){
      ID[x]=++o;
    }

    for(auto &x:a){
      if(x.first==0) continue;
      x.second=ID[x.second];
    }

    for(auto [x,y]:a){
      if(x==0) continue;

      ans+=x*query(y-1);
      update(y,x,o);
    }

    for(int i=1;i<=o;i++) tree[i]=0;

    cout<<ans<<endl;
    ans=0;

    a.clear();
  }

  return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:114:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  114 |   for(auto [U,V]:e){
      |            ^
construction.cpp:124:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  124 |     for(auto [x,y]:a){
      |              ^
construction.cpp:143:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  143 |     for(auto [x,y]:a){
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5036 KB Output is correct
8 Correct 3 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5044 KB Output is correct
11 Correct 3 ms 5036 KB Output is correct
12 Correct 4 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5116 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 5 ms 5040 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5044 KB Output is correct
19 Correct 3 ms 5040 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 3 ms 5040 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 4992 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5040 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 4 ms 5036 KB Output is correct
43 Correct 3 ms 5032 KB Output is correct
44 Correct 3 ms 5036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5036 KB Output is correct
8 Correct 3 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5044 KB Output is correct
11 Correct 3 ms 5036 KB Output is correct
12 Correct 4 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5116 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 5 ms 5040 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5044 KB Output is correct
19 Correct 3 ms 5040 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 3 ms 5040 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 4992 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5040 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 4 ms 5036 KB Output is correct
43 Correct 3 ms 5032 KB Output is correct
44 Correct 3 ms 5036 KB Output is correct
45 Correct 4 ms 5076 KB Output is correct
46 Correct 8 ms 5304 KB Output is correct
47 Correct 8 ms 5332 KB Output is correct
48 Correct 8 ms 5332 KB Output is correct
49 Correct 6 ms 5544 KB Output is correct
50 Correct 6 ms 5588 KB Output is correct
51 Correct 6 ms 5716 KB Output is correct
52 Correct 6 ms 5556 KB Output is correct
53 Correct 6 ms 5588 KB Output is correct
54 Correct 6 ms 5588 KB Output is correct
55 Correct 6 ms 5684 KB Output is correct
56 Correct 5 ms 5424 KB Output is correct
57 Correct 11 ms 5332 KB Output is correct
58 Correct 12 ms 5412 KB Output is correct
59 Correct 12 ms 5332 KB Output is correct
60 Correct 11 ms 5404 KB Output is correct
61 Correct 6 ms 5460 KB Output is correct
62 Correct 6 ms 5460 KB Output is correct
63 Correct 6 ms 5588 KB Output is correct
64 Correct 7 ms 5332 KB Output is correct
65 Correct 7 ms 5300 KB Output is correct
66 Correct 7 ms 5300 KB Output is correct
67 Correct 8 ms 5332 KB Output is correct
68 Correct 6 ms 5560 KB Output is correct
69 Correct 5 ms 5552 KB Output is correct
70 Correct 6 ms 5556 KB Output is correct
71 Correct 6 ms 5460 KB Output is correct
72 Correct 9 ms 5404 KB Output is correct
73 Correct 12 ms 5332 KB Output is correct
74 Correct 6 ms 5428 KB Output is correct
75 Correct 6 ms 5460 KB Output is correct
76 Correct 6 ms 5332 KB Output is correct
77 Correct 6 ms 5344 KB Output is correct
78 Correct 6 ms 5344 KB Output is correct
79 Correct 6 ms 5308 KB Output is correct
80 Correct 5 ms 5332 KB Output is correct
81 Correct 6 ms 5432 KB Output is correct
82 Correct 6 ms 5332 KB Output is correct
83 Correct 6 ms 5360 KB Output is correct
84 Correct 6 ms 5428 KB Output is correct
85 Correct 6 ms 5332 KB Output is correct
86 Correct 6 ms 5332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5036 KB Output is correct
8 Correct 3 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5044 KB Output is correct
11 Correct 3 ms 5036 KB Output is correct
12 Correct 4 ms 5036 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5116 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 5 ms 5040 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 4 ms 5044 KB Output is correct
19 Correct 3 ms 5040 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 3 ms 5040 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 4992 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5040 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5036 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 4 ms 5036 KB Output is correct
43 Correct 3 ms 5032 KB Output is correct
44 Correct 3 ms 5036 KB Output is correct
45 Correct 4 ms 5076 KB Output is correct
46 Correct 8 ms 5304 KB Output is correct
47 Correct 8 ms 5332 KB Output is correct
48 Correct 8 ms 5332 KB Output is correct
49 Correct 6 ms 5544 KB Output is correct
50 Correct 6 ms 5588 KB Output is correct
51 Correct 6 ms 5716 KB Output is correct
52 Correct 6 ms 5556 KB Output is correct
53 Correct 6 ms 5588 KB Output is correct
54 Correct 6 ms 5588 KB Output is correct
55 Correct 6 ms 5684 KB Output is correct
56 Correct 5 ms 5424 KB Output is correct
57 Correct 11 ms 5332 KB Output is correct
58 Correct 12 ms 5412 KB Output is correct
59 Correct 12 ms 5332 KB Output is correct
60 Correct 11 ms 5404 KB Output is correct
61 Correct 6 ms 5460 KB Output is correct
62 Correct 6 ms 5460 KB Output is correct
63 Correct 6 ms 5588 KB Output is correct
64 Correct 7 ms 5332 KB Output is correct
65 Correct 7 ms 5300 KB Output is correct
66 Correct 7 ms 5300 KB Output is correct
67 Correct 8 ms 5332 KB Output is correct
68 Correct 6 ms 5560 KB Output is correct
69 Correct 5 ms 5552 KB Output is correct
70 Correct 6 ms 5556 KB Output is correct
71 Correct 6 ms 5460 KB Output is correct
72 Correct 9 ms 5404 KB Output is correct
73 Correct 12 ms 5332 KB Output is correct
74 Correct 6 ms 5428 KB Output is correct
75 Correct 6 ms 5460 KB Output is correct
76 Correct 6 ms 5332 KB Output is correct
77 Correct 6 ms 5344 KB Output is correct
78 Correct 6 ms 5344 KB Output is correct
79 Correct 6 ms 5308 KB Output is correct
80 Correct 5 ms 5332 KB Output is correct
81 Correct 6 ms 5432 KB Output is correct
82 Correct 6 ms 5332 KB Output is correct
83 Correct 6 ms 5360 KB Output is correct
84 Correct 6 ms 5428 KB Output is correct
85 Correct 6 ms 5332 KB Output is correct
86 Correct 6 ms 5332 KB Output is correct
87 Correct 17 ms 5940 KB Output is correct
88 Correct 51 ms 7732 KB Output is correct
89 Correct 219 ms 14204 KB Output is correct
90 Correct 196 ms 14104 KB Output is correct
91 Correct 192 ms 14084 KB Output is correct
92 Correct 76 ms 20932 KB Output is correct
93 Correct 76 ms 21020 KB Output is correct
94 Correct 80 ms 21008 KB Output is correct
95 Correct 85 ms 20748 KB Output is correct
96 Correct 98 ms 21184 KB Output is correct
97 Correct 103 ms 21148 KB Output is correct
98 Correct 96 ms 21300 KB Output is correct
99 Correct 78 ms 17680 KB Output is correct
100 Correct 259 ms 13912 KB Output is correct
101 Correct 328 ms 14236 KB Output is correct
102 Correct 305 ms 14148 KB Output is correct
103 Correct 321 ms 14304 KB Output is correct
104 Correct 94 ms 17552 KB Output is correct
105 Correct 95 ms 17524 KB Output is correct
106 Correct 92 ms 17540 KB Output is correct
107 Correct 159 ms 13176 KB Output is correct
108 Correct 188 ms 13420 KB Output is correct
109 Correct 200 ms 13916 KB Output is correct
110 Correct 70 ms 20432 KB Output is correct
111 Correct 90 ms 20824 KB Output is correct
112 Correct 78 ms 18220 KB Output is correct
113 Correct 74 ms 17016 KB Output is correct
114 Correct 280 ms 14016 KB Output is correct
115 Correct 324 ms 13580 KB Output is correct
116 Correct 90 ms 17036 KB Output is correct
117 Correct 85 ms 15296 KB Output is correct
118 Correct 82 ms 14672 KB Output is correct
119 Correct 81 ms 14372 KB Output is correct
120 Correct 75 ms 14884 KB Output is correct
121 Correct 78 ms 14372 KB Output is correct
122 Correct 81 ms 14016 KB Output is correct
123 Correct 103 ms 15308 KB Output is correct
124 Correct 98 ms 14776 KB Output is correct
125 Correct 99 ms 14448 KB Output is correct
126 Correct 101 ms 15108 KB Output is correct
127 Correct 96 ms 14396 KB Output is correct
128 Correct 96 ms 14036 KB Output is correct