답안 #109991

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109991 2019-05-08T14:25:54 Z IOrtroiii Construction of Highway (JOI18_construction) C++14
100 / 100
673 ms 104508 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100100;

struct Data {
   int from, to, color;
   Data(int from = 0,int to = 0,int color = 0) : from(from), to(to), color(color) {}
};

int n;
vector<int> g[N];
int from[N], to[N], color[N];
int child[N], nxt[N], root[N];
int par[N], pos[N], ft[N];
deque<Data> dq[N];

void add(int p,int x) { for (; p <= n; p += p & -p) ft[p] += x; }
int get(int p) { int ans = 0; for (; p > 0; p -= p & -p) ans += ft[p]; return ans; }

void dfs(int u) {
   child[u] = 1;
   for (int v : g[u]) {
      par[v] = u;
      dfs(v);
      child[u] += child[v];
   }
}

void hld(int u,int root) {
   ::root[u] = root;
   if (u == root) pos[u] = 1;
   int nxt = 0;
   for (int v : g[u]) {
      if (child[v] > child[nxt]) nxt = v;
   }
   if (nxt) {
      pos[nxt] = pos[u] + 1;
      hld(nxt, root);
   }
   for (int v : g[u]) if (v != nxt) {
      hld(v, v);
   }
}

void get_hld(int u) {
   long long ans = 0;
   vector< pair<int, int> > change;
   while (u) {
      int p = pos[u];
      u = root[u];
      int ptr = 0;
      while (ptr < dq[u].size() && dq[u][ptr].from <= p) ptr++;
      ptr--;
      for (int i = ptr; i >= 0; --i) {
         int cnt = min(p, dq[u][i].to) - dq[u][i].from + 1;
         ans += (long long) cnt * get(dq[u][i].color - 1);
         add(dq[u][i].color, cnt);
         change.push_back({dq[u][i].color, cnt});
      }
      u = par[u];
   }

   for (auto p : change) add(p.first, -p.second);

   printf("%lld\n", ans);
}

void upd_hld(int u,int color) {
   while (u) {
      int p = pos[u];
      u = root[u];
      while (dq[u].size()) {
         if (dq[u][0].to <= p) dq[u].pop_front();
         else { dq[u][0].from = p + 1;  break; }
      }
      dq[u].push_front(Data(1, p, color));
      u = par[u];
   }
}

int main() {
   scanf("%d", &n);
   for (int i = 1; i <= n; ++i) {
      scanf("%d", color + i);
   }
   {
      vector<int> comp;
      for (int i = 1; i <= n; ++i) comp.push_back(color[i]);
      sort(comp.begin(), comp.end());
      comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
      for (int i = 1; i <= n; ++i) color[i] = lower_bound(comp.begin(), comp.end(), color[i]) - comp.begin() + 1;
   }
   for (int i = 1; i < n; ++i) {
      scanf("%d %d", from + i, to + i);
      g[from[i]].push_back(to[i]);
   }
   dfs(1);
   hld(1, 1);
   for (int i = 1; i < n; ++i) {
      get_hld(from[i]);
      upd_hld(to[i], color[to[i]]);
   }
}

Compilation message

construction.cpp: In function 'void get_hld(int)':
construction.cpp:54:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       while (ptr < dq[u].size() && dq[u][ptr].from <= p) ptr++;
              ~~~~^~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:84:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &n);
    ~~~~~^~~~~~~~~~
construction.cpp:86:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", color + i);
       ~~~~~^~~~~~~~~~~~~~~~~
