Submission #330170

# Submission time Handle Problem Language Result Execution time Memory
330170 2020-11-24T05:39:05 Z VROOM_VARUN Construction of Highway (JOI18_construction) C++14
100 / 100
437 ms 27488 KB
/*
ID: varunra2
LANG: C++
TASK: highways
*/

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int n;
VVI adj;
VI wch;   // which chain
VI pos;   // which position in the chain
VI sub;   // subtree size
VI dep;   // distance from the root
VI live;  // liveliness
VII edgs;

void init() {
  adj.resize(n);
  wch.resize(n);
  pos.resize(n);
  sub.resize(n);
  dep.resize(n);
  live.resize(n);
  edgs.resize(n - 1);
}

struct chain {
  VII vals;
  int ind;
  int siz;
  int par;

  void init(int _ind, int _par) {
    ind = _ind;
    par = _par;
    siz = 0;
  }

  void ins(int node) {
    if (vals.empty())
      vals.PB(MP(live[node], 1));
    else if (vals.back().x == live[node])
      vals.back().y++;
    else
      vals.PB(MP(live[node], 1));
    siz++;
    wch[node] = ind;
    pos[node] = siz;
  }

  void done() {
    // call this when you are done with this chain
    reverse(all(vals));
  }

  VII get(int st, int val) {
    // get from 0 -> st
    VII ret;
    int sum = 0;
    while (!vals.empty()) {
      auto x = vals.back();
      int sum1 = sum + x.y;
      if (st <= sum1) {
        ret.PB(MP(x.x, st - sum));
        vals.back().y -= (st - sum);
        if (vals.back().y == 0) vals.pop_back();
        break;
      } else
        ret.PB(x);
      sum = sum1;
      vals.pop_back();
    }
    vals.PB(MP(val, st));
    return ret;
  }

  void deb() {
    debug("new chain");
    debug(ind);
    debug(par);
    debug(siz);
    debug(vals);
    debug("done");
  }
};

vector<chain> chains;

struct BIT {
  VI fen;
  int n;

  void init(int _n) {
    n = _n;
    fen.assign(n + 1, 0);
  }

  void upd(int ind, int val) {
    ind++;
    while (ind <= n) {
      fen[ind] += val;
      ind += (ind & -ind);
    }
  }

  int qry(int ind) {
    int ret = 0;
    ind++;
    while (ind >= 1) {
      ret += fen[ind];
      ind -= (ind & -ind);
    }
    return ret;
  }

  int qry(int l, int r) { return qry(r) - qry(l - 1); }
};

MPII cmp;

// first is val, second is count

void cmprs(VII vals) {
  cmp.clear();
  sort(all(vals));
  vals.resize(unique(all(vals)) - vals.begin());
  int cnt = 0;
  trav(x, vals) cmp[x.x] = cnt++;
}

ll calcinvs(VII vals) {
  ll ret = 0;
  BIT bit;
  reverse(all(vals));
  bit.init(sz(vals));
  cmprs(vals);
  for (int i = 0; i < sz(vals); i++) {
    int cur = bit.qry(cmp[vals[i].x] - 1);
    ret += cur * vals[i].y;
    PII ud = MP(cmp[vals[i].x], vals[i].y);
    bit.upd(ud.x, ud.y);
  }
  return ret;
}

VII gen(int node, int og = -1) {
  if (og == -1) og = node;
  VII ret;
  if (node == -1) return ret;
  ret = gen(chains[wch[node]].par, og);
  auto x = chains[wch[node]].get(pos[node], live[og]);
  trav(xx, x) ret.PB(xx);
  return ret;
}

ll qry(int node, int og) { return calcinvs(gen(node, og)); }

void dfs1(int u, int v) {
  sub[u] = 1;
  for (auto& x : adj[u]) {
    if (x == v) continue;
    dep[x] = dep[u] + 1;
    dfs1(x, u);
    sub[u] += sub[x];
  }
}

