Submission #694683

# Submission time Handle Problem Language Result Execution time Memory
694683 2023-02-04T08:26:56 Z tamthegod Construction of Highway (JOI18_construction) C++17
100 / 100
519 ms 30016 KB
// Make the best become better
// No room for laziness
#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e9;
int n, c[maxN];
vector<int> adj[maxN];
pair<int, int> e[maxN];
int p[maxN], sz[maxN];
int chainhead[maxN], chainid[maxN], nChain = 0;
int tin[maxN], Time = 0;
void ReadInput()
{
    cin >> n;
    vector<int> vc;
    for(int i=1; i<=n; i++)
    {
        cin >> c[i];
        vc.pb(c[i]);
    }
    sort(vc.begin(), vc.end());
    vc.erase(unique(vc.begin(), vc.end()), vc.end());
    for(int i=1; i<=n; i++)
        c[i] = upper_bound(vc.begin(), vc.end(), c[i]) - vc.begin();
    for(int i=1; i<n; i++)
    {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
        e[i] = {u, v};
    }
}
void dfs(int u)
{
    sz[u] = 1;
    for(int v : adj[u])
    {
        if(v == p[u]) continue;
        p[v] = u;
        dfs(v);
        sz[u] += sz[v];
    }
}
void HLD(int u)
{
    if(!chainhead[nChain]) chainhead[nChain] = u;
    chainid[u] = nChain;
    tin[u] = ++Time;
    int bigChild = -1;
    for(int v : adj[u])
    {
        if(v == p[u]) continue;
        if(bigChild == -1 || sz[bigChild] < sz[v]) bigChild = v;
    }
    if(bigChild != -1)
        HLD(bigChild);
    for(int v : adj[u])
    {
        if(v == p[u] || v == bigChild) continue;
        nChain++;
        HLD(v);
    }
}
struct TSeg
{
    int l, r, col;
    bool operator < (const TSeg& other) const
    {
        if(l == other.l) return r < other.r;
        return l < other.l;
    }
};
int bit[maxN];
vector<int> bin;
void update(int pos, int val)
{
    while(pos <= n)
    {
        bin.pb(pos);
        bit[pos] += val;
        pos += pos & -pos;
    }
}
int get(int pos)
{
    int res = 0;
    while(pos)
    {
        res += bit[pos];
        pos -= pos & -pos;
    }
    return res;
}
set<TSeg> S[maxN];
int query(int u, int col)
{
    int res = 0;
    while(u)
    {
        int t = chainhead[chainid[u]];
        TSeg v = {-1, -1, -1};
        while(!S[t].empty())
        {
            auto it = S[t].lower_bound({tin[u], oo, oo});
            if(it == S[t].begin()) break;
            it--;
            TSeg tmp = *it;
            S[t].erase(it);
           // if(col == 8) cout << tmp.l << " " << tmp.r << '\n';
            if(tmp.r <= tin[u])
            {
                res += get(tmp.col - 1) * (tmp.r - tmp.l + 1);
                update(tmp.col, tmp.r - tmp.l + 1);
            }
            else
            {
               // cout << tmp.l << " " << tmp.r << '\n';
                v = {tin[u] + 1, tmp.r, tmp.col};
                res += get(tmp.col - 1) * (tin[u] - tmp.l + 1);
                update(tmp.col, tin[u] - tmp.l + 1);
            }
        }
        if(v.col != -1) S[t].insert(v);
        S[t].insert({tin[t], tin[u], col});
        u = p[t];
    }
    for(int v : bin)
        bit[v] = 0;
    bin.clear();
    return res;
}
void Solve()
{
    dfs(1);
    HLD(1);
    for(int i=1; i<=n; i++)
    {
        S[chainhead[chainid[i]]].insert({tin[i], tin[i], c[i]});
    }
    for(int i=1; i<n; i++)
    {
        int u = e[i].fi, v = e[i].se;
        cout << query(u, c[v]) << '\n';
    }
}
int32_t main()
{
    //freopen("sol.inp", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7392 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7396 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 4 ms 7524 KB Output is correct
13 Correct 4 ms 7520 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 5 ms 7396 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7396 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7416 KB Output is correct
21 Correct 5 ms 7396 KB Output is correct
22 Correct 4 ms 7396 KB Output is correct
23 Correct 4 ms 7392 KB Output is correct
24 Correct 5 ms 7476 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 5 ms 7508 KB Output is correct
27 Correct 4 ms 7508 KB Output is correct
28 Correct 4 ms 7448 KB Output is correct
29 Correct 5 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 5 ms 7388 KB Output is correct
32 Correct 4 ms 7392 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7444 KB Output is correct
35 Correct 5 ms 7380 KB Output is correct
36 Correct 4 ms 7380 KB Output is correct
37 Correct 4 ms 7392 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 4 ms 7380 KB Output is correct
40 Correct 4 ms 7492 KB Output is correct
41 Correct 6 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 5 ms 7472 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7392 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7396 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 4 ms 7524 KB Output is correct
13 Correct 4 ms 7520 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 5 ms 7396 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7396 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7416 KB Output is correct
21 Correct 5 ms 7396 KB Output is correct
22 Correct 4 ms 7396 KB Output is correct
23 Correct 4 ms 7392 KB Output is correct
24 Correct 5 ms 7476 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 5 ms 7508 KB Output is correct
27 Correct 4 ms 7508 KB Output is correct
28 Correct 4 ms 7448 KB Output is correct
29 Correct 5 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 5 ms 7388 KB Output is correct
32 Correct 4 ms 7392 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7444 KB Output is correct
35 Correct 5 ms 7380 KB Output is correct
36 Correct 4 ms 7380 KB Output is correct
37 Correct 4 ms 7392 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 4 ms 7380 KB Output is correct
40 Correct 4 ms 7492 KB Output is correct
41 Correct 6 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 5 ms 7472 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 5 ms 7508 KB Output is correct
46 Correct 9 ms 7892 KB Output is correct
47 Correct 10 ms 7892 KB Output is correct
48 Correct 10 ms 7892 KB Output is correct
49 Correct 8 ms 8296 KB Output is correct
50 Correct 8 ms 8276 KB Output is correct
51 Correct 8 ms 8292 KB Output is correct
52 Correct 8 ms 8148 KB Output is correct
53 Correct 8 ms 8244 KB Output is correct
54 Correct 9 ms 8276 KB Output is correct
55 Correct 10 ms 8276 KB Output is correct
56 Correct 11 ms 8148 KB Output is correct
57 Correct 13 ms 7992 KB Output is correct
58 Correct 12 ms 8004 KB Output is correct
59 Correct 12 ms 8008 KB Output is correct
60 Correct 12 ms 7892 KB Output is correct
61 Correct 8 ms 8164 KB Output is correct
62 Correct 8 ms 8040 KB Output is correct
63 Correct 8 ms 8148 KB Output is correct
64 Correct 10 ms 7872 KB Output is correct
65 Correct 10 ms 7892 KB Output is correct
66 Correct 9 ms 7912 KB Output is correct
67 Correct 9 ms 7892 KB Output is correct
68 Correct 8 ms 8276 KB Output is correct
69 Correct 8 ms 8148 KB Output is correct
70 Correct 8 ms 8276 KB Output is correct
71 Correct 7 ms 8036 KB Output is correct
72 Correct 12 ms 7980 KB Output is correct
73 Correct 13 ms 7972 KB Output is correct
74 Correct 7 ms 8120 KB Output is correct
75 Correct 8 ms 8020 KB Output is correct
76 Correct 8 ms 7988 KB Output is correct
77 Correct 8 ms 7984 KB Output is correct
78 Correct 8 ms 7992 KB Output is correct
79 Correct 11 ms 8020 KB Output is correct
80 Correct 9 ms 7916 KB Output is correct
81 Correct 8 ms 8024 KB Output is correct
82 Correct 9 ms 8044 KB Output is correct
83 Correct 8 ms 7916 KB Output is correct
84 Correct 8 ms 7992 KB Output is correct
85 Correct 8 ms 8008 KB Output is correct
86 Correct 8 ms 7896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7392 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7396 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7508 KB Output is correct
9 Correct 4 ms 7508 KB Output is correct
10 Correct 4 ms 7508 KB Output is correct
11 Correct 4 ms 7508 KB Output is correct
12 Correct 4 ms 7524 KB Output is correct
13 Correct 4 ms 7520 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 5 ms 7396 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 5 ms 7396 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7416 KB Output is correct
21 Correct 5 ms 7396 KB Output is correct
22 Correct 4 ms 7396 KB Output is correct
23 Correct 4 ms 7392 KB Output is correct
24 Correct 5 ms 7476 KB Output is correct
25 Correct 5 ms 7380 KB Output is correct
26 Correct 5 ms 7508 KB Output is correct
27 Correct 4 ms 7508 KB Output is correct
28 Correct 4 ms 7448 KB Output is correct
29 Correct 5 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 5 ms 7388 KB Output is correct
32 Correct 4 ms 7392 KB Output is correct
33 Correct 4 ms 7380 KB Output is correct
34 Correct 5 ms 7444 KB Output is correct
35 Correct 5 ms 7380 KB Output is correct
36 Correct 4 ms 7380 KB Output is correct
37 Correct 4 ms 7392 KB Output is correct
38 Correct 4 ms 7380 KB Output is correct
39 Correct 4 ms 7380 KB Output is correct
40 Correct 4 ms 7492 KB Output is correct
41 Correct 6 ms 7380 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 5 ms 7472 KB Output is correct
44 Correct 4 ms 7380 KB Output is correct
45 Correct 5 ms 7508 KB Output is correct
46 Correct 9 ms 7892 KB Output is correct
47 Correct 10 ms 7892 KB Output is correct
48 Correct 10 ms 7892 KB Output is correct
49 Correct 8 ms 8296 KB Output is correct
50 Correct 8 ms 8276 KB Output is correct
51 Correct 8 ms 8292 KB Output is correct
52 Correct 8 ms 8148 KB Output is correct
53 Correct 8 ms 8244 KB Output is correct
54 Correct 9 ms 8276 KB Output is correct
55 Correct 10 ms 8276 KB Output is correct
56 Correct 11 ms 8148 KB Output is correct
57 Correct 13 ms 7992 KB Output is correct
58 Correct 12 ms 8004 KB Output is correct
59 Correct 12 ms 8008 KB Output is correct
60 Correct 12 ms 7892 KB Output is correct
61 Correct 8 ms 8164 KB Output is correct
62 Correct 8 ms 8040 KB Output is correct
63 Correct 8 ms 8148 KB Output is correct
64 Correct 10 ms 7872 KB Output is correct
65 Correct 10 ms 7892 KB Output is correct
66 Correct 9 ms 7912 KB Output is correct
67 Correct 9 ms 7892 KB Output is correct
68 Correct 8 ms 8276 KB Output is correct
69 Correct 8 ms 8148 KB Output is correct
70 Correct 8 ms 8276 KB Output is correct
71 Correct 7 ms 8036 KB Output is correct
72 Correct 12 ms 7980 KB Output is correct
73 Correct 13 ms 7972 KB Output is correct
74 Correct 7 ms 8120 KB Output is correct
75 Correct 8 ms 8020 KB Output is correct
76 Correct 8 ms 7988 KB Output is correct
77 Correct 8 ms 7984 KB Output is correct
78 Correct 8 ms 7992 KB Output is correct
79 Correct 11 ms 8020 KB Output is correct
80 Correct 9 ms 7916 KB Output is correct
81 Correct 8 ms 8024 KB Output is correct
82 Correct 9 ms 8044 KB Output is correct
83 Correct 8 ms 7916 KB Output is correct
84 Correct 8 ms 7992 KB Output is correct
85 Correct 8 ms 8008 KB Output is correct
86 Correct 8 ms 7896 KB Output is correct
87 Correct 21 ms 8920 KB Output is correct
88 Correct 64 ms 11900 KB Output is correct
89 Correct 282 ms 22592 KB Output is correct
90 Correct 294 ms 22512 KB Output is correct
91 Correct 273 ms 22568 KB Output is correct
92 Correct 153 ms 29968 KB Output is correct
93 Correct 144 ms 30016 KB Output is correct
94 Correct 146 ms 29988 KB Output is correct
95 Correct 145 ms 28088 KB Output is correct
96 Correct 146 ms 28464 KB Output is correct
97 Correct 156 ms 28624 KB Output is correct
98 Correct 157 ms 28440 KB Output is correct
99 Correct 127 ms 26376 KB Output is correct
100 Correct 473 ms 22032 KB Output is correct
101 Correct 494 ms 22336 KB Output is correct
102 Correct 519 ms 22456 KB Output is correct
103 Correct 514 ms 22428 KB Output is correct
104 Correct 141 ms 26404 KB Output is correct
105 Correct 139 ms 26440 KB Output is correct
106 Correct 134 ms 26376 KB Output is correct
107 Correct 276 ms 21340 KB Output is correct
108 Correct 294 ms 21532 KB Output is correct
109 Correct 309 ms 21820 KB Output is correct
110 Correct 146 ms 29004 KB Output is correct
111 Correct 144 ms 28088 KB Output is correct
112 Correct 162 ms 29652 KB Output is correct
113 Correct 116 ms 25544 KB Output is correct
114 Correct 456 ms 22216 KB Output is correct
115 Correct 494 ms 21432 KB Output is correct
116 Correct 137 ms 25448 KB Output is correct
117 Correct 145 ms 24112 KB Output is correct
118 Correct 130 ms 23492 KB Output is correct
119 Correct 129 ms 23084 KB Output is correct
120 Correct 135 ms 23348 KB Output is correct
121 Correct 131 ms 22836 KB Output is correct
122 Correct 122 ms 22508 KB Output is correct
123 Correct 150 ms 24100 KB Output is correct
124 Correct 143 ms 23464 KB Output is correct
125 Correct 150 ms 23352 KB Output is correct
126 Correct 142 ms 23536 KB Output is correct
127 Correct 142 ms 22840 KB Output is correct
128 Correct 163 ms 22472 KB Output is correct