답안 #203229

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
203229 2020-02-19T20:52:40 Z tri Construction of Highway (JOI18_construction) C++14
100 / 100
1699 ms 20752 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;

#define pb push_back
#define f first
#define s second

namespace debug {
    const int DEBUG = true;

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x);

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x);

    template<class T>
    void pr(const vector<T> &x);

    template<class T>
    void pr(const set<T> &x);

    template<class T1, class T2>
    void pr(const map<T1, T2> &x);

    template<class T>
    void pr(const T &x) { if (DEBUG) cout << x; }

    template<class T, class... Ts>
    void pr(const T &first, const Ts &... rest) { pr(first), pr(rest...); }

    template<class T1, class T2>
    void pr(const pair<T1, T2> &x) { pr("{", x.f, ", ", x.s, "}"); }

    template<class T>
    void prIn(const T &x) {
        pr("{");
        bool fst = 1;
        for (auto &a : x) {
            pr(fst ? "" : ", ", a), fst = 0;
        }
        pr("}");
    }

    template<class T, size_t SZ>
    void pr(const array<T, SZ> &x) { prIn(x); }

    template<class T>
    void pr(const vector<T> &x) { prIn(x); }

    template<class T>
    void pr(const set<T> &x) { prIn(x); }

    template<class T1, class T2>
    void pr(const map<T1, T2> &x) { prIn(x); }

    void ps() { pr("\n"), cout << flush; }

    template<class Arg, class... Args>
    void ps(const Arg &first, const Args &... rest) {
        pr(first, " ");
        ps(rest...);
    }
}
using namespace debug;

const int MAXN = 1e5 + 10;

int N;

struct LSegTr {
    int tr[4 * MAXN];

    void push(int i, int l, int r) {
        if (l == r || tr[i] == -1) return;

        tr[i * 2] = tr[i];
        tr[i * 2 + 1] = tr[i];

        tr[i] = -1;
    }

    int q(int i, int l, int r, int x) {
        push(i, l, r);
        if (l == r) {
            return tr[i];
        }
        int mid = (l + r) / 2;
        if (x <= mid) {
            return q(i * 2, l, mid, x);
        } else {
            return q(i * 2 + 1, mid + 1, r, x);
        }
    }

    void u(int i, int l, int r, int s, int e, int d) {
        push(i, l, r);
        if (e < l || r < s) {
            return;
        }
        if (s <= l && r <= e) {
            tr[i] = d;
            return;
        }
        int mid = (l + r) / 2;
        u(i * 2, l, mid, s, e, d);
        u(i * 2 + 1, mid + 1, r, s, e, d);
    }

    int q(int x) {
        return q(1, 0, N - 1, x);
    }

    void u(int s, int e, int d) {
        return u(1, 0, N - 1, s, e, d);
    }
};

int rt;

vi aL[MAXN];

int p[MAXN], d[MAXN], hC[MAXN], cS[MAXN];

LSegTr tr;
int tI[MAXN];
int tV[MAXN];

int initDFS(int cV) {
    int cCnt = 1, mSCnt = 0;

    for (int aV : aL[cV]) {
        if (aV != p[cV]) {
            p[aV] = cV;
            d[aV] = d[cV] + 1;

            int aCnt = initDFS(aV);
            if (aCnt > mSCnt) {
                mSCnt = aCnt;
                hC[cV] = aV;
            }
            cCnt += aCnt;
        }
    }
    return cCnt;
}

void init() {
    fill(hC, hC + MAXN, -1);
    p[rt] = -1;
    initDFS(rt);

    int nTI = 0;
    for (int curS = 0; curS < N; curS++) {
        if (curS == rt || hC[p[curS]] != curS) {
            for (int cV = curS; cV != -1; cV = hC[cV]) {
                cS[cV] = curS;
                tV[nTI] = cV;
                tI[cV] = nTI++;
            }
        }
    }
}

int fSame(int l, int r, int cT) {
    assert(tr.q(r) == cT);
    while (l != r) {
        int mid = (l + r) / 2;
        if (tr.q(mid) == cT) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }
    assert(tr.q(l) == cT);
    return l;
}

vector<pi> path;

void query(int cV) {
    path.clear();

    while (cV != -1) {
        int cCS = cS[cV];
        int cT = tr.q(tI[cV]);

        int cFS = fSame(tI[cCS], tI[cV], cT);

        path.pb({cT, tI[cV] - cFS + 1});

        cV = p[tV[cFS]];
    }
}

