Submission #1109768

# Submission time Handle Problem Language Result Execution time Memory
1109768 2024-11-07T14:08:36 Z ljtunas Construction of Highway (JOI18_construction) C++17
100 / 100
330 ms 22664 KB
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v)    memset(h, v, sizeof h)
#define Forv(i, a)     for(auto& i : a)
#define For(i, a, b)   for(int i = a; i <= b; ++i)
#define Ford(i, a, b)  for(int i = a; i >= b; --i)
#define c_bit(i)     __builtin_popcountll(i)
#define Bit(mask, i)    ((mask >> i) & 1LL)
#define onbit(mask, i)  ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
    template <typename T, int D>
    struct _vector : public vector <_vector <T, D - 1>> {
        static_assert(D >= 1, "Dimension must be positive!");
        template <typename... Args>
        _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
    };
    template <typename T> struct _vector <T, 1> : public vector <T> {
        _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
    };
    template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
    template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e5 + 5;
const  ll  mod = 1e9 + 7;
const  ll   oo = 1e18;

//#define int long long

int n, sz[MAXN], in[MAXN], en[MAXN], p[MAXN], head[MAXN]; ll c[MAXN];
ii E[MAXN];
vector<int> g[MAXN];

void dfsInit(int u, int par){
    sz[u] = 1; p[u] = par;
    Forv(v, g[u]) if(v ^ par){
        dfsInit(v, u), sz[u] += sz[v];
    }
}

vector<int> ListHLD;
void hld (int u, int par){
    static int timedfs = 0;
    in[u] = ++timedfs;
    ListHLD.push_back(u);
    if(g[u].size()==1){
        Forv(x, ListHLD) head[x] = ListHLD[0];
        ListHLD.clear();
    }
    Forv(v, g[u]) if(v ^ par){hld(v, u);}
    en[u] = timedfs;
}

namespace subtrau{
    void Solve(){
        For(i, 1, n - 1){
            auto [u, v] = E[i];
            vector<int> List;
            int a = u;
            while(a){
                List.push_back(a); a = p[a];
            }
            int ans = 0; int cc = c[v];
            For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
                if (c[List[i]] < c[List[j]])++ans;
            }
            set<int> ss;
            Forv(x, List)ss.insert(c[x]);
            Forv(x, List) c[x] = cc;
            cout << ans << '\n';
        }
    }
}

namespace subfull{
    int st[MAXN << 2], lazy[MAXN << 2]; ll tmp = 0;
    int bit[MAXN];

    void updBIT(int u, int val){
        while(u <= 1e5) bit[u] += val, u += u &- u;
    }
    int getBIT(int u){
        ll ans = 0;
        while(u) ans += bit[u], u -= u &- u;
        return ans;
    }

    void down(int id, int l, int r){
        if(!lazy[id]) return;
        st[id << 1] = lazy[id], lazy[id << 1] = lazy[id];
        st[id << 1 | 1] = lazy[id], lazy[id << 1 | 1] = lazy[id];
        lazy[id] = 0;
    }
    vector<ii> ListUPD;
    void update(int id, int l, int r, int u, int v, int val){
        if(v < l || r < u) return;
        if (l == r){
            if(st[id]) tmp += getBIT(st[id] - 1), updBIT(st[id], 1), ListUPD.push_back({st[id], 1});
            st[id] = val, lazy[id] = val;
            return;
        } else if(u <= l && r <= v && st[id]){
            tmp += getBIT(st[id] - 1) * (r - l + 1),updBIT(st[id], r-l+1), ListUPD.push_back({st[id], r-l+1});
            st[id] = val, lazy[id] = val;
            return;
        }
        down(id, l, r);
        int mid = l + r >> 1;
        update(id << 1 | 1, mid + 1, r, u, v, val);
        update(id << 1, l, mid, u, v, val);
        st[id] = (st[id << 1] == st[id << 1 | 1]) * st[id << 1];
    }
    void Solve(){
        update(1, 1, n, 1, 1, c[1]);
        For(i, 1, n - 1){
            auto [u, v] = E[i];
            tmp = 0;
            int a = v;
            while(head[a]!=head[1]){
                update(1, 1, n, in[head[a]], in[a], c[v]);
                a = p[head[a]];
            }
            update(1, 1, n, 1, in[a], c[v]);
//            if (i==4) cerr << 1 << ' ' << in[a] << '\n';
            for(auto [val, cnt] : ListUPD) updBIT(val, -cnt);
            ListUPD.clear();
            cout << tmp << '\n';
        }
    }
}

