Submission #261501

# Submission time Handle Problem Language Result Execution time Memory
261501 2020-08-11T19:33:55 Z Hideo Duathlon (APIO18_duathlon) C++17
71 / 100
248 ms 40176 KB
//1610612741, 1000000007
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;

#define all(s) s.begin(), s.end()
#define ok puts("ok")
#define ll long long
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define pi pair < int, int >
#define pii pair < int, pi >
#define next next123
#define left left123

const int N = 2e5 + 7;
const int INF = 1e9 + 7;

ll S1, S2, S3, S4, S5;
int h[N], d[N], sz[N], mn[N], rev[N], con[N];
int dp[N][2], mns[N][2], total[N][2];
int n, m, csz;

vi g[N], despawn[N][2], spawn[N][2];

void precalc (int v, int p = 0){
    h[v] = h[p] + 1;
    sz[v] = 1;
    mn[v] = h[v];
    for (int to : g[v]){
        if (!h[to]){
            precalc(to, v);
            sz[v] += sz[to];
            mn[v] = min(mn[v], mn[to]);
        }
        else {
            mn[v] = min(mn[v], h[to]);
        }
    }
}

void calc1 (int v){
    for (int to : g[v]){
        if (h[to] - h[v] == 1){
            calc1(to);
            S1 += 1LL * (csz - sz[to] - 1) * sz[to];
        }
    }
    S1 += 1LL * (csz - sz[v]) * (sz[v] - 1);
}

void calc2 (int v){
    int temp = h[v];
    for (int to : g[v]){
        if (h[to] - h[v] == 1){
            calc2(to);
            mns[h[v]][0] += mns[h[to]][0];
            mns[h[v]][1] += mns[h[to]][1];
            total[v][0] += total[to][0];
            total[v][1] += total[to][1];
            mns[h[to]][0] = 0;
            mns[h[to]][1] = 0;
            temp = min(temp, mn[to]);
        }
    }
    if (temp != mn[v])
        spawn[temp][0].pb(v);
    if (temp + 1 != mn[v] && h[v] - mn[v] > 2)
        spawn[min(h[v], temp + 1)][1].pb(v);

    for (int it : spawn[h[v]][0]){
        total[v][0] += dp[it][0];
        despawn[mn[it]][0].pb(it);
    }
    spawn[h[v]][0].clear();

    for (int it : spawn[h[v]][1]){
        total[v][1] += dp[it][0];
        despawn[mn[it] + 1][1].pb(it);
    }
    spawn[h[v]][1].clear();

    for (int it : despawn[h[v]][0])
        total[v][0] -= dp[it][0];
    despawn[h[v]][0].clear();

    for (int it : despawn[h[v]][1])
        total[v][1] -= dp[it][0];
    despawn[h[v]][1].clear();

    dp[v][0] = sz[v] - 1 - mns[h[v]][0] + total[v][0];
    dp[v][1] = sz[v] - 1 - mns[h[v]][1] + total[v][1];
    mns[mn[v]][0]++;
    mns[mn[v] + 1][1]++;
    S2 += 1LL * (csz - sz[v]) * dp[v][0];
}

bool comp (int a, int b){
    return mn[a] > mn[b];
}

void calc3 (int v){
    rev[h[v]] = v;
    for (int to : g[v]){
        if (h[to] - h[v] == 1){
            calc3(to);
            S3 += 1LL * sz[to] * ((h[v] - mn[v] - 1) * (h[v] - mn[v]) - (h[v] - mn[to] - 1) * (h[v] - mn[to])) / 2;
            S3 += 1LL * sz[to] * (mn[v] - mn[rev[mn[v] + 1]]) * (h[v] - mn[v] - 1) - (mn[rev[mn[to] + 1]] - mn[v]) * (max(0, h[v] - mn[to] - 1));
        }
    }
    S3 += 1LL * (h[v] - mn[v] - 1) * (h[v] - mn[v]) / 2;
    S3 += 1LL * (mn[v] - mn[rev[mn[v] + 1]]) * (max(0, h[v] - mn[v] - 1));
}

void calc4 (int v){
    int s = 0;
    sort(all(g[v]), comp);
    for (int to : g[v]){
        if (h[to] - h[v] == 1){
            if (mn[to] < h[v])
                S4 += 1LL * sz[to] * (h[v] - mn[to]) * s;
            s += sz[to];
            calc4(to);
        }
    }
}

