Submission #441129

# Submission time Handle Problem Language Result Execution time Memory
441129 2021-07-04T10:43:38 Z peijar Construction of Highway (JOI18_construction) C++17
100 / 100
679 ms 30420 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

template <class T> class Fenwick {
public:
  int lim;
  vector<T> bit;

  Fenwick(int n) : lim(n + 1), bit(lim) {}

  void upd(int pos, T val) {
    for (pos++; pos < lim; pos += pos & -pos)
      bit[pos] += val;
  }

  T sum(int r) { // < r
    T ret = 0;
    for (; r; r -= r & -r)
      ret += bit[r];
    return ret;
  }

  T sum(int l, int r) { // [l, r)
    return sum(r) - sum(l);
  }
};

const int MAXN = 1e5;

vector<int> adj[MAXN];
int par[MAXN], root[MAXN], sz[MAXN], depth[MAXN], heavy[MAXN];
map<pair<int, int>, int> segs[MAXN];
int nbSommets;

Fenwick<int> fen(MAXN);

void dfs(int u) {
  sz[u] = 1;
  for (int v : adj[u]) {
    depth[v] = depth[u] + 1;
    dfs(v);
    sz[u] += sz[v];
  }

  heavy[u] = -1;
  for (int v : adj[u])
    if (2 * sz[v] >= sz[u])
      heavy[u] = v;
}

void dfs2(int u) {
  for (int v : adj[u]) {
    if (heavy[u] == v)
      root[v] = root[u];
    else
      root[v] = v;
    dfs2(v);
  }
}

int query(int iNoeud) { // Go up from iNoeud !
  vector<pair<int, int>> toErase;
  int ret = 0;

  auto pushChange = [&]() {
    auto it = segs[root[iNoeud]].upper_bound(make_pair(depth[iNoeud], 1e18));
    while (it != segs[root[iNoeud]].begin()) {
      it--;
      auto [deb, fin] = it->first;
      int val = it->second;
      int cb = min(depth[iNoeud], fin) - deb + 1;
      ret += cb * fen.sum(val);
      fen.upd(val, cb);
      toErase.emplace_back(val, cb);
    }
  };

  while (root[iNoeud]) {
    pushChange();
    iNoeud = par[root[iNoeud]];
  }
  pushChange();
  for (auto [val, cb] : toErase)
    fen.upd(val, -cb);
  return ret;
}

