Submission #220194

# Submission time Handle Problem Language Result Execution time Memory
220194 2020-04-07T09:49:04 Z DodgeBallMan Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 103032 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

const int N = 1e5 + 10;
int par[N], rot[N], sz[N], hv[N], c[N], pos[N], n;
long long t[N];
deque<pii> col[N];
vector<pii> qq;
vector<int> g[N];

int dfs( int u, int p ) {
    sz[u] = 1, hv[u] = -1, par[u] = p;
    for( int v : g[u] ) if( v != p ) {
        int mx = -1;
        int now = dfs( v, u );
        sz[u] += now;
        if( now > mx ) mx = now, hv[u] = v;
    }
    return sz[u];
}

void hld() {
    for( int i = 1, idx = 0; i <= n ; i++ ) if( hv[par[i]] != i ) 
        for( int j = i ; j != -1 ; j = hv[j] ) 
            rot[j] = i, pos[j] = ++idx;
}

void up( int idx, long long val ) { for( int i = idx ; i < N ; i += ( i & -i ) ) t[i] += val; }
long long que( int idx, long long ret = 0 ) {
    for( int i = idx ; i > 0 ; i -= i & -i ) ret += t[i];
    return ret;
}

long long query( int u ) {
    long long ret = 0;
    vector<int> coord;
    vector<pii> ar;
    //printf("U:%d\n",u);
    while( u ) {
        //printf("%d\n",u);
        vector<pii> all;
        int sz = pos[u] - pos[rot[u]] + 1;
        for( pii i : col[rot[u]] ) {
            if( i.y >= sz ) {
                all.emplace_back( i.x, sz );
                break;
            }
            else all.emplace_back( i ), sz -= i.y;
        }
        reverse( all.begin(), all.end() );
        for( pii i : all ) ar.emplace_back( i ), coord.emplace_back( i.x );
        u = par[rot[u]];
    }
    memset( t, 0, sizeof t );
    sort( coord.begin(), coord.end() );
    coord.resize( unique( coord.begin(), coord.end() ) - coord.begin() );
    for( int i = ( int )ar.size() - 1 ; i >= 0 ; i-- ) {
        //printf("%d %d\n",ar[i].x,ar[i].y);
        int x = lower_bound( coord.begin(), coord.end(), ar[i].x ) - coord.begin() + 1;
        ret += ( que( N - 1 ) - que( x ) ) * ar[i].y; up( x, ar[i].y );
    }
    return ret;
}

void update( int u, int c ) {
    while( u ) {
        int sz = pos[u] - pos[rot[u]] + 1;
        while( !col[rot[u]].empty() ) {
            if( col[rot[u]].front().y <= sz ) sz -= col[rot[u]].front().y, col[rot[u]].pop_front();
            else {
                col[rot[u]].front().y -= sz; 
                break;
            }
        }
        col[rot[u]].emplace_front( c, pos[u] - pos[rot[u]] + 1 );
        u = par[rot[u]];
    }
}

int main()
{
    scanf("%d",&n);
    for( int i = 1 ; i <= n ; i++ ) scanf("%d",&c[i]);
    for( int i = 1, s, t ; i < n ; i++ ) {
        scanf("%d %d",&s,&t);
        g[s].emplace_back( t ), g[t].emplace_back( s );
        qq.emplace_back( pii( s, t ) );
    }
    dfs( 1, 0 ), hld();
    col[1].emplace_back( pii( c[1], 1 ) );
    for( pii i : qq ) {
        printf("%lld\n",query( i.x ) );
        update( i.y, c[i.y] );
    }
    return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
construction.cpp:87:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for( int i = 1 ; i <= n ; i++ ) scanf("%d",&c[i]);
                                     ~~~~~^~~~~~~~~~~~
construction.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&s,&t);
         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 50 ms 71032 KB Output is correct