void dfs2(int u, int v) {
  if (sub[u] == 1) return;
  if (u == 0) {
    chain chn;
    chn.init(0, -1);
    chn.ins(0);
    chains.PB(chn);
  }
  int mx = adj[u][0];
  if (v == mx) mx = adj[u][1];
  trav(x, adj[u]) {
    if (x == v) continue;
    if (sub[mx] < sub[x]) mx = x;
  }
  trav(x, adj[u]) {
    if (x == v) continue;
    if (x == mx) {
      chains[wch[u]].ins(x);
    } else {
      chain nw;
      nw.init(sz(chains), u);
      nw.ins(x);
      chains.PB(nw);
    }
  }
  trav(x, adj[u]) {
    if (x == v) continue;
    dfs2(x, u);
  }
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("highways.in", "r", stdin);
  // freopen("highways.out", "w", stdout);
  // #endif
  cin.sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  init();

  for (int i = 0; i < n; i++) {
    cin >> live[i];
  }

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    edgs[i] = MP(u, v);
    adj[u].PB(v);
    adj[v].PB(u);
  }

  dfs1(0, -1);
  dfs2(0, -1);

  trav(x, chains) x.done();

  // debug(wch);
  // debug(pos);

  // trav(x, chains) x.deb();

  for (int i = 0; i < n - 1; i++) {
    cout << qry(edgs[i].x, edgs[i].y) << '\n';
  }

  // trav(x, chains) x.deb();

  return 0;
}

Compilation message

construction.cpp: In member function 'void chain::deb()':
construction.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
construction.cpp:124:5: note: in expansion of macro 'debug'
  124 |     debug("new chain");
      |     ^~~~~
construction.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
construction.cpp:125:5: note: in expansion of macro 'debug'
  125 |     debug(ind);
      |     ^~~~~
construction.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
construction.cpp:126:5: note: in expansion of macro 'debug'
  126 |     debug(par);
      |     ^~~~~
construction.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
construction.cpp:127:5: note: in expansion of macro 'debug'
  127 |     debug(siz);
      |     ^~~~~
construction.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
construction.cpp:128:5: note: in expansion of macro 'debug'
  128 |     debug(vals);
      |     ^~~~~
construction.cpp:19:20: warning: statement has no effect [-Wunused-value]
   19 | #define debug(...) 42
      |                    ^~
construction.cpp:129:5: note: in expansion of macro 'debug'
  129 |     debug("done");
      |     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
