Submission #529051

# Submission time Handle Problem Language Result Execution time Memory
529051 2022-02-22T03:53:43 Z wiwiho Construction of Highway (JOI18_construction) C++14
100 / 100
1282 ms 22844 KB
#include<bits/stdc++.h>

#define printv(a, b) { \
    for(auto pv : a) b << pv << " "; \
    b << "\n"; \
}
#define eb emplace_back
#define pob pop_back()
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define mp make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;

const ll MOD = 1000000007;

using pll = pair<ll, ll>;

#define lc (2 * id + 1)
#define rc (2 * id + 2)

int n;
vector<int> c, tme;
vector<vector<int>> g;
vector<int> pr;
vector<int> in, out, eu, dpt;
vector<vector<int>> anc;
int ts = 0;
const int SZ = 20;

void init(){
    g.resize(n + 1);
    pr.resize(n + 1);
    c.resize(n + 1);
    tme.resize(n + 1);
    in.resize(n + 1);
    out.resize(n + 1);
    eu.resize(n + 1);
    dpt.resize(n + 1);
    anc.resize(SZ, vector<int>(n + 1));
}

struct SegmentTree{
    vector<int> st;

    int argmax(int a, int b){
        if(tme[a] >= tme[b]) return a;
        else return b;
    }

    void pull(int id){
        st[id] = argmax(st[lc], st[rc]);
    }
    
    void build(int L, int R, int id){
        if(L == R){
            st[id] = L;
            return;
        }
        int M = (L + R) / 2;
        build(L, M, lc);
        build(M + 1, R, rc);
        pull(id);
    }

    void init(){
        st.resize(4 * n);
    }

    void upd(int x, int L = 1, int R = n, int id = 0){
        if(L == R) return;
        int M = (L + R) / 2;
        if(x <= M) upd(x, L, M, lc);
        else upd(x, M + 1, R, rc);
        pull(id);
    }

    int query(int l, int r, int L = 1, int R = n, int id = 0){
        if(l <= L && R <= r) return st[id];
        int M = (L + R) / 2;
        if(r <= M) return query(l, r, L, M, lc);
        else if(l > M) return query(l, r, M + 1, R, rc);
        else return argmax(query(l, r, L, M, lc), query(l, r, M + 1, R, rc));
    }
};

int lowbit(int x){
    return x & -x;
}

struct BIT{
    vector<ll> bit;
    void init(){
        bit.resize(n + 1);
    }
    void modify(int x, ll v){
        for(; x < bit.size(); x += lowbit(x)) bit[x] += v;
    }
    ll query(int x){
        ll ans = 0;
        for(; x > 0; x -= lowbit(x)) ans += bit[x];
        return ans;
    }
};

void dfs(int now){
    in[now] = ++ts;
    eu[ts] = now;
    dpt[now] = dpt[pr[now]] + 1;
    for(int i : g[now]) dfs(i);
    out[now] = ts;
}

void buildAnc(){
    for(int i = 1; i < SZ; i++){
        for(int j = 1; j <= n; j++){
            anc[i][j] = anc[i - 1][anc[i - 1][j]];
        }
    }
}

bool isAnc(int a, int b){
    return in[a] <= in[b] && out[b] <= out[a];
}

int lca(int a, int b){
    //cerr << "LCA " << a << " " << b << "  ";
    if(isAnc(a, b)){
        //cerr << a << "\n";
        return a;
    }
    for(int i = SZ - 1; i >= 0; i--){
        if(!isAnc(anc[i][a], b)) a = anc[i][a];
    }
    //cerr << anc[0][a] << "\n";
    return anc[0][a];
}

int under(int a, int b){
    for(int i = SZ - 1; i >= 0; i--){
        if(!isAnc(anc[i][a], b)) a = anc[i][a];
    }
    return a;
}

SegmentTree st;

int getp(int v){
    int pos = st.query(in[v], out[v]);
    return pos;
}

int getv(int v){
    int pos = getp(v);
    //cerr << "getv " << v << " " << pos << "\n";
    return c[eu[pos]];
}

