답안 #47404

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
47404 2018-05-02T11:56:00 Z mirbek01 Construction of Highway (JOI18_construction) C++17
16 / 100
1000 ms 29880 KB
# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, c[N], a[N], b[N], h[N], fen[N], u[N];
int p[18][N], tin[N], tout[N], timer;
pair <int, int> t[N * 4];
vector <int> vec, g[N], vcc;


void dfs(int v, int pr = 0){
      tin[v] = ++ timer;
      p[0][v] = pr;
      for(int i = 1; i <= 17; i ++)
            p[i][v] = p[i - 1][p[i - 1][v]];
      for(int to : g[v]){
            if(to == pr) continue;
            h[to] = h[v] + 1;
            dfs(to, v);
      }
      tout[v] = timer;
}
inline bool upper(int a, int b){
      return tin[a] <= tin[b] && tout[a] >= tout[b];
}

inline int lca(int a, int b){
      if(upper(a, b)) return a;
      if(upper(b, a)) return b;
      for(int i = 17; i >= 0; i --)
            if(p[i][a] && !upper(p[i][a], b))
                  a = p[i][a];
      return p[0][a];
}

inline void update(int pos, int val, int vv, int v = 1, int tl = 1, int tr = n){
      if(tl == tr)
            t[v] = {val, vv};
      else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  update(pos, val, vv, v << 1, tl, tm);
            else
                  update(pos, val, vv, (v << 1) | 1, tm + 1, tr);
            t[v] = max(t[v << 1], t[(v << 1) | 1]);
      }
}

inline pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r) return {0, 0};
      if(l <= tl && tr <= r) return t[v];
      int tm = (tl + tr) >> 1;
      return max(get(l, r, v << 1, tl, tm),
                  get(l, r, (v << 1) | 1, tm + 1, tr));
}

inline void Update(int r, int val){
      for(; r > 0; r = (r & (r + 1)) - 1){
            fen[r] += val;
            if(!u[r]){
                  u[r] = 1;
                  vcc.emplace_back(r);
            }
      }
}

inline int Get(int pos){
      int res = 0;
      for(; pos <= n; pos |= pos + 1)
            res += fen[pos];
      return res;
}

int main(){
      ios_base::sync_with_stdio(0);

      scanf("%d", &n);

      for(int i = 1; i <= n; i ++){
            scanf("%d", &c[i]);
            vec.emplace_back(c[i]);
      }

      sort(vec.begin(), vec.end());
      vec.resize(unique(vec.begin(), vec.end()) - vec.begin());

      for(int i = 1; i <= n; i ++)
            c[i] = lower_bound(vec.begin(), vec.end(), c[i]) - vec.begin() + 1;

      for(int i = 1; i < n; i ++){
            scanf("%d %d", &a[i], &b[i]);
            g[a[i]].emplace_back(b[i]);
            g[b[i]].emplace_back(a[i]);
      }

      h[1] = 1;
      dfs(1);

      for(int i = 1; i < n; i ++){
            int v = 1, prelca = 0;
            long long ans = 0;
            vcc.clear();
            while(1){
                  pair <int, int> mx = get(tin[v], tout[v]);
                  int LCA  = lca(mx.second, a[i]);
                  int f = h[LCA] - h[prelca], s = c[mx.second];
                  ans += Get(s + 1) * f;
                  Update(s, f);
                  prelca = LCA;
                  if(v == a[i]) break;
                  int vv = a[i];
                  for(int i = 17; i >= 0; i --){
                        if(p[i][vv] && !upper(p[i][vv], LCA))
                              vv = p[i][vv];
                  }
                  v = vv;
            }
            for(int j = 0; j < vcc.size(); j ++)
                  fen[vcc[j]] = 0, u[vcc[j]] = 0;
            printf("%I64d\n", ans);
            update(tin[b[i]], i, b[i]);
      }
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:120:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int j = 0; j < vcc.size(); j ++)
                            ~~^~~~~~~~~~~~
construction.cpp:122:34: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
             printf("%I64d\n", ans);
                                  ^
construction.cpp:79:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", &n);
       ~~~~~^~~~~~~~~~
construction.cpp:82:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &c[i]);
             ~~~~~^~~~~~~~~~~~~
