Submission #932569

# Submission time Handle Problem Language Result Execution time Memory
932569 2024-02-23T15:58:27 Z ErJ Cat Exercise (JOI23_ho_t4) C++17
51 / 100
330 ms 43116 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<pair<ll, ll>>
#define pp pair<ll, ll>
#define qi queue<ll>
#define qp queue<pp>
#define pqi priority_queue<ll>
#define pqp priority_queue<pp>
#define mi map<ll, ll>
#define mpi map<pp, ll>
#define mip map<ll, pp>
#define mpp map<pp, pp>
#define mb map<ll, bool>
#define si set<ll>
#define sp set<pp>
#define mod 1000000007
#define rep(a, b) for(int a = 0; a < (b); a++)
#define rep2(a, b) for(int a = 1; a < (b); a++)
#define inf 100000000000000


vvi edges;
vi height;
vb kamen;

pp findMAX(ll v) {//distance, vertex
    vp val;
    qp q;
    q.push({ v, 0 });
    vb was(edges.size());
    rep(i, was.size()) was[i] = false;
    while (!q.empty()) {
        ll u = q.front().first;
        ll du = q.front().second;
        val.push_back({ u, du });
        q.pop();
        was[u] = true;
        rep(i, edges[u].size()) {
            ll w = edges[u][i];
            if (!(was[w] || kamen[w])) {
                was[w] = true;
                q.push({ w, du + 1 });
            }
        }
    }
    ll MAX = -1, indx = -1;
    rep(i, val.size()) {
        if (height[val[i].first] > MAX) {
            MAX = height[val[i].first];
            indx = i;
        }
    }
    return { val[indx].second + 1, val[indx].first};
}


ll solve(ll ans, ll cat) {
    kamen[cat] = true;
    ll ANS = ans;
    rep(i, edges[cat].size()) {
        ll v = edges[cat][i];
        if (kamen[v]) continue;
        pp next = findMAX(v);
        ANS = max(ANS, solve(ans + next.first, next.second));
    }
    return ANS;
}

vp Itree;
ll N;

void init(ll n) {
    n += 2;
    while (n & (n - 1)) n++;
    Itree.resize(2 * n);
    for (int i = 1; i < Itree.size(); i++) {
        Itree[i] = { -inf, -1 };
    }
    N = n;
}

void update(ll index, ll val) {
    index += N;
    Itree[index] = { val, index - N };
    index /= 2;
    while (index > 0) {      
        Itree[index] = max(Itree[index * 2], Itree[index * 2 + 1]);
        index /= 2;
    }
}


pp get2(ll v, ll l, ll r, ll a, ll b) { //l in, r out
    if (l == a && r == b) {
        return Itree[v];
    }
    ll s = (a + b) / 2;
    if (s >= r) {
        return get2(v * 2, l, r, a, s);
    }
    else if (s <= l) {
        return get2(v * 2 + 1, l, r, s, b);
    }
    else {
        return max(get2(v * 2, l, s, a, s), get2(v * 2 + 1, s, r, s, b));
    }
}


ll get(ll l, ll r) { // l in r out
    return get2(1, l + N, r + N, N, 2*N).second;
}


ll solve2(ll ans, ll l, ll r, ll cat) { // l in , r in
    if (l > r) {
        return -inf;
    }
    if (l == r) {
        return ans;
    }
    ll ans1 = -inf, ans2 = -inf;
    if (l >= cat) {
        ans1 = -inf;
    }
    else {
        ll indx = get(l, cat);
        ans1 = solve2(ans + abs(indx - cat), l, cat - 1, indx);
    } 
    if (cat + 1 > r) ans2 = -inf;
    else {
        ll indx2 = get(cat + 1, r + 1);
        ans2 = solve2(ans + abs(indx2 - cat), cat + 1, r, indx2);
    }  
    return max(ans, max(ans1, ans2));
}


int main()
{
    int n;
    cin >> n;
    height.resize(n);
    kamen.resize(n);
    init(n);

    ll cat = 0;
    rep(i, n) {
        cin >> height[i];
        update(i, height[i]);
        kamen[i] = false;
        if (height[i] == n) {
            cat = i;
        }
    }
    edges.resize(n);
    rep(i, n - 1) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    if (n < 6000) {
        cout << solve(0, cat) << endl;
    }
    else {
        cout << solve2(0, 0, n - 1, cat) << endl;
    } 
}

Compilation message