void update(int cV, int cT) {
    while (cV != -1) {
        int cCS = cS[cV];

        tr.u(tI[cCS], tI[cV], cT);

        cV = p[cCS];
    }
}

int color[MAXN];

vector<pi> proc;

void processPath() {
    proc.clear();
    reverse(path.begin(), path.end());

    int cC = -1;
    int cCnt = 0;

    for (pi x : path) {
        if (color[x.f] == cC) {
            cCnt += x.s;
        } else {
            if (cCnt > 0) {
                proc.pb({cC, cCnt});
            }

            cC = color[x.f];
            cCnt = x.s;
        }
    }

    if (cCnt > 0) {
        proc.pb({cC, cCnt});
    }
}

int cnt = 0;

vector<pi> cntInversions(vector<pi> &input) {
    if (input.size() == 1) {
        return input;
    }

    int mid = input.size() / 2;
    vector<pi> a = vector<pi>(input.begin(), input.begin() + mid);
    vector<pi> b = vector<pi>(input.begin() + mid, input.end());

    a = cntInversions(a);
    b = cntInversions(b);

    vector<pi> combine;
    int nA = 0;
    int nB = 0;

    int remA = 0;
    for (pi x : a) {
        remA += x.s;
    }

    while (nA < a.size() || nB < b.size()) {
        if (nA < a.size()) {
            if (nB < b.size()) {
                if (b[nB].f < a[nA].f) {
                    cnt += remA * b[nB].s;
                    combine.pb(b[nB++]);
                } else {
                    remA -= a[nA].s;
                    combine.pb(a[nA++]);
                }
            } else {
                remA -= a[nA].s;
                combine.pb(a[nA++]);
            }
        } else {
            assert(nB < b.size());
            combine.pb(b[nB++]);
        }
    }
    return combine;
}

int main() {
    cin >> N;

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

    vi queries;
    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        aL[a].pb(b);
        queries.pb(b);
    }

    rt = 0;
    init();

    fill(tr.tr, tr.tr + 4 * MAXN, -1);
    update(rt, 0);

    for (int cQ : queries) {
//        ps(cQ);
        query(p[cQ]);
//        ps(path);
        processPath();
//        ps(proc);

        cnt = 0;
        cntInversions(proc);
//        pr("ans: ");
        cout << cnt << endl;

        update(cQ, cQ);
    }
}

Compilation message

