Submission #625127

# Submission time Handle Problem Language Result Execution time Memory
625127 2022-08-09T12:35:01 Z MohamedFaresNebili Construction of Highway (JOI18_construction) C++14
100 / 100
382 ms 114088 KB
#include <bits/stdc++.h>
/// #pragma GCC optimize ("Ofast")
/// #pragma GCC target ("avx2")
/// #pragma GCC optimize("unroll-loops")

            using namespace std;

            using ll = long long;
            using ld = long double;

            #define ff first
            #define ss second
            #define pb push_back
            #define all(x) (x).begin(), (x).end()
            #define lb lower_bound
            #define int ll

            const int MOD = 998244353;

            int N, timer, C[100005], X[100005], Y[100005];
            int tin[100005], dep[100005], S[100005], T[100005];
            vector<int> adj[100005]; deque<pair<int, int>> sub[100005];
            int P[400005], ST[400005];

            int dfs_size(int v, int p) {
                P[v] = p; dep[v] = dep[p] + 1; S[v] = 1;
                for(auto u : adj[v]) {
                    if(u == p) continue;
                    S[v] += dfs_size(u, v);
                }
                return S[v];
            }
            void getHeavy(int v, int p, int top) {
                tin[v] = timer++; T[v] = top;
                int heavy = -1, cur = 0;
                for(auto u : adj[v]) {
                    if(u == p) continue;
                    if(S[u] > cur)
                        cur = S[u], heavy = u;
                }
                if(heavy == -1) return;
                getHeavy(heavy, v, top);
                for(auto u : adj[v]) {
                    if(u == p || u == heavy) continue;
                    getHeavy(u, v, u);
                }
            }
            int _N;
            void init(int n) {
                _N = n;
                for(int l = 0; l <= _N; l++) ST[l] = 0;
            }
            void update(int p, int v) {
                for(; p <= _N; p += (p & (-p)))
                    ST[p] += v;
            }
            int query(int p) {
                int res = 0;
                for(; p; p -= (p & (-p)))
                    res += ST[p];
                return res;
            }
            int solve(int v) {
                vector<pair<int, int>> E; vector<int> comp;
                while(v != -1) {
                    int top = T[v];
                    int d = dep[v] - dep[top] + 1;
                    vector<pair<int, int>> A;
                    for(int i = 0; i < sub[top].size(); i++) {
                        int k = sub[top][i].first; int cur = sub[top][i].second;
                        if(k <= d) {
                            A.push_back({k, cur});
                            d -= k; continue;
                        }
                        A.push_back({d, cur});
                        break;
                    }
                    for(int l = A.size() - 1; ~l; l--) {
                        E.push_back(A[l]); comp.push_back(A[l].second);
                    }
                    v = P[top];
                }
                reverse(E.begin(), E.end());
                sort(comp.begin(), comp.end());
                comp.erase(unique(comp.begin(), comp.end()), comp.end());
                int ans = 0, tot = 0; init(comp.size());
                for(auto u : E) {
                    u.second = lower_bound(comp.begin(), comp.end(), u.second) - comp.begin();
                    ans += u.first * (tot - query(++u.second ));
                    tot += u.first;
                    update(u.second, u.first);
                }
                return ans;
            }
            void add(int v, int cur) {
                while(v != -1) {
                    int top = T[v], d = dep[v] - dep[top] + 1;
                    while(!sub[top].empty()) {
                        int k = sub[top][0].first;
                        if(k <= d) {
                            sub[top].pop_front();
                            d -= k; continue;
                        }
                        sub[top].front().first -= d;
                        break;
                    }
                    sub[top].push_front({dep[v] - dep[top] + 1, cur});
                    v = P[top];
                }
            }

            int32_t main() {
                ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
                cin >> N;
                for(int l = 1; l <= N; l++)
                    cin >> C[l];
                for(int l = 0; l < N - 1; l++) {
                    cin >> X[l] >> Y[l];
                    adj[X[l]].push_back(Y[l]);
                    adj[Y[l]].push_back(X[l]);
                }
                dep[1] = -1; dfs_size(1, 1); getHeavy(1, 1, 1);
                P[1] = -1; add(1, C[1]);
                for(int l = 0; l < N - 1; l++) {
                    int U = X[l], V = Y[l];
                    cout << solve(U) << "\n";
                    add(V, C[V]);
                }
                return 0;
            }


Compilation message

