답안 #315358

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
315358 2020-10-22T15:26:59 Z jainbot27 Construction of Highway (JOI18_construction) C++17
100 / 100
462 ms 21752 KB
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()

#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;

template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const char nl = '\n';
const int mxN = 1e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;

const int inf = 1e9;


int n, a[mxN], par[mxN], heavy[mxN], depth[mxN], root[mxN], treePos[mxN]; 
pii e[mxN];
vi adj[mxN];
vpii vals[mxN];
typedef int node;
struct BIT{
    vector<node> bit;
    vpii ops;
    int n;
    void init(int x){
        n=x+1;
        bit.assign(n + 1,0);
    }
    node sum(int r){
        node ret = 0;
        for(r++; r ; r -= r & -r){
            ret += bit[r];
        }
        return ret;
    }
    node sum(int l, int r){
        return sum(r) - sum(l-1);
    }
    void add(int idx, node delta){
        ops.pb({idx, delta});
        for(idx++; idx < n; idx += idx & -idx){
            bit[idx] += delta;
        }
    }
    void clear(){
        trav(x, ops){
            add(x.f, -x.s);
        }
        ops.clear();
    }
};

BIT B;
int dfs(int u, int p){
    int sz = 1, mxSub = 0;
    trav(v, adj[u]){
        if(v == p)
            continue;
        par[v] = u;
        depth[v] = depth[u] + 1;
        int d = dfs(v, u);
        if(ckmax(mxSub, d)) heavy[u] = v;
        sz += d;
    }
    return sz;
}
ll query(int v){
    int r = root[v], tot = depth[v]-depth[r]+1;
    vpii tmp;
    R0F(i, siz(vals[r])){
        auto cur = vals[r][i];
        if(cur.s >= tot){
            tmp.pb({cur.f, tot});
            break;
        }
        else{
            tot -= cur.s;
            tmp.pb(cur);
        }
    }
    reverse(all(tmp));
    ll res = 0;
    trav(x, tmp){
        res += (ll)x.s * B.sum(x.f-1);
        B.add(x.f, x.s);
    }
    return res;
}
ll queryAns(int v){
    ll ans = 0;
    B.clear();
    for(; v != -1; v = par[root[v]]) ans+= query(v);
    return ans;
}
void upd(int v, int val){
    int r = root[v], tot = depth[v] - depth[r] + 1;
    int tmp=tot;
    while(vals[r].back().s < tot) {
        tot -= vals[r].back().s;
        vals[r].pop_back();
    }
    vals[r].back().s -= tot;
    if(vals[r].back().s == 0) vals[r].pop_back();
    vals[r].pb({val, tmp});
}
void updAns(int v, int val){
    for(; v != -1; v = par[root[v]]) upd(v, val);
}
void compress(){
    map<int, int> m;
    F0R(i, n) m[a[i]]=0;
    int xx = 0;
    trav(xd, m) xd.s = xx ++;
    F0R(i, n) a[i] = m[a[i]];
}
int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    B.init(mxN);
    cin >> n;
    F0R(i, n) cin >> a[i];
    compress();
    F0R(i, n-1){
        cin >> e[i].f >> e[i].s;
        e[i].f--; e[i].s--;
        adj[e[i].f].pb(e[i].s);
        adj[e[i].s].pb(e[i].f);
    }
    F0R(i, n) heavy[i] = -1;
    par[0] = -1; depth[0] = 0;
    dfs(0, -1);
    F0R(i, n){
        if(par[i] == -1 || heavy[par[i]] != i){
            for(int j = i; j != -1; j = heavy[j]){
                root[j] = i;
                vals[i].pb({0, 1});
            }
        }
    }
    updAns(0, a[0]);
    F0R(i, n-1){
        cout << queryAns(e[i].f) << nl;
        updAns(e[i].s, a[e[i].s]);
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5504 KB Output is correct
2 Correct 4 ms 5504 KB Output is correct
3 Correct 4 ms 5504 KB Output is correct
4 Correct 4 ms 5504 KB Output is correct
5 Correct 5 ms 5504 KB Output is correct
6 Correct 4 ms 5504 KB Output is correct
7 Correct 4 ms 5504 KB Output is correct
8 Correct 4 ms 5504 KB Output is correct
9 Correct 4 ms 5504 KB Output is correct
10 Correct 4 ms 5504 KB Output is correct
11 Correct 4 ms 5504 KB Output is correct
12 Correct 4 ms 5504 KB Output is correct
13 Correct 4 ms 5504 KB Output is correct
14 Correct 4 ms 5504 KB Output is correct
15 Correct 5 ms 5504 KB Output is correct
16 Correct 5 ms 5504 KB Output is correct
17 Correct 5 ms 5504 KB Output is correct
18 Correct 5 ms 5504 KB Output is correct
19 Correct 4 ms 5504 KB Output is correct
20 Correct 5 ms 5504 KB Output is correct
21 Correct 4 ms 5504 KB Output is correct
22 Correct 4 ms 5504 KB Output is correct
23 Correct 4 ms 5504 KB Output is correct
24 Correct 4 ms 5504 KB Output is correct
25 Correct 4 ms 5504 KB Output is correct
26 Correct 4 ms 5504 KB Output is correct
27 Correct 4 ms 5480 KB Output is correct
28 Correct 4 ms 5504 KB Output is correct
29 Correct 4 ms 5504 KB Output is correct
30 Correct 5 ms 5504 KB Output is correct
31 Correct 5 ms 5504 KB Output is correct
32 Correct 4 ms 5504 KB Output is correct
33 Correct 5 ms 5504 KB Output is correct
34 Correct 4 ms 5504 KB Output is correct
35 Correct 4 ms 5504 KB Output is correct
36 Correct 4 ms 5504 KB Output is correct
37 Correct 4 ms 5504 KB Output is correct
38 Correct 4 ms 5504 KB Output is correct
39 Correct 4 ms 5504 KB Output is correct
40 Correct 4 ms 5504 KB Output is correct
41 Correct 4 ms 5504 KB Output is correct
42 Correct 4 ms 5504 KB Output is correct
43 Correct 4 ms 5504 KB Output is correct
44 Correct 4 ms 5504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5504 KB Output is correct
2 Correct 4 ms 5504 KB Output is correct
3 Correct 4 ms 5504 KB Output is correct
4 Correct 4 ms 5504 KB Output is correct
5 Correct 5 ms 5504 KB Output is correct
6 Correct 4 ms 5504 KB Output is correct
7 Correct 4 ms 5504 KB Output is correct
8 Correct 4 ms 5504 KB Output is correct
9 Correct 4 ms 5504 KB Output is correct
10 Correct 4 ms 5504 KB Output is correct
11 Correct 4 ms 5504 KB Output is correct
12 Correct 4 ms 5504 KB Output is correct
13 Correct 4 ms 5504 KB Output is correct
14 Correct 4 ms 5504 KB Output is correct
15 Correct 5 ms 5504 KB Output is correct
16 Correct 5 ms 5504 KB Output is correct
17 Correct 5 ms 5504 KB Output is correct
18 Correct 5 ms 5504 KB Output is correct
19 Correct 4 ms 5504 KB Output is correct
20 Correct 5 ms 5504 KB Output is correct
21 Correct 4 ms 5504 KB Output is correct
22 Correct 4 ms 5504 KB Output is correct
23 Correct 4 ms 5504 KB Output is correct
24 Correct 4 ms 5504 KB Output is correct
25 Correct 4 ms 5504 KB Output is correct
26 Correct 4 ms 5504 KB Output is correct
27 Correct 4 ms 5480 KB Output is correct
28 Correct 4 ms 5504 KB Output is correct
29 Correct 4 ms 5504 KB Output is correct
30 Correct 5 ms 5504 KB Output is correct
31 Correct 5 ms 5504 KB Output is correct
32 Correct 4 ms 5504 KB Output is correct
33 Correct 5 ms 5504 KB Output is correct
34 Correct 4 ms 5504 KB Output is correct
35 Correct 4 ms 5504 KB Output is correct
36 Correct 4 ms 5504 KB Output is correct
37 Correct 4 ms 5504 KB Output is correct
38 Correct 4 ms 5504 KB Output is correct
39 Correct 4 ms 5504 KB Output is correct
40 Correct 4 ms 5504 KB Output is correct
41 Correct 4 ms 5504 KB Output is correct
42 Correct 4 ms 5504 KB Output is correct
43 Correct 4 ms 5504 KB Output is correct
44 Correct 4 ms 5504 KB Output is correct
45 Correct 5 ms 5632 KB Output is correct
46 Correct 11 ms 5760 KB Output is correct
47 Correct 14 ms 5760 KB Output is correct
48 Correct 11 ms 5760 KB Output is correct
49 Correct 9 ms 6144 KB Output is correct
50 Correct 8 ms 6144 KB Output is correct
51 Correct 8 ms 6144 KB Output is correct
52 Correct 9 ms 6016 KB Output is correct
53 Correct 8 ms 6016 KB Output is correct
54 Correct 8 ms 6016 KB Output is correct
55 Correct 8 ms 6016 KB Output is correct
56 Correct 9 ms 6016 KB Output is correct
57 Correct 13 ms 5760 KB Output is correct
58 Correct 16 ms 5760 KB Output is correct
59 Correct 14 ms 5760 KB Output is correct
60 Correct 14 ms 5780 KB Output is correct
61 Correct 9 ms 6016 KB Output is correct
62 Correct 8 ms 6016 KB Output is correct
63 Correct 9 ms 6016 KB Output is correct
64 Correct 9 ms 5760 KB Output is correct
65 Correct 10 ms 5760 KB Output is correct
66 Correct 10 ms 5760 KB Output is correct
67 Correct 11 ms 5760 KB Output is correct
68 Correct 7 ms 6016 KB Output is correct
69 Correct 8 ms 6016 KB Output is correct
70 Correct 7 ms 6016 KB Output is correct
71 Correct 8 ms 5888 KB Output is correct
72 Correct 13 ms 5760 KB Output is correct
73 Correct 12 ms 5760 KB Output is correct
74 Correct 7 ms 5888 KB Output is correct
75 Correct 9 ms 5888 KB Output is correct
76 Correct 9 ms 5888 KB Output is correct
77 Correct 9 ms 5888 KB Output is correct
78 Correct 9 ms 5888 KB Output is correct
79 Correct 8 ms 5888 KB Output is correct
80 Correct 8 ms 5888 KB Output is correct
81 Correct 9 ms 5888 KB Output is correct
82 Correct 9 ms 5888 KB Output is correct
83 Correct 9 ms 5888 KB Output is correct
84 Correct 10 ms 5920 KB Output is correct
85 Correct 8 ms 5888 KB Output is correct
86 Correct 9 ms 5888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5504 KB Output is correct
2 Correct 4 ms 5504 KB Output is correct
3 Correct 4 ms 5504 KB Output is correct
4 Correct 4 ms 5504 KB Output is correct
5 Correct 5 ms 5504 KB Output is correct
6 Correct 4 ms 5504 KB Output is correct
7 Correct 4 ms 5504 KB Output is correct
8 Correct 4 ms 5504 KB Output is correct
9 Correct 4 ms 5504 KB Output is correct
10 Correct 4 ms 5504 KB Output is correct
11 Correct 4 ms 5504 KB Output is correct
12 Correct 4 ms 5504 KB Output is correct
13 Correct 4 ms 5504 KB Output is correct
14 Correct 4 ms 5504 KB Output is correct
15 Correct 5 ms 5504 KB Output is correct
16 Correct 5 ms 5504 KB Output is correct
17 Correct 5 ms 5504 KB Output is correct
18 Correct 5 ms 5504 KB Output is correct
19 Correct 4 ms 5504 KB Output is correct
20 Correct 5 ms 5504 KB Output is correct
21 Correct 4 ms 5504 KB Output is correct
22 Correct 4 ms 5504 KB Output is correct
23 Correct 4 ms 5504 KB Output is correct
24 Correct 4 ms 5504 KB Output is correct
25 Correct 4 ms 5504 KB Output is correct
26 Correct 4 ms 5504 KB Output is correct
27 Correct 4 ms 5480 KB Output is correct
28 Correct 4 ms 5504 KB Output is correct
29 Correct 4 ms 5504 KB Output is correct
30 Correct 5 ms 5504 KB Output is correct
31 Correct 5 ms 5504 KB Output is correct
32 Correct 4 ms 5504 KB Output is correct
33 Correct 5 ms 5504 KB Output is correct
34 Correct 4 ms 5504 KB Output is correct
35 Correct 4 ms 5504 KB Output is correct
36 Correct 4 ms 5504 KB Output is correct
37 Correct 4 ms 5504 KB Output is correct
38 Correct 4 ms 5504 KB Output is correct
39 Correct 4 ms 5504 KB Output is correct
40 Correct 4 ms 5504 KB Output is correct
41 Correct 4 ms 5504 KB Output is correct
42 Correct 4 ms 5504 KB Output is correct
43 Correct 4 ms 5504 KB Output is correct
44 Correct 4 ms 5504 KB Output is correct
45 Correct 5 ms 5632 KB Output is correct
46 Correct 11 ms 5760 KB Output is correct
47 Correct 14 ms 5760 KB Output is correct
48 Correct 11 ms 5760 KB Output is correct
49 Correct 9 ms 6144 KB Output is correct
50 Correct 8 ms 6144 KB Output is correct
51 Correct 8 ms 6144 KB Output is correct
52 Correct 9 ms 6016 KB Output is correct
53 Correct 8 ms 6016 KB Output is correct
54 Correct 8 ms 6016 KB Output is correct
55 Correct 8 ms 6016 KB Output is correct
56 Correct 9 ms 6016 KB Output is correct
57 Correct 13 ms 5760 KB Output is correct
58 Correct 16 ms 5760 KB Output is correct
59 Correct 14 ms 5760 KB Output is correct
60 Correct 14 ms 5780 KB Output is correct
61 Correct 9 ms 6016 KB Output is correct
62 Correct 8 ms 6016 KB Output is correct
63 Correct 9 ms 6016 KB Output is correct
64 Correct 9 ms 5760 KB Output is correct
65 Correct 10 ms 5760 KB Output is correct
66 Correct 10 ms 5760 KB Output is correct
67 Correct 11 ms 5760 KB Output is correct
68 Correct 7 ms 6016 KB Output is correct
69 Correct 8 ms 6016 KB Output is correct
70 Correct 7 ms 6016 KB Output is correct
71 Correct 8 ms 5888 KB Output is correct
72 Correct 13 ms 5760 KB Output is correct
73 Correct 12 ms 5760 KB Output is correct
74 Correct 7 ms 5888 KB Output is correct
75 Correct 9 ms 5888 KB Output is correct
76 Correct 9 ms 5888 KB Output is correct
77 Correct 9 ms 5888 KB Output is correct
78 Correct 9 ms 5888 KB Output is correct
79 Correct 8 ms 5888 KB Output is correct
80 Correct 8 ms 5888 KB Output is correct
81 Correct 9 ms 5888 KB Output is correct
82 Correct 9 ms 5888 KB Output is correct
83 Correct 9 ms 5888 KB Output is correct
84 Correct 10 ms 5920 KB Output is correct
85 Correct 8 ms 5888 KB Output is correct
86 Correct 9 ms 5888 KB Output is correct
87 Correct 23 ms 6272 KB Output is correct
88 Correct 71 ms 7928 KB Output is correct
89 Correct 311 ms 13656 KB Output is correct
90 Correct 305 ms 13560 KB Output is correct
91 Correct 299 ms 13664 KB Output is correct
92 Correct 171 ms 21752 KB Output is correct
93 Correct 165 ms 21752 KB Output is correct
94 Correct 164 ms 21736 KB Output is correct
95 Correct 173 ms 18800 KB Output is correct
96 Correct 172 ms 18672 KB Output is correct
97 Correct 179 ms 18672 KB Output is correct
98 Correct 178 ms 18672 KB Output is correct
99 Correct 180 ms 17656 KB Output is correct
100 Correct 408 ms 13304 KB Output is correct
101 Correct 454 ms 13176 KB Output is correct
102 Correct 462 ms 13304 KB Output is correct
103 Correct 462 ms 13304 KB Output is correct
104 Correct 191 ms 17784 KB Output is correct
105 Correct 189 ms 17660 KB Output is correct
106 Correct 192 ms 17784 KB Output is correct
107 Correct 234 ms 13560 KB Output is correct
108 Correct 240 ms 13692 KB Output is correct
109 Correct 275 ms 13688 KB Output is correct
110 Correct 107 ms 20336 KB Output is correct
111 Correct 170 ms 18672 KB Output is correct
112 Correct 117 ms 18544 KB Output is correct
113 Correct 117 ms 17520 KB Output is correct
114 Correct 422 ms 13304 KB Output is correct
115 Correct 397 ms 13176 KB Output is correct
116 Correct 128 ms 17520 KB Output is correct
117 Correct 182 ms 15736 KB Output is correct
118 Correct 180 ms 15092 KB Output is correct
119 Correct 180 ms 14712 KB Output is correct
120 Correct 126 ms 15476 KB Output is correct
121 Correct 128 ms 14964 KB Output is correct
122 Correct 126 ms 14580 KB Output is correct
123 Correct 196 ms 15864 KB Output is correct
124 Correct 196 ms 15220 KB Output is correct
125 Correct 192 ms 14760 KB Output is correct
126 Correct 135 ms 15732 KB Output is correct
127 Correct 140 ms 14964 KB Output is correct
128 Correct 136 ms 14584 KB Output is correct