Submission #575161

# Submission time Handle Problem Language Result Execution time Memory
575161 2022-06-09T19:39:42 Z toloraia Reconstruction Project (JOI22_reconstruction) C++14
100 / 100
3509 ms 54164 KB
#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pb push_back
using namespace std;

const int N = 100005;

int n, m;
pair < int, pair < int, int > > P[N];

vector < int > g[505];

void del (int x, int y){
    for (int i = 0; i < g[x].size(); i++){
        if (g[x][i] == y){
            swap (g[x][i], g[x].back());
            g[x].pop_back();
            break;
        }
    }
}

int G[505][505];
int par[505];

void dfs (int k, int p){
    par[k] = p;
    for (int to : g[k])
        if (to != p)
            dfs (to, k);
}

int st[N], en[N];

int q;
int X[N*10], ans[N*10], C[N*10];

main() {
    //freopen ("in.in", "r", stdin);freopen ("out.out", "w", stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for (int u,v,x,i = 1; i <= m; i++){
        cin >> u >> v >> x;
        P[i] = {x, {u, v}};
    }
    sort (P+1, P+m+1);
    for (int u,v,i = 1; i <= m; i++){
        u = P[i].S.F;
        v = P[i].S.S;
        dfs (u, 0);
        if (par[v] == 0){
            g[u].pb (v);
            g[v].pb (u);
            st[i] = 0;
            G[u][v] = G[v][u] = P[i].F;
        }
        else {
            int x = P[i].F+1;
            int node = v, c = 0;
            while (node != u){
                if (G[node][par[node]] < x){
                    x = G[node][par[node]];
                    c = node;
                }
                node = par[node];
            }
            del (c, par[c]);
            del (par[c], c);
            g[u].pb (v);
            g[v].pb (u);
            st[i] = (P[i].F + G[c][par[c]] + 2) / 2;
            G[u][v] = G[v][u] = P[i].F;
        }
        for (int j = 1; j <= n; j++)
            par[j] = 0;
    }
    for (int i = 1; i <= n; i++)
        g[i].clear();
    for (int u,v, i = m; i >= 1; i--){
        u = P[i].S.F;
        v = P[i].S.S;
        dfs (u, 0);
        if (par[v] == 0){
            g[u].pb (v);
            g[v].pb (u);
            en[i] = 1e9;
        }
        else {
            int x = P[i].F-1;
            int node = v, c = 0;
            while (node != u){
                if (G[node][par[node]] > x){
                    x = G[node][par[node]];
                    c = node;
                }
                node = par[node];
            }
            del (c, par[c]);
            del (par[c], c);
            g[u].pb (v);
            g[v].pb (u);
            en[i] = (P[i].F + G[c][par[c]]) / 2;
            G[u][v] = G[v][u] = P[i].F;
        }
        for (int j = 1; j <= n; j++)
            par[j] = 0;
    }
    cin >> q;
    for (int i = 1; i <= q; i++){
        cin >> X[i];
    }
    for (int i = 1; i <= m; i++){
        int a1, b1;
        int l = 1, r = q+1;
        while (l < r){
            int mid = l + r >> 1;
            if (X[mid] < st[i])
                l = mid + 1;
            else
                r = mid;
        }
        a1 = l;
        l = 0, r = q;
        while (l < r){
            int mid = l + r + 1 >> 1;
            if (X[mid] > P[i].F)
                r = mid - 1;
            else
                l = mid;
        }
        b1 = l;
        if (a1 <= b1){
        C[a1]--;
        C[b1+1]++;
        ans[a1] += P[i].F;
        ans[b1+1] -= P[i].F;
        }
        a1 = b1 + 1;

        l = 0, r = q;
        while (l < r){
            int mid = l + r + 1 >> 1;
            if (X[mid] > en[i])
                r = mid - 1;
            else
                l = mid;
        }
        b1 = l;
        if (a1 <= b1){
        C[a1]++;
        C[b1+1]--;
        ans[a1] -= P[i].F;
        ans[b1+1] += P[i].F;
        }
    }
    for (int i = 1; i <= q; i++){
        ans[i] += ans[i-1];
        C[i] += C[i-1];
        cout << abs(X[i] * C[i] + ans[i]) << endl;
    }
}

Compilation message

