답안 #932567

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932567 2024-02-23T15:57:00 Z ErJ Cat Exercise (JOI23_ho_t4) C++17
41 / 100
203 ms 46828 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);
    }


    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++) {
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 500 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 500 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 5 ms 1624 KB Output is correct
19 Correct 5 ms 1372 KB Output is correct
20 Correct 5 ms 1372 KB Output is correct
21 Correct 4 ms 912 KB Output is correct
22 Correct 4 ms 1076 KB Output is correct
23 Correct 4 ms 864 KB Output is correct
24 Correct 4 ms 868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 500 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 5 ms 1624 KB Output is correct
19 Correct 5 ms 1372 KB Output is correct
20 Correct 5 ms 1372 KB Output is correct
21 Correct 4 ms 912 KB Output is correct
22 Correct 4 ms 1076 KB Output is correct
23 Correct 4 ms 864 KB Output is correct
24 Correct 4 ms 868 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Incorrect 4 ms 864 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 500 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 5 ms 1624 KB Output is correct
19 Correct 5 ms 1372 KB Output is correct
20 Correct 5 ms 1372 KB Output is correct
21 Correct 4 ms 912 KB Output is correct
22 Correct 4 ms 1076 KB Output is correct
23 Correct 4 ms 864 KB Output is correct
24 Correct 4 ms 868 KB Output is correct
25 Correct 203 ms 46828 KB Output is correct
26 Correct 194 ms 44640 KB Output is correct
27 Correct 191 ms 44784 KB Output is correct
28 Correct 166 ms 24928 KB Output is correct
29 Correct 165 ms 24912 KB Output is correct
30 Correct 165 ms 24928 KB Output is correct
31 Correct 169 ms 24924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 500 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 5 ms 1624 KB Output is correct
19 Correct 5 ms 1372 KB Output is correct
20 Correct 5 ms 1372 KB Output is correct
21 Correct 4 ms 912 KB Output is correct
22 Correct 4 ms 1076 KB Output is correct
23 Correct 4 ms 864 KB Output is correct
24 Correct 4 ms 868 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Incorrect 4 ms 864 KB Output isn't correct
27 Halted 0 ms 0 KB -