void Solve() {
    cin >> n;
    vector<int> compress;
    For(i, 1, n) cin >> c[i], compress.push_back(c[i]);
    sort(all(compress)), compress.resize(unique(all(compress)) - compress.begin());
    For(i, 1, n) c[i] = lower_bound(all(compress), c[i]) - compress.begin() + 1;
    For(i, 1, n - 1){
        auto&[u, v] = E[i];
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    For(i, 1, n) sort(all(g[i]), [&](int x, int y){
        return sz[x] > sz[y];
    });
    dfsInit(1, 0); hld(1, 0);
//    subtrau::Solve();
    subfull::Solve();
}

int32_t main() {
    cin.tie(0) -> sync_with_stdio(0);
    if(fopen("JOI18_construction.inp", "r")) {
        freopen("JOI18_construction.inp", "r", stdin);
        freopen("JOI18_construction.out", "w", stdout);
    }

    int t = 1;
//    cin >> t;

    for (int test = 1; test <= t; test++) {
        Solve();
    }
    return 0;
}

Compilation message

construction.cpp: In function 'void subtrau::Solve()':
construction.cpp:10:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 | #define For(i, a, b)   for(int i = a; i <= b; ++i)
......
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |                 ~~~~~~~~~~~~~~~~~~~~~    
construction.cpp:71:13: note: in expansion of macro 'For'
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |             ^~~
construction.cpp:10:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 | #define For(i, a, b)   for(int i = a; i <= b; ++i)
......
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |                                            ~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:71:40: note: in expansion of macro 'For'
   71 |             For(i, 0, List.size() - 1) For(j, i + 1, List.size() - 1){
      |                                        ^~~
construction.cpp: In function 'void subfull::update(int, int, int, int, int, int)':
construction.cpp:114:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  114 |         int mid = l + r >> 1;
      |                   ~~^~~
construction.cpp: In function 'int32_t main()':
construction.cpp:161:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |         freopen("JOI18_construction.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  162 |         freopen("JOI18_construction.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Correct 2 ms 6648 KB Output is correct
4 Correct 2 ms 6480 KB Output is correct
5 Correct 3 ms 6480 KB Output is correct
6 Correct 3 ms 6480 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
8 Correct 3 ms 6736 KB Output is correct
9 Correct 2 ms 6904 KB Output is correct
10 Correct 3 ms 6736 KB Output is correct
11 Correct 3 ms 6736 KB Output is correct
12 Correct 3 ms 6736 KB Output is correct
13 Correct 2 ms 6736 KB Output is correct
14 Correct 2 ms 6736 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 2 ms 6480 KB Output is correct
17 Correct 2 ms 6736 KB Output is correct
18 Correct 2 ms 6628 KB Output is correct
19 Correct 2 ms 6988 KB Output is correct
20 Correct 2 ms 6628 KB Output is correct
21 Correct 3 ms 6736 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 3 ms 6724 KB Output is correct
24 Correct 2 ms 6480 KB Output is correct
25 Correct 2 ms 6480 KB Output is correct
26 Correct 3 ms 6908 KB Output is correct
27 Correct 2 ms 6736 KB Output is correct
28 Correct 2 ms 6736 KB Output is correct
29 Correct 2 ms 6736 KB Output is correct
30 Correct 3 ms 6480 KB Output is correct
31 Correct 3 ms 6480 KB Output is correct
32 Correct 2 ms 6736 KB Output is correct
33 Correct 3 ms 6736 KB Output is correct
34 Correct 2 ms 6480 KB Output is correct
35 Correct 2 ms 6480 KB Output is correct
36 Correct 2 ms 6480 KB Output is correct
37 Correct 2 ms 6480 KB Output is correct
38 Correct 2 ms 6724 KB Output is correct
39 Correct 2 ms 6736 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 3 ms 6480 KB Output is correct
42 Correct 3 ms 6736 KB Output is correct
43 Correct 2 ms 6736 KB Output is correct
44 Correct 2 ms 6624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Correct 2 ms 6648 KB Output is correct
4 Correct 2 ms 6480 KB Output is correct
5 Correct 3 ms 6480 KB Output is correct
6 Correct 3 ms 6480 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
8 Correct 3 ms 6736 KB Output is correct
9 Correct 2 ms 6904 KB Output is correct
10 Correct 3 ms 6736 KB Output is correct
11 Correct 3 ms 6736 KB Output is correct
12 Correct 3 ms 6736 KB Output is correct
13 Correct 2 ms 6736 KB Output is correct
14 Correct 2 ms 6736 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 2 ms 6480 KB Output is correct
17 Correct 2 ms 6736 KB Output is correct
18 Correct 2 ms 6628 KB Output is correct
19 Correct 2 ms 6988 KB Output is correct
20 Correct 2 ms 6628 KB Output is correct
21 Correct 3 ms 6736 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 3 ms 6724 KB Output is correct
24 Correct 2 ms 6480 KB Output is correct
25 Correct 2 ms 6480 KB Output is correct
26 Correct 3 ms 6908 KB Output is correct
27 Correct 2 ms 6736 KB Output is correct
28 Correct 2 ms 6736 KB Output is correct
29 Correct 2 ms 6736 KB Output is correct
30 Correct 3 ms 6480 KB Output is correct
31 Correct 3 ms 6480 KB Output is correct
32 Correct 2 ms 6736 KB Output is correct
33 Correct 3 ms 6736 KB Output is correct
34 Correct 2 ms 6480 KB Output is correct
35 Correct 2 ms 6480 KB Output is correct
36 Correct 2 ms 6480 KB Output is correct
37 Correct 2 ms 6480 KB Output is correct
38 Correct 2 ms 6724 KB Output is correct
39 Correct 2 ms 6736 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 3 ms 6480 KB Output is correct
42 Correct 3 ms 6736 KB Output is correct
43 Correct 2 ms 6736 KB Output is correct
44 Correct 2 ms 6624 KB Output is correct
45 Correct 3 ms 6736 KB Output is correct
46 Correct 7 ms 6992 KB Output is correct
47 Correct 8 ms 7160 KB Output is correct
48 Correct 8 ms 6992 KB Output is correct
49 Correct 7 ms 7248 KB Output is correct
50 Correct 7 ms 7152 KB Output is correct
51 Correct 9 ms 7248 KB Output is correct
52 Correct 11 ms 7160 KB Output is correct
53 Correct 9 ms 7164 KB Output is correct
54 Correct 8 ms 6992 KB Output is correct
55 Correct 8 ms 7004 KB Output is correct
56 Correct 6 ms 6992 KB Output is correct
57 Correct 9 ms 7160 KB Output is correct
58 Correct 8 ms 6992 KB Output is correct
59 Correct 9 ms 6992 KB Output is correct
60 Correct 8 ms 7040 KB Output is correct
61 Correct 6 ms 7164 KB Output is correct
62 Correct 6 ms 7180 KB Output is correct
63 Correct 6 ms 6992 KB Output is correct
64 Correct 8 ms 6992 KB Output is correct
65 Correct 7 ms 6992 KB Output is correct
66 Correct 7 ms 6992 KB Output is correct
67 Correct 7 ms 6992 KB Output is correct
68 Correct 7 ms 7248 KB Output is correct
69 Correct 7 ms 7004 KB Output is correct
70 Correct 7 ms 6992 KB Output is correct
71 Correct 6 ms 6992 KB Output is correct
72 Correct 8 ms 6992 KB Output is correct
73 Correct 8 ms 6992 KB Output is correct
74 Correct 6 ms 6992 KB Output is correct
75 Correct 6 ms 6992 KB Output is correct
76 Correct 6 ms 6976 KB Output is correct
77 Correct 6 ms 6992 KB Output is correct
78 Correct 6 ms 6992 KB Output is correct
79 Correct 6 ms 6964 KB Output is correct
80 Correct 9 ms 6992 KB Output is correct
81 Correct 8 ms 6992 KB Output is correct
82 Correct 7 ms 6992 KB Output is correct
83 Correct 8 ms 6992 KB Output is correct
84 Correct 7 ms 6992 KB Output is correct
85 Correct 6 ms 6992 KB Output is correct
86 Correct 7 ms 6992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6480 KB Output is correct
2 Correct 2 ms 6480 KB Output is correct
3 Correct 2 ms 6648 KB Output is correct
4 Correct 2 ms 6480 KB Output is correct
5 Correct 3 ms 6480 KB Output is correct
6 Correct 3 ms 6480 KB Output is correct
7 Correct 2 ms 6736 KB Output is correct
8 Correct 3 ms 6736 KB Output is correct
9 Correct 2 ms 6904 KB Output is correct
10 Correct 3 ms 6736 KB Output is correct
11 Correct 3 ms 6736 KB Output is correct
12 Correct 3 ms 6736 KB Output is correct
13 Correct 2 ms 6736 KB Output is correct
14 Correct 2 ms 6736 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 2 ms 6480 KB Output is correct
17 Correct 2 ms 6736 KB Output is correct
18 Correct 2 ms 6628 KB Output is correct
19 Correct 2 ms 6988 KB Output is correct
20 Correct 2 ms 6628 KB Output is correct
21 Correct 3 ms 6736 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 3 ms 6724 KB Output is correct
24 Correct 2 ms 6480 KB Output is correct
25 Correct 2 ms 6480 KB Output is correct
26 Correct 3 ms 6908 KB Output is correct
27 Correct 2 ms 6736 KB Output is correct
28 Correct 2 ms 6736 KB Output is correct
29 Correct 2 ms 6736 KB Output is correct
30 Correct 3 ms 6480 KB Output is correct
31 Correct 3 ms 6480 KB Output is correct
32 Correct 2 ms 6736 KB Output is correct
33 Correct 3 ms 6736 KB Output is correct
34 Correct 2 ms 6480 KB Output is correct
35 Correct 2 ms 6480 KB Output is correct
36 Correct 2 ms 6480 KB Output is correct
37 Correct 2 ms 6480 KB Output is correct
38 Correct 2 ms 6724 KB Output is correct
39 Correct 2 ms 6736 KB Output is correct
40 Correct 2 ms 6492 KB Output is correct
41 Correct 3 ms 6480 KB Output is correct
42 Correct 3 ms 6736 KB Output is correct
43 Correct 2 ms 6736 KB Output is correct
44 Correct 2 ms 6624 KB Output is correct
45 Correct 3 ms 6736 KB Output is correct
46 Correct 7 ms 6992 KB Output is correct
47 Correct 8 ms 7160 KB Output is correct
48 Correct 8 ms 6992 KB Output is correct
49 Correct 7 ms 7248 KB Output is correct
50 Correct 7 ms 7152 KB Output is correct
51 Correct 9 ms 7248 KB Output is correct
52 Correct 11 ms 7160 KB Output is correct
53 Correct 9 ms 7164 KB Output is correct
54 Correct 8 ms 6992 KB Output is correct
55 Correct 8 ms 7004 KB Output is correct
56 Correct 6 ms 6992 KB Output is correct
57 Correct 9 ms 7160 KB Output is correct
58 Correct 8 ms 6992 KB Output is correct
59 Correct 9 ms 6992 KB Output is correct
60 Correct 8 ms 7040 KB Output is correct
61 Correct 6 ms 7164 KB Output is correct
62 Correct 6 ms 7180 KB Output is correct
63 Correct 6 ms 6992 KB Output is correct
64 Correct 8 ms 6992 KB Output is correct
65 Correct 7 ms 6992 KB Output is correct
66 Correct 7 ms 6992 KB Output is correct
67 Correct 7 ms 6992 KB Output is correct
68 Correct 7 ms 7248 KB Output is correct
69 Correct 7 ms 7004 KB Output is correct
70 Correct 7 ms 6992 KB Output is correct
71 Correct 6 ms 6992 KB Output is correct
72 Correct 8 ms 6992 KB Output is correct
73 Correct 8 ms 6992 KB Output is correct
74 Correct 6 ms 6992 KB Output is correct
75 Correct 6 ms 6992 KB Output is correct
76 Correct 6 ms 6976 KB Output is correct
77 Correct 6 ms 6992 KB Output is correct
78 Correct 6 ms 6992 KB Output is correct
79 Correct 6 ms 6964 KB Output is correct
80 Correct 9 ms 6992 KB Output is correct
81 Correct 8 ms 6992 KB Output is correct
82 Correct 7 ms 6992 KB Output is correct
83 Correct 8 ms 6992 KB Output is correct
84 Correct 7 ms 6992 KB Output is correct
85 Correct 6 ms 6992 KB Output is correct
86 Correct 7 ms 6992 KB Output is correct
87 Correct 19 ms 7504 KB Output is correct
88 Correct 58 ms 9300 KB Output is correct
89 Correct 256 ms 15956 KB Output is correct
90 Correct 275 ms 16216 KB Output is correct
91 Correct 242 ms 15816 KB Output is correct
92 Correct 174 ms 22664 KB Output is correct
93 Correct 175 ms 22496 KB Output is correct
94 Correct 179 ms 22508 KB Output is correct
95 Correct 179 ms 20168 KB Output is correct
96 Correct 188 ms 20680 KB Output is correct
97 Correct 198 ms 20572 KB Output is correct
98 Correct 190 ms 20680 KB Output is correct
99 Correct 135 ms 19404 KB Output is correct
100 Correct 309 ms 15304 KB Output is correct
101 Correct 319 ms 15560 KB Output is correct
102 Correct 310 ms 15560 KB Output is correct
103 Correct 309 ms 15560 KB Output is correct
104 Correct 138 ms 19540 KB Output is correct
105 Correct 135 ms 19400 KB Output is correct
106 Correct 153 ms 19408 KB Output is correct
107 Correct 206 ms 14864 KB Output is correct
108 Correct 221 ms 14984 KB Output is correct
109 Correct 272 ms 15308 KB Output is correct
110 Correct 181 ms 21632 KB Output is correct
111 Correct 187 ms 20124 KB Output is correct
112 Correct 208 ms 19548 KB Output is correct
113 Correct 120 ms 18376 KB Output is correct
114 Correct 289 ms 15300 KB Output is correct
115 Correct 330 ms 14792 KB Output is correct
116 Correct 147 ms 18376 KB Output is correct
117 Correct 148 ms 17356 KB Output is correct
118 Correct 136 ms 16724 KB Output is correct
119 Correct 160 ms 16328 KB Output is correct
120 Correct 149 ms 16584 KB Output is correct
121 Correct 133 ms 15988 KB Output is correct
122 Correct 148 ms 15816 KB Output is correct
123 Correct 200 ms 17428 KB Output is correct
124 Correct 164 ms 16816 KB Output is correct
125 Correct 183 ms 16584 KB Output is correct
126 Correct 184 ms 16836 KB Output is correct
127 Correct 194 ms 16192 KB Output is correct
128 Correct 180 ms 15816 KB Output is correct