reconstruction.cpp: In function 'void del(long long int, long long int)':
reconstruction.cpp:16:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for (int i = 0; i < g[x].size(); i++){
      |                     ~~^~~~~~~~~~~~~
reconstruction.cpp: At global scope:
reconstruction.cpp:40:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   40 | main() {
      | ^~~~
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:118:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  118 |             int mid = l + r >> 1;
      |                       ~~^~~
reconstruction.cpp:127:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  127 |             int mid = l + r + 1 >> 1;
      |                       ~~~~~~^~~
reconstruction.cpp:144:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  144 |             int mid = l + r + 1 >> 1;
      |                       ~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 2087 ms 6384 KB Output is correct
21 Correct 1030 ms 6676 KB Output is correct
22 Correct 1678 ms 6692 KB Output is correct
23 Correct 1883 ms 6792 KB Output is correct
24 Correct 1990 ms 6660 KB Output is correct
25 Correct 1988 ms 6764 KB Output is correct
26 Correct 2037 ms 6708 KB Output is correct
27 Correct 2072 ms 6644 KB Output is correct
28 Correct 2047 ms 6652 KB Output is correct
29 Correct 973 ms 6784 KB Output is correct
30 Correct 2091 ms 6980 KB Output is correct
31 Correct 2070 ms 6576 KB Output is correct
32 Correct 2060 ms 6676 KB Output is correct
33 Correct 2085 ms 6744 KB Output is correct
34 Correct 734 ms 7668 KB Output is correct
35 Correct 2076 ms 6744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 2807 ms 40784 KB Output is correct
5 Correct 2834 ms 52108 KB Output is correct
6 Correct 2838 ms 52156 KB Output is correct
7 Correct 1402 ms 54020 KB Output is correct
8 Correct 1397 ms 54044 KB Output is correct
9 Correct 1381 ms 54132 KB Output is correct
10 Correct 2737 ms 52272 KB Output is correct
11 Correct 1437 ms 54164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1318 ms 38124 KB Output is correct
21 Correct 1296 ms 43388 KB Output is correct
22 Correct 1315 ms 43440 KB Output is correct
23 Correct 1297 ms 43368 KB Output is correct
24 Correct 1353 ms 43572 KB Output is correct
25 Correct 1279 ms 43376 KB Output is correct
26 Correct 1319 ms 43372 KB Output is correct
27 Correct 1271 ms 43424 KB Output is correct
28 Correct 1320 ms 43336 KB Output is correct
29 Correct 1257 ms 43416 KB Output is correct
30 Correct 1264 ms 43596 KB Output is correct
31 Correct 1263 ms 43296 KB Output is correct
32 Correct 1290 ms 43920 KB Output is correct
33 Correct 1259 ms 43396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 2087 ms 6384 KB Output is correct
21 Correct 1030 ms 6676 KB Output is correct
22 Correct 1678 ms 6692 KB Output is correct
23 Correct 1883 ms 6792 KB Output is correct
24 Correct 1990 ms 6660 KB Output is correct
25 Correct 1988 ms 6764 KB Output is correct
26 Correct 2037 ms 6708 KB Output is correct
27 Correct 2072 ms 6644 KB Output is correct
28 Correct 2047 ms 6652 KB Output is correct
29 Correct 973 ms 6784 KB Output is correct
30 Correct 2091 ms 6980 KB Output is correct
31 Correct 2070 ms 6576 KB Output is correct
32 Correct 2060 ms 6676 KB Output is correct
33 Correct 2085 ms 6744 KB Output is correct
34 Correct 734 ms 7668 KB Output is correct
35 Correct 2076 ms 6744 KB Output is correct
36 Correct 2020 ms 7768 KB Output is correct
37 Correct 1067 ms 7520 KB Output is correct
38 Correct 1664 ms 7692 KB Output is correct
39 Correct 1851 ms 7568 KB Output is correct
40 Correct 2007 ms 7372 KB Output is correct
41 Correct 1991 ms 7384 KB Output is correct
42 Correct 2050 ms 7464 KB Output is correct
43 Correct 2027 ms 7456 KB Output is correct
44 Correct 2015 ms 7516 KB Output is correct
45 Correct 952 ms 7712 KB Output is correct
46 Correct 2017 ms 7532 KB Output is correct
47 Correct 2035 ms 7592 KB Output is correct
48 Correct 2023 ms 7400 KB Output is correct
49 Correct 2037 ms 7428 KB Output is correct
50 Correct 721 ms 8652 KB Output is correct
51 Correct 2025 ms 7608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 2087 ms 6384 KB Output is correct
21 Correct 1030 ms 6676 KB Output is correct
22 Correct 1678 ms 6692 KB Output is correct
23 Correct 1883 ms 6792 KB Output is correct
24 Correct 1990 ms 6660 KB Output is correct
25 Correct 1988 ms 6764 KB Output is correct
26 Correct 2037 ms 6708 KB Output is correct
27 Correct 2072 ms 6644 KB Output is correct
28 Correct 2047 ms 6652 KB Output is correct
29 Correct 973 ms 6784 KB Output is correct
30 Correct 2091 ms 6980 KB Output is correct
31 Correct 2070 ms 6576 KB Output is correct
32 Correct 2060 ms 6676 KB Output is correct
33 Correct 2085 ms 6744 KB Output is correct
34 Correct 734 ms 7668 KB Output is correct
35 Correct 2076 ms 6744 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 2807 ms 40784 KB Output is correct
40 Correct 2834 ms 52108 KB Output is correct
41 Correct 2838 ms 52156 KB Output is correct
42 Correct 1402 ms 54020 KB Output is correct
43 Correct 1397 ms 54044 KB Output is correct
44 Correct 1381 ms 54132 KB Output is correct
45 Correct 2737 ms 52272 KB Output is correct
46 Correct 1437 ms 54164 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1318 ms 38124 KB Output is correct
49 Correct 1296 ms 43388 KB Output is correct
50 Correct 1315 ms 43440 KB Output is correct
51 Correct 1297 ms 43368 KB Output is correct
52 Correct 1353 ms 43572 KB Output is correct
53 Correct 1279 ms 43376 KB Output is correct
54 Correct 1319 ms 43372 KB Output is correct
55 Correct 1271 ms 43424 KB Output is correct
56 Correct 1320 ms 43336 KB Output is correct
57 Correct 1257 ms 43416 KB Output is correct
58 Correct 1264 ms 43596 KB Output is correct
59 Correct 1263 ms 43296 KB Output is correct
60 Correct 1290 ms 43920 KB Output is correct
61 Correct 1259 ms 43396 KB Output is correct
62 Correct 2020 ms 7768 KB Output is correct
63 Correct 1067 ms 7520 KB Output is correct
64 Correct 1664 ms 7692 KB Output is correct
65 Correct 1851 ms 7568 KB Output is correct
66 Correct 2007 ms 7372 KB Output is correct
67 Correct 1991 ms 7384 KB Output is correct
68 Correct 2050 ms 7464 KB Output is correct
69 Correct 2027 ms 7456 KB Output is correct
70 Correct 2015 ms 7516 KB Output is correct
71 Correct 952 ms 7712 KB Output is correct
72 Correct 2017 ms 7532 KB Output is correct
73 Correct 2035 ms 7592 KB Output is correct
74 Correct 2023 ms 7400 KB Output is correct
75 Correct 2037 ms 7428 KB Output is correct
76 Correct 721 ms 8652 KB Output is correct
77 Correct 2025 ms 7608 KB Output is correct
78 Correct 3219 ms 51144 KB Output is correct
79 Correct 2199 ms 52812 KB Output is correct
80 Correct 2870 ms 52268 KB Output is correct
81 Correct 3056 ms 52140 KB Output is correct
82 Correct 3164 ms 51328 KB Output is correct
83 Correct 3197 ms 51172 KB Output is correct
84 Correct 3243 ms 48788 KB Output is correct
85 Correct 3222 ms 51328 KB Output is correct
86 Correct 3198 ms 40244 KB Output is correct
87 Correct 2156 ms 46212 KB Output is correct
88 Correct 3229 ms 51388 KB Output is correct
89 Correct 3323 ms 51224 KB Output is correct
90 Correct 3509 ms 51320 KB Output is correct
91 Correct 3275 ms 43904 KB Output is correct
92 Correct 1956 ms 44648 KB Output is correct
93 Correct 3258 ms 52384 KB Output is correct