void upd(int iNoeud, int val) { // change all values above iNoeud !

  auto pullChange = [&]() {
    while (!segs[root[iNoeud]].empty() and
           segs[root[iNoeud]].begin()->first.second <= depth[iNoeud])
      segs[root[iNoeud]].erase(segs[root[iNoeud]].begin());
    if (!segs[root[iNoeud]].empty() and
        depth[iNoeud] < segs[root[iNoeud]].begin()->first.second) {
      auto [deb, fin] = segs[root[iNoeud]].begin()->first;
      auto v = segs[root[iNoeud]].begin()->second;
      segs[root[iNoeud]].erase(segs[root[iNoeud]].begin());
      segs[root[iNoeud]][make_pair(depth[iNoeud] + 1, fin)] = v;
    }
    segs[root[iNoeud]][make_pair(depth[root[iNoeud]], depth[iNoeud])] = val;
  };
  while (root[iNoeud]) {
    pullChange();
    iNoeud = par[root[iNoeud]];
  }
  pullChange();
}

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> nbSommets;
  vector<int> distinct(nbSommets);
  vector<int> liveliness(nbSommets);
  for (int i = 0; i < nbSommets; ++i) {
    cin >> liveliness[i];
    distinct[i] = liveliness[i];
  }
  sort(distinct.begin(), distinct.end());
  distinct.resize(unique(distinct.begin(), distinct.end()) - distinct.begin());

  for (int i = 0; i < nbSommets; ++i)
    liveliness[i] =
        lower_bound(distinct.begin(), distinct.end(), liveliness[i]) -
        distinct.begin();
  vector<pair<int, int>> edges(nbSommets - 1);
  for (auto &[A, B] : edges) {
    cin >> A >> B;
    --A, --B;
    par[B] = A;
    adj[A].push_back(B);
  }
  dfs(0);
  dfs2(0);

  segs[0][{0, 0}] = liveliness[0];

  for (auto [a, b] : edges) {
    cout << query(a) << '\n';
    upd(b, liveliness[b]);
    /*cout << "==============" << endl;
    for (int i = 0; i < nbSommets; ++i)
      if (i == root[i]) {
        cout << i + 1 << ": ";
        for (auto it : segs[i])
          cout << it.first.first << ' ' << it.first.second << " = "
               << it.second + 1 << ' ';
        cout << endl;
      }*/
  }
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8172 KB Output is correct
3 Correct 5 ms 8172 KB Output is correct
4 Correct 6 ms 8140 KB Output is correct
5 Correct 6 ms 8140 KB Output is correct
6 Correct 6 ms 8176 KB Output is correct
7 Correct 6 ms 8172 KB Output is correct
8 Correct 7 ms 8268 KB Output is correct
9 Correct 6 ms 8268 KB Output is correct
10 Correct 6 ms 8184 KB Output is correct
11 Correct 6 ms 8268 KB Output is correct
12 Correct 6 ms 8268 KB Output is correct
13 Correct 6 ms 8268 KB Output is correct
14 Correct 6 ms 8268 KB Output is correct
15 Correct 6 ms 8140 KB Output is correct
16 Correct 7 ms 8268 KB Output is correct
17 Correct 7 ms 8268 KB Output is correct
18 Correct 6 ms 8268 KB Output is correct
19 Correct 6 ms 8268 KB Output is correct
20 Correct 6 ms 8268 KB Output is correct
21 Correct 6 ms 8268 KB Output is correct
22 Correct 6 ms 8140 KB Output is correct
23 Correct 7 ms 8140 KB Output is correct
24 Correct 6 ms 8176 KB Output is correct
25 Correct 6 ms 8172 KB Output is correct
26 Correct 6 ms 8268 KB Output is correct
27 Correct 6 ms 8268 KB Output is correct
28 Correct 6 ms 8268 KB Output is correct
29 Correct 6 ms 8268 KB Output is correct
30 Correct 6 ms 8140 KB Output is correct
31 Correct 7 ms 8140 KB Output is correct
32 Correct 6 ms 8140 KB Output is correct
33 Correct 6 ms 8140 KB Output is correct
34 Correct 6 ms 8140 KB Output is correct
35 Correct 6 ms 8140 KB Output is correct
36 Correct 6 ms 8140 KB Output is correct
37 Correct 6 ms 8140 KB Output is correct
38 Correct 6 ms 8140 KB Output is correct
39 Correct 6 ms 8140 KB Output is correct
40 Correct 6 ms 8172 KB Output is correct
41 Correct 6 ms 8140 KB Output is correct
42 Correct 6 ms 8140 KB Output is correct
43 Correct 6 ms 8140 KB Output is correct
44 Correct 7 ms 8140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8172 KB Output is correct
3 Correct 5 ms 8172 KB Output is correct
4 Correct 6 ms 8140 KB Output is correct
5 Correct 6 ms 8140 KB Output is correct
6 Correct 6 ms 8176 KB Output is correct
7 Correct 6 ms 8172 KB Output is correct
8 Correct 7 ms 8268 KB Output is correct
9 Correct 6 ms 8268 KB Output is correct
10 Correct 6 ms 8184 KB Output is correct
11 Correct 6 ms 8268 KB Output is correct
12 Correct 6 ms 8268 KB Output is correct
13 Correct 6 ms 8268 KB Output is correct
14 Correct 6 ms 8268 KB Output is correct
15 Correct 6 ms 8140 KB Output is correct
16 Correct 7 ms 8268 KB Output is correct
17 Correct 7 ms 8268 KB Output is correct
18 Correct 6 ms 8268 KB Output is correct
19 Correct 6 ms 8268 KB Output is correct
20 Correct 6 ms 8268 KB Output is correct
21 Correct 6 ms 8268 KB Output is correct
22 Correct 6 ms 8140 KB Output is correct
23 Correct 7 ms 8140 KB Output is correct
24 Correct 6 ms 8176 KB Output is correct
25 Correct 6 ms 8172 KB Output is correct
26 Correct 6 ms 8268 KB Output is correct
27 Correct 6 ms 8268 KB Output is correct
28 Correct 6 ms 8268 KB Output is correct
29 Correct 6 ms 8268 KB Output is correct
30 Correct 6 ms 8140 KB Output is correct
31 Correct 7 ms 8140 KB Output is correct
32 Correct 6 ms 8140 KB Output is correct
33 Correct 6 ms 8140 KB Output is correct
34 Correct 6 ms 8140 KB Output is correct
35 Correct 6 ms 8140 KB Output is correct
36 Correct 6 ms 8140 KB Output is correct
37 Correct 6 ms 8140 KB Output is correct
38 Correct 6 ms 8140 KB Output is correct
39 Correct 6 ms 8140 KB Output is correct
40 Correct 6 ms 8172 KB Output is correct
41 Correct 6 ms 8140 KB Output is correct
42 Correct 6 ms 8140 KB Output is correct
43 Correct 6 ms 8140 KB Output is correct
44 Correct 7 ms 8140 KB Output is correct
45 Correct 7 ms 8312 KB Output is correct
46 Correct 13 ms 8748 KB Output is correct
47 Correct 13 ms 8784 KB Output is correct
48 Correct 13 ms 8688 KB Output is correct
49 Correct 9 ms 8920 KB Output is correct
50 Correct 10 ms 8968 KB Output is correct
51 Correct 9 ms 8920 KB Output is correct
52 Correct 10 ms 9048 KB Output is correct
53 Correct 10 ms 9048 KB Output is correct
54 Correct 10 ms 9048 KB Output is correct
55 Correct 10 ms 8960 KB Output is correct
56 Correct 10 ms 8792 KB Output is correct
57 Correct 16 ms 8792 KB Output is correct
58 Correct 17 ms 8764 KB Output is correct
59 Correct 17 ms 8788 KB Output is correct
60 Correct 16 ms 8832 KB Output is correct
61 Correct 10 ms 8836 KB Output is correct
62 Correct 10 ms 8792 KB Output is correct
63 Correct 10 ms 8916 KB Output is correct
64 Correct 12 ms 8760 KB Output is correct
65 Correct 13 ms 8704 KB Output is correct
66 Correct 14 ms 8664 KB Output is correct
67 Correct 13 ms 8776 KB Output is correct
68 Correct 8 ms 8948 KB Output is correct
69 Correct 10 ms 9048 KB Output is correct
70 Correct 10 ms 8972 KB Output is correct
71 Correct 9 ms 8840 KB Output is correct
72 Correct 16 ms 8840 KB Output is correct
73 Correct 20 ms 8792 KB Output is correct
74 Correct 10 ms 8792 KB Output is correct
75 Correct 10 ms 8792 KB Output is correct
76 Correct 10 ms 8792 KB Output is correct
77 Correct 10 ms 8776 KB Output is correct
78 Correct 9 ms 8708 KB Output is correct
79 Correct 10 ms 8772 KB Output is correct
80 Correct 10 ms 8664 KB Output is correct
81 Correct 10 ms 8768 KB Output is correct
82 Correct 11 ms 8820 KB Output is correct
83 Correct 11 ms 8664 KB Output is correct
84 Correct 10 ms 8792 KB Output is correct
85 Correct 10 ms 8664 KB Output is correct
86 Correct 10 ms 8664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8172 KB Output is correct
3 Correct 5 ms 8172 KB Output is correct
4 Correct 6 ms 8140 KB Output is correct
5 Correct 6 ms 8140 KB Output is correct
6 Correct 6 ms 8176 KB Output is correct
7 Correct 6 ms 8172 KB Output is correct
8 Correct 7 ms 8268 KB Output is correct
9 Correct 6 ms 8268 KB Output is correct
10 Correct 6 ms 8184 KB Output is correct
11 Correct 6 ms 8268 KB Output is correct
12 Correct 6 ms 8268 KB Output is correct
13 Correct 6 ms 8268 KB Output is correct
14 Correct 6 ms 8268 KB Output is correct
15 Correct 6 ms 8140 KB Output is correct
16 Correct 7 ms 8268 KB Output is correct
17 Correct 7 ms 8268 KB Output is correct
18 Correct 6 ms 8268 KB Output is correct
19 Correct 6 ms 8268 KB Output is correct
20 Correct 6 ms 8268 KB Output is correct
21 Correct 6 ms 8268 KB Output is correct
22 Correct 6 ms 8140 KB Output is correct
23 Correct 7 ms 8140 KB Output is correct
24 Correct 6 ms 8176 KB Output is correct
25 Correct 6 ms 8172 KB Output is correct
26 Correct 6 ms 8268 KB Output is correct
27 Correct 6 ms 8268 KB Output is correct
28 Correct 6 ms 8268 KB Output is correct
29 Correct 6 ms 8268 KB Output is correct
30 Correct 6 ms 8140 KB Output is correct
31 Correct 7 ms 8140 KB Output is correct
32 Correct 6 ms 8140 KB Output is correct
33 Correct 6 ms 8140 KB Output is correct
34 Correct 6 ms 8140 KB Output is correct
35 Correct 6 ms 8140 KB Output is correct
36 Correct 6 ms 8140 KB Output is correct
37 Correct 6 ms 8140 KB Output is correct
38 Correct 6 ms 8140 KB Output is correct
39 Correct 6 ms 8140 KB Output is correct
40 Correct 6 ms 8172 KB Output is correct
41 Correct 6 ms 8140 KB Output is correct
42 Correct 6 ms 8140 KB Output is correct
43 Correct 6 ms 8140 KB Output is correct
44 Correct 7 ms 8140 KB Output is correct
45 Correct 7 ms 8312 KB Output is correct
46 Correct 13 ms 8748 KB Output is correct
47 Correct 13 ms 8784 KB Output is correct
48 Correct 13 ms 8688 KB Output is correct
49 Correct 9 ms 8920 KB Output is correct
50 Correct 10 ms 8968 KB Output is correct
51 Correct 9 ms 8920 KB Output is correct
52 Correct 10 ms 9048 KB Output is correct
53 Correct 10 ms 9048 KB Output is correct
54 Correct 10 ms 9048 KB Output is correct
55 Correct 10 ms 8960 KB Output is correct
56 Correct 10 ms 8792 KB Output is correct
57 Correct 16 ms 8792 KB Output is correct
58 Correct 17 ms 8764 KB Output is correct
59 Correct 17 ms 8788 KB Output is correct
60 Correct 16 ms 8832 KB Output is correct
61 Correct 10 ms 8836 KB Output is correct
62 Correct 10 ms 8792 KB Output is correct
63 Correct 10 ms 8916 KB Output is correct
64 Correct 12 ms 8760 KB Output is correct
65 Correct 13 ms 8704 KB Output is correct
66 Correct 14 ms 8664 KB Output is correct
67 Correct 13 ms 8776 KB Output is correct
68 Correct 8 ms 8948 KB Output is correct
69 Correct 10 ms 9048 KB Output is correct
70 Correct 10 ms 8972 KB Output is correct
71 Correct 9 ms 8840 KB Output is correct
72 Correct 16 ms 8840 KB Output is correct
73 Correct 20 ms 8792 KB Output is correct
74 Correct 10 ms 8792 KB Output is correct
75 Correct 10 ms 8792 KB Output is correct
76 Correct 10 ms 8792 KB Output is correct
77 Correct 10 ms 8776 KB Output is correct
78 Correct 9 ms 8708 KB Output is correct
79 Correct 10 ms 8772 KB Output is correct
80 Correct 10 ms 8664 KB Output is correct
81 Correct 10 ms 8768 KB Output is correct
82 Correct 11 ms 8820 KB Output is correct
83 Correct 11 ms 8664 KB Output is correct
84 Correct 10 ms 8792 KB Output is correct
85 Correct 10 ms 8664 KB Output is correct
86 Correct 10 ms 8664 KB Output is correct
87 Correct 27 ms 9688 KB Output is correct
88 Correct 77 ms 12724 KB Output is correct
89 Correct 355 ms 23512 KB Output is correct
90 Correct 339 ms 23544 KB Output is correct
91 Correct 346 ms 23492 KB Output is correct
92 Correct 119 ms 28408 KB Output is correct
93 Correct 120 ms 28356 KB Output is correct
94 Correct 121 ms 28356 KB Output is correct
95 Correct 157 ms 30008 KB Output is correct
96 Correct 172 ms 30420 KB Output is correct
97 Correct 201 ms 30376 KB Output is correct
98 Correct 162 ms 30404 KB Output is correct
99 Correct 138 ms 26252 KB Output is correct
100 Correct 545 ms 23432 KB Output is correct
101 Correct 679 ms 24036 KB Output is correct
102 Correct 587 ms 23876 KB Output is correct
103 Correct 575 ms 23816 KB Output is correct
104 Correct 152 ms 26180 KB Output is correct
105 Correct 152 ms 26180 KB Output is correct
106 Correct 163 ms 26180 KB Output is correct
107 Correct 314 ms 22640 KB Output is correct
108 Correct 311 ms 22848 KB Output is correct
109 Correct 346 ms 23236 KB Output is correct
110 Correct 110 ms 27752 KB Output is correct
111 Correct 155 ms 29944 KB Output is correct
112 Correct 147 ms 29884 KB Output is correct
113 Correct 128 ms 25540 KB Output is correct
114 Correct 490 ms 23520 KB Output is correct
115 Correct 514 ms 23220 KB Output is correct
116 Correct 136 ms 25520 KB Output is correct
117 Correct 147 ms 23912 KB Output is correct
118 Correct 147 ms 23276 KB Output is correct
119 Correct 144 ms 22852 KB Output is correct
120 Correct 126 ms 23548 KB Output is correct
121 Correct 143 ms 23100 KB Output is correct
122 Correct 178 ms 22596 KB Output is correct
123 Correct 161 ms 23940 KB Output is correct
124 Correct 173 ms 23348 KB Output is correct
125 Correct 168 ms 22980 KB Output is correct
126 Correct 158 ms 23696 KB Output is correct
127 Correct 187 ms 23036 KB Output is correct
128 Correct 170 ms 22720 KB Output is correct