Submission #600828

# Submission time Handle Problem Language Result Execution time Memory
600828 2022-07-21T08:06:52 Z AA_Surely Construction of Highway (JOI18_construction) C++14
100 / 100
1124 ms 22136 KB
#include <bits/stdc++.h>

#define FOR(i, x, n) for(int i = x; i < n; i++)
#define F0R(i, n) FOR(i, 0, n)
#define ROF(i, x, n) for(int i = n - 1; i >= x; i--)
#define R0F(i, n) ROF(i, 0, n)

#define WTF cout << "WTF" << endl

#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define F first
#define S second
#define PB push_back

#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()

using namespace std;

typedef long long LL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;

#define lc now << 1
#define rc now << 1 | 1

const int N = 1e5 + 5;

int n;
int cs[N], sz[N], t[N], cnt;
int head[N], height[N], par[N];
int order[N];
PII eset[N];
VI adj[N], comp;

struct Fenwick {
    int fen[N];
    VI changed;

    void add(int id, int val) {
        for(; id < N; id += id & -id) {
            fen[id] += val;
            changed.PB(id);
        }
        return;
    }

    int get(int id) {
        int ret = 0;
        for(; id; id -= id & -id) ret += fen[id];
        return ret;
    }

    void clear() {
        for(const int &on : changed) fen[on] = 0;
        changed.clear();
    }
} sum;

struct Segment {
    bool tree[N << 2];
    int lz[N << 2], cnt = 0;

    void init() {
        memset(lz, -1, sizeof lz);
        return;
    }

    void shift(int now) {
        if (lz[now] == -1) return;
        
        tree[lc] = lz[lc] = lz[now];
        tree[rc] = lz[rc] = lz[now];

        lz[now] = -1;
        return;
    }

    void change(int lq, int rq, bool val, int now = 1, int ls = 0, int rs = n - 1) {
        if (rq < lq || rq < ls || rs < lq) return;
        if (lq <= ls && rs <= rq) {
            tree[now] = lz[now] = val;
            return;
        }

        int mid = (ls + rs) >> 1;
        shift(now);
        change(lq, rq, val, lc, ls, mid);
        change(lq, rq, val, rc, mid + 1, rs);
        
        tree[now] = tree[lc] | tree[rc];
        return;
    }

    int rightest1(int q, int now = 1, int ls = 0, int rs = n - 1) {
        if (ls >= q || !tree[now]) return -1;
        if (ls == rs) return ls;

        int mid = (ls + rs) >> 1;
        shift(now);
        int case1 = rightest1(q, rc, mid + 1, rs);
        return (case1 == -1 ? rightest1(q, lc, ls, mid) : case1);
    }
} hldq;

struct MaxSeg {
    PII tree[N << 2];
    int cnt = 0;

    void init() {
        fill(tree, tree + (N << 2), PII(-1, -1));
        return;
    }

    void cseg(int lq, int rq, PII val, int now = 1, int ls = 0, int rs = n - 1) {
        if (rq < lq || rq < ls || rs < lq) return;
        if (lq <= ls && rs <= rq) {
            tree[now] = val;
            return;
        }

        int mid = (ls + rs) >> 1;
        cseg(lq, rq, val, lc, ls, mid);
        cseg(lq, rq, val, rc, mid + 1, rs);

        return;
    }

    void change(int lq, int rq, int val) {
        cseg(lq, rq, PII(cnt, val));
        cnt++;
    }
    
    PII gseg(int id, int now = 1, int ls = 0, int rs = n - 1) {
        if (ls == rs) return tree[now];
        int mid = (ls + rs) >> 1;
        if (id <= mid) return max(tree[now], gseg(id, lc, ls, mid));
        return max(tree[now], gseg(id, rc, mid + 1, rs));
    }

    int get(int id) {
        return gseg(id).S;
    }
} color;

void init() {
    cin >> n;
    comp.resize(n);
    F0R(i, n) {
        cin >> cs[i];
        comp[i] = cs[i];
    }

    par[0] = -1;

    sort(ALL(comp)); 
    comp.resize(unique(ALL(comp)) - comp.begin());
    F0R(i, n) cs[i] = lower_bound(ALL(comp), cs[i]) - comp.begin() + 1;

    F0R(i, n - 1) {
        int u, v; cin >> u >> v;
        adj[--u].PB(--v);
        eset[i] = {u, v};

        par[v] = u;
    }

    return;
}

int preD(int now) {
    sz[now] = 1;
    for(int on : adj[now]) {
        height[on] = height[now] + 1;
        sz[now] += preD(on);
    }

    return sz[now];
}