16 Correct 2 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
16 Correct 2 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 2 ms 492 KB Output is correct
46 Correct 7 ms 876 KB Output is correct
47 Correct 7 ms 876 KB Output is correct
48 Correct 7 ms 876 KB Output is correct
49 Correct 4 ms 1388 KB Output is correct
50 Correct 4 ms 1388 KB Output is correct
51 Correct 4 ms 1388 KB Output is correct
52 Correct 5 ms 1388 KB Output is correct
53 Correct 5 ms 1388 KB Output is correct
54 Correct 6 ms 1388 KB Output is correct
55 Correct 8 ms 1388 KB Output is correct
56 Correct 5 ms 1260 KB Output is correct
57 Correct 9 ms 876 KB Output is correct
58 Correct 10 ms 876 KB Output is correct
59 Correct 10 ms 876 KB Output is correct
60 Correct 10 ms 876 KB Output is correct
61 Correct 5 ms 1260 KB Output is correct
62 Correct 5 ms 1260 KB Output is correct
63 Correct 5 ms 1260 KB Output is correct
64 Correct 6 ms 876 KB Output is correct
65 Correct 7 ms 876 KB Output is correct
66 Correct 11 ms 1004 KB Output is correct
67 Correct 7 ms 876 KB Output is correct
68 Correct 4 ms 1388 KB Output is correct
69 Correct 5 ms 1388 KB Output is correct
70 Correct 4 ms 1260 KB Output is correct
71 Correct 4 ms 1260 KB Output is correct
72 Correct 9 ms 876 KB Output is correct
73 Correct 10 ms 876 KB Output is correct
74 Correct 7 ms 1260 KB Output is correct
75 Correct 5 ms 1132 KB Output is correct
76 Correct 5 ms 1004 KB Output is correct
77 Correct 5 ms 1004 KB Output is correct
78 Correct 4 ms 1004 KB Output is correct
79 Correct 5 ms 1004 KB Output is correct
80 Correct 5 ms 1004 KB Output is correct
81 Correct 5 ms 1152 KB Output is correct
82 Correct 5 ms 1004 KB Output is correct
83 Correct 5 ms 1024 KB Output is correct
84 Correct 7 ms 1004 KB Output is correct
85 Correct 5 ms 1004 KB Output is correct
86 Correct 5 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 2 ms 364 KB Output is correct
16 Correct 2 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 2 ms 364 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 1 ms 364 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 1 ms 364 KB Output is correct
41 Correct 1 ms 364 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 1 ms 364 KB Output is correct
45 Correct 2 ms 492 KB Output is correct
46 Correct 7 ms 876 KB Output is correct
47 Correct 7 ms 876 KB Output is correct
48 Correct 7 ms 876 KB Output is correct
49 Correct 4 ms 1388 KB Output is correct
50 Correct 4 ms 1388 KB Output is correct
51 Correct 4 ms 1388 KB Output is correct
52 Correct 5 ms 1388 KB Output is correct
53 Correct 5 ms 1388 KB Output is correct
54 Correct 6 ms 1388 KB Output is correct
55 Correct 8 ms 1388 KB Output is correct
56 Correct 5 ms 1260 KB Output is correct
57 Correct 9 ms 876 KB Output is correct
58 Correct 10 ms 876 KB Output is correct
59 Correct 10 ms 876 KB Output is correct
60 Correct 10 ms 876 KB Output is correct
61 Correct 5 ms 1260 KB Output is correct
62 Correct 5 ms 1260 KB Output is correct
63 Correct 5 ms 1260 KB Output is correct
64 Correct 6 ms 876 KB Output is correct
65 Correct 7 ms 876 KB Output is correct
66 Correct 11 ms 1004 KB Output is correct
67 Correct 7 ms 876 KB Output is correct
68 Correct 4 ms 1388 KB Output is correct
69 Correct 5 ms 1388 KB Output is correct
70 Correct 4 ms 1260 KB Output is correct
71 Correct 4 ms 1260 KB Output is correct
72 Correct 9 ms 876 KB Output is correct
73 Correct 10 ms 876 KB Output is correct
74 Correct 7 ms 1260 KB Output is correct
75 Correct 5 ms 1132 KB Output is correct
76 Correct 5 ms 1004 KB Output is correct
77 Correct 5 ms 1004 KB Output is correct
78 Correct 4 ms 1004 KB Output is correct
79 Correct 5 ms 1004 KB Output is correct
80 Correct 5 ms 1004 KB Output is correct
81 Correct 5 ms 1152 KB Output is correct
82 Correct 5 ms 1004 KB Output is correct
83 Correct 5 ms 1024 KB Output is correct
84 Correct 7 ms 1004 KB Output is correct
85 Correct 5 ms 1004 KB Output is correct
86 Correct 5 ms 1004 KB Output is correct
87 Correct 22 ms 1900 KB Output is correct
88 Correct 60 ms 4740 KB Output is correct
89 Correct 272 ms 14968 KB Output is correct
90 Correct 270 ms 14968 KB Output is correct
91 Correct 269 ms 15096 KB Output is correct
92 Correct 132 ms 27488 KB Output is correct
93 Correct 125 ms 27488 KB Output is correct
94 Correct 129 ms 27488 KB Output is correct
95 Correct 138 ms 25596 KB Output is correct
96 Correct 161 ms 25976 KB Output is correct
97 Correct 154 ms 25976 KB Output is correct
98 Correct 160 ms 25976 KB Output is correct
99 Correct 131 ms 22908 KB Output is correct
100 Correct 364 ms 14336 KB Output is correct
101 Correct 437 ms 14720 KB Output is correct
102 Correct 431 ms 14720 KB Output is correct
103 Correct 426 ms 14720 KB Output is correct
104 Correct 142 ms 22776 KB Output is correct
105 Correct 146 ms 22776 KB Output is correct
106 Correct 143 ms 22776 KB Output is correct
107 Correct 227 ms 14200 KB Output is correct
108 Correct 269 ms 14328 KB Output is correct
109 Correct 265 ms 14584 KB Output is correct
110 Correct 120 ms 26848 KB Output is correct
111 Correct 137 ms 25720 KB Output is correct
112 Correct 127 ms 23368 KB Output is correct
113 Correct 125 ms 22128 KB Output is correct
114 Correct 364 ms 14336 KB Output is correct
115 Correct 422 ms 14208 KB Output is correct
116 Correct 142 ms 22264 KB Output is correct
117 Correct 129 ms 18552 KB Output is correct
118 Correct 124 ms 17400 KB Output is correct
119 Correct 129 ms 16632 KB Output is correct
120 Correct 122 ms 18168 KB Output is correct
121 Correct 127 ms 17016 KB Output is correct
122 Correct 127 ms 16248 KB Output is correct
123 Correct 148 ms 18684 KB Output is correct
124 Correct 153 ms 17400 KB Output is correct
125 Correct 151 ms 16632 KB Output is correct
126 Correct 148 ms 18296 KB Output is correct
127 Correct 146 ms 17144 KB Output is correct
128 Correct 152 ms 16504 KB Output is correct