Submission #933339

# Submission time Handle Problem Language Result Execution time Memory
933339 2024-02-25T14:11:07 Z parlimoos Construction of Highway (JOI18_construction) C++14
100 / 100
500 ms 34260 KB
//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'


int n;
vector<int> a , cmp;
int tms[100000] , cs[100000] , hs[100000];
int pth[100000] , bl[100000][20];
int seg[400001][2] , seginf[400001][2] , is[100000];
int fen[100001];
int segcnt = 0;
vector<int> tr[100000];
vector<arr(2)> ops;
vector<arr(2)> ns;

int timer = -1;
void dfs1(int v = 0 , int p = -1){
    cs[v] = 1 , bl[v][0] = p;
    if(p != -1) hs[v] = hs[p] + 1;
    for(int i = 1 ; i < 20 ; i++){
        if(bl[v][i - 1] == -1) bl[v][i] = -1;
        else bl[v][i] = bl[bl[v][i - 1]][i - 1];
    }
    for(int u : tr[v]) if(u != p) dfs1(u , v) , cs[v] += cs[u];
}
void hld(int v = 0){
    tms[v] = ++timer;
    int mxi = -1;
    for(int u : tr[v]) if(u != bl[v][0] and (mxi == -1 or cs[mxi] < cs[u])) mxi = u;
    if(mxi == -1) return;
    pth[mxi] = pth[v] , hld(mxi);
    for(int u : tr[v]) if(u != bl[v][0] and u != mxi) pth[u] = u , hld(u);
}
void buildSeg(int l = 0 , int r = n - 1 , int i = 1){
    seginf[i][0] = l , seginf[i][1] = r , seg[i][1] = -1;
    if(l == r) is[l] = i;
    else{
        int c = (r + l - 1) >> 1;
        buildSeg(l , c , i << 1) , buildSeg(c + 1 , r , (i << 1) | 1);
    }
}
void updSeg(int l , int r , int i , int val){
    if(seginf[i][0] >= l and seginf[i][1] <= r) seg[i][0] = val , seg[i][1] = segcnt;
    else if(seginf[i][1] >= l and seginf[i][0] <= r) updSeg(l , r , i << 1 , val) , updSeg(l , r , (i << 1) | 1 , val);
}
arr(2) getSeg(int v){
    int i = is[tms[v]] , res = seg[i][0] , time = seg[i][1];
    while(i > 1){
        i >>= 1;
        if(time < seg[i][1]) time = seg[i][1] , res = seg[i][0];
    }
    return {res , time};
}
void cmpInt(){
    cmp = a;
    sort(cmp.bg() , cmp.end());
    cmp.resize(int(unique(cmp.bg() , cmp.end()) - cmp.bg()));
    for(int &el : a) el = int(lb(cmp.bg() , cmp.end() , el) - cmp.bg());
}
void addFen(int i , int val){
    i++;
    while(i < n + 1) fen[i] += val , i += (i & (-i));
}
int getFen(int l , int r){
    int res = 0;
    r++;
    while(r > 0) res += fen[r] , r -= (r & (-r));
    while(l > 0) res -= fen[l] , l -= (l & (-l));
    return res;
}
void updPth(int v , int val){
    while(v != -1){
        updSeg(tms[pth[v]] , tms[v] , 1 , val);
        v = bl[pth[v]][0];
    }
    segcnt++;
}
ll getPth(int v){
    ns.cl();
    ll res = 0;
    while(v != -1){
        auto d = getSeg(v);
        int u = v;
        for(int i = 19 ; i >= 0 ; i--){
            if(bl[u][i] == -1) continue;
            if(getSeg(bl[u][i]) == d) u = bl[u][i];
        }
        ns.pb({d[0] , hs[v] - hs[u] + 1});
        v = bl[u][0];
    }
    for(int i = 0 ; i < (int)ns.size() ; i++){
        if(ns[i][0] > 0) res += (1ll * ns[i][1]) * (1ll * getFen(0 , ns[i][0] - 1));
        addFen(ns[i][0] , ns[i][1]);
    }
    for(int i = 0 ; i < (int)ns.size() ; i++) addFen(ns[i][0] , -(ns[i][1]));
    return res;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    for(int i = 0 ; i < n ; i++){
        int d;
        cin >> d;
        a.pb(d);
    }
    for(int i = 1 ; i < n ; i++){
        int v , u;
        cin >> v >> u;
        v-- , u--;
        tr[v].pb(u) , tr[u].pb(v);
        ops.pb({v , u});
    }
    dfs1() , hld() , cmpInt() , buildSeg();
    for(auto op : ops){
        ll o = getPth(op[0]);
        cout << o << endl;
        updPth(op[1] , a[op[1]]);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10852 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 3 ms 11004 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 2 ms 10844 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10892 KB Output is correct
26 Correct 3 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 3 ms 10844 KB Output is correct
31 Correct 2 ms 10844 KB Output is correct
32 Correct 3 ms 10844 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
34 Correct 2 ms 10844 KB Output is correct
35 Correct 2 ms 10872 KB Output is correct
36 Correct 2 ms 10844 KB Output is correct
37 Correct 3 ms 10840 KB Output is correct
38 Correct 2 ms 10844 KB Output is correct
39 Correct 2 ms 10932 KB Output is correct
40 Correct 3 ms 10844 KB Output is correct
41 Correct 2 ms 10844 KB Output is correct
42 Correct 3 ms 10844 KB Output is correct
43 Correct 2 ms 10840 KB Output is correct
44 Correct 2 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10852 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 3 ms 11004 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 2 ms 10844 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10892 KB Output is correct
26 Correct 3 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 3 ms 10844 KB Output is correct
31 Correct 2 ms 10844 KB Output is correct
32 Correct 3 ms 10844 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
34 Correct 2 ms 10844 KB Output is correct
35 Correct 2 ms 10872 KB Output is correct
36 Correct 2 ms 10844 KB Output is correct
37 Correct 3 ms 10840 KB Output is correct
38 Correct 2 ms 10844 KB Output is correct
39 Correct 2 ms 10932 KB Output is correct
40 Correct 3 ms 10844 KB Output is correct
41 Correct 2 ms 10844 KB Output is correct
42 Correct 3 ms 10844 KB Output is correct
43 Correct 2 ms 10840 KB Output is correct
44 Correct 2 ms 10844 KB Output is correct
45 Correct 3 ms 10844 KB Output is correct
46 Correct 8 ms 11176 KB Output is correct
47 Correct 8 ms 11100 KB Output is correct
48 Correct 8 ms 11100 KB Output is correct
49 Correct 6 ms 11352 KB Output is correct
50 Correct 6 ms 11356 KB Output is correct
51 Correct 6 ms 11356 KB Output is correct
52 Correct 7 ms 11100 KB Output is correct
53 Correct 6 ms 11356 KB Output is correct
54 Correct 7 ms 11352 KB Output is correct
55 Correct 6 ms 11356 KB Output is correct
56 Correct 7 ms 11096 KB Output is correct
57 Correct 10 ms 11096 KB Output is correct
58 Correct 14 ms 11156 KB Output is correct
59 Correct 11 ms 11100 KB Output is correct
60 Correct 11 ms 11100 KB Output is correct
61 Correct 8 ms 11096 KB Output is correct
62 Correct 7 ms 11100 KB Output is correct
63 Correct 7 ms 11100 KB Output is correct
64 Correct 7 ms 11096 KB Output is correct
65 Correct 7 ms 11100 KB Output is correct
66 Correct 7 ms 11100 KB Output is correct
67 Correct 7 ms 11100 KB Output is correct
68 Correct 5 ms 11236 KB Output is correct
69 Correct 7 ms 11160 KB Output is correct
70 Correct 6 ms 11100 KB Output is correct
71 Correct 6 ms 11100 KB Output is correct
72 Correct 13 ms 11100 KB Output is correct
73 Correct 10 ms 11096 KB Output is correct
74 Correct 8 ms 11100 KB Output is correct
75 Correct 6 ms 11100 KB Output is correct
76 Correct 7 ms 10984 KB Output is correct
77 Correct 7 ms 11100 KB Output is correct
78 Correct 6 ms 11100 KB Output is correct
79 Correct 6 ms 11100 KB Output is correct
80 Correct 6 ms 11100 KB Output is correct
81 Correct 7 ms 11100 KB Output is correct
82 Correct 7 ms 11100 KB Output is correct
83 Correct 7 ms 11096 KB Output is correct
84 Correct 8 ms 11100 KB Output is correct
85 Correct 8 ms 11152 KB Output is correct
86 Correct 6 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10852 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 3 ms 11004 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 2 ms 10844 KB Output is correct
23 Correct 2 ms 10844 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10892 KB Output is correct
26 Correct 3 ms 10844 KB Output is correct
27 Correct 2 ms 10844 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10844 KB Output is correct
30 Correct 3 ms 10844 KB Output is correct
31 Correct 2 ms 10844 KB Output is correct
32 Correct 3 ms 10844 KB Output is correct
33 Correct 2 ms 10844 KB Output is correct
34 Correct 2 ms 10844 KB Output is correct
35 Correct 2 ms 10872 KB Output is correct
36 Correct 2 ms 10844 KB Output is correct
37 Correct 3 ms 10840 KB Output is correct
38 Correct 2 ms 10844 KB Output is correct
39 Correct 2 ms 10932 KB Output is correct
40 Correct 3 ms 10844 KB Output is correct
41 Correct 2 ms 10844 KB Output is correct
42 Correct 3 ms 10844 KB Output is correct
43 Correct 2 ms 10840 KB Output is correct
44 Correct 2 ms 10844 KB Output is correct
45 Correct 3 ms 10844 KB Output is correct
46 Correct 8 ms 11176 KB Output is correct
47 Correct 8 ms 11100 KB Output is correct
48 Correct 8 ms 11100 KB Output is correct
49 Correct 6 ms 11352 KB Output is correct
50 Correct 6 ms 11356 KB Output is correct
51 Correct 6 ms 11356 KB Output is correct
52 Correct 7 ms 11100 KB Output is correct
53 Correct 6 ms 11356 KB Output is correct
54 Correct 7 ms 11352 KB Output is correct
55 Correct 6 ms 11356 KB Output is correct
56 Correct 7 ms 11096 KB Output is correct
57 Correct 10 ms 11096 KB Output is correct
58 Correct 14 ms 11156 KB Output is correct
59 Correct 11 ms 11100 KB Output is correct
60 Correct 11 ms 11100 KB Output is correct
61 Correct 8 ms 11096 KB Output is correct
62 Correct 7 ms 11100 KB Output is correct
63 Correct 7 ms 11100 KB Output is correct
64 Correct 7 ms 11096 KB Output is correct
65 Correct 7 ms 11100 KB Output is correct
66 Correct 7 ms 11100 KB Output is correct
67 Correct 7 ms 11100 KB Output is correct
68 Correct 5 ms 11236 KB Output is correct
69 Correct 7 ms 11160 KB Output is correct
70 Correct 6 ms 11100 KB Output is correct
71 Correct 6 ms 11100 KB Output is correct
72 Correct 13 ms 11100 KB Output is correct
73 Correct 10 ms 11096 KB Output is correct
74 Correct 8 ms 11100 KB Output is correct
75 Correct 6 ms 11100 KB Output is correct
76 Correct 7 ms 10984 KB Output is correct
77 Correct 7 ms 11100 KB Output is correct
78 Correct 6 ms 11100 KB Output is correct
79 Correct 6 ms 11100 KB Output is correct
80 Correct 6 ms 11100 KB Output is correct
81 Correct 7 ms 11100 KB Output is correct
82 Correct 7 ms 11100 KB Output is correct
83 Correct 7 ms 11096 KB Output is correct
84 Correct 8 ms 11100 KB Output is correct
85 Correct 8 ms 11152 KB Output is correct
86 Correct 6 ms 11100 KB Output is correct
87 Correct 19 ms 13660 KB Output is correct
88 Correct 61 ms 17104 KB Output is correct
89 Correct 263 ms 26412 KB Output is correct
90 Correct 261 ms 26320 KB Output is correct
91 Correct 287 ms 26808 KB Output is correct
92 Correct 159 ms 34000 KB Output is correct
93 Correct 160 ms 33992 KB Output is correct
94 Correct 193 ms 34260 KB Output is correct
95 Correct 179 ms 30580 KB Output is correct
96 Correct 187 ms 30820 KB Output is correct
97 Correct 203 ms 30924 KB Output is correct
98 Correct 183 ms 30852 KB Output is correct
99 Correct 233 ms 30348 KB Output is correct
100 Correct 435 ms 25964 KB Output is correct
101 Correct 500 ms 26600 KB Output is correct
102 Correct 468 ms 26316 KB Output is correct
103 Correct 463 ms 26488 KB Output is correct
104 Correct 267 ms 30076 KB Output is correct
105 Correct 283 ms 30512 KB Output is correct
106 Correct 273 ms 30076 KB Output is correct
107 Correct 251 ms 25520 KB Output is correct
108 Correct 243 ms 25692 KB Output is correct
109 Correct 269 ms 25808 KB Output is correct
110 Correct 164 ms 33568 KB Output is correct
111 Correct 177 ms 30652 KB Output is correct
112 Correct 165 ms 30416 KB Output is correct
113 Correct 209 ms 29976 KB Output is correct
114 Correct 424 ms 25912 KB Output is correct
115 Correct 431 ms 25960 KB Output is correct
116 Correct 253 ms 29476 KB Output is correct
117 Correct 215 ms 28260 KB Output is correct
118 Correct 206 ms 27180 KB Output is correct
119 Correct 183 ms 26924 KB Output is correct
120 Correct 208 ms 27636 KB Output is correct
121 Correct 187 ms 27024 KB Output is correct
122 Correct 178 ms 26576 KB Output is correct
123 Correct 261 ms 28544 KB Output is correct
124 Correct 253 ms 27344 KB Output is correct
125 Correct 237 ms 27084 KB Output is correct
126 Correct 263 ms 27628 KB Output is correct
127 Correct 231 ms 26864 KB Output is correct
128 Correct 232 ms 26732 KB Output is correct