Submission #576542

# Submission time Handle Problem Language Result Execution time Memory
576542 2022-06-13T07:31:20 Z IWannaHaveAGirlfriend Construction of Highway (JOI18_construction) C++17
100 / 100
308 ms 17856 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 5032 KB Output is correct
3 Correct 3 ms 4948 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 3 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5036 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5040 KB Output is correct
16 Correct 4 ms 5036 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5040 KB Output is correct
19 Correct 3 ms 5104 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 5032 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 5076 KB Output is correct
26 Correct 3 ms 5032 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5036 KB Output is correct
29 Correct 4 ms 5088 KB Output is correct
30 Correct 4 ms 5040 KB Output is correct
31 Correct 3 ms 5036 KB Output is correct
32 Correct 3 ms 5040 KB Output is correct
33 Correct 3 ms 5032 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 4 ms 4984 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 4 ms 5076 KB Output is correct
42 Correct 4 ms 5044 KB Output is correct
43 Correct 4 ms 5076 KB Output is correct
44 Correct 3 ms 5036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 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 3 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5036 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5040 KB Output is correct
16 Correct 4 ms 5036 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5040 KB Output is correct
19 Correct 3 ms 5104 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 5032 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 5076 KB Output is correct
26 Correct 3 ms 5032 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5036 KB Output is correct
29 Correct 4 ms 5088 KB Output is correct
30 Correct 4 ms 5040 KB Output is correct
31 Correct 3 ms 5036 KB Output is correct
32 Correct 3 ms 5040 KB Output is correct
33 Correct 3 ms 5032 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 4 ms 4984 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 4 ms 5076 KB Output is correct
42 Correct 4 ms 5044 KB Output is correct
43 Correct 4 ms 5076 KB Output is correct
44 Correct 3 ms 5036 KB Output is correct
45 Correct 4 ms 5048 KB Output is correct
46 Correct 7 ms 5300 KB Output is correct
47 Correct 8 ms 5332 KB Output is correct
48 Correct 7 ms 5332 KB Output is correct
49 Correct 7 ms 5460 KB Output is correct
50 Correct 5 ms 5556 KB Output is correct
51 Correct 5 ms 5460 KB Output is correct
52 Correct 5 ms 5460 KB Output is correct
53 Correct 6 ms 5460 KB Output is correct
54 Correct 6 ms 5420 KB Output is correct
55 Correct 5 ms 5460 KB Output is correct
56 Correct 6 ms 5460 KB Output is correct
57 Correct 9 ms 5332 KB Output is correct
58 Correct 8 ms 5300 KB Output is correct
59 Correct 8 ms 5320 KB Output is correct
60 Correct 8 ms 5332 KB Output is correct
61 Correct 5 ms 5416 KB Output is correct
62 Correct 6 ms 5428 KB Output is correct
63 Correct 6 ms 5460 KB Output is correct
64 Correct 7 ms 5332 KB Output is correct
65 Correct 9 ms 5344 KB Output is correct
66 Correct 8 ms 5332 KB Output is correct
67 Correct 8 ms 5316 KB Output is correct
68 Correct 5 ms 5460 KB Output is correct
69 Correct 5 ms 5428 KB Output is correct
70 Correct 5 ms 5460 KB Output is correct
71 Correct 5 ms 5428 KB Output is correct
72 Correct 8 ms 5332 KB Output is correct
73 Correct 9 ms 5332 KB Output is correct
74 Correct 8 ms 5332 KB Output is correct
75 Correct 6 ms 5384 KB Output is correct
76 Correct 6 ms 5332 KB Output is correct
77 Correct 7 ms 5332 KB Output is correct
78 Correct 5 ms 5332 KB Output is correct
79 Correct 6 ms 5332 KB Output is correct
80 Correct 6 ms 5332 KB Output is correct
81 Correct 6 ms 5428 KB Output is correct
82 Correct 7 ms 5332 KB Output is correct
83 Correct 6 ms 5296 KB Output is correct
84 Correct 8 ms 5372 KB Output is correct
85 Correct 5 ms 5332 KB Output is correct
86 Correct 6 ms 5264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5032 KB Output is correct
3 Correct 3 ms 4948 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 3 ms 5076 KB Output is correct
9 Correct 4 ms 5076 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5036 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5040 KB Output is correct
16 Correct 4 ms 5036 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5040 KB Output is correct
19 Correct 3 ms 5104 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 5032 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 5076 KB Output is correct
26 Correct 3 ms 5032 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5036 KB Output is correct
29 Correct 4 ms 5088 KB Output is correct
30 Correct 4 ms 5040 KB Output is correct
31 Correct 3 ms 5036 KB Output is correct
32 Correct 3 ms 5040 KB Output is correct
33 Correct 3 ms 5032 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 3 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 3 ms 5076 KB Output is correct
38 Correct 4 ms 4984 KB Output is correct
39 Correct 3 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 4 ms 5076 KB Output is correct
42 Correct 4 ms 5044 KB Output is correct
43 Correct 4 ms 5076 KB Output is correct
44 Correct 3 ms 5036 KB Output is correct
45 Correct 4 ms 5048 KB Output is correct
46 Correct 7 ms 5300 KB Output is correct
47 Correct 8 ms 5332 KB Output is correct
48 Correct 7 ms 5332 KB Output is correct
49 Correct 7 ms 5460 KB Output is correct
50 Correct 5 ms 5556 KB Output is correct
51 Correct 5 ms 5460 KB Output is correct
52 Correct 5 ms 5460 KB Output is correct
53 Correct 6 ms 5460 KB Output is correct
54 Correct 6 ms 5420 KB Output is correct
55 Correct 5 ms 5460 KB Output is correct
56 Correct 6 ms 5460 KB Output is correct
57 Correct 9 ms 5332 KB Output is correct
58 Correct 8 ms 5300 KB Output is correct
59 Correct 8 ms 5320 KB Output is correct
60 Correct 8 ms 5332 KB Output is correct
61 Correct 5 ms 5416 KB Output is correct
62 Correct 6 ms 5428 KB Output is correct
63 Correct 6 ms 5460 KB Output is correct
64 Correct 7 ms 5332 KB Output is correct
65 Correct 9 ms 5344 KB Output is correct
66 Correct 8 ms 5332 KB Output is correct
67 Correct 8 ms 5316 KB Output is correct
68 Correct 5 ms 5460 KB Output is correct
69 Correct 5 ms 5428 KB Output is correct
70 Correct 5 ms 5460 KB Output is correct
71 Correct 5 ms 5428 KB Output is correct
72 Correct 8 ms 5332 KB Output is correct
73 Correct 9 ms 5332 KB Output is correct
74 Correct 8 ms 5332 KB Output is correct
75 Correct 6 ms 5384 KB Output is correct
76 Correct 6 ms 5332 KB Output is correct
77 Correct 7 ms 5332 KB Output is correct
78 Correct 5 ms 5332 KB Output is correct
79 Correct 6 ms 5332 KB Output is correct
80 Correct 6 ms 5332 KB Output is correct
81 Correct 6 ms 5428 KB Output is correct
82 Correct 7 ms 5332 KB Output is correct
83 Correct 6 ms 5296 KB Output is correct
84 Correct 8 ms 5372 KB Output is correct
85 Correct 5 ms 5332 KB Output is correct
86 Correct 6 ms 5264 KB Output is correct
87 Correct 16 ms 5844 KB Output is correct
88 Correct 48 ms 7548 KB Output is correct
89 Correct 204 ms 13672 KB Output is correct
90 Correct 195 ms 13548 KB Output is correct
91 Correct 171 ms 13564 KB Output is correct
92 Correct 78 ms 17832 KB Output is correct
93 Correct 81 ms 17856 KB Output is correct
94 Correct 76 ms 17788 KB Output is correct
95 Correct 81 ms 16984 KB Output is correct
96 Correct 83 ms 17376 KB Output is correct
97 Correct 93 ms 17324 KB Output is correct
98 Correct 104 ms 17328 KB Output is correct
99 Correct 89 ms 15960 KB Output is correct
100 Correct 213 ms 13308 KB Output is correct
101 Correct 308 ms 13580 KB Output is correct
102 Correct 233 ms 13592 KB Output is correct
103 Correct 266 ms 13744 KB Output is correct
104 Correct 87 ms 15872 KB Output is correct
105 Correct 94 ms 15800 KB Output is correct
106 Correct 94 ms 15832 KB Output is correct
107 Correct 153 ms 12492 KB Output is correct
108 Correct 232 ms 12472 KB Output is correct
109 Correct 170 ms 12948 KB Output is correct
110 Correct 71 ms 16820 KB Output is correct
111 Correct 94 ms 16956 KB Output is correct
112 Correct 91 ms 16396 KB Output is correct
113 Correct 70 ms 14880 KB Output is correct
114 Correct 210 ms 13396 KB Output is correct
115 Correct 242 ms 12672 KB Output is correct
116 Correct 80 ms 14828 KB Output is correct
117 Correct 82 ms 14368 KB Output is correct
118 Correct 91 ms 13884 KB Output is correct
119 Correct 124 ms 13748 KB Output is correct
120 Correct 92 ms 13568 KB Output is correct
121 Correct 80 ms 13252 KB Output is correct
122 Correct 86 ms 13076 KB Output is correct
123 Correct 103 ms 14352 KB Output is correct
124 Correct 131 ms 14012 KB Output is correct
125 Correct 114 ms 13716 KB Output is correct
126 Correct 98 ms 13696 KB Output is correct
127 Correct 118 ms 13256 KB Output is correct
128 Correct 108 ms 13072 KB Output is correct