Submission #1174663

#TimeUsernameProblemLanguageResultExecution timeMemory
1174663TsaganaCat Exercise (JOI23_ho_t4)C++20
41 / 100
76 ms22264 KiB
#include<bits/stdc++.h> #define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie(); #define all(x) x.begin(), x.end() #define int long long #define pq priority_queue #define eb emplace_back #define lb lower_bound #define ub upper_bound #define pb push_back #define pp pop_back #define F first #define S second using namespace std; int n; vector<int> N; vector<int> v; vector<int> P; void build(int id, int L, int R) { if (L + 1 == R) {N[id] = v[L]; return ;} int M = (L + R) / 2, x = 2 * id, y = x + 1; build(x, L, M); build(y, M, R); N[id] = max(N[x], N[y]); } int query(int id, int L, int R, int l, int r) { if (L >= r || l >= R) return 0; if (L >= l && r >= R) return N[id]; int M = (L + R) / 2, x = 2 * id, y = x + 1; int sl = query(x, L, M, l, r); int sr = query(y, M, R, l, r); return max(sl, sr); } int calc(int M, int L, int R) { if (M == 0 || L + 1 == R) return 0; if (P[M] == L) { int r = query(1, 0, n, P[M] + 1, R); return calc(r, P[M] + 1, R) + P[r] - P[M]; } if (P[M] + 1 == R) { int l = query(1, 0, n, L, P[M]); return calc(l, L, P[M]) + P[M] - P[l]; } int l = query(1, 0, n, L, P[M]); int r = query(1, 0, n, P[M] + 1, R); return max(calc(l, L, P[M]) + P[M] - P[l], calc(r, P[M] + 1, R) + P[r] - P[M]); } void solve() { cin >> n; v = vector<int>(n); P = vector<int>(n); for (int i = 0; i < n; i++) {cin >> v[i]; v[i]--; P[v[i]] = i;} for (int i = 1, a, b; i < n; i++) cin >> a >> b; N = vector<int>(4*n, 0); build(1, 0, n); cout << calc(n - 1, 0, n) << '\n'; } signed main() {IOS solve(); return 0;}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...