Submission #84374

# Submission time Handle Problem Language Result Execution time Memory
84374 2018-11-14T18:49:01 Z Alexa2001 Construction of Highway (JOI18_construction) C++17
100 / 100
770 ms 156840 KB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 1e5 + 5;
typedef long long ll;

struct Val
{
    int val, cnt;
};

vector<int> v[Nmax];
vector<Val> A;
int n, x[Nmax], y[Nmax], val[Nmax], t[Nmax];

struct AIB
{
    int a[Nmax], n;

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

    void update(int pos, int val)
    {
        for(; pos; pos-=ub(pos)) a[pos] += val;
    }

    int query(int pos)
    {
        int ans = 0;
        for(; pos<=n; pos+=ub(pos)) ans += a[pos];
        return ans;
    }

    void shrink(int n)
    {
        this->n = n;
        int i;
        for(i=0; i<=n; ++i) a[i] = 0;
    }
} aib;


namespace hld
{
    static int where[Nmax], pos[Nmax], w[Nmax];
    static vector<int> p[Nmax];
    static deque<Val> z[Nmax];

    void dfs(int node)
    {
        w[node] = 1;
        for(auto son : v[node])
        {
            dfs(son);
            w[node] += w[son];
        }
    }

    void prepare_chains(int node)
    {
        static int chains = 1;

        where[node] = chains;
        p[where[node]].push_back(node);
        pos[node] = p[where[node]].size();

        if(v[node].empty()) return;

        int xson = v[node][0];
        for(auto son : v[node])
            if(w[son] > w[xson]) xson = son;

        prepare_chains(xson);

        for(auto son : v[node])
            if(son != xson)
            {
                ++chains;
                prepare_chains(son);
            }
    }

    void path(int node, vector<Val> &A)
    {
        int s = 0, i, id = where[node], len = pos[node];
        vector<Val> curr;

        for(i=0; i<z[id].size() && s + z[id][i].cnt <= len; ++i)
        {
            curr.push_back(z[id][i]);
            s += z[id][i].cnt;
        }

        if(i < z[id].size())
            curr.push_back({ z[id][i].val, len - s });

        reverse(curr.begin(), curr.end());
        for(auto it : curr) A.push_back(it);

        if(id == 1) return;
        path(t[p[id][0]], A);
    }

    void update(int node, int val)
    {
        int id = where[node], len = pos[node];

        while(z[id].size() && len >= z[id][0].cnt) len -= z[id][0].cnt, z[id].pop_front();

        if(z[id].empty() && len == 1) /// this is the chain that includes the new node
        {
            z[id].push_back({val, pos[node]});
            if(id == 1) return;
            update(t[p[id][0]], val);
            return;
        }

        /// otherwise the current chain is a "normal" one

        if(z[id].size())
            z[id][0].cnt -= len;
        else assert(len == 0);

        z[id].push_front({val, pos[node]});

        if(id == 1) return;
        update(t[p[id][0]], val);
    }
};

ll solve(vector<Val> &A) /// count the number of inversions
{
    reverse(A.begin(), A.end());

    map<int,int> mp;
    for(auto it : A) mp[it.val] = 1;
    int cnt = 1;
    for(auto &it : mp) it.second = ++cnt;
    for(auto &it : A) it.val = mp[it.val];

    aib.shrink(cnt);
    ll ans = 0;
    for(auto &it : A)
    {
        ans += (ll) aib.query(it.val + 1) * it.cnt;
        aib.update(it.val, it.cnt);
    }
    return ans;
}

int main()
{
  //  freopen("input", "r", stdin);
    cin.sync_with_stdio(false);

    int i;
    cin >> n;
    for(i=1; i<=n; ++i) cin >> val[i];

    for(i=1; i<n; ++i)
    {
        cin >> x[i] >> y[i];
        v[x[i]].push_back(y[i]);
        t[y[i]] = x[i];
    }

    hld :: dfs(1);
    hld :: prepare_chains(1);
    hld :: z[1].push_back({val[1], 1});

    for(i=1; i<n; ++i)
    {
        A.clear();
        hld :: path(x[i], A);
        cout << solve(A) << '\n';
        hld :: update(y[i], val[y[i]]);
    }

    return 0;
}

Compilation message

construction.cpp: In function 'void hld::path(int, std::vector<Val>&)':
construction.cpp:89:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(i=0; i<z[id].size() && s + z[id][i].cnt <= len; ++i)
                  ~^~~~~~~~~~~~~
construction.cpp:95:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(i < z[id].size())
            ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 76 ms 72444 KB Output is correct
