Submission #677418

# Submission time Handle Problem Language Result Execution time Memory
677418 2023-01-03T08:57:50 Z Tien_Noob Construction of Highway (JOI18_construction) C++17
100 / 100
250 ms 17824 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 1e5;
vector<int> adj[N + 1];
int nchain, chain[N + 1], head[N + 1], pos[N + 1], timer, sz[N + 1];
int n, par[N + 1], b[N + 1], c[N + 1];
vector<pair<int, int> > dp[N + 1];
struct FenwickTree
{
    int Tree[N + 1];
    void add(int pos, int val)
    {
        for (; pos <= n; pos |= pos + 1)
        {
            Tree[pos] += val;
        }
    }
    int sum(int pos)
    {
        int ret = 0;
        for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
        {
            ret += Tree[pos];
        }
        return ret;
    }
    int sum(int l, int r)
    {
        return sum(r) - sum(l - 1);
    }
} tree;
void read()
{
    vector<int> id;
    cin >> n;
    for (int i = 1; i <= n; ++ i)
    {
        cin >> c[i];
        id.push_back(i);
    }
    sort(id.begin(), id.end(), [&](const int &x, const int &y)
         {
             return c[x] < c[y];
         });
    int pre = -1, t = 0;
    for (int i : id)
    {
        if (c[i] != pre)
        {
            ++ t;
            pre = c[i];
        }
        c[i] = t;
        //cerr << c[i] << ' ';
    }
    //cerr << '\n';
    for (int i = 1; i < n; ++ i)
    {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        par[v] = u;
        b[i] = v;
    }
}
void preDFS(int u)
{
    sz[u] = 1;
    for (int v : adj[u])
    {
        preDFS(v);
        sz[u] += sz[v];
    }
}
void HLD(int u)
{
    if (head[nchain] == 0)
    {
        head[nchain] = u;
    }
    chain[u] = nchain;
    pos[u] = ++timer;
    int bigchild = -1;
    for (int v : adj[u])
    {
        if (bigchild == -1 || sz[v] > sz[bigchild])
        {
            bigchild = v;
        }
    }
    if (bigchild != -1)
    {
        HLD(bigchild);
    }
    for (int v : adj[u])
    {
        if (v == bigchild)
        {
            continue;
        }
        ++nchain;
        HLD(v);
    }
}
void solve()
{
    preDFS(1);
    HLD(1);
    dp[0] = {{1, c[1]}};
    for (int i = 1; i < n; ++ i)
    {
        long long res = 0;
        int u = par[b[i]];
        vector<pair<int, int> > total;
        int color = c[b[i]];
        while(u > 0)
        {
            vector<pair<int, int> > f;
            int v = head[chain[u]];
            int pre = pos[head[chain[u]]] - 1;
            //cerr << u << ' ' << pre << ' ' << pos[u] - pos[pre] << '\n';
            while(!dp[chain[u]].empty() && pos[dp[chain[u]].back().first] <= pos[u])
            {
                f.emplace_back(pos[dp[chain[u]].back().first] - pre, dp[chain[u]].back().second);
                pre = pos[dp[chain[u]].back().first];
                dp[chain[u]].pop_back();
            }
            if (!dp[chain[u]].empty())
            {
                f.emplace_back(pos[u] - pre, dp[chain[u]].back().second);
            }
            dp[chain[u]].emplace_back(u, color);
            u = par[v];
            move(f.begin(), f.end(), back_inserter(total));
            while(!f.empty())
            {
                res += 1LL * f.back().first * tree.sum(f.back().second - 1);
                tree.add(f.back().second, f.back().first);
                //cerr << f.back().first << ' ' << f.back().second << '\n';
                f.pop_back();
            }
        }
        dp[chain[b[i]]] = {{b[i], c[b[i]]}};
        for (auto &p : total)
        {
            tree.add(p.second, -p.first);
        }
        cout << res ;
        cout << '\n';
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case " << __ << ": ";
        read();
        solve();
    }
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 3 ms 5040 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5044 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5040 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 3 ms 5028 KB Output is correct
26 Correct 4 ms 5040 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5108 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5036 KB Output is correct
37 Correct 3 ms 5064 KB Output is correct
38 Correct 3 ms 5104 KB Output is correct
39 Correct 4 ms 5076 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 3 ms 5036 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 3 ms 5040 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5044 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5040 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 3 ms 5028 KB Output is correct
26 Correct 4 ms 5040 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5108 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5036 KB Output is correct
37 Correct 3 ms 5064 KB Output is correct
38 Correct 3 ms 5104 KB Output is correct
39 Correct 4 ms 5076 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 3 ms 5036 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5044 KB Output is correct
46 Correct 7 ms 5332 KB Output is correct
47 Correct 10 ms 5332 KB Output is correct
48 Correct 7 ms 5392 KB Output is correct
49 Correct 6 ms 5548 KB Output is correct
50 Correct 5 ms 5460 KB Output is correct
51 Correct 6 ms 5460 KB Output is correct
52 Correct 6 ms 5424 KB Output is correct
53 Correct 5 ms 5460 KB Output is correct
54 Correct 5 ms 5424 KB Output is correct
55 Correct 6 ms 5460 KB Output is correct
56 Correct 5 ms 5460 KB Output is correct
57 Correct 8 ms 5332 KB Output is correct
58 Correct 10 ms 5300 KB Output is correct
59 Correct 9 ms 5300 KB Output is correct
60 Correct 8 ms 5332 KB Output is correct
61 Correct 5 ms 5460 KB Output is correct
62 Correct 5 ms 5428 KB Output is correct
63 Correct 5 ms 5460 KB Output is correct
64 Correct 7 ms 5332 KB Output is correct
65 Correct 7 ms 5332 KB Output is correct
66 Correct 6 ms 5296 KB Output is correct
67 Correct 7 ms 5304 KB Output is correct
68 Correct 6 ms 5428 KB Output is correct
69 Correct 5 ms 5460 KB Output is correct
70 Correct 6 ms 5460 KB Output is correct
71 Correct 5 ms 5332 KB Output is correct
72 Correct 8 ms 5332 KB Output is correct
73 Correct 8 ms 5332 KB Output is correct
74 Correct 5 ms 5460 KB Output is correct
75 Correct 6 ms 5428 KB Output is correct
76 Correct 7 ms 5344 KB Output is correct
77 Correct 6 ms 5392 KB Output is correct
78 Correct 5 ms 5344 KB Output is correct
79 Correct 6 ms 5312 KB Output is correct
80 Correct 6 ms 5344 KB Output is correct
81 Correct 6 ms 5312 KB Output is correct
82 Correct 6 ms 5344 KB Output is correct
83 Correct 6 ms 5344 KB Output is correct
84 Correct 6 ms 5308 KB Output is correct
85 Correct 5 ms 5396 KB Output is correct
86 Correct 5 ms 5312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5028 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 4 ms 5036 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5040 KB Output is correct
14 Correct 4 ms 5204 KB Output is correct
15 Correct 3 ms 5040 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5044 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 3 ms 5040 KB Output is correct
20 Correct 3 ms 5076 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 3 ms 5028 KB Output is correct
26 Correct 4 ms 5040 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 3 ms 5108 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5076 KB Output is correct
33 Correct 3 ms 5076 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5036 KB Output is correct
37 Correct 3 ms 5064 KB Output is correct
38 Correct 3 ms 5104 KB Output is correct
39 Correct 4 ms 5076 KB Output is correct
40 Correct 3 ms 5032 KB Output is correct
41 Correct 3 ms 5036 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5076 KB Output is correct
44 Correct 3 ms 5076 KB Output is correct
45 Correct 4 ms 5044 KB Output is correct
46 Correct 7 ms 5332 KB Output is correct
47 Correct 10 ms 5332 KB Output is correct
48 Correct 7 ms 5392 KB Output is correct
49 Correct 6 ms 5548 KB Output is correct
50 Correct 5 ms 5460 KB Output is correct
51 Correct 6 ms 5460 KB Output is correct
52 Correct 6 ms 5424 KB Output is correct
53 Correct 5 ms 5460 KB Output is correct
54 Correct 5 ms 5424 KB Output is correct
55 Correct 6 ms 5460 KB Output is correct
56 Correct 5 ms 5460 KB Output is correct
57 Correct 8 ms 5332 KB Output is correct
58 Correct 10 ms 5300 KB Output is correct
59 Correct 9 ms 5300 KB Output is correct
60 Correct 8 ms 5332 KB Output is correct
61 Correct 5 ms 5460 KB Output is correct
62 Correct 5 ms 5428 KB Output is correct
63 Correct 5 ms 5460 KB Output is correct
64 Correct 7 ms 5332 KB Output is correct
65 Correct 7 ms 5332 KB Output is correct
66 Correct 6 ms 5296 KB Output is correct
67 Correct 7 ms 5304 KB Output is correct
68 Correct 6 ms 5428 KB Output is correct
69 Correct 5 ms 5460 KB Output is correct
70 Correct 6 ms 5460 KB Output is correct
71 Correct 5 ms 5332 KB Output is correct
72 Correct 8 ms 5332 KB Output is correct
73 Correct 8 ms 5332 KB Output is correct
74 Correct 5 ms 5460 KB Output is correct
75 Correct 6 ms 5428 KB Output is correct
76 Correct 7 ms 5344 KB Output is correct
77 Correct 6 ms 5392 KB Output is correct
78 Correct 5 ms 5344 KB Output is correct
79 Correct 6 ms 5312 KB Output is correct
80 Correct 6 ms 5344 KB Output is correct
81 Correct 6 ms 5312 KB Output is correct
82 Correct 6 ms 5344 KB Output is correct
83 Correct 6 ms 5344 KB Output is correct
84 Correct 6 ms 5308 KB Output is correct
85 Correct 5 ms 5396 KB Output is correct
86 Correct 5 ms 5312 KB Output is correct
87 Correct 15 ms 5828 KB Output is correct
88 Correct 58 ms 7524 KB Output is correct
89 Correct 179 ms 13520 KB Output is correct
90 Correct 168 ms 13552 KB Output is correct
91 Correct 182 ms 13620 KB Output is correct
92 Correct 91 ms 17812 KB Output is correct
93 Correct 85 ms 17768 KB Output is correct
94 Correct 96 ms 17824 KB Output is correct
95 Correct 71 ms 16920 KB Output is correct
96 Correct 86 ms 17464 KB Output is correct
97 Correct 76 ms 17316 KB Output is correct
98 Correct 76 ms 17472 KB Output is correct
99 Correct 74 ms 15820 KB Output is correct
100 Correct 227 ms 13236 KB Output is correct
101 Correct 250 ms 13620 KB Output is correct
102 Correct 223 ms 13524 KB Output is correct
103 Correct 225 ms 13632 KB Output is correct
104 Correct 113 ms 15796 KB Output is correct
105 Correct 99 ms 15888 KB Output is correct
106 Correct 95 ms 15968 KB Output is correct
107 Correct 149 ms 12344 KB Output is correct
108 Correct 158 ms 12564 KB Output is correct
109 Correct 175 ms 12944 KB Output is correct
110 Correct 75 ms 16928 KB Output is correct
111 Correct 83 ms 16952 KB Output is correct
112 Correct 70 ms 16396 KB Output is correct
113 Correct 69 ms 14904 KB Output is correct
114 Correct 219 ms 13344 KB Output is correct
115 Correct 236 ms 12568 KB Output is correct
116 Correct 88 ms 14776 KB Output is correct
117 Correct 85 ms 14344 KB Output is correct
118 Correct 90 ms 13956 KB Output is correct
119 Correct 87 ms 13708 KB Output is correct
120 Correct 80 ms 13540 KB Output is correct
121 Correct 94 ms 13300 KB Output is correct
122 Correct 85 ms 12988 KB Output is correct
123 Correct 93 ms 14372 KB Output is correct
124 Correct 118 ms 13964 KB Output is correct
125 Correct 100 ms 13844 KB Output is correct
126 Correct 88 ms 13724 KB Output is correct
127 Correct 95 ms 13324 KB Output is correct
128 Correct 108 ms 13116 KB Output is correct