답안 #999836

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999836 2024-06-16T07:24:27 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
1736 ms 36024 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3") 
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 100'030;
const int LG = 20;
int up[N][LG], depth[N], n, sz[N], tin[N], tout[N], tim=0;
vector<int> g[N];
int c[N];
void dfs(int at, int p) {
  up[at][0] = p;
  sz[at]=1;
  for(int i = 1;i < LG;i++) {
    up[at][i] = up[up[at][i-1]][i-1];
  }
  for(int to:g[at]) {
    if(to == p)continue;
    depth[to] = depth[at]+1;
    dfs(to, at);
    sz[at]+=sz[to];
  }
}
int lca(int a, int b) {
  if(depth[a] < depth[b])swap(a, b);
  int k = depth[a] - depth[b];
  for(int i = LG-1;i>=0;i--) {
    if(k&(1<<i)) {
      a = up[a][i];
    }
  }
  if(a==b)return a;
  for(int i = LG-1;i>=0;i--) {
    if(up[a][i] != up[b][i]) {
      a=up[a][i];
      b=up[b][i];
    }
  }
  return up[a][0];
}
struct node {
  int mx, mn;
  node() {
    mx=-1e9;
    mn=1e9;
  }
};
node merge(node A, node B) {
  node res;
  res.mx = max(A.mx, B.mx);
  res.mn = min(A.mn, B.mn);
  return res;
}
struct DS1 {
  vector<node> T;
  vector<int> lz;
  DS1() {
    T.resize(4*N);
    lz=vector<int>(4*N, -1);
  }
//  void update(int l, int r, int val) {
//    for(int i = l;i<=r;i++)T[i].mx = T[i].mn = val;
//  }
//  node query(int l, int r) {
//    node s;
//    for(int i = l;i<=r;i++)s=merge(s, T[i]);
//    return s;
//  }
  void push(int v) {
    if(lz[v] == -1)return;
    lz[2*v] = lz[v];
    lz[2*v+1] = lz[v];
    T[2*v].mx = T[2*v].mn = lz[v];
    T[2*v+1].mx = T[2*v+1].mn = lz[v];
    lz[v] = -1;
  }
  void update(int l, int r, int val, int tl=0, int tr=N-1, int v=1) {
    if(l>tr or r<tl)return;
    if(l <= tl and tr <= r) {
      lz[v] = val;
      T[v].mx = val;
      T[v].mn = val;
      return;
    }
    int tm=(tl+tr)/2;
    push(v);
    update(l, r, val, tl, tm, 2*v);
    update(l, r, val, tm+1, tr, 2*v+1);
    T[v] = merge(T[2*v], T[2*v+1]);
  }
  node query(int l, int r, int tl=0, int tr = N-1, int v=1) {
    if(l > tr or r<tl)return node();
    if(l <= tl and tr <= r)
      return T[v];
    int tm=(tl+tr)/2;
    push(v);
    return merge(query(l, r, tl, tm, 2*v), query(l, r, tm+1, tr, 2*v+1));
  }
};
namespace HLD {
  DS1 ds;
  int tin[N], top[N], tim=0, par[N], V[N];
  void dfs(int at, int p, int tp) {
    tin[at]=++tim;
    top[at]=tp;
    par[at]=p;
    int big=-1;
    for(int to:g[at]) {
      if(to==p)continue;
      if(big==-1 or sz[to]>sz[big]) {
        big=to;
      }
    }
    if(big==-1)return;
    dfs(big, at, tp);
    for(int to:g[at]) {
      if(to==big or to==p)continue;
      dfs(to, at, to);
    }
  }
  int upd(int u,int v, long long val) {
    int ans=0;
    while(top[u]!=top[v]) {
     if(depth[top[u]] < depth[top[v]])swap(u,v);
//     cout << top[u] << " " << u << " " << tin[top[u]] << " " << tin[u] << " " << val << endl;
     ds.update(tin[top[u]], tin[u], val);
     u=par[top[u]];
    }
    if(depth[u]>depth[v])swap(u,v);
    ds.update(tin[u], tin[v], val);
    return ans;
  }
  node query(int u,int v) {
    node ans;
    while(top[u]!=top[v]) {
     if(depth[top[u]] < depth[top[v]])swap(u,v);
     ans=merge(ans, ds.query(tin[top[u]], tin[u]));
     u=par[top[u]];
    }
    if(depth[u]>depth[v])swap(u,v);
    ans=merge(ans, ds.query(tin[u], tin[v]));
    return ans;
  }
  long long get(int u) {
    return ds.query(tin[u], tin[u]).mx;
  }
};
int get(int par) {
  for(int i = LG-1;i>=0;i--) {
    if(up[par][i]==0)continue;
    node res = HLD::query(up[par][i], par);
    if(res.mx == res.mn) {
      par = up[par][i];
    }
  }
  return up[par][0];
}
long long fen[N];
void upd(int at, int val) {
  at++;
  while(at<N) {
    fen[at]+=val;
    at+=at&(-at);
  }
}
long long que(int at) {
  if(at==0)return 0;
  long long res=0;
  while(at > 0) {
    res+=fen[at];
    at-=at&(-at);
  }
  return res;
}
int main () {
  cin.tie(0)->sync_with_stdio(0);
  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 a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
    e.push_back({a, b});
  }
  {
    map<int,int> mp;
    for(int i = 1;i<=n;i++)mp[c[i]] = 1;
    int cc = 0;
    for(auto &i:mp) {
      i.second=cc++;
    }
    for(int i = 1;i<=n;i++)c[i] = mp[c[i]];
  }
  depth[0]=-1;
  dfs(1, 0);
  HLD::dfs(1, 0, 1);
  int cnt=0;
  for(int i = 1;i<=n;i++) {
    HLD::upd(i, i, c[i]);
  }
  for(auto [a, b]:e) {
    long long ans = 0;
    int cur=a;
    int prev =0;
    vector<pair<int,int>> change;
    do {
      int par = get(cur);
      int val = HLD::get(cur);
      ans+=((long long)depth[cur]-depth[par])*que(val);
      upd(val, depth[cur]-depth[par]);
      change.push_back({val, depth[cur]-depth[par]});
      cur = par;
    }while(cur!=0);
    for(auto i:change) {
      upd(i.first, -i.second);
    }
    cur=a;
    HLD::upd(1, a, c[b]);
    cout << ans << "\n";
  }
  return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:214:9: warning: unused variable 'prev' [-Wunused-variable]
  214 |     int prev =0;
      |         ^~~~
