답안 #1036563

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1036563 2024-07-27T14:03:58 Z Hanksburger Construction of Highway (JOI18_construction) C++17
100 / 100
471 ms 24148 KB
#include <bits/stdc++.h>
using namespace std;
long long a[100005], t[100005], sz[100005], depth[100005], par[100005], heavypar[100005], specialadj[100005], specialpar[100005], seg[400005], cnt;
vector<pair<long long, long long> > vec;
pair<long long, long long> edge[100005];
vector<long long> adj[100005];
void dfs1(long long u)
{
    sz[u]=1;
    for (long long v:adj[u])
    {
        depth[v]=depth[u]+1;
        dfs1(v);
        sz[u]+=sz[v];
    }
}
void dfs2(long long u)
{
    long long heavy=0;
    t[u]=(++cnt);
    for (long long v:adj[u])
        if (!heavy || sz[heavy]<sz[v])
            heavy=v;
    if (heavy)
    {
        heavypar[heavy]=heavypar[u];
        dfs2(heavy);
        for (long long v:adj[u])
        {
            if (v==heavy)
                continue;
            heavypar[v]=v;
            dfs2(v);
        }
    }
}
void push(long long i, long long l, long long r)
{
    if (l==r || !seg[i])
        return;
    seg[i*2]=seg[i*2+1]=seg[i];
    seg[i]=0;
}
void upd(long long i, long long l, long long r, long long ql, long long qr, long long x)
{
    if (ql<=l && r<=qr)
    {
        seg[i]=x;
        return;
    }
    push(i, l, r);
    long long mid=(l+r)/2;
    if (l<=qr && ql<=mid)
        upd(i*2, l, mid, ql, qr, x);
    if (mid<qr && ql<=r)
        upd(i*2+1, mid+1, r, ql, qr, x);
}
long long qry(long long i, long long l, long long r, long long x)
{
    if (l==r)
        return seg[i];
    push(i, l, r);
    long long mid=(l+r)/2;
    if (x<=mid)
        return qry(i*2, l, mid, x);
    else
        return qry(i*2+1, mid+1, r, x);
}
long long calc(long long l, long long r)
{
    if (l==r)
        return 0;
    long long mid=(l+r)/2, ind=0, sum=0, ans=0;
    vector<pair<long long, long long> > vec1, vec2;
    for (long long i=l; i<=mid; i++)
        vec1.push_back(vec[i]);
    for (long long i=mid+1; i<=r; i++)
        vec2.push_back(vec[i]);
    sort(vec1.begin(), vec1.end());
    sort(vec2.begin(), vec2.end());
    for (long long i=0; i<vec2.size(); i++)
    {
        while (ind<vec1.size() && vec1[ind].first<vec2[i].first)
        {
            sum+=vec1[ind].second;
            ind++;
        }
        ans+=sum*vec2[i].second;
    }
    return ans+calc(l, mid)+calc(mid+1, r);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long n;
    cin >> n;
    for (long long i=1; i<=n; i++)
        cin >> a[i];
    for (long long i=1; i<n; i++)
    {
        long long u, v;
        cin >> u >> v;
        edge[i]={u, v};
        adj[u].push_back(v);
        par[v]=u;
    }
    par[1]=heavypar[1]=1;
    dfs1(1);
    dfs2(1);
    for (long long i=1; i<=n; i++)
    {
        upd(1, 1, n, t[i], t[i], i);
        specialpar[i]=i;
    }
    for (long long i=1; i<n; i++)
    {
        long long u=edge[i].first, specialgrp=qry(1, 1, n, t[u]);
        vec.clear();
        vec.push_back({a[specialgrp], depth[u]-depth[specialpar[specialgrp]]+1});
        u=specialpar[specialgrp];
        while (u!=1)
        {
            long long v=par[u], w=specialadj[v], specialgrp2=qry(1, 1, n, t[v]);
            vec.push_back({a[specialgrp2], depth[u]-depth[specialpar[specialgrp2]]});
            specialadj[v]=u;
            u=specialpar[specialgrp]=specialpar[specialgrp2];
            specialpar[specialgrp2]=w;
            
        }
        cout << calc(0, vec.size()-1) << '\n';
        u=edge[i].first;
        long long v=heavypar[u];
        upd(1, 1, n, t[v], t[u], edge[i].second);
        while (v!=1)
        {
            u=par[v];
            v=heavypar[u];
            upd(1, 1, n, t[v], t[u], edge[i].second);
        }
        u=edge[i].second, v=edge[i].first;
        if (specialadj[v])
        {
            long long w=specialadj[v], specialgrp2=qry(1, 1, n, t[w]);
            specialpar[specialgrp2]=w;
        }
        specialadj[v]=u;
        specialpar[u]=1;
    }
}

Compilation message

construction.cpp: In function 'long long int calc(long long int, long long int)':
construction.cpp:81:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (long long i=0; i<vec2.size(); i++)
      |                         ~^~~~~~~~~~~~