BIT bit;
ll calc(int v){
    //cerr << "calc " << v << "\n";
    ll ans = 0;
    int now = 1;
    vector<pll> opt;
    while(now != v){
        int pos = getp(now);
        int cv = c[eu[pos]];
        int t = lca(v, eu[pos]);
        int len = dpt[t] - dpt[now] + 1;
        //cerr << "test " << now << " " << t << " " << eu[pos] << "\n";
        ans += (bit.query(n) - bit.query(cv)) * len;
        bit.modify(cv, len);
        now = under(v, t);
        opt.eb(mp(cv, len));
    }
    for(pll i : opt) bit.modify(i.F, -i.S);

    //cerr << "ok " << ans << "\n";
    return ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cerr.tie(0);

    cin >> n;
    init();

    for(int i = 1; i <= n; i++){
        cin >> c[i];
    }
    vector<int> tmp = c;
    lsort(tmp);
    uni(tmp);
    for(int i = 1; i <= n; i++){
        c[i] = lower_bound(iter(tmp), c[i]) - tmp.begin();
    }
    //printv(c, cerr);

    vector<int> qry(n + 1);
    pr[1] = anc[0][1] = 1;
    for(int i = 2; i <= n; i++){
        int u, v;
        cin >> u >> v;
        g[u].eb(v);
        pr[v] = anc[0][v] = u;
        qry[i] = v;
    }

    dfs(1);
    buildAnc();

    //for(int i = 0; i < SZ; i++) printv(anc[i], cerr);

    st.init();
    st.build(1, n, 0);
    bit.init();

    for(int i = 2; i <= n; i++){
        int now = qry[i];
        cout << calc(now) << "\n";
        tme[in[now]] = i;
        st.upd(in[now]);
    }

    return 0;
}

Compilation message

construction.cpp: In member function 'void BIT::modify(int, ll)':
construction.cpp:102:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for(; x < bit.size(); x += lowbit(x)) bit[x] += v;
      |               ~~^~~~~~~~~~~~
