Submission #1064533

# Submission time Handle Problem Language Result Execution time Memory
1064533 2024-08-18T14:08:57 Z phong Construction of Highway (JOI18_construction) C++17
100 / 100
405 ms 31948 KB
#include<bits/stdc++.h>

#define ll long long
#define int long long
const int nmax = 1e5 + 5, N = 1e6;
const ll oo = 1e9 + 1, base = 311;
const int lg = 19, M = 10;
const ll mod = 1e9 + 2277, mod2 = 1e9 + 5277;
#define pii pair<int, int>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;

int n, c[nmax];
pii a[nmax];
int sz[nmax], par[nmax];
vector<int> adj[nmax];

void dfs(int u, int p){
    sz[u] = 1;
    for(auto v : adj[u]){
        if(v == p) continue;
        par[v] = u;
        dfs(v, u);
        sz[u] += sz[v];
    }
}
int head[nmax], ind[nmax], pos[nmax], Time = 0, nchain = 1;
int rev[nmax];

void dfs_2(int u, int p){
    if(!head[nchain]) head[nchain] = u;
    pos[u] = ++Time;
    ind[u] = nchain;
    rev[Time] = u;
    int idx =-1;
    for(auto v : adj[u]){
        if(v == p) continue;
        if(idx == -1 || sz[idx] < sz[v]){
            idx = v;
        }
    }
    if(idx != -1) dfs_2(idx, u);
    for(auto v : adj[u]){
        if(v == p) continue;
        if(idx == v) continue;
        ++nchain;
        dfs_2(v, u);
    }
}
vector<pii> one;
struct Seg{
    int ma[1 << 18], mi[1 << 18], lz[1 << 18];
    void combine(int id){
        ma[id] = max(ma[id << 1], ma[id << 1 | 1]);
        mi[id] = min(mi[id << 1], mi[id << 1 | 1]);
    }
    void build(int id, int l, int r){
        lz[id] = -1;
        if(l == r){
            ma[id] = mi[id] = c[rev[l]];
            return;
        }
        int mid = r+ l >> 1;
        build(id << 1, l, mid);
        build(id << 1 | 1, mid + 1, r);
        combine(id);
    }
    void fix(int id, int val){
        if(val == -1) return;
        ma[id] = val;
        mi[id] = val;
        lz[id] = val;
    }
    void down(int id){
        fix(id << 1, lz[id]);
        fix(id << 1| 1, lz[id]);
        lz[id] = -1;
    }
    void update(int id, int l, int r, int u, int v, int val){
        if(l > v || r < u) return;
        if(l >= u && r <= v) return fix(id, val);
        down(id);
        int mid = r + l >> 1;
        update(id << 1, l, mid, u, v,val);
        update(id << 1 | 1, mid + 1, r, u, v,val);
        combine(id);
    }
    void get(int id, int l, int r, int u, int v){
        if(l > v || r < u) return;
        if(l >= u && r <= v && ma[id] == mi[id]){
            one.push_back({r - l + 1, ma[id]});
            return;
        }
        down(id);
        int mid = r + l >> 1;
        get(id << 1 | 1, mid + 1, r, u, v);
        get(id << 1, l, mid, u, v);
    }
    int Find(int id, int l, int r, int u){
        if(l == r) return ma[id];
        down(id);
        int mid = r + l >> 1;
        if(mid < u) return Find(id << 1 | 1, mid + 1, r, u);
        else return Find(id << 1,l,mid, u);
    }
}tree;
struct FEN{
    ll f[nmax];
    vector<int> tmp;
    void update(int x, int val){
        for(; x <= n; x += x&-x) f[x] += val, tmp.push_back(x);
    }
    ll get(int x){
        ll cur = 0;
        for(; x; x -= x&-x) cur += f[x];
        return cur;
    }
    void clear(){
        for(auto p : tmp) f[p] = 0;
        tmp.clear();
    }
}F;
ll calc(int u, int x){
    int A = u;
    int uchain = ind[u];
    one.clear();
    while(1){
        if(uchain == 1){
            tree.get(1, 1, n, pos[1], pos[u]);
            tree.update(1, 1, n, pos[1], pos[u], c[x]);
            break;
        }
        tree.get(1, 1, n, pos[head[uchain]], pos[u]);
        tree.update(1, 1, n, pos[head[uchain]], pos[u], c[x]);
        u = par[head[uchain]];
        uchain = ind[u];
    }
    ll ans = 0;
//    cout << A << ' ' << x << "#"<< ' ' << c[x] << endl;
//    for(int i = 1; i <= n; ++i) cout << tree.Find(1, 1, n, pos[i]) << ' ';
//    cout << endl;
    for(auto [x, y] : one){
        ans += 1ll * F.get(y - 1) * x;
        F.update(y, x);
//        cout << x << ' ' << y << endl;
    }
    F.clear();
    return ans;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
//    freopen("code.inp", "r", stdin);
//    freopen("code.out", "w", stdout);
    cin >> n;
    vector<int> nen;
    for(int i = 1; i <= n; ++i) cin >> c[i],nen.push_back(c[i]);
    sort(nen.begin(), nen.end());
    nen.erase(unique(nen.begin(), nen.end()), nen.end());
    for(int i = 1; i <= n; ++i) c[i] = lower_bound(nen.begin(), nen.end(),c[i]) - nen.begin() + 1;
    for(int i = 1; i < n; ++i){
        int x, y;
        cin >> a[i].fi >> a[i].se;
        adj[a[i].fi].push_back(a[i].se);
    }
    dfs(1, -1);
    dfs_2(1, -1);
//    for(int i = 1; i <= n; ++i) cout << c[i] << ' ';
    tree.build(1, 1, n);
//    for(int i = 1; i <= n; ++i) cout << tree.Find(1, 1, n, pos[i]) << ' ';
//    ll ans = 0;
    for(int i = 1; i < n; ++i){
        int x = a[i].fi, y = a[i].se;
//        calc(x, y);
       cout <<  calc(x, y) << endl;
    }
}
/*

*/