construction.cpp:83:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         while (ind<vec1.size() && vec1[ind].first<vec2[i].first)
      |                ~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 1 ms 2908 KB Output is correct
9 Correct 2 ms 2832 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 2 ms 2904 KB Output is correct
13 Correct 1 ms 2908 KB Output is correct
14 Correct 2 ms 2908 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2936 KB Output is correct
17 Correct 2 ms 2740 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 1 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2824 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 2 ms 2828 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 1 ms 2944 KB Output is correct
30 Correct 2 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 2 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 2 ms 2908 KB Output is correct
36 Correct 1 ms 2924 KB Output is correct
37 Correct 2 ms 2908 KB Output is correct
38 Correct 1 ms 2908 KB Output is correct
39 Correct 1 ms 2908 KB Output is correct
40 Correct 2 ms 2908 KB Output is correct
41 Correct 2 ms 2936 KB Output is correct
42 Correct 2 ms 2932 KB Output is correct
43 Correct 1 ms 2820 KB Output is correct
44 Correct 2 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 1 ms 2908 KB Output is correct
9 Correct 2 ms 2832 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 2 ms 2904 KB Output is correct
13 Correct 1 ms 2908 KB Output is correct
14 Correct 2 ms 2908 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2936 KB Output is correct
17 Correct 2 ms 2740 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 1 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2824 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 2 ms 2828 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 1 ms 2944 KB Output is correct
30 Correct 2 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 2 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 2 ms 2908 KB Output is correct
36 Correct 1 ms 2924 KB Output is correct
37 Correct 2 ms 2908 KB Output is correct
38 Correct 1 ms 2908 KB Output is correct
39 Correct 1 ms 2908 KB Output is correct
40 Correct 2 ms 2908 KB Output is correct
41 Correct 2 ms 2936 KB Output is correct
42 Correct 2 ms 2932 KB Output is correct
43 Correct 1 ms 2820 KB Output is correct
44 Correct 2 ms 2908 KB Output is correct
45 Correct 2 ms 2880 KB Output is correct
46 Correct 6 ms 3164 KB Output is correct
47 Correct 6 ms 3188 KB Output is correct
48 Correct 6 ms 3196 KB Output is correct
49 Correct 3 ms 3696 KB Output is correct
50 Correct 3 ms 3604 KB Output is correct
51 Correct 3 ms 3676 KB Output is correct
52 Correct 5 ms 3364 KB Output is correct
53 Correct 4 ms 3416 KB Output is correct
54 Correct 5 ms 3420 KB Output is correct
55 Correct 4 ms 3420 KB Output is correct
56 Correct 3 ms 3528 KB Output is correct
57 Correct 9 ms 3376 KB Output is correct
58 Correct 10 ms 3164 KB Output is correct
59 Correct 10 ms 3188 KB Output is correct
60 Correct 10 ms 3172 KB Output is correct
61 Correct 4 ms 3420 KB Output is correct
62 Correct 4 ms 3420 KB Output is correct
63 Correct 4 ms 3420 KB Output is correct
64 Correct 6 ms 3164 KB Output is correct
65 Correct 6 ms 3164 KB Output is correct
66 Correct 6 ms 3164 KB Output is correct
67 Correct 6 ms 3348 KB Output is correct
68 Correct 3 ms 3420 KB Output is correct
69 Correct 4 ms 3420 KB Output is correct
70 Correct 4 ms 3420 KB Output is correct
71 Correct 4 ms 3420 KB Output is correct
72 Correct 9 ms 3164 KB Output is correct
73 Correct 10 ms 3160 KB Output is correct
74 Correct 4 ms 3420 KB Output is correct
75 Correct 3 ms 3420 KB Output is correct
76 Correct 4 ms 3416 KB Output is correct
77 Correct 4 ms 3164 KB Output is correct
78 Correct 4 ms 3416 KB Output is correct
79 Correct 3 ms 3164 KB Output is correct
80 Correct 3 ms 3164 KB Output is correct
81 Correct 4 ms 3420 KB Output is correct
82 Correct 4 ms 3420 KB Output is correct
83 Correct 4 ms 3416 KB Output is correct
84 Correct 4 ms 3420 KB Output is correct
85 Correct 4 ms 3164 KB Output is correct
86 Correct 4 ms 3348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 2 ms 2908 KB Output is correct
8 Correct 1 ms 2908 KB Output is correct
9 Correct 2 ms 2832 KB Output is correct
10 Correct 1 ms 2908 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 2 ms 2904 KB Output is correct
13 Correct 1 ms 2908 KB Output is correct
14 Correct 2 ms 2908 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2936 KB Output is correct
17 Correct 2 ms 2740 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 1 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 2 ms 2824 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 2 ms 2828 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 1 ms 2944 KB Output is correct
30 Correct 2 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 2 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 2 ms 2908 KB Output is correct
36 Correct 1 ms 2924 KB Output is correct
37 Correct 2 ms 2908 KB Output is correct
38 Correct 1 ms 2908 KB Output is correct
39 Correct 1 ms 2908 KB Output is correct
40 Correct 2 ms 2908 KB Output is correct
41 Correct 2 ms 2936 KB Output is correct
42 Correct 2 ms 2932 KB Output is correct
43 Correct 1 ms 2820 KB Output is correct
44 Correct 2 ms 2908 KB Output is correct
45 Correct 2 ms 2880 KB Output is correct
46 Correct 6 ms 3164 KB Output is correct
47 Correct 6 ms 3188 KB Output is correct
48 Correct 6 ms 3196 KB Output is correct
49 Correct 3 ms 3696 KB Output is correct
50 Correct 3 ms 3604 KB Output is correct
51 Correct 3 ms 3676 KB Output is correct
52 Correct 5 ms 3364 KB Output is correct
53 Correct 4 ms 3416 KB Output is correct
54 Correct 5 ms 3420 KB Output is correct
55 Correct 4 ms 3420 KB Output is correct
56 Correct 3 ms 3528 KB Output is correct
57 Correct 9 ms 3376 KB Output is correct
58 Correct 10 ms 3164 KB Output is correct
59 Correct 10 ms 3188 KB Output is correct
60 Correct 10 ms 3172 KB Output is correct
61 Correct 4 ms 3420 KB Output is correct
62 Correct 4 ms 3420 KB Output is correct
63 Correct 4 ms 3420 KB Output is correct
64 Correct 6 ms 3164 KB Output is correct
65 Correct 6 ms 3164 KB Output is correct
66 Correct 6 ms 3164 KB Output is correct
67 Correct 6 ms 3348 KB Output is correct
68 Correct 3 ms 3420 KB Output is correct
69 Correct 4 ms 3420 KB Output is correct
70 Correct 4 ms 3420 KB Output is correct
71 Correct 4 ms 3420 KB Output is correct
72 Correct 9 ms 3164 KB Output is correct
73 Correct 10 ms 3160 KB Output is correct
74 Correct 4 ms 3420 KB Output is correct
75 Correct 3 ms 3420 KB Output is correct
76 Correct 4 ms 3416 KB Output is correct
77 Correct 4 ms 3164 KB Output is correct
78 Correct 4 ms 3416 KB Output is correct
79 Correct 3 ms 3164 KB Output is correct
80 Correct 3 ms 3164 KB Output is correct
81 Correct 4 ms 3420 KB Output is correct
82 Correct 4 ms 3420 KB Output is correct
83 Correct 4 ms 3416 KB Output is correct
84 Correct 4 ms 3420 KB Output is correct
85 Correct 4 ms 3164 KB Output is correct
86 Correct 4 ms 3348 KB Output is correct
87 Correct 16 ms 4188 KB Output is correct
88 Correct 52 ms 6748 KB Output is correct
89 Correct 220 ms 16552 KB Output is correct
90 Correct 213 ms 16732 KB Output is correct
91 Correct 214 ms 16724 KB Output is correct
92 Correct 69 ms 24148 KB Output is correct
93 Correct 67 ms 24148 KB Output is correct
94 Correct 68 ms 24148 KB Output is correct
95 Correct 83 ms 21304 KB Output is correct
96 Correct 107 ms 21796 KB Output is correct
97 Correct 103 ms 21708 KB Output is correct
98 Correct 102 ms 21704 KB Output is correct
99 Correct 71 ms 19540 KB Output is correct
100 Correct 392 ms 16464 KB Output is correct
101 Correct 432 ms 16976 KB Output is correct
102 Correct 449 ms 16848 KB Output is correct
103 Correct 452 ms 17044 KB Output is correct
104 Correct 87 ms 19344 KB Output is correct
105 Correct 86 ms 19280 KB Output is correct
106 Correct 92 ms 19456 KB Output is correct
107 Correct 212 ms 16020 KB Output is correct
108 Correct 216 ms 16036 KB Output is correct
109 Correct 240 ms 16168 KB Output is correct
110 Correct 69 ms 23636 KB Output is correct
111 Correct 129 ms 21196 KB Output is correct
112 Correct 103 ms 21196 KB Output is correct
113 Correct 74 ms 18784 KB Output is correct
114 Correct 412 ms 16468 KB Output is correct
115 Correct 471 ms 16332 KB Output is correct
116 Correct 93 ms 18772 KB Output is correct
117 Correct 79 ms 17488 KB Output is correct
118 Correct 79 ms 16988 KB Output is correct
119 Correct 91 ms 16724 KB Output is correct
120 Correct 80 ms 17112 KB Output is correct
121 Correct 82 ms 16724 KB Output is correct
122 Correct 84 ms 16468 KB Output is correct
123 Correct 111 ms 17644 KB Output is correct
124 Correct 132 ms 17016 KB Output is correct
125 Correct 108 ms 16980 KB Output is correct
126 Correct 99 ms 17416 KB Output is correct
127 Correct 110 ms 16832 KB Output is correct
128 Correct 129 ms 16636 KB Output is correct