답안 #888179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
888179 2023-12-16T09:43:17 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
813 ms 34564 KB
#include <bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
 
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
 
#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
 
 #define lid (id*2)
 #define rid (id*2+1)
 #define mid ((l+r)/2)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const ll L = 18;
const ll N = 1e5 + 50;
const ll Mod = 1e9 + 7;
 
ll n, c[N], par[N][L], h[N], st[N], ft[N], timer = 1, down[N];
 
vector<int> edge, adj[N];
 
void dfs(int v, int p = 0){
    par[v][0] = p;
    for(int i = 1; i < L; i++){
        if(!par[v][i-1]) break;
        par[v][i] = par[par[v][i-1]][i-1];
    }
    st[v] = timer++;
    for(int u: adj[v]){
        h[u] = h[v] + 1; dfs(u, v);
    }
    ft[v] = timer;

    down[v] = v;
}

int fen[N];

int getfen(int x){
    int out = 0;
    for(; x; x -= x & (-x)) out += fen[x];
    return out;
}

void updfen(int x, int y){
    for(; x <= n+1; x += x & (-x)) fen[x] += y;
}
 
int getPar(int v){
    int x = getfen(st[v]);
    for(int i = L-1; i >= 0; i--){
        if(x == getfen(st[par[v][i]])) v = par[v][i];
    }
    return v;
}
 
vector<int> vec;
vector<pii> block;
 
int bit[N], m;
 
void updsol(int x, int y){
    for(; x <= m; x += x & (-x)) bit[x] += y;
}
 
int getsol(int x){
    int out = 0;
    for(; x; x -= x & (-x)) out += bit[x];
    return out;
}
 
ll solve(){
    vec.clear();
    for(auto i: block) vec.pb(i.F);
 
    sort(all(vec)); vec.resize(unique(all(vec)) - vec.begin());
    m = vec.size();
 
    int sum = 0; ll res = 0;
    for(auto [x, y]: block){
        int id = lower_bound(all(vec), x) - vec.begin() + 1;
        res += 1ll * y * (sum - getsol(id)); updsol(id, y); sum += y;
    }
 
    fill(bit, bit + m+1, 0);
    return res;
}

pii seg[N*4];

void updseg(int x, pii y, int l = 1, int r = n+1, int id = 1){
    if(l+1 == r){
        seg[id] = y; return;
    }
    if(x < mid) updseg(x, y, l, mid, lid);
    else updseg(x, y, mid, r, rid);
    seg[id] = max(seg[lid], seg[rid]);
}

pii getseg(int ql, int qr, int l = 1, int r = n+1, int id = 1){
    if(ql <= l && r <= qr) return seg[id];
    if(qr <= l || r <= ql) return {0, 0};
    return max(getseg(ql, qr, l, mid, lid), getseg(ql, qr, mid, r, rid));
}
 
int main(){
    fast_io;
 
    cin >> n;
 
    for(int i = 1; i <= n; i++) cin >> c[i];
 
    int u, v; edge.pb(1);
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        adj[u].pb(v); edge.pb(v);
    }
 
    dfs(1);
 
    updseg(st[1], {0, c[1]});
    updfen(st[1], 1); updfen(ft[1], -1);

    for(int i = 1; i < n; i++){
        block.clear(); 
        v = par[edge[i]][0];
        while(v){
            u = getPar(v);
            block.pb({getseg(st[v], ft[v]).S, h[v] - h[u] + 1});
            v = par[u][0];
        }
        reverse(all(block));

        cout << solve() << endl;

        v = edge[i];
        updseg(st[v], {i, c[v]});

        int last = v; v = par[v][0];
        while(v){
            u = getPar(v);
            if(down[v] != v){
                updfen(st[down[v]], 1);
                updfen(ft[down[v]], -1);
            }
            down[v] = last;

            if(u != 1){
                updfen(st[u], -1);
                updfen(ft[u], 1);
            }
            last = u;
            v = par[u][0];
        }
    }
 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8680 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8708 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 2 ms 8540 KB Output is correct