construction.cpp: In function 'll solve(ll)':
construction.cpp:69:38: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::deque<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |                     for(int i = 0; i < sub[top].size(); i++) {
      |                                    ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 40 ms 69964 KB Output is correct
2 Correct 43 ms 69964 KB Output is correct
3 Correct 45 ms 69992 KB Output is correct
4 Correct 47 ms 70136 KB Output is correct
5 Correct 51 ms 70248 KB Output is correct
6 Correct 46 ms 70228 KB Output is correct
7 Correct 47 ms 70236 KB Output is correct
8 Correct 42 ms 70116 KB Output is correct
9 Correct 40 ms 70100 KB Output is correct
10 Correct 46 ms 70032 KB Output is correct
11 Correct 41 ms 70228 KB Output is correct
12 Correct 49 ms 70256 KB Output is correct
13 Correct 46 ms 70220 KB Output is correct
14 Correct 44 ms 70132 KB Output is correct
15 Correct 47 ms 70220 KB Output is correct
16 Correct 58 ms 70176 KB Output is correct
17 Correct 55 ms 70136 KB Output is correct
18 Correct 48 ms 70152 KB Output is correct
19 Correct 50 ms 70220 KB Output is correct
20 Correct 46 ms 70240 KB Output is correct
21 Correct 46 ms 70128 KB Output is correct
22 Correct 48 ms 70236 KB Output is correct
23 Correct 46 ms 70212 KB Output is correct
24 Correct 45 ms 70160 KB Output is correct
25 Correct 46 ms 70224 KB Output is correct
26 Correct 41 ms 70084 KB Output is correct
27 Correct 41 ms 70196 KB Output is correct
28 Correct 50 ms 70244 KB Output is correct
29 Correct 45 ms 70140 KB Output is correct
30 Correct 46 ms 70196 KB Output is correct
31 Correct 45 ms 70128 KB Output is correct
32 Correct 48 ms 70232 KB Output is correct
33 Correct 44 ms 70160 KB Output is correct
34 Correct 53 ms 70136 KB Output is correct
35 Correct 44 ms 70224 KB Output is correct
36 Correct 45 ms 70128 KB Output is correct
37 Correct 45 ms 70228 KB Output is correct
38 Correct 45 ms 70232 KB Output is correct
39 Correct 50 ms 70240 KB Output is correct
40 Correct 45 ms 70188 KB Output is correct
41 Correct 46 ms 70168 KB Output is correct
42 Correct 53 ms 70132 KB Output is correct
43 Correct 47 ms 70220 KB Output is correct
44 Correct 50 ms 70120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 69964 KB Output is correct
2 Correct 43 ms 69964 KB Output is correct
3 Correct 45 ms 69992 KB Output is correct
4 Correct 47 ms 70136 KB Output is correct
5 Correct 51 ms 70248 KB Output is correct
6 Correct 46 ms 70228 KB Output is correct
7 Correct 47 ms 70236 KB Output is correct
8 Correct 42 ms 70116 KB Output is correct
9 Correct 40 ms 70100 KB Output is correct
10 Correct 46 ms 70032 KB Output is correct
11 Correct 41 ms 70228 KB Output is correct
12 Correct 49 ms 70256 KB Output is correct
13 Correct 46 ms 70220 KB Output is correct
14 Correct 44 ms 70132 KB Output is correct
15 Correct 47 ms 70220 KB Output is correct
16 Correct 58 ms 70176 KB Output is correct
17 Correct 55 ms 70136 KB Output is correct
18 Correct 48 ms 70152 KB Output is correct
19 Correct 50 ms 70220 KB Output is correct
20 Correct 46 ms 70240 KB Output is correct
21 Correct 46 ms 70128 KB Output is correct
22 Correct 48 ms 70236 KB Output is correct
23 Correct 46 ms 70212 KB Output is correct
24 Correct 45 ms 70160 KB Output is correct
25 Correct 46 ms 70224 KB Output is correct
26 Correct 41 ms 70084 KB Output is correct
27 Correct 41 ms 70196 KB Output is correct
28 Correct 50 ms 70244 KB Output is correct
29 Correct 45 ms 70140 KB Output is correct
30 Correct 46 ms 70196 KB Output is correct
31 Correct 45 ms 70128 KB Output is correct
32 Correct 48 ms 70232 KB Output is correct
33 Correct 44 ms 70160 KB Output is correct
34 Correct 53 ms 70136 KB Output is correct
35 Correct 44 ms 70224 KB Output is correct
36 Correct 45 ms 70128 KB Output is correct
37 Correct 45 ms 70228 KB Output is correct
38 Correct 45 ms 70232 KB Output is correct
39 Correct 50 ms 70240 KB Output is correct
40 Correct 45 ms 70188 KB Output is correct
41 Correct 46 ms 70168 KB Output is correct
42 Correct 53 ms 70132 KB Output is correct
43 Correct 47 ms 70220 KB Output is correct
44 Correct 50 ms 70120 KB Output is correct
45 Correct 48 ms 70388 KB Output is correct
46 Correct 53 ms 71544 KB Output is correct
47 Correct 55 ms 71584 KB Output is correct
48 Correct 55 ms 71428 KB Output is correct
49 Correct 53 ms 70620 KB Output is correct
50 Correct 51 ms 70668 KB Output is correct
51 Correct 44 ms 70668 KB Output is correct
52 Correct 52 ms 71672 KB Output is correct
53 Correct 56 ms 71844 KB Output is correct
54 Correct 64 ms 71756 KB Output is correct
55 Correct 49 ms 71684 KB Output is correct
56 Correct 48 ms 71648 KB Output is correct
57 Correct 51 ms 71500 KB Output is correct
58 Correct 52 ms 71512 KB Output is correct
59 Correct 58 ms 71620 KB Output is correct
60 Correct 55 ms 71516 KB Output is correct
61 Correct 50 ms 71592 KB Output is correct
62 Correct 50 ms 71592 KB Output is correct
63 Correct 50 ms 71644 KB Output is correct
64 Correct 49 ms 71448 KB Output is correct
65 Correct 53 ms 71464 KB Output is correct
66 Correct 54 ms 71500 KB Output is correct
67 Correct 50 ms 71432 KB Output is correct
68 Correct 43 ms 70604 KB Output is correct
69 Correct 48 ms 71656 KB Output is correct
70 Correct 49 ms 71628 KB Output is correct
71 Correct 51 ms 71596 KB Output is correct
72 Correct 56 ms 71472 KB Output is correct
73 Correct 53 ms 71396 KB Output is correct
74 Correct 49 ms 71536 KB Output is correct
75 Correct 50 ms 71516 KB Output is correct
76 Correct 48 ms 71504 KB Output is correct
77 Correct 48 ms 71540 KB Output is correct
78 Correct 48 ms 71556 KB Output is correct
79 Correct 48 ms 71532 KB Output is correct
80 Correct 59 ms 71500 KB Output is correct
81 Correct 50 ms 71540 KB Output is correct
82 Correct 51 ms 71472 KB Output is correct
83 Correct 49 ms 71544 KB Output is correct
84 Correct 49 ms 71512 KB Output is correct
85 Correct 48 ms 71500 KB Output is correct
86 Correct 51 ms 71500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 69964 KB Output is correct
2 Correct 43 ms 69964 KB Output is correct
3 Correct 45 ms 69992 KB Output is correct
4 Correct 47 ms 70136 KB Output is correct
5 Correct 51 ms 70248 KB Output is correct
6 Correct 46 ms 70228 KB Output is correct
7 Correct 47 ms 70236 KB Output is correct
8 Correct 42 ms 70116 KB Output is correct
9 Correct 40 ms 70100 KB Output is correct
10 Correct 46 ms 70032 KB Output is correct
11 Correct 41 ms 70228 KB Output is correct
12 Correct 49 ms 70256 KB Output is correct
13 Correct 46 ms 70220 KB Output is correct
14 Correct 44 ms 70132 KB Output is correct
15 Correct 47 ms 70220 KB Output is correct
16 Correct 58 ms 70176 KB Output is correct
17 Correct 55 ms 70136 KB Output is correct
18 Correct 48 ms 70152 KB Output is correct
19 Correct 50 ms 70220 KB Output is correct
20 Correct 46 ms 70240 KB Output is correct
21 Correct 46 ms 70128 KB Output is correct
22 Correct 48 ms 70236 KB Output is correct
23 Correct 46 ms 70212 KB Output is correct
24 Correct 45 ms 70160 KB Output is correct
25 Correct 46 ms 70224 KB Output is correct
26 Correct 41 ms 70084 KB Output is correct
27 Correct 41 ms 70196 KB Output is correct
28 Correct 50 ms 70244 KB Output is correct
29 Correct 45 ms 70140 KB Output is correct
30 Correct 46 ms 70196 KB Output is correct
31 Correct 45 ms 70128 KB Output is correct
32 Correct 48 ms 70232 KB Output is correct
33 Correct 44 ms 70160 KB Output is correct
34 Correct 53 ms 70136 KB Output is correct
35 Correct 44 ms 70224 KB Output is correct
36 Correct 45 ms 70128 KB Output is correct
37 Correct 45 ms 70228 KB Output is correct
38 Correct 45 ms 70232 KB Output is correct
39 Correct 50 ms 70240 KB Output is correct
40 Correct 45 ms 70188 KB Output is correct
41 Correct 46 ms 70168 KB Output is correct
42 Correct 53 ms 70132 KB Output is correct
43 Correct 47 ms 70220 KB Output is correct
44 Correct 50 ms 70120 KB Output is correct
45 Correct 48 ms 70388 KB Output is correct
46 Correct 53 ms 71544 KB Output is correct
47 Correct 55 ms 71584 KB Output is correct
48 Correct 55 ms 71428 KB Output is correct
49 Correct 53 ms 70620 KB Output is correct
50 Correct 51 ms 70668 KB Output is correct
51 Correct 44 ms 70668 KB Output is correct
52 Correct 52 ms 71672 KB Output is correct
53 Correct 56 ms 71844 KB Output is correct
54 Correct 64 ms 71756 KB Output is correct
55 Correct 49 ms 71684 KB Output is correct
56 Correct 48 ms 71648 KB Output is correct
57 Correct 51 ms 71500 KB Output is correct
58 Correct 52 ms 71512 KB Output is correct
59 Correct 58 ms 71620 KB Output is correct
60 Correct 55 ms 71516 KB Output is correct
61 Correct 50 ms 71592 KB Output is correct
62 Correct 50 ms 71592 KB Output is correct
63 Correct 50 ms 71644 KB Output is correct
64 Correct 49 ms 71448 KB Output is correct
65 Correct 53 ms 71464 KB Output is correct
66 Correct 54 ms 71500 KB Output is correct
67 Correct 50 ms 71432 KB Output is correct
68 Correct 43 ms 70604 KB Output is correct
69 Correct 48 ms 71656 KB Output is correct
70 Correct 49 ms 71628 KB Output is correct
71 Correct 51 ms 71596 KB Output is correct
72 Correct 56 ms 71472 KB Output is correct
73 Correct 53 ms 71396 KB Output is correct
74 Correct 49 ms 71536 KB Output is correct
75 Correct 50 ms 71516 KB Output is correct
76 Correct 48 ms 71504 KB Output is correct
77 Correct 48 ms 71540 KB Output is correct
78 Correct 48 ms 71556 KB Output is correct
79 Correct 48 ms 71532 KB Output is correct
80 Correct 59 ms 71500 KB Output is correct
81 Correct 50 ms 71540 KB Output is correct
82 Correct 51 ms 71472 KB Output is correct
83 Correct 49 ms 71544 KB Output is correct
84 Correct 49 ms 71512 KB Output is correct
85 Correct 48 ms 71500 KB Output is correct
86 Correct 51 ms 71500 KB Output is correct
87 Correct 65 ms 73876 KB Output is correct
88 Correct 104 ms 81460 KB Output is correct
89 Correct 291 ms 108132 KB Output is correct
90 Correct 305 ms 108152 KB Output is correct
91 Correct 286 ms 108192 KB Output is correct
92 Correct 112 ms 86392 KB Output is correct
93 Correct 118 ms 86412 KB Output is correct
94 Correct 117 ms 86424 KB Output is correct
95 Correct 163 ms 113580 KB Output is correct
96 Correct 167 ms 114032 KB Output is correct
97 Correct 163 ms 114000 KB Output is correct
98 Correct 189 ms 114088 KB Output is correct
99 Correct 151 ms 110752 KB Output is correct
100 Correct 306 ms 99740 KB Output is correct
101 Correct 364 ms 100028 KB Output is correct
102 Correct 356 ms 100144 KB Output is correct
103 Correct 382 ms 100036 KB Output is correct
104 Correct 176 ms 110924 KB Output is correct
105 Correct 169 ms 110896 KB Output is correct
106 Correct 184 ms 110740 KB Output is correct
107 Correct 285 ms 107368 KB Output is correct
108 Correct 313 ms 107580 KB Output is correct
109 Correct 333 ms 107800 KB Output is correct
110 Correct 107 ms 85740 KB Output is correct
111 Correct 188 ms 113680 KB Output is correct
112 Correct 161 ms 113424 KB Output is correct
113 Correct 171 ms 110268 KB Output is correct
114 Correct 347 ms 99804 KB Output is correct
115 Correct 368 ms 99508 KB Output is correct
116 Correct 180 ms 110088 KB Output is correct
117 Correct 161 ms 109188 KB Output is correct
118 Correct 178 ms 108876 KB Output is correct
119 Correct 177 ms 108704 KB Output is correct
120 Correct 167 ms 108900 KB Output is correct
121 Correct 161 ms 108500 KB Output is correct
122 Correct 187 ms 108364 KB Output is correct
123 Correct 197 ms 109232 KB Output is correct
124 Correct 192 ms 108876 KB Output is correct
125 Correct 181 ms 108748 KB Output is correct
126 Correct 199 ms 109004 KB Output is correct
127 Correct 183 ms 108644 KB Output is correct
128 Correct 193 ms 108448 KB Output is correct