void calc5 (int v){
    int s = 0;
    for (int to : g[v]){
        if (h[to] - h[v] == 1){
            calc5(to);
            con[to] += dp[to][1];
            if (mn[to] < h[v])
                con[to]++;
            s += con[to];
        }
    }
    for (int to : g[v])
        if (h[to] - h[v] == 1)
            S5 += 1LL * con[to] * (sz[v] - sz[to] - 1) * (csz - sz[v]);
}

main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    for (int i = 1; i <= n; i++)
        if (!h[i])
            precalc(i);

    for (int i = 1; i <= n; i++){
        if (h[i] == 1){
            csz = sz[i];
            calc1(i);
            calc2(i);
            calc3(i);
            calc4(i);
            calc5(i);
        }
    }
    //cout << S1 << ' ' << S2 << ' ' << S3 << ' ' << S4 << ' ' << S5 << endl;
    cout << S1 + 2LL * (S2 + S3 + S4 + S5);
}

Compilation message

count_triplets.cpp:152:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 18 ms 23936 KB Output is correct
4 Correct 18 ms 23840 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23852 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 17 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 19 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 19 ms 23808 KB Output is correct
27 Correct 18 ms 23808 KB Output is correct
28 Correct 18 ms 23808 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 17 ms 23840 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 17 ms 23936 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 18 ms 23936 KB Output is correct
4 Correct 18 ms 23840 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23852 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 17 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 19 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 19 ms 23808 KB Output is correct
27 Correct 18 ms 23808 KB Output is correct
28 Correct 18 ms 23808 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 17 ms 23840 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 17 ms 23936 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Correct 17 ms 23808 KB Output is correct
37 Correct 18 ms 23808 KB Output is correct
38 Incorrect 19 ms 23832 KB Output isn't correct
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 213 ms 40048 KB Output is correct
2 Correct 197 ms 40176 KB Output is correct
3 Correct 248 ms 37748 KB Output is correct
4 Correct 189 ms 38772 KB Output is correct
5 Correct 190 ms 35188 KB Output is correct
6 Correct 205 ms 35568 KB Output is correct
7 Correct 180 ms 33656 KB Output is correct
8 Correct 180 ms 34552 KB Output is correct
9 Correct 159 ms 31868 KB Output is correct
10 Correct 204 ms 33272 KB Output is correct
11 Correct 101 ms 30200 KB Output is correct
12 Correct 101 ms 30072 KB Output is correct
13 Correct 96 ms 29948 KB Output is correct
14 Correct 95 ms 29820 KB Output is correct
15 Correct 80 ms 29560 KB Output is correct
16 Correct 83 ms 29492 KB Output is correct
17 Correct 24 ms 26752 KB Output is correct
18 Correct 24 ms 26880 KB Output is correct
19 Correct 25 ms 26752 KB Output is correct
20 Correct 27 ms 26752 KB Output is correct
21 Correct 25 ms 26496 KB Output is correct
22 Correct 25 ms 26496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 18 ms 23936 KB Output is correct
3 Correct 20 ms 23936 KB Output is correct
4 Correct 19 ms 24064 KB Output is correct
5 Correct 19 ms 23936 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 18 ms 24064 KB Output is correct
8 Correct 19 ms 23936 KB Output is correct
9 Correct 18 ms 23936 KB Output is correct
10 Correct 20 ms 23936 KB Output is correct
11 Correct 18 ms 23936 KB Output is correct
12 Correct 20 ms 23936 KB Output is correct
13 Correct 17 ms 23936 KB Output is correct
14 Correct 20 ms 23936 KB Output is correct
15 Correct 19 ms 23936 KB Output is correct
16 Correct 18 ms 23936 KB Output is correct
17 Correct 19 ms 23936 KB Output is correct
18 Correct 18 ms 23936 KB Output is correct
19 Correct 20 ms 23936 KB Output is correct
20 Correct 19 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 30200 KB Output is correct
2 Correct 161 ms 30200 KB Output is correct
3 Correct 157 ms 30328 KB Output is correct
4 Correct 166 ms 30332 KB Output is correct
5 Correct 153 ms 30200 KB Output is correct
6 Correct 206 ms 38448 KB Output is correct
7 Correct 202 ms 35660 KB Output is correct
8 Correct 220 ms 34296 KB Output is correct
9 Correct 191 ms 32888 KB Output is correct
10 Correct 178 ms 30224 KB Output is correct
11 Correct 176 ms 30200 KB Output is correct
12 Correct 172 ms 30332 KB Output is correct
13 Correct 147 ms 30200 KB Output is correct
14 Correct 113 ms 30200 KB Output is correct
15 Correct 93 ms 29944 KB Output is correct
16 Correct 67 ms 29304 KB Output is correct
17 Correct 102 ms 31088 KB Output is correct
18 Correct 103 ms 30960 KB Output is correct
19 Correct 115 ms 31340 KB Output is correct
20 Correct 119 ms 30968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23936 KB Output is correct
2 Correct 17 ms 23936 KB Output is correct
3 Correct 18 ms 23936 KB Output is correct
4 Correct 18 ms 23936 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 19 ms 23936 KB Output is correct
7 Correct 18 ms 23936 KB Output is correct
8 Correct 18 ms 24064 KB Output is correct
9 Correct 21 ms 23936 KB Output is correct
10 Correct 19 ms 23936 KB Output is correct
11 Correct 18 ms 23936 KB Output is correct
12 Correct 18 ms 24064 KB Output is correct
13 Correct 21 ms 23936 KB Output is correct
14 Correct 20 ms 24064 KB Output is correct
15 Correct 19 ms 23936 KB Output is correct
16 Correct 18 ms 23936 KB Output is correct
17 Correct 21 ms 23936 KB Output is correct
18 Correct 22 ms 23936 KB Output is correct
19 Correct 19 ms 23936 KB Output is correct
20 Correct 18 ms 23936 KB Output is correct
21 Correct 21 ms 23936 KB Output is correct
22 Correct 18 ms 23936 KB Output is correct
23 Correct 19 ms 23936 KB Output is correct
24 Correct 18 ms 23936 KB Output is correct
25 Correct 20 ms 23936 KB Output is correct
26 Correct 17 ms 23936 KB Output is correct
27 Correct 17 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 148 ms 30200 KB Output is correct
2 Correct 146 ms 30200 KB Output is correct
3 Correct 168 ms 30200 KB Output is correct
4 Correct 172 ms 30328 KB Output is correct
5 Correct 175 ms 31736 KB Output is correct
6 Correct 172 ms 31480 KB Output is correct
7 Correct 172 ms 31568 KB Output is correct
8 Correct 185 ms 31608 KB Output is correct
9 Correct 176 ms 31608 KB Output is correct
10 Correct 168 ms 31608 KB Output is correct
11 Correct 183 ms 32120 KB Output is correct
12 Correct 175 ms 32760 KB Output is correct
13 Correct 176 ms 32760 KB Output is correct
14 Correct 183 ms 34808 KB Output is correct
15 Correct 212 ms 37880 KB Output is correct
16 Correct 205 ms 35708 KB Output is correct
17 Correct 231 ms 37840 KB Output is correct
18 Correct 234 ms 35988 KB Output is correct
19 Correct 212 ms 31612 KB Output is correct
20 Correct 212 ms 31736 KB Output is correct
21 Correct 213 ms 31736 KB Output is correct
22 Correct 139 ms 31480 KB Output is correct
23 Correct 131 ms 31352 KB Output is correct
24 Correct 182 ms 32248 KB Output is correct
25 Correct 187 ms 32120 KB Output is correct
26 Correct 191 ms 32244 KB Output is correct
27 Correct 163 ms 31992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 18 ms 23936 KB Output is correct
4 Correct 18 ms 23840 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23852 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 17 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 19 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 19 ms 23808 KB Output is correct
27 Correct 18 ms 23808 KB Output is correct
28 Correct 18 ms 23808 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 17 ms 23840 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 17 ms 23936 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Correct 17 ms 23808 KB Output is correct
37 Correct 18 ms 23808 KB Output is correct
38 Incorrect 19 ms 23832 KB Output isn't correct
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23936 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 18 ms 23936 KB Output is correct
4 Correct 18 ms 23840 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 19 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23852 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 17 ms 23808 KB Output is correct
15 Correct 17 ms 23808 KB Output is correct
16 Correct 17 ms 23808 KB Output is correct
17 Correct 17 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 17 ms 23808 KB Output is correct
21 Correct 18 ms 23808 KB Output is correct
22 Correct 19 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23808 KB Output is correct
25 Correct 17 ms 23808 KB Output is correct
26 Correct 19 ms 23808 KB Output is correct
27 Correct 18 ms 23808 KB Output is correct
28 Correct 18 ms 23808 KB Output is correct
29 Correct 17 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 17 ms 23840 KB Output is correct
33 Correct 17 ms 23808 KB Output is correct
34 Correct 17 ms 23936 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Correct 17 ms 23808 KB Output is correct
37 Correct 18 ms 23808 KB Output is correct
38 Incorrect 19 ms 23832 KB Output isn't correct
39 Halted 0 ms 0 KB -