# Verdict Execution time Memory 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 2 ms 332 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 396 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 416 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 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 340 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 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 3 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 388 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 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory 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 2 ms 332 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 396 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 416 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 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 340 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 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 3 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 388 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 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 11 ms 1028 KB Output is correct
47 Correct 11 ms 1024 KB Output is correct
48 Correct 11 ms 972 KB Output is correct
49 Correct 5 ms 1208 KB Output is correct
50 Correct 5 ms 1100 KB Output is correct
51 Correct 5 ms 1100 KB Output is correct
52 Correct 5 ms 1100 KB Output is correct
53 Correct 6 ms 1052 KB Output is correct
54 Correct 5 ms 1100 KB Output is correct
55 Correct 5 ms 1100 KB Output is correct
56 Correct 6 ms 1080 KB Output is correct
57 Correct 18 ms 972 KB Output is correct
58 Correct 19 ms 1032 KB Output is correct
59 Correct 19 ms 972 KB Output is correct
60 Correct 19 ms 1036 KB Output is correct
61 Correct 6 ms 972 KB Output is correct
62 Correct 6 ms 972 KB Output is correct
63 Correct 6 ms 980 KB Output is correct
64 Correct 11 ms 972 KB Output is correct
65 Correct 10 ms 924 KB Output is correct
66 Correct 10 ms 972 KB Output is correct
67 Correct 11 ms 972 KB Output is correct
68 Correct 4 ms 1100 KB Output is correct
69 Correct 5 ms 1140 KB Output is correct
70 Correct 5 ms 1100 KB Output is correct
71 Correct 5 ms 972 KB Output is correct
72 Correct 24 ms 1100 KB Output is correct
73 Correct 18 ms 972 KB Output is correct
74 Correct 7 ms 1080 KB Output is correct
75 Correct 5 ms 972 KB Output is correct
76 Correct 6 ms 972 KB Output is correct
77 Correct 5 ms 972 KB Output is correct
78 Correct 5 ms 972 KB Output is correct
79 Correct 6 ms 972 KB Output is correct
80 Correct 5 ms 1036 KB Output is correct
81 Correct 8 ms 1048 KB Output is correct
82 Correct 7 ms 972 KB Output is correct
83 Correct 6 ms 972 KB Output is correct
84 Correct 6 ms 972 KB Output is correct
85 Correct 6 ms 972 KB Output is correct
86 Correct 9 ms 972 KB Output is correct
# Verdict Execution time Memory 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 2 ms 332 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 396 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 416 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 2 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 340 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 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 3 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 388 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 332 KB Output is correct
40 Correct 1 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 11 ms 1028 KB Output is correct
47 Correct 11 ms 1024 KB Output is correct
48 Correct 11 ms 972 KB Output is correct
49 Correct 5 ms 1208 KB Output is correct
50 Correct 5 ms 1100 KB Output is correct
51 Correct 5 ms 1100 KB Output is correct
52 Correct 5 ms 1100 KB Output is correct
53 Correct 6 ms 1052 KB Output is correct
54 Correct 5 ms 1100 KB Output is correct
55 Correct 5 ms 1100 KB Output is correct
56 Correct 6 ms 1080 KB Output is correct
57 Correct 18 ms 972 KB Output is correct
58 Correct 19 ms 1032 KB Output is correct
59 Correct 19 ms 972 KB Output is correct
60 Correct 19 ms 1036 KB Output is correct
61 Correct 6 ms 972 KB Output is correct
62 Correct 6 ms 972 KB Output is correct
63 Correct 6 ms 980 KB Output is correct
64 Correct 11 ms 972 KB Output is correct
65 Correct 10 ms 924 KB Output is correct
66 Correct 10 ms 972 KB Output is correct
67 Correct 11 ms 972 KB Output is correct
68 Correct 4 ms 1100 KB Output is correct
69 Correct 5 ms 1140 KB Output is correct
70 Correct 5 ms 1100 KB Output is correct
71 Correct 5 ms 972 KB Output is correct
72 Correct 24 ms 1100 KB Output is correct
73 Correct 18 ms 972 KB Output is correct
74 Correct 7 ms 1080 KB Output is correct
75 Correct 5 ms 972 KB Output is correct
76 Correct 6 ms 972 KB Output is correct
77 Correct 5 ms 972 KB Output is correct
78 Correct 5 ms 972 KB Output is correct
79 Correct 6 ms 972 KB Output is correct
80 Correct 5 ms 1036 KB Output is correct
81 Correct 8 ms 1048 KB Output is correct
82 Correct 7 ms 972 KB Output is correct
83 Correct 6 ms 972 KB Output is correct
84 Correct 6 ms 972 KB Output is correct
85 Correct 6 ms 972 KB Output is correct
86 Correct 9 ms 972 KB Output is correct
87 Correct 31 ms 2108 KB Output is correct
88 Correct 122 ms 5812 KB Output is correct
89 Correct 649 ms 18180 KB Output is correct
90 Correct 664 ms 18220 KB Output is correct
91 Correct 665 ms 18440 KB Output is correct
92 Correct 256 ms 22844 KB Output is correct
93 Correct 304 ms 22820 KB Output is correct
94 Correct 248 ms 22760 KB Output is correct
95 Correct 291 ms 21232 KB Output is correct
96 Correct 288 ms 21240 KB Output is correct
97 Correct 283 ms 21184 KB Output is correct
98 Correct 293 ms 21196 KB Output is correct
99 Correct 277 ms 19880 KB Output is correct
100 Correct 1201 ms 18744 KB Output is correct
101 Correct 1196 ms 18780 KB Output is correct
102 Correct 1195 ms 18760 KB Output is correct
103 Correct 1211 ms 18668 KB Output is correct
104 Correct 384 ms 19848 KB Output is correct
105 Correct 355 ms 19904 KB Output is correct
106 Correct 402 ms 19824 KB Output is correct
107 Correct 604 ms 18280 KB Output is correct
108 Correct 603 ms 18364 KB Output is correct
109 Correct 703 ms 18304 KB Output is correct
110 Correct 258 ms 22736 KB Output is correct
111 Correct 256 ms 21180 KB Output is correct
112 Correct 288 ms 21184 KB Output is correct
113 Correct 292 ms 19756 KB Output is correct
114 Correct 1282 ms 18792 KB Output is correct
115 Correct 1239 ms 18720 KB Output is correct
116 Correct 320 ms 19780 KB Output is correct
117 Correct 312 ms 18948 KB Output is correct
118 Correct 272 ms 18748 KB Output is correct
119 Correct 334 ms 18516 KB Output is correct
120 Correct 265 ms 18916 KB Output is correct
121 Correct 271 ms 18672 KB Output is correct
122 Correct 263 ms 18700 KB Output is correct
123 Correct 367 ms 19024 KB Output is correct
124 Correct 396 ms 18824 KB Output is correct
125 Correct 393 ms 18620 KB Output is correct
126 Correct 350 ms 19072 KB Output is correct
127 Correct 366 ms 18748 KB Output is correct
128 Correct 369 ms 18756 KB Output is correct