2 Correct 69 ms 72512 KB Output is correct
3 Correct 74 ms 72512 KB Output is correct
4 Correct 74 ms 72836 KB Output is correct
5 Correct 75 ms 72836 KB Output is correct
6 Correct 81 ms 72836 KB Output is correct
7 Correct 74 ms 72836 KB Output is correct
8 Correct 78 ms 73032 KB Output is correct
9 Correct 81 ms 73032 KB Output is correct
10 Correct 79 ms 73032 KB Output is correct
11 Correct 73 ms 73032 KB Output is correct
12 Correct 73 ms 73124 KB Output is correct
13 Correct 75 ms 73136 KB Output is correct
14 Correct 84 ms 73228 KB Output is correct
15 Correct 93 ms 73228 KB Output is correct
16 Correct 74 ms 73228 KB Output is correct
17 Correct 79 ms 73228 KB Output is correct
18 Correct 74 ms 73228 KB Output is correct
19 Correct 81 ms 73228 KB Output is correct
20 Correct 80 ms 73232 KB Output is correct
21 Correct 79 ms 73244 KB Output is correct
22 Correct 84 ms 73436 KB Output is correct
23 Correct 82 ms 73436 KB Output is correct
24 Correct 74 ms 73464 KB Output is correct
25 Correct 79 ms 73464 KB Output is correct
26 Correct 81 ms 73464 KB Output is correct
27 Correct 73 ms 73464 KB Output is correct
28 Correct 79 ms 73464 KB Output is correct
29 Correct 80 ms 73504 KB Output is correct
30 Correct 74 ms 73504 KB Output is correct
31 Correct 74 ms 73504 KB Output is correct
32 Correct 81 ms 73504 KB Output is correct
33 Correct 80 ms 73504 KB Output is correct
34 Correct 82 ms 73504 KB Output is correct
35 Correct 73 ms 73504 KB Output is correct
36 Correct 81 ms 73560 KB Output is correct
37 Correct 80 ms 73560 KB Output is correct
38 Correct 73 ms 73560 KB Output is correct
39 Correct 81 ms 73560 KB Output is correct
40 Correct 81 ms 73560 KB Output is correct
41 Correct 82 ms 73596 KB Output is correct
42 Correct 82 ms 73596 KB Output is correct
43 Correct 81 ms 73596 KB Output is correct
44 Correct 81 ms 73596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 72444 KB Output is correct
2 Correct 69 ms 72512 KB Output is correct
3 Correct 74 ms 72512 KB Output is correct
4 Correct 74 ms 72836 KB Output is correct
5 Correct 75 ms 72836 KB Output is correct
6 Correct 81 ms 72836 KB Output is correct
7 Correct 74 ms 72836 KB Output is correct
8 Correct 78 ms 73032 KB Output is correct
9 Correct 81 ms 73032 KB Output is correct
10 Correct 79 ms 73032 KB Output is correct
11 Correct 73 ms 73032 KB Output is correct
12 Correct 73 ms 73124 KB Output is correct
13 Correct 75 ms 73136 KB Output is correct
14 Correct 84 ms 73228 KB Output is correct
15 Correct 93 ms 73228 KB Output is correct
16 Correct 74 ms 73228 KB Output is correct
17 Correct 79 ms 73228 KB Output is correct
18 Correct 74 ms 73228 KB Output is correct
19 Correct 81 ms 73228 KB Output is correct
20 Correct 80 ms 73232 KB Output is correct
21 Correct 79 ms 73244 KB Output is correct
22 Correct 84 ms 73436 KB Output is correct
23 Correct 82 ms 73436 KB Output is correct
24 Correct 74 ms 73464 KB Output is correct
25 Correct 79 ms 73464 KB Output is correct
26 Correct 81 ms 73464 KB Output is correct
27 Correct 73 ms 73464 KB Output is correct
28 Correct 79 ms 73464 KB Output is correct
29 Correct 80 ms 73504 KB Output is correct
30 Correct 74 ms 73504 KB Output is correct
31 Correct 74 ms 73504 KB Output is correct
32 Correct 81 ms 73504 KB Output is correct
33 Correct 80 ms 73504 KB Output is correct
34 Correct 82 ms 73504 KB Output is correct
35 Correct 73 ms 73504 KB Output is correct
36 Correct 81 ms 73560 KB Output is correct
37 Correct 80 ms 73560 KB Output is correct
38 Correct 73 ms 73560 KB Output is correct
39 Correct 81 ms 73560 KB Output is correct
40 Correct 81 ms 73560 KB Output is correct
41 Correct 82 ms 73596 KB Output is correct
42 Correct 82 ms 73596 KB Output is correct
43 Correct 81 ms 73596 KB Output is correct
44 Correct 81 ms 73596 KB Output is correct
45 Correct 75 ms 73632 KB Output is correct
46 Correct 81 ms 73780 KB Output is correct
47 Correct 82 ms 73868 KB Output is correct
48 Correct 83 ms 74128 KB Output is correct
49 Correct 84 ms 74396 KB Output is correct
50 Correct 83 ms 74396 KB Output is correct
51 Correct 83 ms 74420 KB Output is correct
52 Correct 76 ms 74496 KB Output is correct
53 Correct 76 ms 74500 KB Output is correct
54 Correct 80 ms 74628 KB Output is correct
55 Correct 80 ms 74744 KB Output is correct
56 Correct 81 ms 74744 KB Output is correct
57 Correct 83 ms 74744 KB Output is correct
58 Correct 86 ms 74868 KB Output is correct
59 Correct 87 ms 74868 KB Output is correct
60 Correct 87 ms 74868 KB Output is correct
61 Correct 78 ms 75172 KB Output is correct
62 Correct 76 ms 75172 KB Output is correct
63 Correct 84 ms 75172 KB Output is correct
64 Correct 76 ms 75240 KB Output is correct
65 Correct 80 ms 75240 KB Output is correct
66 Correct 84 ms 75272 KB Output is correct
67 Correct 83 ms 75272 KB Output is correct
68 Correct 82 ms 75504 KB Output is correct
69 Correct 76 ms 75620 KB Output is correct
70 Correct 85 ms 75620 KB Output is correct
71 Correct 77 ms 75620 KB Output is correct
72 Correct 83 ms 75620 KB Output is correct
73 Correct 86 ms 75620 KB Output is correct
74 Correct 76 ms 75772 KB Output is correct
75 Correct 85 ms 75772 KB Output is correct
76 Correct 78 ms 75772 KB Output is correct
77 Correct 77 ms 75796 KB Output is correct
78 Correct 79 ms 75860 KB Output is correct
79 Correct 78 ms 75892 KB Output is correct
80 Correct 76 ms 75984 KB Output is correct
81 Correct 78 ms 76004 KB Output is correct
82 Correct 78 ms 76076 KB Output is correct
83 Correct 79 ms 76260 KB Output is correct
84 Correct 77 ms 76260 KB Output is correct
85 Correct 78 ms 76328 KB Output is correct
86 Correct 78 ms 76328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 72444 KB Output is correct
2 Correct 69 ms 72512 KB Output is correct
3 Correct 74 ms 72512 KB Output is correct
4 Correct 74 ms 72836 KB Output is correct
5 Correct 75 ms 72836 KB Output is correct
6 Correct 81 ms 72836 KB Output is correct
7 Correct 74 ms 72836 KB Output is correct
8 Correct 78 ms 73032 KB Output is correct
9 Correct 81 ms 73032 KB Output is correct
10 Correct 79 ms 73032 KB Output is correct
11 Correct 73 ms 73032 KB Output is correct
12 Correct 73 ms 73124 KB Output is correct
13 Correct 75 ms 73136 KB Output is correct
14 Correct 84 ms 73228 KB Output is correct
15 Correct 93 ms 73228 KB Output is correct
16 Correct 74 ms 73228 KB Output is correct
17 Correct 79 ms 73228 KB Output is correct
18 Correct 74 ms 73228 KB Output is correct
19 Correct 81 ms 73228 KB Output is correct
20 Correct 80 ms 73232 KB Output is correct
21 Correct 79 ms 73244 KB Output is correct
22 Correct 84 ms 73436 KB Output is correct
23 Correct 82 ms 73436 KB Output is correct
24 Correct 74 ms 73464 KB Output is correct
25 Correct 79 ms 73464 KB Output is correct
26 Correct 81 ms 73464 KB Output is correct
27 Correct 73 ms 73464 KB Output is correct
28 Correct 79 ms 73464 KB Output is correct
29 Correct 80 ms 73504 KB Output is correct
30 Correct 74 ms 73504 KB Output is correct
31 Correct 74 ms 73504 KB Output is correct
32 Correct 81 ms 73504 KB Output is correct
33 Correct 80 ms 73504 KB Output is correct
34 Correct 82 ms 73504 KB Output is correct
35 Correct 73 ms 73504 KB Output is correct
36 Correct 81 ms 73560 KB Output is correct
37 Correct 80 ms 73560 KB Output is correct
38 Correct 73 ms 73560 KB Output is correct
39 Correct 81 ms 73560 KB Output is correct
40 Correct 81 ms 73560 KB Output is correct
41 Correct 82 ms 73596 KB Output is correct
42 Correct 82 ms 73596 KB Output is correct
43 Correct 81 ms 73596 KB Output is correct
44 Correct 81 ms 73596 KB Output is correct
45 Correct 75 ms 73632 KB Output is correct
46 Correct 81 ms 73780 KB Output is correct
47 Correct 82 ms 73868 KB Output is correct
48 Correct 83 ms 74128 KB Output is correct
49 Correct 84 ms 74396 KB Output is correct
50 Correct 83 ms 74396 KB Output is correct
51 Correct 83 ms 74420 KB Output is correct
52 Correct 76 ms 74496 KB Output is correct
53 Correct 76 ms 74500 KB Output is correct
54 Correct 80 ms 74628 KB Output is correct
55 Correct 80 ms 74744 KB Output is correct
56 Correct 81 ms 74744 KB Output is correct
57 Correct 83 ms 74744 KB Output is correct
58 Correct 86 ms 74868 KB Output is correct
59 Correct 87 ms 74868 KB Output is correct
60 Correct 87 ms 74868 KB Output is correct
61 Correct 78 ms 75172 KB Output is correct
62 Correct 76 ms 75172 KB Output is correct
63 Correct 84 ms 75172 KB Output is correct
64 Correct 76 ms 75240 KB Output is correct
65 Correct 80 ms 75240 KB Output is correct
66 Correct 84 ms 75272 KB Output is correct
67 Correct 83 ms 75272 KB Output is correct
68 Correct 82 ms 75504 KB Output is correct
69 Correct 76 ms 75620 KB Output is correct
70 Correct 85 ms 75620 KB Output is correct
71 Correct 77 ms 75620 KB Output is correct
72 Correct 83 ms 75620 KB Output is correct
73 Correct 86 ms 75620 KB Output is correct
74 Correct 76 ms 75772 KB Output is correct
75 Correct 85 ms 75772 KB Output is correct
76 Correct 78 ms 75772 KB Output is correct
77 Correct 77 ms 75796 KB Output is correct
78 Correct 79 ms 75860 KB Output is correct
79 Correct 78 ms 75892 KB Output is correct
80 Correct 76 ms 75984 KB Output is correct
81 Correct 78 ms 76004 KB Output is correct
82 Correct 78 ms 76076 KB Output is correct
83 Correct 79 ms 76260 KB Output is correct
84 Correct 77 ms 76260 KB Output is correct
85 Correct 78 ms 76328 KB Output is correct
86 Correct 78 ms 76328 KB Output is correct
87 Correct 97 ms 76856 KB Output is correct
88 Correct 164 ms 78856 KB Output is correct
89 Correct 468 ms 85364 KB Output is correct
90 Correct 494 ms 87636 KB Output is correct
91 Correct 473 ms 89552 KB Output is correct
92 Correct 220 ms 96616 KB Output is correct
93 Correct 191 ms 98784 KB Output is correct
94 Correct 211 ms 100864 KB Output is correct
95 Correct 222 ms 103364 KB Output is correct
96 Correct 272 ms 105432 KB Output is correct
97 Correct 248 ms 107544 KB Output is correct
98 Correct 267 ms 109704 KB Output is correct
99 Correct 217 ms 109704 KB Output is correct
100 Correct 637 ms 109704 KB Output is correct
101 Correct 675 ms 110072 KB Output is correct
102 Correct 730 ms 112020 KB Output is correct
103 Correct 770 ms 114160 KB Output is correct
104 Correct 264 ms 118812 KB Output is correct
105 Correct 225 ms 120836 KB Output is correct
106 Correct 206 ms 122988 KB Output is correct
107 Correct 350 ms 122988 KB Output is correct
108 Correct 466 ms 123372 KB Output is correct
109 Correct 511 ms 125080 KB Output is correct
110 Correct 190 ms 131516 KB Output is correct
111 Correct 216 ms 134036 KB Output is correct
112 Correct 194 ms 134036 KB Output is correct
113 Correct 185 ms 134036 KB Output is correct
114 Correct 567 ms 134036 KB Output is correct
115 Correct 739 ms 134656 KB Output is correct
116 Correct 202 ms 138588 KB Output is correct
117 Correct 209 ms 139280 KB Output is correct
118 Correct 214 ms 140684 KB Output is correct
119 Correct 223 ms 142228 KB Output is correct
120 Correct 200 ms 144304 KB Output is correct
121 Correct 214 ms 145448 KB Output is correct
122 Correct 204 ms 146700 KB Output is correct
123 Correct 220 ms 149208 KB Output is correct
124 Correct 240 ms 150648 KB Output is correct
125 Correct 232 ms 152300 KB Output is correct
126 Correct 220 ms 154408 KB Output is correct
127 Correct 247 ms 155460 KB Output is correct
128 Correct 237 ms 156840 KB Output is correct