답안 #314118

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
314118 2020-10-18T13:06:30 Z davooddkareshki Construction of Highway (JOI18_construction) C++17
100 / 100
799 ms 36080 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair

const int maxn = 1e5+10;
const int mod = 1e9+7;
const ll inf = 1e18+10;

int c[maxn], n;
int U[maxn], V[maxn];
int st[maxn], par[maxn], dis[maxn], hp[maxn];
vector<int> g[maxn];

struct HLD
{
    vector<int> comp;
    set<pii> se;

    vector<pii> upd(int pos, int val)
    {
        vector<pii> res;
        auto it_end = se.upper_bound(mpr(pos,inf));
        vector<pii> del;
        auto it = se.begin();

        /*if(pos == 3 && val == 3)
        {
            for(auto x : se)
                cout<< x.F <<" "<< x.S <<"\n";
            cout<<"\n";
        }*/

        for(it = se.begin(); it != it_end; it++)
        {
            del.push_back(*it);
            if(it == se.begin())
                res.push_back(*it);
            else
            {
                auto it2 = it; it2--;
                int num = (*it).F - (*it2).F;
                res.push_back({num,(*it).S});
            }
        }
        if(it == se.begin())
            res.push_back({pos,(*it).S});
        else
        {
            if(it != se.end())
            {
                auto it2 = it; it2--;
                res.push_back({pos-(*it2).F, (*it).S});
            }
        }

        for(auto X : del) se.erase(X);
        se.insert({pos,val});

        return res;
    }
} hld[maxn];

void pfs(int v)
{
    st[v] = 1;
    for(auto u : g[v])
        if(u != par[v])
        {
            par[u] = v;
            pfs(u);
            st[v] += st[u];
        }
}

void dfs(int v)
{
    hld[hp[v]].comp.push_back(v);

    for(auto u : g[v])
        if(u != par[v])
        {
            if(st[v] > 2 * st[u])
            {
                hp[u] = u;
                dis[u] = 1;
            }
            else
            {
                hp[u] = hp[v];
                dis[u] = dis[v] + 1;
            }

            dfs(u);
        }
}

struct fenwick
{
    int fen[maxn];
    fenwick() {memset(fen, 0, sizeof fen);}

    void add(int pos, int val)
    {
        for(; pos <= n; pos |= (pos+1))
            fen[pos] += val;
    }

    int qu(int pos)
    {
        int res = 0;
        for(; pos >= 0; pos = (pos&(pos+1))-1)
            res += fen[pos];
        return res;
    }
} fen;

int upd(int v, int val)
{
    vector<pii> Y;
    while(v != 0)
    {
        vector<pii> X = hld[hp[v]].upd(dis[v],val);
        reverse(X.begin(), X.end());
        for(auto x : X) Y.push_back(x);
        v = par[hp[v]];
    }

    int ans = 0;
    //cout<<"\n";
    for(auto X : Y)
    {
        int num = X.F, val = X.S;
        ans += fen.qu(val-1) * num;
        fen.add(val, num);
        //cout<< num <<" "<< val <<"\n";
    }

    for(auto X : Y)
    {
        int num = X.F, val = X.S;
        fen.add(val, -num);
    }

    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    cin>> n; vector<int> ve;
    for(int i = 1; i <= n; i++)
    {
        cin>> c[i];
        ve.push_back(c[i]);
    }
    sort(ve.begin(), ve.end());
    ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
    for(int i = 1; i <= n; i++)
        c[i] = upper_bound(ve.begin(), ve.end(), c[i]) - ve.begin();

    for(int i = 1; i < n; i++)
    {
        cin>> U[i] >> V[i];
        g[U[i]].push_back(V[i]);
        g[V[i]].push_back(U[i]);
    }

    pfs(1);
    dis[1] = 1; hp[1] = 1;
    dfs(1);

    for(int i = 1; i <= n; i++)
    {
        if(hp[i] == i)
            hld[i].se.insert({(int)hld[i].comp.size(),n+1});
        //cout<< i <<" "<< hp[i] <<" "<< dis[i] <<" "<< c[i] <<"\n";
    }

    for(int i = 1, u, v; i < n; i++)
    {
        u = U[i]; v = V[i];
        cout<< upd(v,c[v]) <<"\n";
    }
}

/*
5
1 2 3 4 5
1 2
2 3
2 4
3 5

10
1 7 3 4 8 6 2 9 10 5
1 2
1 3
2 4
3 5
2 6
3 7
4 8
5 9
6 10
*/

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:187:20: warning: variable 'u' set but not used [-Wunused-but-set-variable]
  187 |     for(int i = 1, u, v; i < n; i++)
      |                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10496 KB Output is correct