void dfs(int now) {
    order[cnt] = now;
    t[now] = cnt++;

    sort(ALL(adj[now]), [](const int &a, const int &b) {return sz[a] > sz[b];});
    if (!adj[now].empty()) head[ adj[now][0] ] = head[now];

    for(int on : adj[now]) 
        dfs(on);

    return;
}

LL get(int v) {
    LL ret = 0;
    sum.clear();

    while(v != -1) {
        int pl = hldq.rightest1(t[v]);
        if (pl >= t[ head[v] ]) {
            pl = order[pl];

            LL us = height[v] - height[pl];
            int num = color.get(t[v]);

            ret += sum.get(num - 1) * us;
            sum.add(num, us);

            v = pl;
        }

        else {
            LL us = height[v] - height[ head[v] ] + 1;
            int num = color.get(t[v]);
            
            ret += sum.get(num - 1) * us;
            sum.add(num, us);

            v = par[ head[v] ];
        }
    }

    return ret;
}

void change(int v, int col) {
    while(v != -1) {
        hldq.change(t[v], t[v], 1);
        color.change(t[ head[v] ], t[v], col);

        if (v == head[v]) {
            v = par[v];
            continue;
        }

        v = par[v];
        hldq.change(t[ head[v] ], t[v], 0);
        v = par[ head[v] ];
    }

    return;
}

#define endl '\n'

int main() {
    IOS;

    init();
    preD(0);
    iota(head, head + n, 0);
    dfs(0);

    color.init();
    color.change(0, 0, cs[0]);
    hldq.init();

    F0R(i, n - 1) {
        cout << get(eset[i].F) << endl;
        change(eset[i].S, cs[ eset[i].S ]);
    }

    return 0;
}

Compilation message

construction.cpp: In member function 'void Segment::shift(int)':
construction.cpp:78:27: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   78 |         tree[lc] = lz[lc] = lz[now];
      |                    ~~~~~~~^~~~~~~~~
construction.cpp:79:27: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   79 |         tree[rc] = lz[rc] = lz[now];
      |                    ~~~~~~~^~~~~~~~~
construction.cpp: In member function 'void Segment::change(int, int, bool, int, int, int)':
construction.cpp:88:33: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   88 |             tree[now] = lz[now] = val;
      |                         ~~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7420 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 6 ms 7420 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 7 ms 7380 KB Output is correct
