Submission #600739

# Submission time Handle Problem Language Result Execution time Memory
600739 2022-07-21T07:33:00 Z AA_Surely Construction of Highway (JOI18_construction) C++14
100 / 100
1237 ms 22148 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);
        //cout << "changing " << t[head[v]] << ' ' << t[v] << " to " << x
        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;
}

int main() {
    IOS;

    init();

    /*color.change(0, 0, 1);
    color.change(0, 0, 2);
    cout << color.get(0) << endl;*/

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

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

    //F0R(i, n) cout << order[i] + 1 << ' '; cout << endl;

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

        //cout << "after edge " << i + 1 << " colors are ";
        //F0R(i, n) cout << color.get(i) << ' '; cout << endl;
    }

    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 7380 KB Output is correct
2 Correct 6 ms 7380 KB Output is correct
3 Correct 6 ms 7428 KB Output is correct
4 Correct 4 ms 7464 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7428 KB Output is correct
9 Correct 5 ms 7428 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 5 ms 7400 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 7 ms 7380 KB Output is correct
16 Correct 6 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 6 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 6 ms 7428 KB Output is correct
21 Correct 5 ms 7380 KB Output is correct
22 Correct 6 ms 7428 KB Output is correct
23 Correct 6 ms 7380 KB Output is correct
24 Correct 7 ms 7424 KB Output is correct
25 Correct 8 ms 7400 KB Output is correct
26 Correct 6 ms 7428 KB Output is correct
27 Correct 5 ms 7500 KB Output is correct
28 Correct 5 ms 7432 KB Output is correct
29 Correct 5 ms 7420 KB Output is correct
30 Correct 6 ms 7380 KB Output is correct
31 Correct 6 ms 7428 KB Output is correct
32 Correct 6 ms 7380 KB Output is correct
33 Correct 5 ms 7432 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 ms 7380 KB Output is correct
36 Correct 5 ms 7380 KB Output is correct
37 Correct 6 ms 7380 KB Output is correct
38 Correct 5 ms 7380 KB Output is correct
39 Correct 6 ms 7436 KB Output is correct
40 Correct 6 ms 7380 KB Output is correct
41 Correct 7 ms 7380 KB Output is correct
42 Correct 6 ms 7380 KB Output is correct
43 Correct 6 ms 7380 KB Output is correct
44 Correct 5 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 6 ms 7380 KB Output is correct
3 Correct 6 ms 7428 KB Output is correct
4 Correct 4 ms 7464 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7428 KB Output is correct
9 Correct 5 ms 7428 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 5 ms 7400 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 7 ms 7380 KB Output is correct
16 Correct 6 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 6 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 6 ms 7428 KB Output is correct
21 Correct 5 ms 7380 KB Output is correct
22 Correct 6 ms 7428 KB Output is correct
23 Correct 6 ms 7380 KB Output is correct
24 Correct 7 ms 7424 KB Output is correct
25 Correct 8 ms 7400 KB Output is correct
26 Correct 6 ms 7428 KB Output is correct
27 Correct 5 ms 7500 KB Output is correct
28 Correct 5 ms 7432 KB Output is correct
29 Correct 5 ms 7420 KB Output is correct
30 Correct 6 ms 7380 KB Output is correct
31 Correct 6 ms 7428 KB Output is correct
32 Correct 6 ms 7380 KB Output is correct
33 Correct 5 ms 7432 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 ms 7380 KB Output is correct
36 Correct 5 ms 7380 KB Output is correct
37 Correct 6 ms 7380 KB Output is correct
38 Correct 5 ms 7380 KB Output is correct
39 Correct 6 ms 7436 KB Output is correct
40 Correct 6 ms 7380 KB Output is correct
41 Correct 7 ms 7380 KB Output is correct
42 Correct 6 ms 7380 KB Output is correct
43 Correct 6 ms 7380 KB Output is correct
44 Correct 5 ms 7380 KB Output is correct
45 Correct 7 ms 7508 KB Output is correct
46 Correct 20 ms 7688 KB Output is correct
47 Correct 23 ms 7692 KB Output is correct
48 Correct 19 ms 7636 KB Output is correct
49 Correct 14 ms 7948 KB Output is correct
50 Correct 12 ms 7892 KB Output is correct
51 Correct 12 ms 7892 KB Output is correct
52 Correct 13 ms 7956 KB Output is correct
53 Correct 14 ms 8016 KB Output is correct
54 Correct 13 ms 7976 KB Output is correct
55 Correct 14 ms 8052 KB Output is correct
56 Correct 14 ms 7764 KB Output is correct
57 Correct 32 ms 7636 KB Output is correct
58 Correct 33 ms 7764 KB Output is correct
59 Correct 31 ms 7748 KB Output is correct
60 Correct 28 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 14 ms 7800 KB Output is correct
64 Correct 23 ms 7696 KB Output is correct
65 Correct 20 ms 7636 KB Output is correct
66 Correct 20 ms 7736 KB Output is correct
67 Correct 19 ms 7764 KB Output is correct
68 Correct 14 ms 7892 KB Output is correct
69 Correct 20 ms 8020 KB Output is correct
70 Correct 14 ms 8060 KB Output is correct
71 Correct 14 ms 7824 KB Output is correct
72 Correct 26 ms 7736 KB Output is correct
73 Correct 26 ms 7724 KB Output is correct
74 Correct 13 ms 7816 KB Output is correct
75 Correct 14 ms 7764 KB Output is correct
76 Correct 15 ms 7748 KB Output is correct
77 Correct 14 ms 7688 KB Output is correct
78 Correct 15 ms 7688 KB Output is correct
79 Correct 15 ms 7636 KB Output is correct
80 Correct 14 ms 7636 KB Output is correct
81 Correct 18 ms 7764 KB Output is correct
82 Correct 15 ms 7672 KB Output is correct
83 Correct 17 ms 7764 KB Output is correct
84 Correct 15 ms 7692 KB Output is correct
85 Correct 16 ms 7756 KB Output is correct
86 Correct 15 ms 7636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 6 ms 7380 KB Output is correct
3 Correct 6 ms 7428 KB Output is correct
4 Correct 4 ms 7464 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7428 KB Output is correct
9 Correct 5 ms 7428 KB Output is correct
10 Correct 5 ms 7508 KB Output is correct
11 Correct 5 ms 7508 KB Output is correct
12 Correct 5 ms 7400 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 7 ms 7380 KB Output is correct
16 Correct 6 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 6 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 6 ms 7428 KB Output is correct
21 Correct 5 ms 7380 KB Output is correct
22 Correct 6 ms 7428 KB Output is correct
23 Correct 6 ms 7380 KB Output is correct
24 Correct 7 ms 7424 KB Output is correct
25 Correct 8 ms 7400 KB Output is correct
26 Correct 6 ms 7428 KB Output is correct
27 Correct 5 ms 7500 KB Output is correct
28 Correct 5 ms 7432 KB Output is correct
29 Correct 5 ms 7420 KB Output is correct
30 Correct 6 ms 7380 KB Output is correct
31 Correct 6 ms 7428 KB Output is correct
32 Correct 6 ms 7380 KB Output is correct
33 Correct 5 ms 7432 KB Output is correct
34 Correct 5 ms 7380 KB Output is correct
35 Correct 5 ms 7380 KB Output is correct
36 Correct 5 ms 7380 KB Output is correct
37 Correct 6 ms 7380 KB Output is correct
38 Correct 5 ms 7380 KB Output is correct
39 Correct 6 ms 7436 KB Output is correct
40 Correct 6 ms 7380 KB Output is correct
41 Correct 7 ms 7380 KB Output is correct
42 Correct 6 ms 7380 KB Output is correct
43 Correct 6 ms 7380 KB Output is correct
44 Correct 5 ms 7380 KB Output is correct
45 Correct 7 ms 7508 KB Output is correct
46 Correct 20 ms 7688 KB Output is correct
47 Correct 23 ms 7692 KB Output is correct
48 Correct 19 ms 7636 KB Output is correct
49 Correct 14 ms 7948 KB Output is correct
50 Correct 12 ms 7892 KB Output is correct
51 Correct 12 ms 7892 KB Output is correct
52 Correct 13 ms 7956 KB Output is correct
53 Correct 14 ms 8016 KB Output is correct
54 Correct 13 ms 7976 KB Output is correct
55 Correct 14 ms 8052 KB Output is correct
56 Correct 14 ms 7764 KB Output is correct
57 Correct 32 ms 7636 KB Output is correct
58 Correct 33 ms 7764 KB Output is correct
59 Correct 31 ms 7748 KB Output is correct
60 Correct 28 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 14 ms 7800 KB Output is correct
64 Correct 23 ms 7696 KB Output is correct
65 Correct 20 ms 7636 KB Output is correct
66 Correct 20 ms 7736 KB Output is correct
67 Correct 19 ms 7764 KB Output is correct
68 Correct 14 ms 7892 KB Output is correct
69 Correct 20 ms 8020 KB Output is correct
70 Correct 14 ms 8060 KB Output is correct
71 Correct 14 ms 7824 KB Output is correct
72 Correct 26 ms 7736 KB Output is correct
73 Correct 26 ms 7724 KB Output is correct
74 Correct 13 ms 7816 KB Output is correct
75 Correct 14 ms 7764 KB Output is correct
76 Correct 15 ms 7748 KB Output is correct
77 Correct 14 ms 7688 KB Output is correct
78 Correct 15 ms 7688 KB Output is correct
79 Correct 15 ms 7636 KB Output is correct
80 Correct 14 ms 7636 KB Output is correct
81 Correct 18 ms 7764 KB Output is correct
82 Correct 15 ms 7672 KB Output is correct
83 Correct 17 ms 7764 KB Output is correct
84 Correct 15 ms 7692 KB Output is correct
85 Correct 16 ms 7756 KB Output is correct
86 Correct 15 ms 7636 KB Output is correct
87 Correct 66 ms 8192 KB Output is correct
88 Correct 181 ms 9860 KB Output is correct
89 Correct 708 ms 15920 KB Output is correct
90 Correct 715 ms 16004 KB Output is correct
91 Correct 688 ms 16020 KB Output is correct
92 Correct 278 ms 22032 KB Output is correct
93 Correct 271 ms 22148 KB Output is correct
94 Correct 262 ms 21940 KB Output is correct
95 Correct 300 ms 20060 KB Output is correct
96 Correct 302 ms 20284 KB Output is correct
97 Correct 280 ms 20228 KB Output is correct
98 Correct 306 ms 20292 KB Output is correct
99 Correct 293 ms 18320 KB Output is correct
100 Correct 1020 ms 16244 KB Output is correct
101 Correct 1000 ms 16476 KB Output is correct
102 Correct 1028 ms 16512 KB Output is correct
103 Correct 1006 ms 16528 KB Output is correct
104 Correct 358 ms 18264 KB Output is correct
105 Correct 328 ms 18312 KB Output is correct
106 Correct 343 ms 18236 KB Output is correct
107 Correct 671 ms 14708 KB Output is correct
108 Correct 734 ms 14944 KB Output is correct
109 Correct 736 ms 15288 KB Output is correct
110 Correct 243 ms 21020 KB Output is correct
111 Correct 297 ms 19864 KB Output is correct
112 Correct 330 ms 21324 KB Output is correct
113 Correct 333 ms 17288 KB Output is correct
114 Correct 1062 ms 16156 KB Output is correct
115 Correct 1237 ms 15376 KB Output is correct
116 Correct 426 ms 17240 KB Output is correct
117 Correct 364 ms 16784 KB Output is correct
118 Correct 397 ms 16392 KB Output is correct
119 Correct 360 ms 16164 KB Output is correct
120 Correct 344 ms 16020 KB Output is correct
121 Correct 400 ms 15656 KB Output is correct
122 Correct 422 ms 15472 KB Output is correct
123 Correct 534 ms 16820 KB Output is correct
124 Correct 491 ms 16416 KB Output is correct
125 Correct 474 ms 16216 KB Output is correct
126 Correct 416 ms 16164 KB Output is correct
127 Correct 412 ms 15752 KB Output is correct
128 Correct 427 ms 15532 KB Output is correct