2 Correct 7 ms 10496 KB Output is correct
3 Correct 7 ms 10624 KB Output is correct
4 Correct 7 ms 10624 KB Output is correct
5 Correct 8 ms 10624 KB Output is correct
6 Correct 8 ms 10624 KB Output is correct
7 Correct 8 ms 10624 KB Output is correct
8 Correct 8 ms 10624 KB Output is correct
9 Correct 8 ms 10624 KB Output is correct
10 Correct 8 ms 10624 KB Output is correct
11 Correct 8 ms 10624 KB Output is correct
12 Correct 7 ms 10624 KB Output is correct
13 Correct 8 ms 10624 KB Output is correct
14 Correct 8 ms 10624 KB Output is correct
15 Correct 8 ms 10624 KB Output is correct
16 Correct 8 ms 10624 KB Output is correct
17 Correct 9 ms 10624 KB Output is correct
18 Correct 8 ms 10624 KB Output is correct
19 Correct 8 ms 10624 KB Output is correct
20 Correct 7 ms 10624 KB Output is correct
21 Correct 8 ms 10624 KB Output is correct
22 Correct 9 ms 10752 KB Output is correct
23 Correct 8 ms 10624 KB Output is correct
24 Correct 8 ms 10624 KB Output is correct
25 Correct 8 ms 10624 KB Output is correct
26 Correct 8 ms 10624 KB Output is correct
27 Correct 7 ms 10624 KB Output is correct
28 Correct 8 ms 10624 KB Output is correct
29 Correct 8 ms 10624 KB Output is correct
30 Correct 8 ms 10624 KB Output is correct
31 Correct 8 ms 10624 KB Output is correct
32 Correct 8 ms 10624 KB Output is correct
33 Correct 8 ms 10624 KB Output is correct
34 Correct 8 ms 10624 KB Output is correct
35 Correct 8 ms 10624 KB Output is correct
36 Correct 8 ms 10624 KB Output is correct
37 Correct 8 ms 10624 KB Output is correct
38 Correct 8 ms 10624 KB Output is correct
39 Correct 8 ms 10624 KB Output is correct
40 Correct 8 ms 10624 KB Output is correct
41 Correct 8 ms 10624 KB Output is correct
42 Correct 8 ms 10624 KB Output is correct
43 Correct 8 ms 10624 KB Output is correct
44 Correct 8 ms 10624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10496 KB Output is correct
2 Correct 7 ms 10496 KB Output is correct
3 Correct 7 ms 10624 KB Output is correct
4 Correct 7 ms 10624 KB Output is correct
5 Correct 8 ms 10624 KB Output is correct
6 Correct 8 ms 10624 KB Output is correct
7 Correct 8 ms 10624 KB Output is correct
8 Correct 8 ms 10624 KB Output is correct
9 Correct 8 ms 10624 KB Output is correct
10 Correct 8 ms 10624 KB Output is correct
11 Correct 8 ms 10624 KB Output is correct
12 Correct 7 ms 10624 KB Output is correct
13 Correct 8 ms 10624 KB Output is correct
14 Correct 8 ms 10624 KB Output is correct
15 Correct 8 ms 10624 KB Output is correct
16 Correct 8 ms 10624 KB Output is correct
17 Correct 9 ms 10624 KB Output is correct
18 Correct 8 ms 10624 KB Output is correct
19 Correct 8 ms 10624 KB Output is correct
20 Correct 7 ms 10624 KB Output is correct
21 Correct 8 ms 10624 KB Output is correct
22 Correct 9 ms 10752 KB Output is correct
23 Correct 8 ms 10624 KB Output is correct
24 Correct 8 ms 10624 KB Output is correct
25 Correct 8 ms 10624 KB Output is correct
26 Correct 8 ms 10624 KB Output is correct
27 Correct 7 ms 10624 KB Output is correct
28 Correct 8 ms 10624 KB Output is correct
29 Correct 8 ms 10624 KB Output is correct
30 Correct 8 ms 10624 KB Output is correct
31 Correct 8 ms 10624 KB Output is correct
32 Correct 8 ms 10624 KB Output is correct
33 Correct 8 ms 10624 KB Output is correct
34 Correct 8 ms 10624 KB Output is correct
35 Correct 8 ms 10624 KB Output is correct
36 Correct 8 ms 10624 KB Output is correct
37 Correct 8 ms 10624 KB Output is correct
38 Correct 8 ms 10624 KB Output is correct
39 Correct 8 ms 10624 KB Output is correct
40 Correct 8 ms 10624 KB Output is correct
41 Correct 8 ms 10624 KB Output is correct
42 Correct 8 ms 10624 KB Output is correct
43 Correct 8 ms 10624 KB Output is correct
44 Correct 8 ms 10624 KB Output is correct
45 Correct 10 ms 10752 KB Output is correct
46 Correct 17 ms 11392 KB Output is correct
47 Correct 17 ms 11264 KB Output is correct
48 Correct 19 ms 11264 KB Output is correct
49 Correct 14 ms 11264 KB Output is correct
50 Correct 14 ms 11264 KB Output is correct
51 Correct 12 ms 11264 KB Output is correct
52 Correct 13 ms 11520 KB Output is correct
53 Correct 13 ms 11520 KB Output is correct
54 Correct 13 ms 11520 KB Output is correct
55 Correct 13 ms 11520 KB Output is correct
56 Correct 12 ms 11392 KB Output is correct
57 Correct 21 ms 11392 KB Output is correct
58 Correct 21 ms 11392 KB Output is correct
59 Correct 28 ms 11392 KB Output is correct
60 Correct 21 ms 11648 KB Output is correct
61 Correct 13 ms 11384 KB Output is correct
62 Correct 14 ms 11392 KB Output is correct
63 Correct 13 ms 11392 KB Output is correct
64 Correct 16 ms 11264 KB Output is correct
65 Correct 16 ms 11264 KB Output is correct
66 Correct 17 ms 11264 KB Output is correct
67 Correct 18 ms 11264 KB Output is correct
68 Correct 11 ms 11264 KB Output is correct
69 Correct 13 ms 11520 KB Output is correct
70 Correct 12 ms 11520 KB Output is correct
71 Correct 12 ms 11392 KB Output is correct
72 Correct 21 ms 11392 KB Output is correct
73 Correct 21 ms 11392 KB Output is correct
74 Correct 12 ms 11264 KB Output is correct
75 Correct 14 ms 11264 KB Output is correct
76 Correct 13 ms 11264 KB Output is correct
77 Correct 13 ms 11264 KB Output is correct
78 Correct 13 ms 11264 KB Output is correct
79 Correct 13 ms 11264 KB Output is correct
80 Correct 13 ms 11264 KB Output is correct
81 Correct 14 ms 11264 KB Output is correct
82 Correct 14 ms 11264 KB Output is correct
83 Correct 14 ms 11264 KB Output is correct
84 Correct 14 ms 11264 KB Output is correct
85 Correct 14 ms 11264 KB Output is correct
86 Correct 15 ms 11264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10496 KB Output is correct
2 Correct 7 ms 10496 KB Output is correct
3 Correct 7 ms 10624 KB Output is correct
4 Correct 7 ms 10624 KB Output is correct
5 Correct 8 ms 10624 KB Output is correct
6 Correct 8 ms 10624 KB Output is correct
7 Correct 8 ms 10624 KB Output is correct
8 Correct 8 ms 10624 KB Output is correct
9 Correct 8 ms 10624 KB Output is correct
10 Correct 8 ms 10624 KB Output is correct
11 Correct 8 ms 10624 KB Output is correct
12 Correct 7 ms 10624 KB Output is correct
13 Correct 8 ms 10624 KB Output is correct
14 Correct 8 ms 10624 KB Output is correct
15 Correct 8 ms 10624 KB Output is correct
16 Correct 8 ms 10624 KB Output is correct
17 Correct 9 ms 10624 KB Output is correct
18 Correct 8 ms 10624 KB Output is correct
19 Correct 8 ms 10624 KB Output is correct
20 Correct 7 ms 10624 KB Output is correct
21 Correct 8 ms 10624 KB Output is correct
22 Correct 9 ms 10752 KB Output is correct
23 Correct 8 ms 10624 KB Output is correct
24 Correct 8 ms 10624 KB Output is correct
25 Correct 8 ms 10624 KB Output is correct
26 Correct 8 ms 10624 KB Output is correct
27 Correct 7 ms 10624 KB Output is correct
28 Correct 8 ms 10624 KB Output is correct
29 Correct 8 ms 10624 KB Output is correct
30 Correct 8 ms 10624 KB Output is correct
31 Correct 8 ms 10624 KB Output is correct
32 Correct 8 ms 10624 KB Output is correct
33 Correct 8 ms 10624 KB Output is correct
34 Correct 8 ms 10624 KB Output is correct
35 Correct 8 ms 10624 KB Output is correct
36 Correct 8 ms 10624 KB Output is correct
37 Correct 8 ms 10624 KB Output is correct
38 Correct 8 ms 10624 KB Output is correct
39 Correct 8 ms 10624 KB Output is correct
40 Correct 8 ms 10624 KB Output is correct
41 Correct 8 ms 10624 KB Output is correct
42 Correct 8 ms 10624 KB Output is correct
43 Correct 8 ms 10624 KB Output is correct
44 Correct 8 ms 10624 KB Output is correct
45 Correct 10 ms 10752 KB Output is correct
46 Correct 17 ms 11392 KB Output is correct
47 Correct 17 ms 11264 KB Output is correct
48 Correct 19 ms 11264 KB Output is correct
49 Correct 14 ms 11264 KB Output is correct
50 Correct 14 ms 11264 KB Output is correct
51 Correct 12 ms 11264 KB Output is correct
52 Correct 13 ms 11520 KB Output is correct
53 Correct 13 ms 11520 KB Output is correct
54 Correct 13 ms 11520 KB Output is correct
55 Correct 13 ms 11520 KB Output is correct
56 Correct 12 ms 11392 KB Output is correct
57 Correct 21 ms 11392 KB Output is correct
58 Correct 21 ms 11392 KB Output is correct
59 Correct 28 ms 11392 KB Output is correct
60 Correct 21 ms 11648 KB Output is correct
61 Correct 13 ms 11384 KB Output is correct
62 Correct 14 ms 11392 KB Output is correct
63 Correct 13 ms 11392 KB Output is correct
64 Correct 16 ms 11264 KB Output is correct
65 Correct 16 ms 11264 KB Output is correct
66 Correct 17 ms 11264 KB Output is correct
67 Correct 18 ms 11264 KB Output is correct
68 Correct 11 ms 11264 KB Output is correct
69 Correct 13 ms 11520 KB Output is correct
70 Correct 12 ms 11520 KB Output is correct
71 Correct 12 ms 11392 KB Output is correct
72 Correct 21 ms 11392 KB Output is correct
73 Correct 21 ms 11392 KB Output is correct
74 Correct 12 ms 11264 KB Output is correct
75 Correct 14 ms 11264 KB Output is correct
76 Correct 13 ms 11264 KB Output is correct
77 Correct 13 ms 11264 KB Output is correct
78 Correct 13 ms 11264 KB Output is correct
79 Correct 13 ms 11264 KB Output is correct
80 Correct 13 ms 11264 KB Output is correct
81 Correct 14 ms 11264 KB Output is correct
82 Correct 14 ms 11264 KB Output is correct
83 Correct 14 ms 11264 KB Output is correct
84 Correct 14 ms 11264 KB Output is correct
85 Correct 14 ms 11264 KB Output is correct
86 Correct 15 ms 11264 KB Output is correct
87 Correct 36 ms 12416 KB Output is correct
88 Correct 112 ms 16376 KB Output is correct
89 Correct 521 ms 29848 KB Output is correct
90 Correct 527 ms 29840 KB Output is correct
91 Correct 521 ms 29972 KB Output is correct
92 Correct 158 ms 28268 KB Output is correct
93 Correct 160 ms 28272 KB Output is correct
94 Correct 163 ms 28268 KB Output is correct
95 Correct 226 ms 35568 KB Output is correct
96 Correct 223 ms 35948 KB Output is correct
97 Correct 229 ms 35984 KB Output is correct
98 Correct 228 ms 36080 KB Output is correct
99 Correct 212 ms 30828 KB Output is correct
100 Correct 725 ms 31092 KB Output is correct
101 Correct 786 ms 31340 KB Output is correct
102 Correct 797 ms 31256 KB Output is correct
103 Correct 799 ms 31488 KB Output is correct
104 Correct 237 ms 30700 KB Output is correct
105 Correct 229 ms 30576 KB Output is correct
106 Correct 232 ms 30784 KB Output is correct
107 Correct 483 ms 29164 KB Output is correct
108 Correct 484 ms 29168 KB Output is correct
109 Correct 497 ms 29572 KB Output is correct
110 Correct 138 ms 27756 KB Output is correct
111 Correct 213 ms 35568 KB Output is correct
112 Correct 211 ms 35308 KB Output is correct
113 Correct 184 ms 29932 KB Output is correct
114 Correct 750 ms 31084 KB Output is correct
115 Correct 788 ms 30700 KB Output is correct
116 Correct 214 ms 29932 KB Output is correct
117 Correct 227 ms 29128 KB Output is correct
118 Correct 224 ms 28692 KB Output is correct
119 Correct 229 ms 28524 KB Output is correct
120 Correct 211 ms 28804 KB Output is correct
121 Correct 214 ms 28420 KB Output is correct
122 Correct 216 ms 28268 KB Output is correct
123 Correct 253 ms 29168 KB Output is correct
124 Correct 261 ms 28780 KB Output is correct
125 Correct 253 ms 28652 KB Output is correct
126 Correct 238 ms 28780 KB Output is correct
127 Correct 254 ms 28400 KB Output is correct
128 Correct 238 ms 28276 KB Output is correct