construction.cpp: In function 'std::vector<std::pair<int, int> > cntInversions(std::vector<std::pair<int, int> >&)':
construction.cpp:268:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (nA < a.size() || nB < b.size()) {
            ~~~^~~~~~~~~~
construction.cpp:268:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (nA < a.size() || nB < b.size()) {
                             ~~~^~~~~~~~~~
construction.cpp:269:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (nA < a.size()) {
             ~~~^~~~~~~~~~
construction.cpp:270:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (nB < b.size()) {
                 ~~~^~~~~~~~~~
In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from construction.cpp:1:
construction.cpp:283:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             assert(nB < b.size());
                    ~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4728 KB Output is correct
2 Correct 7 ms 4600 KB Output is correct
3 Correct 8 ms 4728 KB Output is correct
4 Correct 10 ms 4600 KB Output is correct
5 Correct 10 ms 4728 KB Output is correct
6 Correct 10 ms 4728 KB Output is correct
7 Correct 14 ms 4732 KB Output is correct
8 Correct 9 ms 4728 KB Output is correct
9 Correct 10 ms 4728 KB Output is correct
10 Correct 9 ms 4728 KB Output is correct
11 Correct 12 ms 4728 KB Output is correct
12 Correct 10 ms 4728 KB Output is correct
13 Correct 9 ms 4732 KB Output is correct
14 Correct 9 ms 4728 KB Output is correct
15 Correct 11 ms 4728 KB Output is correct
16 Correct 11 ms 4728 KB Output is correct
17 Correct 11 ms 4728 KB Output is correct
18 Correct 13 ms 4728 KB Output is correct
19 Correct 10 ms 4728 KB Output is correct
20 Correct 9 ms 4728 KB Output is correct
21 Correct 10 ms 4728 KB Output is correct
22 Correct 9 ms 4728 KB Output is correct
23 Correct 10 ms 4728 KB Output is correct
24 Correct 10 ms 4728 KB Output is correct
25 Correct 10 ms 4728 KB Output is correct
26 Correct 11 ms 4856 KB Output is correct
27 Correct 9 ms 4728 KB Output is correct
28 Correct 9 ms 4728 KB Output is correct
29 Correct 9 ms 4728 KB Output is correct
30 Correct 11 ms 4728 KB Output is correct
31 Correct 11 ms 4728 KB Output is correct
32 Correct 13 ms 4728 KB Output is correct
33 Correct 10 ms 4728 KB Output is correct
34 Correct 10 ms 4728 KB Output is correct
35 Correct 9 ms 4728 KB Output is correct
36 Correct 10 ms 4728 KB Output is correct
37 Correct 9 ms 4728 KB Output is correct
38 Correct 10 ms 4728 KB Output is correct
39 Correct 9 ms 4728 KB Output is correct
40 Correct 10 ms 4728 KB Output is correct
41 Correct 10 ms 4728 KB Output is correct
42 Correct 11 ms 4732 KB Output is correct
43 Correct 10 ms 4728 KB Output is correct
44 Correct 10 ms 4728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4728 KB Output is correct
2 Correct 7 ms 4600 KB Output is correct
3 Correct 8 ms 4728 KB Output is correct
4 Correct 10 ms 4600 KB Output is correct
5 Correct 10 ms 4728 KB Output is correct
6 Correct 10 ms 4728 KB Output is correct
7 Correct 14 ms 4732 KB Output is correct
8 Correct 9 ms 4728 KB Output is correct
9 Correct 10 ms 4728 KB Output is correct
10 Correct 9 ms 4728 KB Output is correct
11 Correct 12 ms 4728 KB Output is correct
12 Correct 10 ms 4728 KB Output is correct
13 Correct 9 ms 4732 KB Output is correct
14 Correct 9 ms 4728 KB Output is correct
15 Correct 11 ms 4728 KB Output is correct
16 Correct 11 ms 4728 KB Output is correct
17 Correct 11 ms 4728 KB Output is correct
18 Correct 13 ms 4728 KB Output is correct
19 Correct 10 ms 4728 KB Output is correct
20 Correct 9 ms 4728 KB Output is correct
21 Correct 10 ms 4728 KB Output is correct
22 Correct 9 ms 4728 KB Output is correct
23 Correct 10 ms 4728 KB Output is correct
24 Correct 10 ms 4728 KB Output is correct
25 Correct 10 ms 4728 KB Output is correct
26 Correct 11 ms 4856 KB Output is correct
27 Correct 9 ms 4728 KB Output is correct
28 Correct 9 ms 4728 KB Output is correct
29 Correct 9 ms 4728 KB Output is correct
30 Correct 11 ms 4728 KB Output is correct
31 Correct 11 ms 4728 KB Output is correct
32 Correct 13 ms 4728 KB Output is correct
33 Correct 10 ms 4728 KB Output is correct
34 Correct 10 ms 4728 KB Output is correct
35 Correct 9 ms 4728 KB Output is correct
36 Correct 10 ms 4728 KB Output is correct
37 Correct 9 ms 4728 KB Output is correct
38 Correct 10 ms 4728 KB Output is correct
39 Correct 9 ms 4728 KB Output is correct
40 Correct 10 ms 4728 KB Output is correct
41 Correct 10 ms 4728 KB Output is correct
42 Correct 11 ms 4732 KB Output is correct
43 Correct 10 ms 4728 KB Output is correct
44 Correct 10 ms 4728 KB Output is correct
45 Correct 13 ms 4728 KB Output is correct
46 Correct 33 ms 4984 KB Output is correct
47 Correct 34 ms 4984 KB Output is correct
48 Correct 32 ms 4856 KB Output is correct
49 Correct 25 ms 5240 KB Output is correct
50 Correct 24 ms 5368 KB Output is correct
51 Correct 24 ms 5240 KB Output is correct
52 Correct 26 ms 5112 KB Output is correct
53 Correct 27 ms 5112 KB Output is correct
54 Correct 27 ms 5112 KB Output is correct
55 Correct 28 ms 5112 KB Output is correct
56 Correct 26 ms 5112 KB Output is correct
57 Correct 46 ms 4856 KB Output is correct
58 Correct 47 ms 4984 KB Output is correct
59 Correct 48 ms 4860 KB Output is correct
60 Correct 48 ms 4856 KB Output is correct
61 Correct 28 ms 5116 KB Output is correct
62 Correct 28 ms 5112 KB Output is correct
63 Correct 28 ms 5112 KB Output is correct
64 Correct 29 ms 4856 KB Output is correct
65 Correct 31 ms 4984 KB Output is correct
66 Correct 31 ms 4856 KB Output is correct
67 Correct 33 ms 4984 KB Output is correct
68 Correct 23 ms 5240 KB Output is correct
69 Correct 27 ms 5112 KB Output is correct
70 Correct 25 ms 5112 KB Output is correct
71 Correct 25 ms 4984 KB Output is correct
72 Correct 46 ms 4856 KB Output is correct
73 Correct 47 ms 4856 KB Output is correct
74 Correct 27 ms 5112 KB Output is correct
75 Correct 28 ms 4984 KB Output is correct
76 Correct 27 ms 4984 KB Output is correct
77 Correct 26 ms 4984 KB Output is correct
78 Correct 25 ms 4984 KB Output is correct
79 Correct 26 ms 4984 KB Output is correct
80 Correct 26 ms 4984 KB Output is correct
81 Correct 30 ms 4984 KB Output is correct
82 Correct 29 ms 5032 KB Output is correct
83 Correct 28 ms 4984 KB Output is correct
84 Correct 27 ms 4984 KB Output is correct
85 Correct 28 ms 4856 KB Output is correct
86 Correct 27 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4728 KB Output is correct
2 Correct 7 ms 4600 KB Output is correct
3 Correct 8 ms 4728 KB Output is correct
4 Correct 10 ms 4600 KB Output is correct
5 Correct 10 ms 4728 KB Output is correct
6 Correct 10 ms 4728 KB Output is correct
7 Correct 14 ms 4732 KB Output is correct
8 Correct 9 ms 4728 KB Output is correct
9 Correct 10 ms 4728 KB Output is correct
10 Correct 9 ms 4728 KB Output is correct
11 Correct 12 ms 4728 KB Output is correct
12 Correct 10 ms 4728 KB Output is correct
13 Correct 9 ms 4732 KB Output is correct
14 Correct 9 ms 4728 KB Output is correct
15 Correct 11 ms 4728 KB Output is correct
16 Correct 11 ms 4728 KB Output is correct
17 Correct 11 ms 4728 KB Output is correct
18 Correct 13 ms 4728 KB Output is correct
19 Correct 10 ms 4728 KB Output is correct
20 Correct 9 ms 4728 KB Output is correct
21 Correct 10 ms 4728 KB Output is correct
22 Correct 9 ms 4728 KB Output is correct
23 Correct 10 ms 4728 KB Output is correct
24 Correct 10 ms 4728 KB Output is correct
25 Correct 10 ms 4728 KB Output is correct
26 Correct 11 ms 4856 KB Output is correct
27 Correct 9 ms 4728 KB Output is correct
28 Correct 9 ms 4728 KB Output is correct
29 Correct 9 ms 4728 KB Output is correct
30 Correct 11 ms 4728 KB Output is correct
31 Correct 11 ms 4728 KB Output is correct
32 Correct 13 ms 4728 KB Output is correct
33 Correct 10 ms 4728 KB Output is correct
34 Correct 10 ms 4728 KB Output is correct
35 Correct 9 ms 4728 KB Output is correct
36 Correct 10 ms 4728 KB Output is correct
37 Correct 9 ms 4728 KB Output is correct
38 Correct 10 ms 4728 KB Output is correct
39 Correct 9 ms 4728 KB Output is correct
40 Correct 10 ms 4728 KB Output is correct
41 Correct 10 ms 4728 KB Output is correct
42 Correct 11 ms 4732 KB Output is correct
43 Correct 10 ms 4728 KB Output is correct
44 Correct 10 ms 4728 KB Output is correct
45 Correct 13 ms 4728 KB Output is correct
46 Correct 33 ms 4984 KB Output is correct
47 Correct 34 ms 4984 KB Output is correct
48 Correct 32 ms 4856 KB Output is correct
49 Correct 25 ms 5240 KB Output is correct
50 Correct 24 ms 5368 KB Output is correct
51 Correct 24 ms 5240 KB Output is correct
52 Correct 26 ms 5112 KB Output is correct
53 Correct 27 ms 5112 KB Output is correct
54 Correct 27 ms 5112 KB Output is correct
55 Correct 28 ms 5112 KB Output is correct
56 Correct 26 ms 5112 KB Output is correct
57 Correct 46 ms 4856 KB Output is correct
58 Correct 47 ms 4984 KB Output is correct
59 Correct 48 ms 4860 KB Output is correct
60 Correct 48 ms 4856 KB Output is correct
61 Correct 28 ms 5116 KB Output is correct
62 Correct 28 ms 5112 KB Output is correct
63 Correct 28 ms 5112 KB Output is correct
64 Correct 29 ms 4856 KB Output is correct
65 Correct 31 ms 4984 KB Output is correct
66 Correct 31 ms 4856 KB Output is correct
67 Correct 33 ms 4984 KB Output is correct
68 Correct 23 ms 5240 KB Output is correct
69 Correct 27 ms 5112 KB Output is correct
70 Correct 25 ms 5112 KB Output is correct
71 Correct 25 ms 4984 KB Output is correct
72 Correct 46 ms 4856 KB Output is correct
73 Correct 47 ms 4856 KB Output is correct
74 Correct 27 ms 5112 KB Output is correct
75 Correct 28 ms 4984 KB Output is correct
76 Correct 27 ms 4984 KB Output is correct
77 Correct 26 ms 4984 KB Output is correct
78 Correct 25 ms 4984 KB Output is correct
79 Correct 26 ms 4984 KB Output is correct
80 Correct 26 ms 4984 KB Output is correct
81 Correct 30 ms 4984 KB Output is correct
82 Correct 29 ms 5032 KB Output is correct
83 Correct 28 ms 4984 KB Output is correct
84 Correct 27 ms 4984 KB Output is correct
85 Correct 28 ms 4856 KB Output is correct
86 Correct 27 ms 4984 KB Output is correct
87 Correct 77 ms 5368 KB Output is correct
88 Correct 238 ms 6648 KB Output is correct
89 Correct 935 ms 11504 KB Output is correct
90 Correct 953 ms 11632 KB Output is correct
91 Correct 969 ms 11756 KB Output is correct
92 Correct 513 ms 20716 KB Output is correct
93 Correct 507 ms 20724 KB Output is correct
94 Correct 544 ms 20752 KB Output is correct
95 Correct 595 ms 17168 KB Output is correct
96 Correct 626 ms 17452 KB Output is correct
97 Correct 619 ms 17500 KB Output is correct
98 Correct 647 ms 17648 KB Output is correct
99 Correct 619 ms 15344 KB Output is correct
100 Correct 1648 ms 11760 KB Output is correct
101 Correct 1699 ms 12008 KB Output is correct
102 Correct 1614 ms 12140 KB Output is correct
103 Correct 1627 ms 12016 KB Output is correct
104 Correct 698 ms 15488 KB Output is correct
105 Correct 671 ms 15380 KB Output is correct
106 Correct 700 ms 15340 KB Output is correct
107 Correct 858 ms 10608 KB Output is correct
108 Correct 969 ms 10956 KB Output is correct
109 Correct 947 ms 11156 KB Output is correct
110 Correct 473 ms 20208 KB Output is correct
111 Correct 581 ms 16968 KB Output is correct
112 Correct 570 ms 17224 KB Output is correct
113 Correct 563 ms 14832 KB Output is correct
114 Correct 1510 ms 11688 KB Output is correct
115 Correct 1554 ms 11576 KB Output is correct
116 Correct 649 ms 14704 KB Output is correct
117 Correct 602 ms 13036 KB Output is correct
118 Correct 606 ms 12524 KB Output is correct
119 Correct 602 ms 12168 KB Output is correct
120 Correct 574 ms 12912 KB Output is correct
121 Correct 572 ms 12140 KB Output is correct
122 Correct 595 ms 11888 KB Output is correct
123 Correct 696 ms 13168 KB Output is correct
124 Correct 691 ms 12532 KB Output is correct
125 Correct 689 ms 12396 KB Output is correct
126 Correct 665 ms 12784 KB Output is correct
127 Correct 667 ms 12400 KB Output is correct
128 Correct 673 ms 11796 KB Output is correct