Compilation message

construction.cpp: In member function 'void Seg::build(long long int, long long int, long long int)':
construction.cpp:66:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |         int mid = r+ l >> 1;
      |                   ~^~~
construction.cpp: In member function 'void Seg::update(long long int, long long int, long long int, long long int, long long int, long long int)':
construction.cpp:86:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |         int mid = r + l >> 1;
      |                   ~~^~~
construction.cpp: In member function 'void Seg::get(long long int, long long int, long long int, long long int, long long int)':
construction.cpp:98:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   98 |         int mid = r + l >> 1;
      |                   ~~^~~
construction.cpp: In member function 'long long int Seg::Find(long long int, long long int, long long int, long long int)':
construction.cpp:105:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  105 |         int mid = r + l >> 1;
      |                   ~~^~~
construction.cpp: In function 'long long int calc(long long int, long long int)':
construction.cpp:127:9: warning: unused variable 'A' [-Wunused-variable]
  127 |     int A = u;
      |         ^
construction.cpp: At global scope:
construction.cpp:153:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  153 | main(){
      | ^~~~
construction.cpp: In function 'int main()':
construction.cpp:165:13: warning: unused variable 'x' [-Wunused-variable]
  165 |         int x, y;
      |             ^
construction.cpp:165:16: warning: unused variable 'y' [-Wunused-variable]
  165 |         int x, y;
      |                ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2832 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2904 KB Output is correct
5 Correct 2 ms 2904 KB Output is correct
6 Correct 2 ms 2904 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 1 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 1 ms 2844 KB Output is correct
15 Correct 2 ms 2904 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2920 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 1 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 1 ms 2908 KB Output is correct
27 Correct 2 ms 2908 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 1 ms 2908 KB Output is correct
30 Correct 1 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 1 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 1 ms 2912 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 1 ms 2908 KB Output is correct
38 Correct 1 ms 2908 KB Output is correct
39 Correct 1 ms 2908 KB Output is correct
40 Correct 2 ms 2908 KB Output is correct
41 Correct 1 ms 2908 KB Output is correct
42 Correct 1 ms 2908 KB Output is correct
43 Correct 1 ms 2908 KB Output is correct
44 Correct 1 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2832 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2904 KB Output is correct
5 Correct 2 ms 2904 KB Output is correct
6 Correct 2 ms 2904 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 1 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 1 ms 2844 KB Output is correct
15 Correct 2 ms 2904 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2920 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 1 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 1 ms 2908 KB Output is correct
27 Correct 2 ms 2908 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 1 ms 2908 KB Output is correct
30 Correct 1 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 1 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 1 ms 2912 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 1 ms 2908 KB Output is correct
38 Correct 1 ms 2908 KB Output is correct
39 Correct 1 ms 2908 KB Output is correct
40 Correct 2 ms 2908 KB Output is correct
41 Correct 1 ms 2908 KB Output is correct
42 Correct 1 ms 2908 KB Output is correct
43 Correct 1 ms 2908 KB Output is correct
44 Correct 1 ms 2908 KB Output is correct
45 Correct 3 ms 2908 KB Output is correct
46 Correct 6 ms 3420 KB Output is correct
47 Correct 6 ms 3420 KB Output is correct
48 Correct 6 ms 3464 KB Output is correct
49 Correct 4 ms 3840 KB Output is correct
50 Correct 4 ms 3676 KB Output is correct
51 Correct 4 ms 3840 KB Output is correct
52 Correct 6 ms 3788 KB Output is correct
53 Correct 4 ms 3676 KB Output is correct
54 Correct 4 ms 3676 KB Output is correct
55 Correct 6 ms 3720 KB Output is correct
56 Correct 4 ms 3672 KB Output is correct
57 Correct 7 ms 3532 KB Output is correct
58 Correct 10 ms 3500 KB Output is correct
59 Correct 8 ms 3420 KB Output is correct
60 Correct 7 ms 3420 KB Output is correct
61 Correct 7 ms 3676 KB Output is correct
62 Correct 4 ms 3676 KB Output is correct
63 Correct 4 ms 3676 KB Output is correct
64 Correct 5 ms 3420 KB Output is correct
65 Correct 6 ms 3420 KB Output is correct
66 Correct 6 ms 3420 KB Output is correct
67 Correct 6 ms 3420 KB Output is correct
68 Correct 4 ms 3676 KB Output is correct
69 Correct 5 ms 3672 KB Output is correct
70 Correct 5 ms 3932 KB Output is correct
71 Correct 4 ms 3492 KB Output is correct
72 Correct 7 ms 3512 KB Output is correct
73 Correct 8 ms 3420 KB Output is correct
74 Correct 8 ms 3420 KB Output is correct
75 Correct 5 ms 3416 KB Output is correct
76 Correct 5 ms 3416 KB Output is correct
77 Correct 5 ms 3416 KB Output is correct
78 Correct 5 ms 3420 KB Output is correct
79 Correct 4 ms 3420 KB Output is correct
80 Correct 5 ms 3420 KB Output is correct
81 Correct 5 ms 3420 KB Output is correct
82 Correct 8 ms 3416 KB Output is correct
83 Correct 5 ms 3484 KB Output is correct
84 Correct 5 ms 3552 KB Output is correct
85 Correct 5 ms 3360 KB Output is correct
86 Correct 5 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2832 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2904 KB Output is correct
5 Correct 2 ms 2904 KB Output is correct
6 Correct 2 ms 2904 KB Output is correct
7 Correct 2 ms 2904 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 1 ms 2908 KB Output is correct
12 Correct 1 ms 2908 KB Output is correct
13 Correct 2 ms 2908 KB Output is correct
14 Correct 1 ms 2844 KB Output is correct
15 Correct 2 ms 2904 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2920 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2908 KB Output is correct
22 Correct 1 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 1 ms 2908 KB Output is correct
27 Correct 2 ms 2908 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 1 ms 2908 KB Output is correct
30 Correct 1 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 1 ms 2908 KB Output is correct
33 Correct 1 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 1 ms 2912 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 1 ms 2908 KB Output is correct
38 Correct 1 ms 2908 KB Output is correct
39 Correct 1 ms 2908 KB Output is correct
40 Correct 2 ms 2908 KB Output is correct
41 Correct 1 ms 2908 KB Output is correct
42 Correct 1 ms 2908 KB Output is correct
43 Correct 1 ms 2908 KB Output is correct
44 Correct 1 ms 2908 KB Output is correct
45 Correct 3 ms 2908 KB Output is correct
46 Correct 6 ms 3420 KB Output is correct
47 Correct 6 ms 3420 KB Output is correct
48 Correct 6 ms 3464 KB Output is correct
49 Correct 4 ms 3840 KB Output is correct
50 Correct 4 ms 3676 KB Output is correct
51 Correct 4 ms 3840 KB Output is correct
52 Correct 6 ms 3788 KB Output is correct
53 Correct 4 ms 3676 KB Output is correct
54 Correct 4 ms 3676 KB Output is correct
55 Correct 6 ms 3720 KB Output is correct
56 Correct 4 ms 3672 KB Output is correct
57 Correct 7 ms 3532 KB Output is correct
58 Correct 10 ms 3500 KB Output is correct
59 Correct 8 ms 3420 KB Output is correct
60 Correct 7 ms 3420 KB Output is correct
61 Correct 7 ms 3676 KB Output is correct
62 Correct 4 ms 3676 KB Output is correct
63 Correct 4 ms 3676 KB Output is correct
64 Correct 5 ms 3420 KB Output is correct
65 Correct 6 ms 3420 KB Output is correct
66 Correct 6 ms 3420 KB Output is correct
67 Correct 6 ms 3420 KB Output is correct
68 Correct 4 ms 3676 KB Output is correct
69 Correct 5 ms 3672 KB Output is correct
70 Correct 5 ms 3932 KB Output is correct
71 Correct 4 ms 3492 KB Output is correct
72 Correct 7 ms 3512 KB Output is correct
73 Correct 8 ms 3420 KB Output is correct
74 Correct 8 ms 3420 KB Output is correct
75 Correct 5 ms 3416 KB Output is correct
76 Correct 5 ms 3416 KB Output is correct
77 Correct 5 ms 3416 KB Output is correct
78 Correct 5 ms 3420 KB Output is correct
79 Correct 4 ms 3420 KB Output is correct
80 Correct 5 ms 3420 KB Output is correct
81 Correct 5 ms 3420 KB Output is correct
82 Correct 8 ms 3416 KB Output is correct
83 Correct 5 ms 3484 KB Output is correct
84 Correct 5 ms 3552 KB Output is correct
85 Correct 5 ms 3360 KB Output is correct
86 Correct 5 ms 3420 KB Output is correct
87 Correct 16 ms 4700 KB Output is correct
88 Correct 57 ms 7904 KB Output is correct
89 Correct 286 ms 21264 KB Output is correct
90 Correct 278 ms 21304 KB Output is correct
91 Correct 266 ms 21544 KB Output is correct
92 Correct 104 ms 28364 KB Output is correct
93 Correct 125 ms 28424 KB Output is correct
94 Correct 108 ms 28324 KB Output is correct
95 Correct 98 ms 28628 KB Output is correct
96 Correct 112 ms 28964 KB Output is correct
97 Correct 129 ms 28964 KB Output is correct
98 Correct 110 ms 29124 KB Output is correct
99 Correct 108 ms 24272 KB Output is correct
100 Correct 358 ms 21000 KB Output is correct
101 Correct 404 ms 21448 KB Output is correct
102 Correct 405 ms 21460 KB Output is correct
103 Correct 400 ms 21460 KB Output is correct
104 Correct 139 ms 24268 KB Output is correct
105 Correct 132 ms 24268 KB Output is correct
106 Correct 136 ms 24104 KB Output is correct
107 Correct 234 ms 19668 KB Output is correct
108 Correct 225 ms 19916 KB Output is correct
109 Correct 246 ms 20684 KB Output is correct
110 Correct 106 ms 27088 KB Output is correct
111 Correct 105 ms 28624 KB Output is correct
112 Correct 117 ms 31948 KB Output is correct
113 Correct 119 ms 22896 KB Output is correct
114 Correct 369 ms 20992 KB Output is correct
115 Correct 365 ms 20172 KB Output is correct
116 Correct 134 ms 22984 KB Output is correct
117 Correct 151 ms 22220 KB Output is correct
118 Correct 141 ms 21712 KB Output is correct
119 Correct 179 ms 21460 KB Output is correct
120 Correct 153 ms 21144 KB Output is correct
121 Correct 132 ms 20684 KB Output is correct
122 Correct 189 ms 20448 KB Output is correct
123 Correct 182 ms 22304 KB Output is correct
124 Correct 168 ms 21708 KB Output is correct
125 Correct 195 ms 21456 KB Output is correct
126 Correct 195 ms 21452 KB Output is correct
127 Correct 165 ms 20940 KB Output is correct
128 Correct 206 ms 20580 KB Output is correct