6 Correct 5 ms 7432 KB Output is correct
7 Correct 6 ms 7380 KB Output is correct
8 Correct 6 ms 7432 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 6 ms 7512 KB Output is correct
11 Correct 7 ms 7508 KB Output is correct
12 Correct 6 ms 7428 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7424 KB Output is correct
16 Correct 7 ms 7380 KB Output is correct
17 Correct 7 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 6 ms 7380 KB Output is correct
21 Correct 7 ms 7380 KB Output is correct
22 Correct 7 ms 7380 KB Output is correct
23 Correct 7 ms 7380 KB Output is correct
24 Correct 5 ms 7380 KB Output is correct
25 Correct 7 ms 7428 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 7 ms 7552 KB Output is correct
28 Correct 7 ms 7428 KB Output is correct
29 Correct 6 ms 7380 KB Output is correct
30 Correct 8 ms 7380 KB Output is correct
31 Correct 6 ms 7380 KB Output is correct
32 Correct 7 ms 7508 KB Output is correct
33 Correct 7 ms 7432 KB Output is correct
34 Correct 6 ms 7380 KB Output is correct
35 Correct 5 ms 7420 KB Output is correct
36 Correct 6 ms 7432 KB Output is correct
37 Correct 5 ms 7468 KB Output is correct
38 Correct 5 ms 7424 KB Output is correct
39 Correct 5 ms 7428 KB Output is correct
40 Correct 5 ms 7380 KB Output is correct
41 Correct 7 ms 7380 KB Output is correct
42 Correct 5 ms 7380 KB Output is correct
43 Correct 5 ms 7380 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7420 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 6 ms 7420 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 7 ms 7380 KB Output is correct
6 Correct 5 ms 7432 KB Output is correct
7 Correct 6 ms 7380 KB Output is correct
8 Correct 6 ms 7432 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 6 ms 7512 KB Output is correct
11 Correct 7 ms 7508 KB Output is correct
12 Correct 6 ms 7428 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7424 KB Output is correct
16 Correct 7 ms 7380 KB Output is correct
17 Correct 7 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 6 ms 7380 KB Output is correct
21 Correct 7 ms 7380 KB Output is correct
22 Correct 7 ms 7380 KB Output is correct
23 Correct 7 ms 7380 KB Output is correct
24 Correct 5 ms 7380 KB Output is correct
25 Correct 7 ms 7428 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 7 ms 7552 KB Output is correct
28 Correct 7 ms 7428 KB Output is correct
29 Correct 6 ms 7380 KB Output is correct
30 Correct 8 ms 7380 KB Output is correct
31 Correct 6 ms 7380 KB Output is correct
32 Correct 7 ms 7508 KB Output is correct
33 Correct 7 ms 7432 KB Output is correct
34 Correct 6 ms 7380 KB Output is correct
35 Correct 5 ms 7420 KB Output is correct
36 Correct 6 ms 7432 KB Output is correct
37 Correct 5 ms 7468 KB Output is correct
38 Correct 5 ms 7424 KB Output is correct
39 Correct 5 ms 7428 KB Output is correct
40 Correct 5 ms 7380 KB Output is correct
41 Correct 7 ms 7380 KB Output is correct
42 Correct 5 ms 7380 KB Output is correct
43 Correct 5 ms 7380 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 8 ms 7448 KB Output is correct
46 Correct 23 ms 7736 KB Output is correct
47 Correct 18 ms 7764 KB Output is correct
48 Correct 17 ms 7652 KB Output is correct
49 Correct 9 ms 7892 KB Output is correct
50 Correct 9 ms 7948 KB Output is correct
51 Correct 13 ms 7988 KB Output is correct
52 Correct 10 ms 8024 KB Output is correct
53 Correct 13 ms 8052 KB Output is correct
54 Correct 9 ms 7988 KB Output is correct
55 Correct 14 ms 8036 KB Output is correct
56 Correct 10 ms 7776 KB Output is correct
57 Correct 25 ms 7712 KB Output is correct
58 Correct 23 ms 7752 KB Output is correct
59 Correct 22 ms 7752 KB Output is correct
60 Correct 23 ms 7756 KB Output is correct
61 Correct 14 ms 7764 KB Output is correct
62 Correct 14 ms 7764 KB Output is correct
63 Correct 11 ms 7816 KB Output is correct
64 Correct 16 ms 7716 KB Output is correct
65 Correct 17 ms 7684 KB Output is correct
66 Correct 15 ms 7720 KB Output is correct
67 Correct 20 ms 7664 KB Output is correct
68 Correct 11 ms 7948 KB Output is correct
69 Correct 14 ms 8020 KB Output is correct
70 Correct 10 ms 7960 KB Output is correct
71 Correct 10 ms 7824 KB Output is correct
72 Correct 25 ms 7728 KB Output is correct
73 Correct 27 ms 7720 KB Output is correct
74 Correct 9 ms 7808 KB Output is correct
75 Correct 10 ms 7764 KB Output is correct
76 Correct 10 ms 7780 KB Output is correct
77 Correct 11 ms 7764 KB Output is correct
78 Correct 10 ms 7764 KB Output is correct
79 Correct 13 ms 7636 KB Output is correct
80 Correct 13 ms 7636 KB Output is correct
81 Correct 13 ms 7772 KB Output is correct
82 Correct 13 ms 7816 KB Output is correct
83 Correct 11 ms 7768 KB Output is correct
84 Correct 12 ms 7688 KB Output is correct
85 Correct 14 ms 7760 KB Output is correct
86 Correct 13 ms 7680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7420 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 6 ms 7420 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 7 ms 7380 KB Output is correct
6 Correct 5 ms 7432 KB Output is correct
7 Correct 6 ms 7380 KB Output is correct
8 Correct 6 ms 7432 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 6 ms 7512 KB Output is correct
11 Correct 7 ms 7508 KB Output is correct
12 Correct 6 ms 7428 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 6 ms 7424 KB Output is correct
16 Correct 7 ms 7380 KB Output is correct
17 Correct 7 ms 7380 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 6 ms 7380 KB Output is correct
21 Correct 7 ms 7380 KB Output is correct
22 Correct 7 ms 7380 KB Output is correct
23 Correct 7 ms 7380 KB Output is correct
24 Correct 5 ms 7380 KB Output is correct
25 Correct 7 ms 7428 KB Output is correct
26 Correct 6 ms 7508 KB Output is correct
27 Correct 7 ms 7552 KB Output is correct
28 Correct 7 ms 7428 KB Output is correct
29 Correct 6 ms 7380 KB Output is correct
30 Correct 8 ms 7380 KB Output is correct
31 Correct 6 ms 7380 KB Output is correct
32 Correct 7 ms 7508 KB Output is correct
33 Correct 7 ms 7432 KB Output is correct
34 Correct 6 ms 7380 KB Output is correct
35 Correct 5 ms 7420 KB Output is correct
36 Correct 6 ms 7432 KB Output is correct
37 Correct 5 ms 7468 KB Output is correct
38 Correct 5 ms 7424 KB Output is correct
39 Correct 5 ms 7428 KB Output is correct
40 Correct 5 ms 7380 KB Output is correct
41 Correct 7 ms 7380 KB Output is correct
42 Correct 5 ms 7380 KB Output is correct
43 Correct 5 ms 7380 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 8 ms 7448 KB Output is correct
46 Correct 23 ms 7736 KB Output is correct
47 Correct 18 ms 7764 KB Output is correct
48 Correct 17 ms 7652 KB Output is correct
49 Correct 9 ms 7892 KB Output is correct
50 Correct 9 ms 7948 KB Output is correct
51 Correct 13 ms 7988 KB Output is correct
52 Correct 10 ms 8024 KB Output is correct
53 Correct 13 ms 8052 KB Output is correct
54 Correct 9 ms 7988 KB Output is correct
55 Correct 14 ms 8036 KB Output is correct
56 Correct 10 ms 7776 KB Output is correct
57 Correct 25 ms 7712 KB Output is correct
58 Correct 23 ms 7752 KB Output is correct
59 Correct 22 ms 7752 KB Output is correct
60 Correct 23 ms 7756 KB Output is correct
61 Correct 14 ms 7764 KB Output is correct
62 Correct 14 ms 7764 KB Output is correct
63 Correct 11 ms 7816 KB Output is correct
64 Correct 16 ms 7716 KB Output is correct
65 Correct 17 ms 7684 KB Output is correct
66 Correct 15 ms 7720 KB Output is correct
67 Correct 20 ms 7664 KB Output is correct
68 Correct 11 ms 7948 KB Output is correct
69 Correct 14 ms 8020 KB Output is correct
70 Correct 10 ms 7960 KB Output is correct
71 Correct 10 ms 7824 KB Output is correct
72 Correct 25 ms 7728 KB Output is correct
73 Correct 27 ms 7720 KB Output is correct
74 Correct 9 ms 7808 KB Output is correct
75 Correct 10 ms 7764 KB Output is correct
76 Correct 10 ms 7780 KB Output is correct
77 Correct 11 ms 7764 KB Output is correct
78 Correct 10 ms 7764 KB Output is correct
79 Correct 13 ms 7636 KB Output is correct
80 Correct 13 ms 7636 KB Output is correct
81 Correct 13 ms 7772 KB Output is correct
82 Correct 13 ms 7816 KB Output is correct
83 Correct 11 ms 7768 KB Output is correct
84 Correct 12 ms 7688 KB Output is correct
85 Correct 14 ms 7760 KB Output is correct
86 Correct 13 ms 7680 KB Output is correct
87 Correct 51 ms 8244 KB Output is correct
88 Correct 132 ms 9924 KB Output is correct
89 Correct 640 ms 15920 KB Output is correct
90 Correct 639 ms 15828 KB Output is correct
91 Correct 624 ms 15908 KB Output is correct
92 Correct 184 ms 22012 KB Output is correct
93 Correct 162 ms 22136 KB Output is correct
94 Correct 178 ms 22036 KB Output is correct
95 Correct 181 ms 19840 KB Output is correct
96 Correct 178 ms 20348 KB Output is correct
97 Correct 213 ms 20292 KB Output is correct
98 Correct 175 ms 20252 KB Output is correct
99 Correct 179 ms 18252 KB Output is correct
100 Correct 923 ms 16060 KB Output is correct
101 Correct 945 ms 16364 KB Output is correct
102 Correct 974 ms 16344 KB Output is correct
103 Correct 1109 ms 16380 KB Output is correct
104 Correct 256 ms 18188 KB Output is correct
105 Correct 235 ms 18168 KB Output is correct
106 Correct 260 ms 18264 KB Output is correct
107 Correct 657 ms 14664 KB Output is correct
108 Correct 624 ms 14880 KB Output is correct
109 Correct 732 ms 15184 KB Output is correct
110 Correct 205 ms 21036 KB Output is correct
111 Correct 251 ms 19936 KB Output is correct
112 Correct 252 ms 21416 KB Output is correct
113 Correct 194 ms 17292 KB Output is correct
114 Correct 1124 ms 16056 KB Output is correct
115 Correct 1018 ms 15384 KB Output is correct
116 Correct 254 ms 17204 KB Output is correct
117 Correct 265 ms 16652 KB Output is correct
118 Correct 273 ms 16336 KB Output is correct
119 Correct 300 ms 16132 KB Output is correct
120 Correct 243 ms 16016 KB Output is correct
121 Correct 278 ms 15696 KB Output is correct
122 Correct 242 ms 15412 KB Output is correct
123 Correct 302 ms 16836 KB Output is correct
124 Correct 321 ms 16388 KB Output is correct
125 Correct 375 ms 16196 KB Output is correct
126 Correct 304 ms 16132 KB Output is correct
127 Correct 293 ms 15700 KB Output is correct
128 Correct 292 ms 15520 KB Output is correct