/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.77 MiB
#2 Accepted 4ms 2.77 MiB
#3 Accepted 242ms 2.883 MiB
#4 Accepted 255ms 3.066 MiB
#5 Accepted 683ms 3.02 MiB
#6 Accepted 1974ms 4.891 MiB
#7 Accepted 1998ms 5.02 MiB
#8 Time Exceeded ≥2002ms ≥25.117 MiB
#9 Time Exceeded ≥2099ms ≥25.105 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';
typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

const int N = 1e5 + 5;
vector<ordered_set> t;
vector<int> adj[N];
int n, q, arr[N], tin[N], tout[N], id[N], timer;

void euler_tour(int now, int par) {
    tin[now] = ++timer;
    id[timer] = now;
    for (auto &it: adj[now]) {
        if (it == par) continue;
        euler_tour(it, now);
    }
    tout[now] = timer;
}

ordered_set merge(const ordered_set &a, const ordered_set &b) {
    ordered_set ret = a;
    for (auto &it: b) ret.insert(it);
    return ret;
}

ordered_set build(int n, int s, int e) {
    if (s == e) {
        t[n].insert(arr[id[s]]);
        return t[n];
    }
    int m = (s + e) / 2;
    return t[n] = merge(build(n * 2, s, m), build(n * 2 + 1, m + 1, e));
}

ordered_set query(int n, int s, int e, int l, int r) {
    if (s > r || e < l) return {};
    if (s >= l && e <= r) return t[n];
    int m = (s + e) / 2;
    return merge(query(n * 2, s, m, l, r), query(n * 2 + 1, m + 1, e, l, r));
}

void shelby() {
    cin >> n;
    timer = 0, t.assign(4 * n, {});
    for (int i = 1; i <= n; ++i) cin >> arr[i], adj[i].clear();
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    euler_tour(1, -1);
    build(1, 1, n);
    debug(t);
    int q;
    cin >> q;
    while (q--) {
        int x;
        cin >> x;
        ordered_set o = query(1, 1, n, tin[x], tout[x]);
        int sz = tout[x] - tin[x] + 1, ans = sz;
        ans -= (o.order_of_key(sz + 1));
        cout << ans << '\n';
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-12 19:42:43
Judged At
2025-01-12 19:42:43
Judged By
Score
35
Total Time
≥2099ms
Peak Memory
≥25.117 MiB