답안 #1045032

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045032 2024-08-05T15:46:37 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
1189 ms 48532 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3")
#define endl '\n'
#define db double
#define ll __int128
#define int long long
#define pb push_back
#define fs first
#define sd second
#define Mod long(1e9 + 7)
#define all(x) x.begin(), x.end()
#define unvisited long(-1)
#define Eps double(1e-9)
#define _for(i, n) for(int i = 0; i < (n); i++)
#define dbg(x) cout << #x ": " << x << endl;

const int Max = 1e6 + 7, Inf = 1e9 + 7;

void print(bool x) { cout << (x ? "YES" : "NO") << endl; }

string tostring (__int128 x)
{
    string ans = "";
    while(x > 0)
    {
        ans += (x % 10 + '0');
        x /= 10;
    }
    reverse(all(ans));
    return ans;
}

vector <vector<int>> v; 
vector <int> in, out, s; 

struct SegmentTree1
{
    vector <int> tree; int l;

    void update(int k, int x){
        k += (l - 1)+1; tree[k] = x;
        for(k /= 2; k > 0; k /= 2){
            tree[k] = max(tree[2*k], tree[2*k+1]); 
        }
    } 

    int query(int node, int x, int y, int s, int e){
        if(s > y || e < x) return 0; 
        if(s >= x && e <= y){
            return tree[node];
        }
        int mid = (s + e) / 2;
        return max(query(node*2, x, y, s, mid),
            query(node*2+1, x, y, mid+1, e)); 
    }

    int query(int x, int y){
        return query(1, x+1, y+1, 1, l); 
    }

    SegmentTree1 (int n)
    {
        for(l = 1; l < n; l *= 2);
        tree.assign(2*l+7, 0); 
    }
};

struct SegmentTree2
{
    vector <int> tree; int l;

    void update(int k, int x){
        k += (l - 1) + 1; tree[k] += x; 
        for(k /= 2; k > 0; k /= 2){
            tree[k] = tree[2*k] + tree[2*k+1]; 
        }
    } 

    int query(int node, int x, int y, int s, int e){
        if(s > y || e < x) return 0; 
        if(s >= x && e <= y){
            return tree[node];
        }
        int mid = (s + e) / 2;
        return query(node*2, x, y, s, mid) +
            query(node*2+1, x, y, mid+1, e); 
    }

    int query(int x, int y){
        return query(1, x+1, y+1, 1, l); 
    }

    SegmentTree2 (int n)
    {
        for(l = 1; l < n; l *= 2);
        tree.assign(2*l+7, 0); 
    }
};

void dfs(int node)
{
    in[node] = s.size(); 
    s.push_back(node);
    for(auto& u : v[node]){
        dfs(u);  
    }
    out[node] = s.size(); 
    s.push_back(node); 
}

vector <int> cmp(vector <int> c){
    map <int, int> mp; int id = 0;
    vector <pair<int, int>> t; 
    _for(i, c.size()) t.push_back({ c[i], i });
    sort(all(t));  
    _for(i, c.size()) {
        if(mp[t[i].fs] == 0){
            mp[t[i].fs] = id;
            id++; 
        }
        c[t[i].sd] = mp[t[i].fs];
    }
    return c; 
} 