2 Correct 53 ms 70832 KB Output is correct
3 Correct 55 ms 70904 KB Output is correct
4 Correct 60 ms 70904 KB Output is correct
5 Correct 66 ms 71040 KB Output is correct
6 Correct 66 ms 70904 KB Output is correct
7 Correct 66 ms 70904 KB Output is correct
8 Correct 61 ms 70904 KB Output is correct
9 Correct 61 ms 70904 KB Output is correct
10 Correct 61 ms 70904 KB Output is correct
11 Correct 76 ms 70904 KB Output is correct
12 Correct 85 ms 71032 KB Output is correct
13 Correct 87 ms 70904 KB Output is correct
14 Correct 76 ms 71024 KB Output is correct
15 Correct 65 ms 70912 KB Output is correct
16 Correct 66 ms 70904 KB Output is correct
17 Correct 66 ms 70904 KB Output is correct
18 Correct 66 ms 70904 KB Output is correct
19 Correct 75 ms 71000 KB Output is correct
20 Correct 78 ms 71024 KB Output is correct
21 Correct 77 ms 71032 KB Output is correct
22 Correct 66 ms 70904 KB Output is correct
23 Correct 67 ms 71160 KB Output is correct
24 Correct 67 ms 70904 KB Output is correct
25 Correct 66 ms 70904 KB Output is correct
26 Correct 65 ms 70908 KB Output is correct
27 Correct 77 ms 70904 KB Output is correct
28 Correct 77 ms 71056 KB Output is correct
29 Correct 79 ms 71160 KB Output is correct
30 Correct 66 ms 70904 KB Output is correct
31 Correct 72 ms 71032 KB Output is correct
32 Correct 77 ms 70940 KB Output is correct
33 Correct 72 ms 71032 KB Output is correct
34 Correct 74 ms 71160 KB Output is correct
35 Correct 68 ms 70904 KB Output is correct
36 Correct 69 ms 70904 KB Output is correct
37 Correct 78 ms 70904 KB Output is correct
38 Correct 69 ms 70904 KB Output is correct
39 Correct 71 ms 70904 KB Output is correct
40 Correct 72 ms 70904 KB Output is correct
41 Correct 69 ms 71040 KB Output is correct
42 Correct 73 ms 70904 KB Output is correct
43 Correct 68 ms 70904 KB Output is correct
44 Correct 68 ms 70904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 71032 KB Output is correct
2 Correct 53 ms 70832 KB Output is correct
3 Correct 55 ms 70904 KB Output is correct
4 Correct 60 ms 70904 KB Output is correct
5 Correct 66 ms 71040 KB Output is correct
6 Correct 66 ms 70904 KB Output is correct
7 Correct 66 ms 70904 KB Output is correct
8 Correct 61 ms 70904 KB Output is correct
9 Correct 61 ms 70904 KB Output is correct
10 Correct 61 ms 70904 KB Output is correct
11 Correct 76 ms 70904 KB Output is correct
12 Correct 85 ms 71032 KB Output is correct
13 Correct 87 ms 70904 KB Output is correct
14 Correct 76 ms 71024 KB Output is correct
15 Correct 65 ms 70912 KB Output is correct
16 Correct 66 ms 70904 KB Output is correct
17 Correct 66 ms 70904 KB Output is correct
18 Correct 66 ms 70904 KB Output is correct
19 Correct 75 ms 71000 KB Output is correct
20 Correct 78 ms 71024 KB Output is correct
21 Correct 77 ms 71032 KB Output is correct
22 Correct 66 ms 70904 KB Output is correct
23 Correct 67 ms 71160 KB Output is correct
24 Correct 67 ms 70904 KB Output is correct
25 Correct 66 ms 70904 KB Output is correct
26 Correct 65 ms 70908 KB Output is correct
27 Correct 77 ms 70904 KB Output is correct
28 Correct 77 ms 71056 KB Output is correct
29 Correct 79 ms 71160 KB Output is correct
30 Correct 66 ms 70904 KB Output is correct
31 Correct 72 ms 71032 KB Output is correct
32 Correct 77 ms 70940 KB Output is correct
33 Correct 72 ms 71032 KB Output is correct
34 Correct 74 ms 71160 KB Output is correct
35 Correct 68 ms 70904 KB Output is correct
36 Correct 69 ms 70904 KB Output is correct
37 Correct 78 ms 70904 KB Output is correct
38 Correct 69 ms 70904 KB Output is correct
39 Correct 71 ms 70904 KB Output is correct
40 Correct 72 ms 70904 KB Output is correct
41 Correct 69 ms 71040 KB Output is correct
42 Correct 73 ms 70904 KB Output is correct
43 Correct 68 ms 70904 KB Output is correct
44 Correct 68 ms 70904 KB Output is correct
45 Correct 79 ms 71160 KB Output is correct
46 Correct 179 ms 72184 KB Output is correct
47 Correct 170 ms 72184 KB Output is correct
48 Correct 156 ms 72184 KB Output is correct
49 Correct 152 ms 71416 KB Output is correct
50 Correct 145 ms 71416 KB Output is correct
51 Correct 146 ms 71416 KB Output is correct
52 Correct 934 ms 72340 KB Output is correct
53 Correct 909 ms 72312 KB Output is correct
54 Correct 893 ms 72396 KB Output is correct
55 Correct 903 ms 72352 KB Output is correct
56 Correct 844 ms 72368 KB Output is correct
57 Correct 172 ms 72228 KB Output is correct
58 Correct 161 ms 72184 KB Output is correct
59 Correct 164 ms 72184 KB Output is correct
60 Correct 162 ms 72188 KB Output is correct
61 Correct 895 ms 72292 KB Output is correct
62 Correct 892 ms 72288 KB Output is correct
63 Correct 891 ms 72312 KB Output is correct
64 Correct 166 ms 72184 KB Output is correct
65 Correct 158 ms 72184 KB Output is correct
66 Correct 192 ms 72184 KB Output is correct
67 Correct 169 ms 72344 KB Output is correct
68 Correct 151 ms 71288 KB Output is correct
69 Correct 904 ms 72440 KB Output is correct
70 Correct 909 ms 72448 KB Output is correct
71 Correct 874 ms 72440 KB Output is correct
72 Correct 164 ms 72184 KB Output is correct
73 Correct 164 ms 72056 KB Output is correct
74 Correct 897 ms 72440 KB Output is correct
75 Correct 494 ms 72184 KB Output is correct
76 Correct 391 ms 72184 KB Output is correct
77 Correct 325 ms 72184 KB Output is correct
78 Correct 507 ms 72312 KB Output is correct
79 Correct 383 ms 72184 KB Output is correct
80 Correct 333 ms 72312 KB Output is correct
81 Correct 526 ms 72232 KB Output is correct
82 Correct 419 ms 72568 KB Output is correct
83 Correct 344 ms 72212 KB Output is correct
84 Correct 529 ms 72184 KB Output is correct
85 Correct 406 ms 72184 KB Output is correct
86 Correct 340 ms 72184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 71032 KB Output is correct
2 Correct 53 ms 70832 KB Output is correct
3 Correct 55 ms 70904 KB Output is correct
4 Correct 60 ms 70904 KB Output is correct
5 Correct 66 ms 71040 KB Output is correct
6 Correct 66 ms 70904 KB Output is correct
7 Correct 66 ms 70904 KB Output is correct
8 Correct 61 ms 70904 KB Output is correct
9 Correct 61 ms 70904 KB Output is correct
10 Correct 61 ms 70904 KB Output is correct
11 Correct 76 ms 70904 KB Output is correct
12 Correct 85 ms 71032 KB Output is correct
13 Correct 87 ms 70904 KB Output is correct
14 Correct 76 ms 71024 KB Output is correct
15 Correct 65 ms 70912 KB Output is correct
16 Correct 66 ms 70904 KB Output is correct
17 Correct 66 ms 70904 KB Output is correct
18 Correct 66 ms 70904 KB Output is correct
19 Correct 75 ms 71000 KB Output is correct
20 Correct 78 ms 71024 KB Output is correct
21 Correct 77 ms 71032 KB Output is correct
22 Correct 66 ms 70904 KB Output is correct
23 Correct 67 ms 71160 KB Output is correct
24 Correct 67 ms 70904 KB Output is correct
25 Correct 66 ms 70904 KB Output is correct
26 Correct 65 ms 70908 KB Output is correct
27 Correct 77 ms 70904 KB Output is correct
28 Correct 77 ms 71056 KB Output is correct
29 Correct 79 ms 71160 KB Output is correct
30 Correct 66 ms 70904 KB Output is correct
31 Correct 72 ms 71032 KB Output is correct
32 Correct 77 ms 70940 KB Output is correct
33 Correct 72 ms 71032 KB Output is correct
34 Correct 74 ms 71160 KB Output is correct
35 Correct 68 ms 70904 KB Output is correct
36 Correct 69 ms 70904 KB Output is correct
37 Correct 78 ms 70904 KB Output is correct
38 Correct 69 ms 70904 KB Output is correct
39 Correct 71 ms 70904 KB Output is correct
40 Correct 72 ms 70904 KB Output is correct
41 Correct 69 ms 71040 KB Output is correct
42 Correct 73 ms 70904 KB Output is correct
43 Correct 68 ms 70904 KB Output is correct
44 Correct 68 ms 70904 KB Output is correct
45 Correct 79 ms 71160 KB Output is correct
46 Correct 179 ms 72184 KB Output is correct
47 Correct 170 ms 72184 KB Output is correct
48 Correct 156 ms 72184 KB Output is correct
49 Correct 152 ms 71416 KB Output is correct
50 Correct 145 ms 71416 KB Output is correct
51 Correct 146 ms 71416 KB Output is correct
52 Correct 934 ms 72340 KB Output is correct
53 Correct 909 ms 72312 KB Output is correct
54 Correct 893 ms 72396 KB Output is correct
55 Correct 903 ms 72352 KB Output is correct
56 Correct 844 ms 72368 KB Output is correct
57 Correct 172 ms 72228 KB Output is correct
58 Correct 161 ms 72184 KB Output is correct
59 Correct 164 ms 72184 KB Output is correct
60 Correct 162 ms 72188 KB Output is correct
61 Correct 895 ms 72292 KB Output is correct
62 Correct 892 ms 72288 KB Output is correct
63 Correct 891 ms 72312 KB Output is correct
64 Correct 166 ms 72184 KB Output is correct
65 Correct 158 ms 72184 KB Output is correct
66 Correct 192 ms 72184 KB Output is correct
67 Correct 169 ms 72344 KB Output is correct
68 Correct 151 ms 71288 KB Output is correct
69 Correct 904 ms 72440 KB Output is correct
70 Correct 909 ms 72448 KB Output is correct
71 Correct 874 ms 72440 KB Output is correct
72 Correct 164 ms 72184 KB Output is correct
73 Correct 164 ms 72056 KB Output is correct
74 Correct 897 ms 72440 KB Output is correct
75 Correct 494 ms 72184 KB Output is correct
76 Correct 391 ms 72184 KB Output is correct
77 Correct 325 ms 72184 KB Output is correct
78 Correct 507 ms 72312 KB Output is correct
79 Correct 383 ms 72184 KB Output is correct
80 Correct 333 ms 72312 KB Output is correct
81 Correct 526 ms 72232 KB Output is correct
82 Correct 419 ms 72568 KB Output is correct
83 Correct 344 ms 72212 KB Output is correct
84 Correct 529 ms 72184 KB Output is correct
85 Correct 406 ms 72184 KB Output is correct
86 Correct 340 ms 72184 KB Output is correct
87 Correct 328 ms 74232 KB Output is correct
88 Correct 872 ms 81232 KB Output is correct
89 Execution timed out 2088 ms 103032 KB Time limit exceeded
90 Halted 0 ms 0 KB -