construction.cpp:96:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d", from + i, to + i);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 68600 KB Output is correct
2 Correct 75 ms 68472 KB Output is correct
3 Correct 101 ms 68572 KB Output is correct
4 Correct 82 ms 68600 KB Output is correct
5 Correct 92 ms 68724 KB Output is correct
6 Correct 84 ms 68600 KB Output is correct
7 Correct 82 ms 68732 KB Output is correct
8 Correct 76 ms 68604 KB Output is correct
9 Correct 72 ms 68540 KB Output is correct
10 Correct 72 ms 68600 KB Output is correct
11 Correct 82 ms 68728 KB Output is correct
12 Correct 90 ms 68856 KB Output is correct
13 Correct 88 ms 68728 KB Output is correct
14 Correct 80 ms 68728 KB Output is correct
15 Correct 85 ms 68728 KB Output is correct
16 Correct 84 ms 68728 KB Output is correct
17 Correct 81 ms 68740 KB Output is correct
18 Correct 80 ms 68728 KB Output is correct
19 Correct 85 ms 68768 KB Output is correct
20 Correct 90 ms 68728 KB Output is correct
21 Correct 93 ms 68728 KB Output is correct
22 Correct 82 ms 68828 KB Output is correct
23 Correct 92 ms 68728 KB Output is correct
24 Correct 92 ms 68700 KB Output is correct
25 Correct 87 ms 68600 KB Output is correct
26 Correct 79 ms 68572 KB Output is correct
27 Correct 94 ms 68728 KB Output is correct
28 Correct 80 ms 68728 KB Output is correct
29 Correct 86 ms 68856 KB Output is correct
30 Correct 83 ms 68728 KB Output is correct
31 Correct 84 ms 68784 KB Output is correct
32 Correct 89 ms 68700 KB Output is correct
33 Correct 89 ms 68856 KB Output is correct
34 Correct 91 ms 68728 KB Output is correct
35 Correct 81 ms 68728 KB Output is correct
36 Correct 108 ms 68832 KB Output is correct
37 Correct 83 ms 68728 KB Output is correct
38 Correct 80 ms 68728 KB Output is correct
39 Correct 85 ms 68720 KB Output is correct
40 Correct 85 ms 68728 KB Output is correct
41 Correct 84 ms 68660 KB Output is correct
42 Correct 86 ms 68728 KB Output is correct
43 Correct 85 ms 68776 KB Output is correct
44 Correct 87 ms 68712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 68600 KB Output is correct
2 Correct 75 ms 68472 KB Output is correct
3 Correct 101 ms 68572 KB Output is correct
4 Correct 82 ms 68600 KB Output is correct
5 Correct 92 ms 68724 KB Output is correct
6 Correct 84 ms 68600 KB Output is correct
7 Correct 82 ms 68732 KB Output is correct
8 Correct 76 ms 68604 KB Output is correct
9 Correct 72 ms 68540 KB Output is correct
10 Correct 72 ms 68600 KB Output is correct
11 Correct 82 ms 68728 KB Output is correct
12 Correct 90 ms 68856 KB Output is correct
13 Correct 88 ms 68728 KB Output is correct
14 Correct 80 ms 68728 KB Output is correct
15 Correct 85 ms 68728 KB Output is correct
16 Correct 84 ms 68728 KB Output is correct
17 Correct 81 ms 68740 KB Output is correct
18 Correct 80 ms 68728 KB Output is correct
19 Correct 85 ms 68768 KB Output is correct
20 Correct 90 ms 68728 KB Output is correct
21 Correct 93 ms 68728 KB Output is correct
22 Correct 82 ms 68828 KB Output is correct
23 Correct 92 ms 68728 KB Output is correct
24 Correct 92 ms 68700 KB Output is correct
25 Correct 87 ms 68600 KB Output is correct
26 Correct 79 ms 68572 KB Output is correct
27 Correct 94 ms 68728 KB Output is correct
28 Correct 80 ms 68728 KB Output is correct
29 Correct 86 ms 68856 KB Output is correct
30 Correct 83 ms 68728 KB Output is correct
31 Correct 84 ms 68784 KB Output is correct
32 Correct 89 ms 68700 KB Output is correct
33 Correct 89 ms 68856 KB Output is correct
34 Correct 91 ms 68728 KB Output is correct
35 Correct 81 ms 68728 KB Output is correct
36 Correct 108 ms 68832 KB Output is correct
37 Correct 83 ms 68728 KB Output is correct
38 Correct 80 ms 68728 KB Output is correct
39 Correct 85 ms 68720 KB Output is correct
40 Correct 85 ms 68728 KB Output is correct
41 Correct 84 ms 68660 KB Output is correct
42 Correct 86 ms 68728 KB Output is correct
43 Correct 85 ms 68776 KB Output is correct
44 Correct 87 ms 68712 KB Output is correct
45 Correct 99 ms 68960 KB Output is correct
46 Correct 88 ms 69756 KB Output is correct
47 Correct 88 ms 69816 KB Output is correct
48 Correct 95 ms 69880 KB Output is correct
49 Correct 82 ms 68984 KB Output is correct
50 Correct 83 ms 68984 KB Output is correct
51 Correct 80 ms 69084 KB Output is correct
52 Correct 88 ms 69880 KB Output is correct
53 Correct 94 ms 70008 KB Output is correct
54 Correct 91 ms 70008 KB Output is correct
55 Correct 88 ms 70008 KB Output is correct
56 Correct 86 ms 69852 KB Output is correct
57 Correct 103 ms 69880 KB Output is correct
58 Correct 96 ms 69752 KB Output is correct
59 Correct 91 ms 69724 KB Output is correct
60 Correct 88 ms 69880 KB Output is correct
61 Correct 95 ms 69880 KB Output is correct
62 Correct 83 ms 69980 KB Output is correct
63 Correct 86 ms 69880 KB Output is correct
64 Correct 91 ms 69880 KB Output is correct
65 Correct 99 ms 69880 KB Output is correct
66 Correct 88 ms 69752 KB Output is correct
67 Correct 95 ms 69752 KB Output is correct
68 Correct 81 ms 69112 KB Output is correct
69 Correct 90 ms 69880 KB Output is correct
70 Correct 89 ms 69916 KB Output is correct
71 Correct 90 ms 69880 KB Output is correct
72 Correct 98 ms 69760 KB Output is correct
73 Correct 105 ms 69704 KB Output is correct
74 Correct 84 ms 69880 KB Output is correct
75 Correct 94 ms 69916 KB Output is correct
76 Correct 107 ms 69852 KB Output is correct
77 Correct 91 ms 69856 KB Output is correct
78 Correct 92 ms 69868 KB Output is correct
79 Correct 92 ms 69936 KB Output is correct
80 Correct 92 ms 69752 KB Output is correct
81 Correct 91 ms 69880 KB Output is correct
82 Correct 88 ms 69852 KB Output is correct
83 Correct 83 ms 70008 KB Output is correct
84 Correct 91 ms 69880 KB Output is correct
85 Correct 96 ms 69828 KB Output is correct
86 Correct 95 ms 69752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 68600 KB Output is correct
2 Correct 75 ms 68472 KB Output is correct
3 Correct 101 ms 68572 KB Output is correct
4 Correct 82 ms 68600 KB Output is correct
5 Correct 92 ms 68724 KB Output is correct
6 Correct 84 ms 68600 KB Output is correct
7 Correct 82 ms 68732 KB Output is correct
8 Correct 76 ms 68604 KB Output is correct
9 Correct 72 ms 68540 KB Output is correct
10 Correct 72 ms 68600 KB Output is correct
11 Correct 82 ms 68728 KB Output is correct
12 Correct 90 ms 68856 KB Output is correct
13 Correct 88 ms 68728 KB Output is correct
14 Correct 80 ms 68728 KB Output is correct
15 Correct 85 ms 68728 KB Output is correct
16 Correct 84 ms 68728 KB Output is correct
17 Correct 81 ms 68740 KB Output is correct
18 Correct 80 ms 68728 KB Output is correct
19 Correct 85 ms 68768 KB Output is correct
20 Correct 90 ms 68728 KB Output is correct
21 Correct 93 ms 68728 KB Output is correct
22 Correct 82 ms 68828 KB Output is correct
23 Correct 92 ms 68728 KB Output is correct
24 Correct 92 ms 68700 KB Output is correct
25 Correct 87 ms 68600 KB Output is correct
26 Correct 79 ms 68572 KB Output is correct
27 Correct 94 ms 68728 KB Output is correct
28 Correct 80 ms 68728 KB Output is correct
29 Correct 86 ms 68856 KB Output is correct
30 Correct 83 ms 68728 KB Output is correct
31 Correct 84 ms 68784 KB Output is correct
32 Correct 89 ms 68700 KB Output is correct
33 Correct 89 ms 68856 KB Output is correct
34 Correct 91 ms 68728 KB Output is correct
35 Correct 81 ms 68728 KB Output is correct
36 Correct 108 ms 68832 KB Output is correct
37 Correct 83 ms 68728 KB Output is correct
38 Correct 80 ms 68728 KB Output is correct
39 Correct 85 ms 68720 KB Output is correct
40 Correct 85 ms 68728 KB Output is correct
41 Correct 84 ms 68660 KB Output is correct
42 Correct 86 ms 68728 KB Output is correct
43 Correct 85 ms 68776 KB Output is correct
44 Correct 87 ms 68712 KB Output is correct
45 Correct 99 ms 68960 KB Output is correct
46 Correct 88 ms 69756 KB Output is correct
47 Correct 88 ms 69816 KB Output is correct
48 Correct 95 ms 69880 KB Output is correct
49 Correct 82 ms 68984 KB Output is correct
50 Correct 83 ms 68984 KB Output is correct
51 Correct 80 ms 69084 KB Output is correct
52 Correct 88 ms 69880 KB Output is correct
53 Correct 94 ms 70008 KB Output is correct
54 Correct 91 ms 70008 KB Output is correct
55 Correct 88 ms 70008 KB Output is correct
56 Correct 86 ms 69852 KB Output is correct
57 Correct 103 ms 69880 KB Output is correct
58 Correct 96 ms 69752 KB Output is correct
59 Correct 91 ms 69724 KB Output is correct
60 Correct 88 ms 69880 KB Output is correct
61 Correct 95 ms 69880 KB Output is correct
62 Correct 83 ms 69980 KB Output is correct
63 Correct 86 ms 69880 KB Output is correct
64 Correct 91 ms 69880 KB Output is correct
65 Correct 99 ms 69880 KB Output is correct
66 Correct 88 ms 69752 KB Output is correct
67 Correct 95 ms 69752 KB Output is correct
68 Correct 81 ms 69112 KB Output is correct
69 Correct 90 ms 69880 KB Output is correct
70 Correct 89 ms 69916 KB Output is correct
71 Correct 90 ms 69880 KB Output is correct
72 Correct 98 ms 69760 KB Output is correct
73 Correct 105 ms 69704 KB Output is correct
74 Correct 84 ms 69880 KB Output is correct
75 Correct 94 ms 69916 KB Output is correct
76 Correct 107 ms 69852 KB Output is correct
77 Correct 91 ms 69856 KB Output is correct
78 Correct 92 ms 69868 KB Output is correct
79 Correct 92 ms 69936 KB Output is correct
80 Correct 92 ms 69752 KB Output is correct
81 Correct 91 ms 69880 KB Output is correct
82 Correct 88 ms 69852 KB Output is correct
83 Correct 83 ms 70008 KB Output is correct
84 Correct 91 ms 69880 KB Output is correct
85 Correct 96 ms 69828 KB Output is correct
86 Correct 95 ms 69752 KB Output is correct
87 Correct 102 ms 71772 KB Output is correct
88 Correct 171 ms 78444 KB Output is correct
89 Correct 556 ms 100900 KB Output is correct
90 Correct 520 ms 100828 KB Output is correct
91 Correct 582 ms 100836 KB Output is correct
92 Correct 299 ms 81752 KB Output is correct
93 Correct 258 ms 81884 KB Output is correct
94 Correct 241 ms 82088 KB Output is correct
95 Correct 307 ms 104128 KB Output is correct
96 Correct 338 ms 104468 KB Output is correct
97 Correct 297 ms 104508 KB Output is correct
98 Correct 281 ms 104408 KB Output is correct
99 Correct 269 ms 103000 KB Output is correct
100 Correct 481 ms 93280 KB Output is correct
101 Correct 673 ms 93520 KB Output is correct
102 Correct 592 ms 93400 KB Output is correct
103 Correct 650 ms 93604 KB Output is correct
104 Correct 335 ms 103100 KB Output is correct
105 Correct 345 ms 103132 KB Output is correct
106 Correct 311 ms 103172 KB Output is correct
107 Correct 400 ms 99676 KB Output is correct
108 Correct 439 ms 99896 KB Output is correct
109 Correct 524 ms 100188 KB Output is correct
110 Correct 221 ms 80856 KB Output is correct
111 Correct 263 ms 104092 KB Output is correct
112 Correct 260 ms 103636 KB Output is correct
113 Correct 293 ms 102048 KB Output is correct
114 Correct 542 ms 93216 KB Output is correct
115 Correct 481 ms 92408 KB Output is correct
116 Correct 288 ms 102248 KB Output is correct
117 Correct 305 ms 101468 KB Output is correct
118 Correct 273 ms 101208 KB Output is correct
119 Correct 267 ms 100952 KB Output is correct
120 Correct 247 ms 101080 KB Output is correct
121 Correct 286 ms 100628 KB Output is correct
122 Correct 249 ms 100208 KB Output is correct
123 Correct 280 ms 101724 KB Output is correct
124 Correct 334 ms 101184 KB Output is correct
125 Correct 303 ms 101212 KB Output is correct
126 Correct 329 ms 100880 KB Output is correct
127 Correct 337 ms 100620 KB Output is correct
128 Correct 337 ms 100488 KB Output is correct