void solve()
{
    int n; cin >> n; 
    vector <int> c(n+1), d(n+1); 
    _for(i, n) cin >> c[i+1];
    c = cmp(c); 
    vector <pair<int, int>> edge(n-1); 
    v.assign(n+1, vector <int> ()); 
    for(auto& u : edge){
        cin >> u.fs >> u.sd; 
        v[u.fs].pb(u.sd); //v[u.sd].pb(u.fs); 
    }

    in.assign(2*n+7, 0); 
    out.assign(2*n+7, 0);
    dfs(1);

    vector <vector<int>> p(n+1, vector <int> (21, 0)); 
    SegmentTree1 St1(2*n+7); SegmentTree2 St2(2*n+7);

    _for(i, n-1)
    {
        vector <pair<int, int>> f; 
        int ans = 0, x = edge[i].fs;

        while (x != 0)
        { 
            f.push_back({ 1, St1.query(in[x], out[x]) });
            for(int j = 20; j >= 0; j--) if(p[x][j] != 0){
                int y = p[x][j]; 
                if(St1.query(in[y], out[y]) == f.back().sd){
                    x = y;
                    f.back().fs += (1LL << j);
                }
            }
            x = p[x][0]; 
        } 

        p[edge[i].sd][0] = edge[i].fs; 
        for(int j = 1; j <= 20; j++){
            p[edge[i].sd][j] = p[p[edge[i].sd][j-1]][j-1];
        }

        reverse(all(f)); 

        for(auto& u : f){
            ans = ans + u.fs * (St2.query(c[edge[u.sd].sd]+1, n+2)); 
            St2.update(c[edge[u.sd].sd], u.fs); 
        }

        for(auto& u : f){
            St2.update(c[edge[u.sd].sd], -u.fs); 
        }

        St1.update(in[edge[i].sd], i);

        cout << ans << endl; 
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int Q = 1; //cin >> Q;

    while (Q--)
    {
        solve();
    }

    return 0;
}

Compilation message

construction.cpp: In function 'std::vector<long long int> cmp(std::vector<long long int>)':
construction.cpp:16:37: 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 | #define _for(i, n) for(int i = 0; i < (n); i++)
      |                                     ^
construction.cpp:116:5: note: in expansion of macro '_for'
  116 |     _for(i, c.size()) t.push_back({ c[i], i });
      |     ^~~~
construction.cpp:16:37: 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 | #define _for(i, n) for(int i = 0; i < (n); i++)
      |                                     ^
construction.cpp:118:5: note: in expansion of macro '_for'
  118 |     _for(i, c.size()) {
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 648 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 648 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 11 ms 1884 KB Output is correct
47 Correct 11 ms 1884 KB Output is correct
48 Correct 10 ms 1884 KB Output is correct
49 Correct 7 ms 2240 KB Output is correct
50 Correct 6 ms 2140 KB Output is correct
51 Correct 6 ms 2140 KB Output is correct
52 Correct 8 ms 2140 KB Output is correct
53 Correct 9 ms 2140 KB Output is correct
54 Correct 9 ms 2140 KB Output is correct
55 Correct 9 ms 2220 KB Output is correct
56 Correct 11 ms 2136 KB Output is correct
57 Correct 19 ms 1884 KB Output is correct
58 Correct 20 ms 2040 KB Output is correct
59 Correct 21 ms 1880 KB Output is correct
60 Correct 20 ms 1884 KB Output is correct
61 Correct 10 ms 2140 KB Output is correct
62 Correct 10 ms 2156 KB Output is correct
63 Correct 11 ms 2136 KB Output is correct
64 Correct 11 ms 1880 KB Output is correct
65 Correct 10 ms 1884 KB Output is correct
66 Correct 10 ms 2056 KB Output is correct
67 Correct 10 ms 2064 KB Output is correct
68 Correct 7 ms 2140 KB Output is correct
69 Correct 9 ms 2140 KB Output is correct
70 Correct 8 ms 2140 KB Output is correct
71 Correct 10 ms 2152 KB Output is correct
72 Correct 19 ms 2056 KB Output is correct
73 Correct 19 ms 2056 KB Output is correct
74 Correct 9 ms 2140 KB Output is correct
75 Correct 8 ms 2000 KB Output is correct
76 Correct 8 ms 1884 KB Output is correct
77 Correct 8 ms 1880 KB Output is correct
78 Correct 7 ms 2000 KB Output is correct
79 Correct 8 ms 1884 KB Output is correct
80 Correct 7 ms 1880 KB Output is correct
81 Correct 9 ms 2100 KB Output is correct
82 Correct 9 ms 1884 KB Output is correct
83 Correct 9 ms 2084 KB Output is correct
84 Correct 9 ms 2084 KB Output is correct
85 Correct 9 ms 2084 KB Output is correct
86 Correct 8 ms 1884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 648 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 2 ms 604 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 2 ms 604 KB Output is correct
46 Correct 11 ms 1884 KB Output is correct
47 Correct 11 ms 1884 KB Output is correct
48 Correct 10 ms 1884 KB Output is correct
49 Correct 7 ms 2240 KB Output is correct
50 Correct 6 ms 2140 KB Output is correct
51 Correct 6 ms 2140 KB Output is correct
52 Correct 8 ms 2140 KB Output is correct
53 Correct 9 ms 2140 KB Output is correct
54 Correct 9 ms 2140 KB Output is correct
55 Correct 9 ms 2220 KB Output is correct
56 Correct 11 ms 2136 KB Output is correct
57 Correct 19 ms 1884 KB Output is correct
58 Correct 20 ms 2040 KB Output is correct
59 Correct 21 ms 1880 KB Output is correct
60 Correct 20 ms 1884 KB Output is correct
61 Correct 10 ms 2140 KB Output is correct
62 Correct 10 ms 2156 KB Output is correct
63 Correct 11 ms 2136 KB Output is correct
64 Correct 11 ms 1880 KB Output is correct
65 Correct 10 ms 1884 KB Output is correct
66 Correct 10 ms 2056 KB Output is correct
67 Correct 10 ms 2064 KB Output is correct
68 Correct 7 ms 2140 KB Output is correct
69 Correct 9 ms 2140 KB Output is correct
70 Correct 8 ms 2140 KB Output is correct
71 Correct 10 ms 2152 KB Output is correct
72 Correct 19 ms 2056 KB Output is correct
73 Correct 19 ms 2056 KB Output is correct
74 Correct 9 ms 2140 KB Output is correct
75 Correct 8 ms 2000 KB Output is correct
76 Correct 8 ms 1884 KB Output is correct
77 Correct 8 ms 1880 KB Output is correct
78 Correct 7 ms 2000 KB Output is correct
79 Correct 8 ms 1884 KB Output is correct
80 Correct 7 ms 1880 KB Output is correct
81 Correct 9 ms 2100 KB Output is correct
82 Correct 9 ms 1884 KB Output is correct
83 Correct 9 ms 2084 KB Output is correct
84 Correct 9 ms 2084 KB Output is correct
85 Correct 9 ms 2084 KB Output is correct
86 Correct 8 ms 1884 KB Output is correct
87 Correct 34 ms 4664 KB Output is correct
88 Correct 117 ms 12636 KB Output is correct
89 Correct 520 ms 42484 KB Output is correct
90 Correct 529 ms 42460 KB Output is correct
91 Correct 554 ms 42640 KB Output is correct
92 Correct 295 ms 48440 KB Output is correct
93 Correct 307 ms 48532 KB Output is correct
94 Correct 286 ms 48532 KB Output is correct
95 Correct 379 ms 45904 KB Output is correct
96 Correct 395 ms 46248 KB Output is correct
97 Correct 400 ms 46372 KB Output is correct
98 Correct 421 ms 46228 KB Output is correct
99 Correct 425 ms 44692 KB Output is correct
100 Correct 1090 ms 42388 KB Output is correct
101 Correct 1151 ms 42644 KB Output is correct
102 Correct 1139 ms 42788 KB Output is correct
103 Correct 1189 ms 42892 KB Output is correct
104 Correct 484 ms 44616 KB Output is correct
105 Correct 513 ms 44692 KB Output is correct
106 Correct 563 ms 44772 KB Output is correct
107 Correct 550 ms 41828 KB Output is correct
108 Correct 576 ms 41820 KB Output is correct
109 Correct 635 ms 42044 KB Output is correct
110 Correct 325 ms 47932 KB Output is correct
111 Correct 427 ms 45884 KB Output is correct
112 Correct 448 ms 45972 KB Output is correct
113 Correct 382 ms 43924 KB Output is correct
114 Correct 1085 ms 42388 KB Output is correct
115 Correct 1111 ms 42044 KB Output is correct
116 Correct 452 ms 43920 KB Output is correct
117 Correct 366 ms 43156 KB Output is correct
118 Correct 358 ms 42868 KB Output is correct
119 Correct 337 ms 42648 KB Output is correct
120 Correct 341 ms 42896 KB Output is correct
121 Correct 324 ms 42384 KB Output is correct
122 Correct 303 ms 42648 KB Output is correct
123 Correct 466 ms 43156 KB Output is correct
124 Correct 449 ms 42816 KB Output is correct
125 Correct 473 ms 42552 KB Output is correct
126 Correct 412 ms 42900 KB Output is correct
127 Correct 410 ms 42384 KB Output is correct
128 Correct 421 ms 42872 KB Output is correct