31 Correct 2 ms 8704 KB Output is correct
32 Correct 2 ms 8544 KB Output is correct
33 Correct 2 ms 8540 KB Output is correct
34 Correct 2 ms 8536 KB Output is correct
35 Correct 2 ms 8536 KB Output is correct
36 Correct 2 ms 8540 KB Output is correct
37 Correct 2 ms 8540 KB Output is correct
38 Correct 2 ms 8540 KB Output is correct
39 Correct 2 ms 8540 KB Output is correct
40 Correct 2 ms 8540 KB Output is correct
41 Correct 2 ms 8540 KB Output is correct
42 Correct 2 ms 8540 KB Output is correct
43 Correct 2 ms 8736 KB Output is correct
44 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8680 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8708 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 2 ms 8540 KB Output is correct
31 Correct 2 ms 8704 KB Output is correct
32 Correct 2 ms 8544 KB Output is correct
33 Correct 2 ms 8540 KB Output is correct
34 Correct 2 ms 8536 KB Output is correct
35 Correct 2 ms 8536 KB Output is correct
36 Correct 2 ms 8540 KB Output is correct
37 Correct 2 ms 8540 KB Output is correct
38 Correct 2 ms 8540 KB Output is correct
39 Correct 2 ms 8540 KB Output is correct
40 Correct 2 ms 8540 KB Output is correct
41 Correct 2 ms 8540 KB Output is correct
42 Correct 2 ms 8540 KB Output is correct
43 Correct 2 ms 8736 KB Output is correct
44 Correct 2 ms 8540 KB Output is correct
45 Correct 3 ms 8536 KB Output is correct
46 Correct 9 ms 8792 KB Output is correct
47 Correct 10 ms 8796 KB Output is correct
48 Correct 11 ms 8796 KB Output is correct
49 Correct 5 ms 9052 KB Output is correct
50 Correct 5 ms 9124 KB Output is correct
51 Correct 5 ms 9052 KB Output is correct
52 Correct 7 ms 8796 KB Output is correct
53 Correct 6 ms 8968 KB Output is correct
54 Correct 6 ms 8796 KB Output is correct
55 Correct 6 ms 8788 KB Output is correct
56 Correct 5 ms 8892 KB Output is correct
57 Correct 15 ms 8792 KB Output is correct
58 Correct 16 ms 8868 KB Output is correct
59 Correct 17 ms 8792 KB Output is correct
60 Correct 16 ms 8796 KB Output is correct
61 Correct 7 ms 8936 KB Output is correct
62 Correct 6 ms 8796 KB Output is correct
63 Correct 6 ms 8796 KB Output is correct
64 Correct 9 ms 8796 KB Output is correct
65 Correct 9 ms 8796 KB Output is correct
66 Correct 9 ms 8796 KB Output is correct
67 Correct 9 ms 8868 KB Output is correct
68 Correct 4 ms 9052 KB Output is correct
69 Correct 5 ms 8796 KB Output is correct
70 Correct 5 ms 8796 KB Output is correct
71 Correct 5 ms 8796 KB Output is correct
72 Correct 15 ms 8796 KB Output is correct
73 Correct 16 ms 8796 KB Output is correct
74 Correct 6 ms 8796 KB Output is correct
75 Correct 5 ms 8792 KB Output is correct
76 Correct 5 ms 8796 KB Output is correct
77 Correct 5 ms 8892 KB Output is correct
78 Correct 6 ms 8796 KB Output is correct
79 Correct 5 ms 8792 KB Output is correct
80 Correct 6 ms 8792 KB Output is correct
81 Correct 6 ms 8796 KB Output is correct
82 Correct 6 ms 8792 KB Output is correct
83 Correct 6 ms 8796 KB Output is correct
84 Correct 7 ms 8884 KB Output is correct
85 Correct 6 ms 8908 KB Output is correct
86 Correct 6 ms 8796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8680 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 3 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8708 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 2 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8540 KB Output is correct
30 Correct 2 ms 8540 KB Output is correct
31 Correct 2 ms 8704 KB Output is correct
32 Correct 2 ms 8544 KB Output is correct
33 Correct 2 ms 8540 KB Output is correct
34 Correct 2 ms 8536 KB Output is correct
35 Correct 2 ms 8536 KB Output is correct
36 Correct 2 ms 8540 KB Output is correct
37 Correct 2 ms 8540 KB Output is correct
38 Correct 2 ms 8540 KB Output is correct
39 Correct 2 ms 8540 KB Output is correct
40 Correct 2 ms 8540 KB Output is correct
41 Correct 2 ms 8540 KB Output is correct
42 Correct 2 ms 8540 KB Output is correct
43 Correct 2 ms 8736 KB Output is correct
44 Correct 2 ms 8540 KB Output is correct
45 Correct 3 ms 8536 KB Output is correct
46 Correct 9 ms 8792 KB Output is correct
47 Correct 10 ms 8796 KB Output is correct
48 Correct 11 ms 8796 KB Output is correct
49 Correct 5 ms 9052 KB Output is correct
50 Correct 5 ms 9124 KB Output is correct
51 Correct 5 ms 9052 KB Output is correct
52 Correct 7 ms 8796 KB Output is correct
53 Correct 6 ms 8968 KB Output is correct
54 Correct 6 ms 8796 KB Output is correct
55 Correct 6 ms 8788 KB Output is correct
56 Correct 5 ms 8892 KB Output is correct
57 Correct 15 ms 8792 KB Output is correct
58 Correct 16 ms 8868 KB Output is correct
59 Correct 17 ms 8792 KB Output is correct
60 Correct 16 ms 8796 KB Output is correct
61 Correct 7 ms 8936 KB Output is correct
62 Correct 6 ms 8796 KB Output is correct
63 Correct 6 ms 8796 KB Output is correct
64 Correct 9 ms 8796 KB Output is correct
65 Correct 9 ms 8796 KB Output is correct
66 Correct 9 ms 8796 KB Output is correct
67 Correct 9 ms 8868 KB Output is correct
68 Correct 4 ms 9052 KB Output is correct
69 Correct 5 ms 8796 KB Output is correct
70 Correct 5 ms 8796 KB Output is correct
71 Correct 5 ms 8796 KB Output is correct
72 Correct 15 ms 8796 KB Output is correct
73 Correct 16 ms 8796 KB Output is correct
74 Correct 6 ms 8796 KB Output is correct
75 Correct 5 ms 8792 KB Output is correct
76 Correct 5 ms 8796 KB Output is correct
77 Correct 5 ms 8892 KB Output is correct
78 Correct 6 ms 8796 KB Output is correct
79 Correct 5 ms 8792 KB Output is correct
80 Correct 6 ms 8792 KB Output is correct
81 Correct 6 ms 8796 KB Output is correct
82 Correct 6 ms 8792 KB Output is correct
83 Correct 6 ms 8796 KB Output is correct
84 Correct 7 ms 8884 KB Output is correct
85 Correct 6 ms 8908 KB Output is correct
86 Correct 6 ms 8796 KB Output is correct
87 Correct 25 ms 13028 KB Output is correct
88 Correct 91 ms 15964 KB Output is correct
89 Correct 382 ms 27004 KB Output is correct
90 Correct 374 ms 26996 KB Output is correct
91 Correct 384 ms 26920 KB Output is correct
92 Correct 117 ms 34500 KB Output is correct
93 Correct 154 ms 34508 KB Output is correct
94 Correct 115 ms 34508 KB Output is correct
95 Correct 168 ms 30796 KB Output is correct
96 Correct 177 ms 30796 KB Output is correct
97 Correct 208 ms 31004 KB Output is correct
98 Correct 179 ms 30796 KB Output is correct
99 Correct 157 ms 30144 KB Output is correct
100 Correct 738 ms 27456 KB Output is correct
101 Correct 777 ms 27332 KB Output is correct
102 Correct 792 ms 27336 KB Output is correct
103 Correct 813 ms 27336 KB Output is correct
104 Correct 215 ms 29900 KB Output is correct
105 Correct 225 ms 30112 KB Output is correct
106 Correct 217 ms 29900 KB Output is correct
107 Correct 418 ms 26880 KB Output is correct
108 Correct 360 ms 27012 KB Output is correct
109 Correct 395 ms 27080 KB Output is correct
110 Correct 117 ms 34564 KB Output is correct
111 Correct 184 ms 30836 KB Output is correct
112 Correct 185 ms 30680 KB Output is correct
113 Correct 188 ms 30152 KB Output is correct
114 Correct 745 ms 27824 KB Output is correct
115 Correct 779 ms 27640 KB Output is correct
116 Correct 205 ms 30116 KB Output is correct
117 Correct 184 ms 28472 KB Output is correct
118 Correct 155 ms 27940 KB Output is correct
119 Correct 154 ms 27624 KB Output is correct
120 Correct 152 ms 28260 KB Output is correct
121 Correct 146 ms 27848 KB Output is correct
122 Correct 168 ms 27896 KB Output is correct
123 Correct 209 ms 28584 KB Output is correct
124 Correct 223 ms 27848 KB Output is correct
125 Correct 221 ms 27652 KB Output is correct
126 Correct 260 ms 28444 KB Output is correct
127 Correct 205 ms 27864 KB Output is correct
128 Correct 227 ms 27800 KB Output is correct