construction.cpp:207:7: warning: unused variable 'cnt' [-Wunused-variable]
  207 |   int cnt=0;
      |       ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11352 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11140 KB Output is correct
4 Correct 4 ms 11352 KB Output is correct
5 Correct 5 ms 11356 KB Output is correct
6 Correct 4 ms 11356 KB Output is correct
7 Correct 4 ms 11356 KB Output is correct
8 Correct 4 ms 11356 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 4 ms 11356 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 4 ms 11356 KB Output is correct
13 Correct 4 ms 11352 KB Output is correct
14 Correct 4 ms 11356 KB Output is correct
15 Correct 7 ms 11356 KB Output is correct
16 Correct 5 ms 11356 KB Output is correct
17 Correct 6 ms 11356 KB Output is correct
18 Correct 7 ms 11356 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 4 ms 11356 KB Output is correct
21 Correct 4 ms 11356 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 5 ms 11356 KB Output is correct
24 Correct 4 ms 11356 KB Output is correct
25 Correct 4 ms 11356 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 5 ms 11376 KB Output is correct
28 Correct 4 ms 11352 KB Output is correct
29 Correct 4 ms 11356 KB Output is correct
30 Correct 6 ms 11428 KB Output is correct
31 Correct 6 ms 11356 KB Output is correct
32 Correct 5 ms 11356 KB Output is correct
33 Correct 5 ms 11356 KB Output is correct
34 Correct 5 ms 11356 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 4 ms 11356 KB Output is correct
37 Correct 4 ms 11352 KB Output is correct
38 Correct 4 ms 11352 KB Output is correct
39 Correct 4 ms 11356 KB Output is correct
40 Correct 4 ms 11356 KB Output is correct
41 Correct 4 ms 11356 KB Output is correct
42 Correct 6 ms 11356 KB Output is correct
43 Correct 4 ms 11232 KB Output is correct
44 Correct 4 ms 11356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11352 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11140 KB Output is correct
4 Correct 4 ms 11352 KB Output is correct
5 Correct 5 ms 11356 KB Output is correct
6 Correct 4 ms 11356 KB Output is correct
7 Correct 4 ms 11356 KB Output is correct
8 Correct 4 ms 11356 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 4 ms 11356 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 4 ms 11356 KB Output is correct
13 Correct 4 ms 11352 KB Output is correct
14 Correct 4 ms 11356 KB Output is correct
15 Correct 7 ms 11356 KB Output is correct
16 Correct 5 ms 11356 KB Output is correct
17 Correct 6 ms 11356 KB Output is correct
18 Correct 7 ms 11356 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 4 ms 11356 KB Output is correct
21 Correct 4 ms 11356 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 5 ms 11356 KB Output is correct
24 Correct 4 ms 11356 KB Output is correct
25 Correct 4 ms 11356 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 5 ms 11376 KB Output is correct
28 Correct 4 ms 11352 KB Output is correct
29 Correct 4 ms 11356 KB Output is correct
30 Correct 6 ms 11428 KB Output is correct
31 Correct 6 ms 11356 KB Output is correct
32 Correct 5 ms 11356 KB Output is correct
33 Correct 5 ms 11356 KB Output is correct
34 Correct 5 ms 11356 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 4 ms 11356 KB Output is correct
37 Correct 4 ms 11352 KB Output is correct
38 Correct 4 ms 11352 KB Output is correct
39 Correct 4 ms 11356 KB Output is correct
40 Correct 4 ms 11356 KB Output is correct
41 Correct 4 ms 11356 KB Output is correct
42 Correct 6 ms 11356 KB Output is correct
43 Correct 4 ms 11232 KB Output is correct
44 Correct 4 ms 11356 KB Output is correct
45 Correct 6 ms 11356 KB Output is correct
46 Correct 19 ms 11608 KB Output is correct
47 Correct 23 ms 11752 KB Output is correct
48 Correct 17 ms 11608 KB Output is correct
49 Correct 10 ms 11868 KB Output is correct
50 Correct 10 ms 12116 KB Output is correct
51 Correct 11 ms 12104 KB Output is correct
52 Correct 12 ms 11868 KB Output is correct
53 Correct 12 ms 11944 KB Output is correct
54 Correct 13 ms 11868 KB Output is correct
55 Correct 12 ms 11868 KB Output is correct
56 Correct 13 ms 11724 KB Output is correct
57 Correct 37 ms 11608 KB Output is correct
58 Correct 37 ms 11612 KB Output is correct
59 Correct 38 ms 11612 KB Output is correct
60 Correct 39 ms 11612 KB Output is correct
61 Correct 14 ms 11868 KB Output is correct
62 Correct 13 ms 11868 KB Output is correct
63 Correct 13 ms 11708 KB Output is correct
64 Correct 14 ms 11352 KB Output is correct
65 Correct 21 ms 11352 KB Output is correct
66 Correct 17 ms 11612 KB Output is correct
67 Correct 16 ms 11612 KB Output is correct
68 Correct 10 ms 11908 KB Output is correct
69 Correct 13 ms 11868 KB Output is correct
70 Correct 12 ms 11612 KB Output is correct
71 Correct 15 ms 11536 KB Output is correct
72 Correct 36 ms 11620 KB Output is correct
73 Correct 36 ms 11356 KB Output is correct
74 Correct 13 ms 11612 KB Output is correct
75 Correct 12 ms 11756 KB Output is correct
76 Correct 12 ms 11612 KB Output is correct
77 Correct 12 ms 11848 KB Output is correct
78 Correct 13 ms 11608 KB Output is correct
79 Correct 11 ms 11612 KB Output is correct
80 Correct 12 ms 11612 KB Output is correct
81 Correct 14 ms 11880 KB Output is correct
82 Correct 14 ms 11864 KB Output is correct
83 Correct 14 ms 11612 KB Output is correct
84 Correct 18 ms 11684 KB Output is correct
85 Correct 12 ms 11644 KB Output is correct
86 Correct 13 ms 11612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11352 KB Output is correct
2 Correct 3 ms 11356 KB Output is correct
3 Correct 3 ms 11140 KB Output is correct
4 Correct 4 ms 11352 KB Output is correct
5 Correct 5 ms 11356 KB Output is correct
6 Correct 4 ms 11356 KB Output is correct
7 Correct 4 ms 11356 KB Output is correct
8 Correct 4 ms 11356 KB Output is correct
9 Correct 3 ms 11356 KB Output is correct
10 Correct 4 ms 11356 KB Output is correct
11 Correct 4 ms 11356 KB Output is correct
12 Correct 4 ms 11356 KB Output is correct
13 Correct 4 ms 11352 KB Output is correct
14 Correct 4 ms 11356 KB Output is correct
15 Correct 7 ms 11356 KB Output is correct
16 Correct 5 ms 11356 KB Output is correct
17 Correct 6 ms 11356 KB Output is correct
18 Correct 7 ms 11356 KB Output is correct
19 Correct 4 ms 11356 KB Output is correct
20 Correct 4 ms 11356 KB Output is correct
21 Correct 4 ms 11356 KB Output is correct
22 Correct 4 ms 11356 KB Output is correct
23 Correct 5 ms 11356 KB Output is correct
24 Correct 4 ms 11356 KB Output is correct
25 Correct 4 ms 11356 KB Output is correct
26 Correct 4 ms 11356 KB Output is correct
27 Correct 5 ms 11376 KB Output is correct
28 Correct 4 ms 11352 KB Output is correct
29 Correct 4 ms 11356 KB Output is correct
30 Correct 6 ms 11428 KB Output is correct
31 Correct 6 ms 11356 KB Output is correct
32 Correct 5 ms 11356 KB Output is correct
33 Correct 5 ms 11356 KB Output is correct
34 Correct 5 ms 11356 KB Output is correct
35 Correct 4 ms 11356 KB Output is correct
36 Correct 4 ms 11356 KB Output is correct
37 Correct 4 ms 11352 KB Output is correct
38 Correct 4 ms 11352 KB Output is correct
39 Correct 4 ms 11356 KB Output is correct
40 Correct 4 ms 11356 KB Output is correct
41 Correct 4 ms 11356 KB Output is correct
42 Correct 6 ms 11356 KB Output is correct
43 Correct 4 ms 11232 KB Output is correct
44 Correct 4 ms 11356 KB Output is correct
45 Correct 6 ms 11356 KB Output is correct
46 Correct 19 ms 11608 KB Output is correct
47 Correct 23 ms 11752 KB Output is correct
48 Correct 17 ms 11608 KB Output is correct
49 Correct 10 ms 11868 KB Output is correct
50 Correct 10 ms 12116 KB Output is correct
51 Correct 11 ms 12104 KB Output is correct
52 Correct 12 ms 11868 KB Output is correct
53 Correct 12 ms 11944 KB Output is correct
54 Correct 13 ms 11868 KB Output is correct
55 Correct 12 ms 11868 KB Output is correct
56 Correct 13 ms 11724 KB Output is correct
57 Correct 37 ms 11608 KB Output is correct
58 Correct 37 ms 11612 KB Output is correct
59 Correct 38 ms 11612 KB Output is correct
60 Correct 39 ms 11612 KB Output is correct
61 Correct 14 ms 11868 KB Output is correct
62 Correct 13 ms 11868 KB Output is correct
63 Correct 13 ms 11708 KB Output is correct
64 Correct 14 ms 11352 KB Output is correct
65 Correct 21 ms 11352 KB Output is correct
66 Correct 17 ms 11612 KB Output is correct
67 Correct 16 ms 11612 KB Output is correct
68 Correct 10 ms 11908 KB Output is correct
69 Correct 13 ms 11868 KB Output is correct
70 Correct 12 ms 11612 KB Output is correct
71 Correct 15 ms 11536 KB Output is correct
72 Correct 36 ms 11620 KB Output is correct
73 Correct 36 ms 11356 KB Output is correct
74 Correct 13 ms 11612 KB Output is correct
75 Correct 12 ms 11756 KB Output is correct
76 Correct 12 ms 11612 KB Output is correct
77 Correct 12 ms 11848 KB Output is correct
78 Correct 13 ms 11608 KB Output is correct
79 Correct 11 ms 11612 KB Output is correct
80 Correct 12 ms 11612 KB Output is correct
81 Correct 14 ms 11880 KB Output is correct
82 Correct 14 ms 11864 KB Output is correct
83 Correct 14 ms 11612 KB Output is correct
84 Correct 18 ms 11684 KB Output is correct
85 Correct 12 ms 11644 KB Output is correct
86 Correct 13 ms 11612 KB Output is correct
87 Correct 48 ms 12380 KB Output is correct
88 Correct 154 ms 16848 KB Output is correct
89 Correct 659 ms 28100 KB Output is correct
90 Correct 656 ms 28164 KB Output is correct
91 Correct 661 ms 28360 KB Output is correct
92 Correct 291 ms 35780 KB Output is correct
93 Correct 281 ms 35784 KB Output is correct
94 Correct 280 ms 36024 KB Output is correct
95 Correct 381 ms 31944 KB Output is correct
96 Correct 391 ms 31864 KB Output is correct
97 Correct 378 ms 31944 KB Output is correct
98 Correct 396 ms 31944 KB Output is correct
99 Correct 390 ms 32028 KB Output is correct
100 Correct 1470 ms 28016 KB Output is correct
101 Correct 1634 ms 28164 KB Output is correct
102 Correct 1736 ms 29432 KB Output is correct
103 Correct 1620 ms 29552 KB Output is correct
104 Correct 465 ms 33220 KB Output is correct
105 Correct 470 ms 33204 KB Output is correct
106 Correct 485 ms 33508 KB Output is correct
107 Correct 437 ms 23644 KB Output is correct
108 Correct 610 ms 23752 KB Output is correct
109 Correct 740 ms 25240 KB Output is correct
110 Correct 256 ms 31356 KB Output is correct
111 Correct 393 ms 32852 KB Output is correct
112 Correct 350 ms 28360 KB Output is correct
113 Correct 358 ms 27548 KB Output is correct
114 Correct 1558 ms 29128 KB Output is correct
115 Correct 1547 ms 23748 KB Output is correct
116 Correct 431 ms 27588 KB Output is correct
117 Correct 383 ms 30948 KB Output is correct
118 Correct 378 ms 30276 KB Output is correct
119 Correct 392 ms 30148 KB Output is correct
120 Correct 344 ms 25796 KB Output is correct
121 Correct 344 ms 25236 KB Output is correct
122 Correct 336 ms 24724 KB Output is correct
123 Correct 481 ms 31168 KB Output is correct
124 Correct 476 ms 30292 KB Output is correct
125 Correct 452 ms 30108 KB Output is correct
126 Correct 447 ms 25952 KB Output is correct
127 Correct 465 ms 24972 KB Output is correct
128 Correct 426 ms 24656 KB Output is correct