답안 #465897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
465897 2021-08-17T08:55:07 Z erray Construction of Highway (JOI18_construction) C++17
100 / 100
415 ms 21544 KB
// author: erray
#include <bits/stdc++.h>
 
using namespace std;
template<typename A, typename B> string to_string(const pair<A, B>& p);
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t);
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char& c) {
  return string("'") + c + "'";
}

string to_string(const char *c) {
  return to_string(string(c));
}

string to_string(const bool& b) {
  return (b ? "true" : "false");
}

string to_string(const vector<bool>& v) {
  string res = "{";
  for (int i = 0; i < (int) v.size(); ++i) {
    if (i > 0) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template<size_t T> string to_string(const bitset<T>& bs) {
  return bs.to_string();
}

template<typename T> string to_string(queue<T> q) {
  string res = "{";
  size_t sz = q.size();
  while (sz--) {
    T cur = q.front();
    q.pop();
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
  }
  res += "}";
  return res;
}

template<typename T, class C> string to_string(priority_queue<T, vector<T>, C> pq) {
  string res = "{";
  while (!pq.empty()) {
    T cur = pq.top();
    pq.pop();
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
  }
  res += "}";
  return res;
}

template<typename T> string to_string(const T& v) {
  string res = "{";
  for (auto &el : v) {
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(el);
  }
  res += "}";
  return res;
}

template<typename A, typename B> string to_string(const pair<A, B>& p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ')';
}

void debug_out(int size, bool first, string name) {
  vector<string> tmp = {name};
  vector<int> tmp2 = {size, first};
  cerr << endl;
}

constexpr int buffer_size = 255;

template<typename Head, typename... Tail> void debug_out(int size, bool first, string name, Head H, Tail... T) {
  string tmp;
  int off = 0;
  while ((!name.empty() && name[0] != ',') || off != 0) {
    tmp += name[0];
    name.erase(name.begin());
    char c = tmp.back();
    if (c == '{' || c == '(') {
      ++off;
    } else if (c == '}' || c == ')'){
      --off;
    }
  }
  if (!name.empty()) {
    name.erase(name.begin());
  }
  if (tmp[0] == ' ') {
    tmp.erase(tmp.begin());
  }

  string buff = to_string(H);
  if ((int) buff.size() + size + (int) tmp.size() > buffer_size - 5 && !first) {
    cerr << '\n';
    size = 0;
  }
  cerr << '[' << tmp << ": " << buff << "] ";
  debug_out(((int) buff.size() + size + (int) tmp.size() + 5) % buffer_size, false, name, T...);
}

#ifdef DEBUG
#define debug(...) cerr << "-> ", debug_out(3, true, string(#__VA_ARGS__), __VA_ARGS__)
#define here cerr << "-> " << __LINE__ << endl
#else
#define debug(...) void(37)
#define here void(37)
#endif

template<typename T> struct fenwick {
  int n;
  vector<T> tree;
  fenwick(int _n, T def = T{}) : n(_n) {
    tree.resize(n + 1);
    tree[0] = def;
  }

  void modify(int i, T x) {
    assert(i >= 0 && i < n);
    ++i;
    while (i <= n) {
      tree[i] += x;
      i += (i & -i);
    }
  }

  T get(int i) {
    assert(i >= 0 && i < n);
    ++i;
    T res = tree[0];
    while (i > 0) {
      res += tree[i];
      i -= (i & -i);
    }
    return res;
  }

  T get(int l, int r) {
    return get(r) - (l == 0 ? tree[0] : get(l - 1));
  }

  template<class F> int find_first(F op) {
    int cur = 0;
    T sum = tree[0];
    for (int i = __lg(n); i >= 0; --i) {
      if (cur + (1 << i) <= n && !op(sum + tree[cur + (1 << i)])) {
        cur += (1 << i);
        sum = sum + tree[cur];
      }
    }
    return (cur == n ? -1 : cur);
  }

  void clear() {
    tree.assign(n + 1, tree[0]);
  }
};
 
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> c(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }

  vector<int> sc = c;
  sort(sc.begin(), sc.end());
  sc.erase(unique(sc.begin(), sc.end()), sc.end());
  int c_size = int(sc.size()) + 1;
  for (int i = 0; i < n; ++i) {
    c[i] = int(lower_bound(sc.begin(), sc.end(), c[i]) - sc.begin()) + 1;
  }
  vector<vector<int>> g(n);
  vector<array<int, 2>> es(n - 1);
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    g[x].push_back(y);
    g[y].push_back(x);
    es[i] = {x, y};
  }
  
  vector<int> par(n, -1);
  vector<int> sz(n, 1);
  vector<int> ord;
  vector<int> d(n);
  function<void(int)> Dfs = [&](int v) {
    ord.push_back(v);
    for (auto& u : g[v]) {
      par[u] = v;
      g[u].erase(find(g[u].begin(), g[u].end(), v));
      d[u] = d[v] + 1;
      Dfs(u);
      sz[v] += sz[u];
      if (sz[u] > sz[g[v][0]]) {
        swap(u, g[v][0]);
      }    
    }
  };
  Dfs(0);
  debug(par, sz, ord, d);
  debug(c);
  vector<int> root(n);
  iota(root.begin(), root.end(), 0);
  for (auto v : ord) {
    if (!g[v].empty()) {
      root[g[v][0]] = root[v];
    }
  }

  debug(g, root);
  // number, size
  vector<vector<pair<int, int>>> ch(n);
  for (auto v : ord) {
    ch[root[v]].emplace_back(c[v], 1);
  }
  for (int i = 0; i < n; ++i) {
    reverse(ch[i].begin(), ch[i].end());
  }

  debug(ch);
  fenwick<int> inv(c_size);
  auto Apply = [&](int v) {
    vector<pair<int, int>> ns;
    int change = c[v];
    v = par[v];
    while (v != -1) {
      int size = d[v] - d[root[v]] + 1;
      v = root[v];
      int cur = 0;
      vector<pair<int, int>> pref;
      while (cur < size) {
        int dec = min(size - cur, ch[v].back().second);
        pref.emplace_back(ch[v].back().first, dec);
        if (dec == ch[v].back().second) {
          ch[v].pop_back();
        } else {
          ch[v].back().second -= dec;
        }
        cur += dec;
      }
      ns.insert(ns.end(), pref.rbegin(), pref.rend());
      ch[v].emplace_back(change, size);
      v = par[v];   
    }

    debug(ns);
    long long res = 0;
    for (auto[x, ct] : ns) {
      inv.modify(x, +ct);
      res += 1LL * ct * inv.get(x - 1);
    }
    for (auto[x, ct] : ns) {
      inv.modify(x, -ct);
    }
    return res;
  };

  for (auto[ignore, v] : es) {
    debug(v, c[v]);
    cout << Apply(v) << '\n';
    debug(ch);
  }
   
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 380 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 316 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 320 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 380 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 316 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 320 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 6 ms 840 KB Output is correct
47 Correct 8 ms 948 KB Output is correct
48 Correct 6 ms 944 KB Output is correct
49 Correct 5 ms 1096 KB Output is correct
50 Correct 6 ms 1100 KB Output is correct
51 Correct 4 ms 1100 KB Output is correct
52 Correct 6 ms 972 KB Output is correct
53 Correct 5 ms 1100 KB Output is correct
54 Correct 5 ms 1064 KB Output is correct
55 Correct 5 ms 1100 KB Output is correct
56 Correct 5 ms 972 KB Output is correct
57 Correct 8 ms 908 KB Output is correct
58 Correct 9 ms 856 KB Output is correct
59 Correct 9 ms 944 KB Output is correct
60 Correct 9 ms 840 KB Output is correct
61 Correct 5 ms 972 KB Output is correct
62 Correct 5 ms 972 KB Output is correct
63 Correct 6 ms 972 KB Output is correct
64 Correct 5 ms 836 KB Output is correct
65 Correct 5 ms 844 KB Output is correct
66 Correct 6 ms 844 KB Output is correct
67 Correct 6 ms 920 KB Output is correct
68 Correct 5 ms 1100 KB Output is correct
69 Correct 5 ms 972 KB Output is correct
70 Correct 4 ms 972 KB Output is correct
71 Correct 4 ms 972 KB Output is correct
72 Correct 8 ms 844 KB Output is correct
73 Correct 7 ms 844 KB Output is correct
74 Correct 4 ms 972 KB Output is correct
75 Correct 5 ms 972 KB Output is correct
76 Correct 5 ms 972 KB Output is correct
77 Correct 5 ms 972 KB Output is correct
78 Correct 4 ms 968 KB Output is correct
79 Correct 5 ms 892 KB Output is correct
80 Correct 4 ms 844 KB Output is correct
81 Correct 6 ms 1000 KB Output is correct
82 Correct 6 ms 972 KB Output is correct
83 Correct 5 ms 972 KB Output is correct
84 Correct 5 ms 844 KB Output is correct
85 Correct 4 ms 844 KB Output is correct
86 Correct 4 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 320 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 380 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 316 KB Output is correct
42 Correct 1 ms 320 KB Output is correct
43 Correct 1 ms 320 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 6 ms 840 KB Output is correct
47 Correct 8 ms 948 KB Output is correct
48 Correct 6 ms 944 KB Output is correct
49 Correct 5 ms 1096 KB Output is correct
50 Correct 6 ms 1100 KB Output is correct
51 Correct 4 ms 1100 KB Output is correct
52 Correct 6 ms 972 KB Output is correct
53 Correct 5 ms 1100 KB Output is correct
54 Correct 5 ms 1064 KB Output is correct
55 Correct 5 ms 1100 KB Output is correct
56 Correct 5 ms 972 KB Output is correct
57 Correct 8 ms 908 KB Output is correct
58 Correct 9 ms 856 KB Output is correct
59 Correct 9 ms 944 KB Output is correct
60 Correct 9 ms 840 KB Output is correct
61 Correct 5 ms 972 KB Output is correct
62 Correct 5 ms 972 KB Output is correct
63 Correct 6 ms 972 KB Output is correct
64 Correct 5 ms 836 KB Output is correct
65 Correct 5 ms 844 KB Output is correct
66 Correct 6 ms 844 KB Output is correct
67 Correct 6 ms 920 KB Output is correct
68 Correct 5 ms 1100 KB Output is correct
69 Correct 5 ms 972 KB Output is correct
70 Correct 4 ms 972 KB Output is correct
71 Correct 4 ms 972 KB Output is correct
72 Correct 8 ms 844 KB Output is correct
73 Correct 7 ms 844 KB Output is correct
74 Correct 4 ms 972 KB Output is correct
75 Correct 5 ms 972 KB Output is correct
76 Correct 5 ms 972 KB Output is correct
77 Correct 5 ms 972 KB Output is correct
78 Correct 4 ms 968 KB Output is correct
79 Correct 5 ms 892 KB Output is correct
80 Correct 4 ms 844 KB Output is correct
81 Correct 6 ms 1000 KB Output is correct
82 Correct 6 ms 972 KB Output is correct
83 Correct 5 ms 972 KB Output is correct
84 Correct 5 ms 844 KB Output is correct
85 Correct 4 ms 844 KB Output is correct
86 Correct 4 ms 844 KB Output is correct
87 Correct 17 ms 1868 KB Output is correct
88 Correct 57 ms 5140 KB Output is correct
89 Correct 252 ms 16372 KB Output is correct
90 Correct 237 ms 16476 KB Output is correct
91 Correct 253 ms 16556 KB Output is correct
92 Correct 126 ms 21324 KB Output is correct
93 Correct 123 ms 21544 KB Output is correct
94 Correct 120 ms 21404 KB Output is correct
95 Correct 124 ms 19896 KB Output is correct
96 Correct 123 ms 20284 KB Output is correct
97 Correct 131 ms 20272 KB Output is correct
98 Correct 130 ms 20280 KB Output is correct
99 Correct 127 ms 19652 KB Output is correct
100 Correct 296 ms 15548 KB Output is correct
101 Correct 346 ms 15984 KB Output is correct
102 Correct 344 ms 15988 KB Output is correct
103 Correct 415 ms 15932 KB Output is correct
104 Correct 145 ms 19516 KB Output is correct
105 Correct 133 ms 19508 KB Output is correct
106 Correct 136 ms 19516 KB Output is correct
107 Correct 151 ms 15172 KB Output is correct
108 Correct 185 ms 15336 KB Output is correct
109 Correct 225 ms 15784 KB Output is correct
110 Correct 107 ms 20672 KB Output is correct
111 Correct 124 ms 19888 KB Output is correct
112 Correct 104 ms 19268 KB Output is correct
113 Correct 104 ms 18492 KB Output is correct
114 Correct 300 ms 15700 KB Output is correct
115 Correct 293 ms 14844 KB Output is correct
116 Correct 113 ms 18488 KB Output is correct
117 Correct 129 ms 17632 KB Output is correct
118 Correct 136 ms 17072 KB Output is correct
119 Correct 139 ms 16900 KB Output is correct
120 Correct 117 ms 16828 KB Output is correct
121 Correct 112 ms 16444 KB Output is correct
122 Correct 114 ms 16128 KB Output is correct
123 Correct 164 ms 17712 KB Output is correct
124 Correct 179 ms 17212 KB Output is correct
125 Correct 148 ms 16956 KB Output is correct
126 Correct 130 ms 16956 KB Output is correct
127 Correct 122 ms 16444 KB Output is correct
128 Correct 129 ms 16268 KB Output is correct