construction.cpp:93:18: 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 4 ms 2808 KB Output is correct
2 Correct 4 ms 2916 KB Output is correct
3 Correct 4 ms 3112 KB Output is correct
4 Correct 5 ms 3112 KB Output is correct
5 Correct 5 ms 3112 KB Output is correct
6 Correct 5 ms 3136 KB Output is correct
7 Correct 4 ms 3152 KB Output is correct
8 Correct 4 ms 3168 KB Output is correct
9 Correct 4 ms 3168 KB Output is correct
10 Correct 4 ms 3168 KB Output is correct
11 Correct 6 ms 3168 KB Output is correct
12 Correct 4 ms 3168 KB Output is correct
13 Correct 4 ms 3168 KB Output is correct
14 Correct 7 ms 3168 KB Output is correct
15 Correct 5 ms 3168 KB Output is correct
16 Correct 5 ms 3168 KB Output is correct
17 Correct 5 ms 3168 KB Output is correct
18 Correct 5 ms 3168 KB Output is correct
19 Correct 5 ms 3168 KB Output is correct
20 Correct 4 ms 3168 KB Output is correct
21 Correct 7 ms 3168 KB Output is correct
22 Correct 5 ms 3168 KB Output is correct
23 Correct 5 ms 3168 KB Output is correct
24 Correct 5 ms 3168 KB Output is correct
25 Correct 5 ms 3168 KB Output is correct
26 Correct 4 ms 3172 KB Output is correct
27 Correct 5 ms 3172 KB Output is correct
28 Correct 4 ms 3172 KB Output is correct
29 Correct 4 ms 3172 KB Output is correct
30 Correct 5 ms 3172 KB Output is correct
31 Correct 6 ms 3172 KB Output is correct
32 Correct 4 ms 3172 KB Output is correct
33 Correct 4 ms 3172 KB Output is correct
34 Correct 4 ms 3172 KB Output is correct
35 Correct 4 ms 3172 KB Output is correct
36 Correct 5 ms 3172 KB Output is correct
37 Correct 5 ms 3172 KB Output is correct
38 Correct 5 ms 3172 KB Output is correct
39 Correct 4 ms 3240 KB Output is correct
40 Correct 5 ms 3240 KB Output is correct
41 Correct 12 ms 3240 KB Output is correct
42 Correct 4 ms 3260 KB Output is correct
43 Correct 5 ms 3260 KB Output is correct
44 Correct 5 ms 3352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2916 KB Output is correct
3 Correct 4 ms 3112 KB Output is correct
4 Correct 5 ms 3112 KB Output is correct
5 Correct 5 ms 3112 KB Output is correct
6 Correct 5 ms 3136 KB Output is correct
7 Correct 4 ms 3152 KB Output is correct
8 Correct 4 ms 3168 KB Output is correct
9 Correct 4 ms 3168 KB Output is correct
10 Correct 4 ms 3168 KB Output is correct
11 Correct 6 ms 3168 KB Output is correct
12 Correct 4 ms 3168 KB Output is correct
13 Correct 4 ms 3168 KB Output is correct
14 Correct 7 ms 3168 KB Output is correct
15 Correct 5 ms 3168 KB Output is correct
16 Correct 5 ms 3168 KB Output is correct
17 Correct 5 ms 3168 KB Output is correct
18 Correct 5 ms 3168 KB Output is correct
19 Correct 5 ms 3168 KB Output is correct
20 Correct 4 ms 3168 KB Output is correct
21 Correct 7 ms 3168 KB Output is correct
22 Correct 5 ms 3168 KB Output is correct
23 Correct 5 ms 3168 KB Output is correct
24 Correct 5 ms 3168 KB Output is correct
25 Correct 5 ms 3168 KB Output is correct
26 Correct 4 ms 3172 KB Output is correct
27 Correct 5 ms 3172 KB Output is correct
28 Correct 4 ms 3172 KB Output is correct
29 Correct 4 ms 3172 KB Output is correct
30 Correct 5 ms 3172 KB Output is correct
31 Correct 6 ms 3172 KB Output is correct
32 Correct 4 ms 3172 KB Output is correct
33 Correct 4 ms 3172 KB Output is correct
34 Correct 4 ms 3172 KB Output is correct
35 Correct 4 ms 3172 KB Output is correct
36 Correct 5 ms 3172 KB Output is correct
37 Correct 5 ms 3172 KB Output is correct
38 Correct 5 ms 3172 KB Output is correct
39 Correct 4 ms 3240 KB Output is correct
40 Correct 5 ms 3240 KB Output is correct
41 Correct 12 ms 3240 KB Output is correct
42 Correct 4 ms 3260 KB Output is correct
43 Correct 5 ms 3260 KB Output is correct
44 Correct 5 ms 3352 KB Output is correct
45 Correct 6 ms 3388 KB Output is correct
46 Correct 17 ms 4028 KB Output is correct
47 Correct 18 ms 4028 KB Output is correct
48 Correct 21 ms 4036 KB Output is correct
49 Correct 17 ms 4188 KB Output is correct
50 Correct 12 ms 4188 KB Output is correct
51 Correct 11 ms 4188 KB Output is correct
52 Correct 12 ms 4188 KB Output is correct
53 Correct 13 ms 4188 KB Output is correct
54 Correct 13 ms 4188 KB Output is correct
55 Correct 18 ms 4188 KB Output is correct
56 Correct 17 ms 4188 KB Output is correct
57 Correct 25 ms 4188 KB Output is correct
58 Correct 26 ms 4188 KB Output is correct
59 Correct 28 ms 4188 KB Output is correct
60 Correct 29 ms 4188 KB Output is correct
61 Correct 15 ms 4188 KB Output is correct
62 Correct 14 ms 4188 KB Output is correct
63 Correct 14 ms 4188 KB Output is correct
64 Correct 17 ms 4188 KB Output is correct
65 Correct 16 ms 4188 KB Output is correct
66 Correct 16 ms 4188 KB Output is correct
67 Correct 18 ms 4188 KB Output is correct
68 Correct 11 ms 4188 KB Output is correct
69 Correct 12 ms 4188 KB Output is correct
70 Correct 13 ms 4188 KB Output is correct
71 Correct 12 ms 4188 KB Output is correct
72 Correct 24 ms 4188 KB Output is correct
73 Correct 25 ms 4188 KB Output is correct
74 Correct 16 ms 4188 KB Output is correct
75 Correct 12 ms 4188 KB Output is correct
76 Correct 11 ms 4188 KB Output is correct
77 Correct 11 ms 4188 KB Output is correct
78 Correct 11 ms 4188 KB Output is correct
79 Correct 11 ms 4188 KB Output is correct
80 Correct 12 ms 4188 KB Output is correct
81 Correct 14 ms 4188 KB Output is correct
82 Correct 15 ms 4188 KB Output is correct
83 Correct 14 ms 4188 KB Output is correct
84 Correct 13 ms 4188 KB Output is correct
85 Correct 15 ms 4188 KB Output is correct
86 Correct 13 ms 4188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2916 KB Output is correct
3 Correct 4 ms 3112 KB Output is correct
4 Correct 5 ms 3112 KB Output is correct
5 Correct 5 ms 3112 KB Output is correct
6 Correct 5 ms 3136 KB Output is correct
7 Correct 4 ms 3152 KB Output is correct
8 Correct 4 ms 3168 KB Output is correct
9 Correct 4 ms 3168 KB Output is correct
10 Correct 4 ms 3168 KB Output is correct
11 Correct 6 ms 3168 KB Output is correct
12 Correct 4 ms 3168 KB Output is correct
13 Correct 4 ms 3168 KB Output is correct
14 Correct 7 ms 3168 KB Output is correct
15 Correct 5 ms 3168 KB Output is correct
16 Correct 5 ms 3168 KB Output is correct
17 Correct 5 ms 3168 KB Output is correct
18 Correct 5 ms 3168 KB Output is correct
19 Correct 5 ms 3168 KB Output is correct
20 Correct 4 ms 3168 KB Output is correct
21 Correct 7 ms 3168 KB Output is correct
22 Correct 5 ms 3168 KB Output is correct
23 Correct 5 ms 3168 KB Output is correct
24 Correct 5 ms 3168 KB Output is correct
25 Correct 5 ms 3168 KB Output is correct
26 Correct 4 ms 3172 KB Output is correct
27 Correct 5 ms 3172 KB Output is correct
28 Correct 4 ms 3172 KB Output is correct
29 Correct 4 ms 3172 KB Output is correct
30 Correct 5 ms 3172 KB Output is correct
31 Correct 6 ms 3172 KB Output is correct
32 Correct 4 ms 3172 KB Output is correct
33 Correct 4 ms 3172 KB Output is correct
34 Correct 4 ms 3172 KB Output is correct
35 Correct 4 ms 3172 KB Output is correct
36 Correct 5 ms 3172 KB Output is correct
37 Correct 5 ms 3172 KB Output is correct
38 Correct 5 ms 3172 KB Output is correct
39 Correct 4 ms 3240 KB Output is correct
40 Correct 5 ms 3240 KB Output is correct
41 Correct 12 ms 3240 KB Output is correct
42 Correct 4 ms 3260 KB Output is correct
43 Correct 5 ms 3260 KB Output is correct
44 Correct 5 ms 3352 KB Output is correct
45 Correct 6 ms 3388 KB Output is correct
46 Correct 17 ms 4028 KB Output is correct
47 Correct 18 ms 4028 KB Output is correct
48 Correct 21 ms 4036 KB Output is correct
49 Correct 17 ms 4188 KB Output is correct
50 Correct 12 ms 4188 KB Output is correct
51 Correct 11 ms 4188 KB Output is correct
52 Correct 12 ms 4188 KB Output is correct
53 Correct 13 ms 4188 KB Output is correct
54 Correct 13 ms 4188 KB Output is correct
55 Correct 18 ms 4188 KB Output is correct
56 Correct 17 ms 4188 KB Output is correct
57 Correct 25 ms 4188 KB Output is correct
58 Correct 26 ms 4188 KB Output is correct
59 Correct 28 ms 4188 KB Output is correct
60 Correct 29 ms 4188 KB Output is correct
61 Correct 15 ms 4188 KB Output is correct
62 Correct 14 ms 4188 KB Output is correct
63 Correct 14 ms 4188 KB Output is correct
64 Correct 17 ms 4188 KB Output is correct
65 Correct 16 ms 4188 KB Output is correct
66 Correct 16 ms 4188 KB Output is correct
67 Correct 18 ms 4188 KB Output is correct
68 Correct 11 ms 4188 KB Output is correct
69 Correct 12 ms 4188 KB Output is correct
70 Correct 13 ms 4188 KB Output is correct
71 Correct 12 ms 4188 KB Output is correct
72 Correct 24 ms 4188 KB Output is correct
73 Correct 25 ms 4188 KB Output is correct
74 Correct 16 ms 4188 KB Output is correct
75 Correct 12 ms 4188 KB Output is correct
76 Correct 11 ms 4188 KB Output is correct
77 Correct 11 ms 4188 KB Output is correct
78 Correct 11 ms 4188 KB Output is correct
79 Correct 11 ms 4188 KB Output is correct
80 Correct 12 ms 4188 KB Output is correct
81 Correct 14 ms 4188 KB Output is correct
82 Correct 15 ms 4188 KB Output is correct
83 Correct 14 ms 4188 KB Output is correct
84 Correct 13 ms 4188 KB Output is correct
85 Correct 15 ms 4188 KB Output is correct
86 Correct 13 ms 4188 KB Output is correct
87 Correct 46 ms 5436 KB Output is correct
88 Correct 233 ms 9768 KB Output is correct
89 Correct 890 ms 25352 KB Output is correct
90 Correct 868 ms 25352 KB Output is correct
91 Correct 844 ms 25352 KB Output is correct
92 Correct 483 ms 29832 KB Output is correct
93 Correct 473 ms 29880 KB Output is correct
94 Correct 464 ms 29880 KB Output is correct
95 Correct 587 ms 29880 KB Output is correct
96 Correct 517 ms 29880 KB Output is correct
97 Correct 612 ms 29880 KB Output is correct
98 Correct 610 ms 29880 KB Output is correct
99 Correct 573 ms 29880 KB Output is correct
100 Execution timed out 1079 ms 29880 KB Time limit exceeded
101 Halted 0 ms 0 KB -