Main.cpp: In function 'std::pair<long long int, long long int> findMAX(long long int)':
Main.cpp:24:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bool>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 | #define rep(a, b) for(int a = 0; a < (b); a++)
      |                                    ^
Main.cpp:38:5: note: in expansion of macro 'rep'
   38 |     rep(i, was.size()) was[i] = false;
      |     ^~~
Main.cpp:24:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 | #define rep(a, b) for(int a = 0; a < (b); a++)
      |                                    ^
Main.cpp:45:9: note: in expansion of macro 'rep'
   45 |         rep(i, edges[u].size()) {
      |         ^~~
Main.cpp:24:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 | #define rep(a, b) for(int a = 0; a < (b); a++)
      |                                    ^
Main.cpp:54:5: note: in expansion of macro 'rep'
   54 |     rep(i, val.size()) {
      |     ^~~
Main.cpp: In function 'long long int solve(long long int, long long int)':
Main.cpp:24:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 | #define rep(a, b) for(int a = 0; a < (b); a++)
      |                                    ^
Main.cpp:67:5: note: in expansion of macro 'rep'
   67 |     rep(i, edges[cat].size()) {
      |     ^~~
Main.cpp: In function 'void init(long long int)':
Main.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |     for (int i = 1; i < Itree.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 330 ms 1584 KB Output is correct
19 Correct 293 ms 1304 KB Output is correct
20 Correct 313 ms 1232 KB Output is correct
21 Correct 45 ms 1140 KB Output is correct
22 Correct 44 ms 1112 KB Output is correct
23 Correct 44 ms 1112 KB Output is correct
24 Correct 44 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 330 ms 1584 KB Output is correct
19 Correct 293 ms 1304 KB Output is correct
20 Correct 313 ms 1232 KB Output is correct
21 Correct 45 ms 1140 KB Output is correct
22 Correct 44 ms 1112 KB Output is correct
23 Correct 44 ms 1112 KB Output is correct
24 Correct 44 ms 1116 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 227 ms 1228 KB Output is correct
27 Correct 232 ms 1296 KB Output is correct
28 Correct 224 ms 1392 KB Output is correct
29 Correct 232 ms 1352 KB Output is correct
30 Correct 52 ms 1116 KB Output is correct
31 Correct 61 ms 1320 KB Output is correct
32 Correct 54 ms 1312 KB Output is correct
33 Correct 60 ms 1312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 330 ms 1584 KB Output is correct
19 Correct 293 ms 1304 KB Output is correct
20 Correct 313 ms 1232 KB Output is correct
21 Correct 45 ms 1140 KB Output is correct
22 Correct 44 ms 1112 KB Output is correct
23 Correct 44 ms 1112 KB Output is correct
24 Correct 44 ms 1116 KB Output is correct
25 Correct 213 ms 43116 KB Output is correct
26 Correct 207 ms 40944 KB Output is correct
27 Correct 210 ms 41016 KB Output is correct
28 Correct 194 ms 21192 KB Output is correct
29 Correct 176 ms 21072 KB Output is correct
30 Correct 175 ms 21072 KB Output is correct
31 Correct 180 ms 20968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 175 ms 26248 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 330 ms 1584 KB Output is correct
19 Correct 293 ms 1304 KB Output is correct
20 Correct 313 ms 1232 KB Output is correct
21 Correct 45 ms 1140 KB Output is correct
22 Correct 44 ms 1112 KB Output is correct
23 Correct 44 ms 1112 KB Output is correct
24 Correct 44 ms 1116 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 227 ms 1228 KB Output is correct
27 Correct 232 ms 1296 KB Output is correct
28 Correct 224 ms 1392 KB Output is correct
29 Correct 232 ms 1352 KB Output is correct
30 Correct 52 ms 1116 KB Output is correct
31 Correct 61 ms 1320 KB Output is correct
32 Correct 54 ms 1312 KB Output is correct
33 Correct 60 ms 1312 KB Output is correct
34 Correct 213 ms 43116 KB Output is correct
35 Correct 207 ms 40944 KB Output is correct
36 Correct 210 ms 41016 KB Output is correct
37 Correct 194 ms 21192 KB Output is correct
38 Correct 176 ms 21072 KB Output is correct
39 Correct 175 ms 21072 KB Output is correct
40 Correct 180 ms 20968 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Incorrect 175 ms 26248 KB Output isn't correct
44 Halted 0 ms 0 KB -