# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
932505 |
2024-02-23T14:20:49 Z |
ErJ |
Cat Exercise (JOI23_ho_t4) |
C++17 |
|
2000 ms |
544 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;
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);
ans1 = 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++) {
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2081 ms |
344 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2081 ms |
344 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2081 ms |
344 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2081 ms |
344 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2081 ms |
344 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
544 KB |
Output is correct |
2 |
Execution timed out |
2021 ms |
348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
2081 ms |
344 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |