/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 384.0 KiB
#3 Accepted 71ms 600.0 KiB
#4 Accepted 78ms 608.0 KiB
#5 Accepted 158ms 836.0 KiB
#6 Accepted 278ms 4.492 MiB
#7 Accepted 286ms 4.77 MiB
#8 Accepted 676ms 66.594 MiB
#9 Accepted 756ms 94.027 MiB
#10 Accepted 853ms 155.469 MiB
#11 Memory Exceeded ≥2175ms ≥256.016 MiB
#12 Memory Exceeded ≥2173ms ≥256.016 MiB

Code

/**
 *    author:   Binoy Barman
 *    created:  2025-01-02 22:34:25
**/

#include<bits/stdc++.h>
#ifdef LOCAL
#include "debug/trace.hpp"
#else
#define dbg(...) 42
#define print(...) 42
#endif
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = 1e9;

// #define int long long
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Testcase_Handler int tts, tc = 1; cin >> tts; hell: while(tts--)
#define uniq(v) sort(all(v)), v.resize(distance(v.begin(), unique(v.begin(), v.end())))
template<class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};
template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {bool first = true;for(auto &x : a) {if(!first) out << ' ';first = false;out << x;}return out;};

namespace Dark_Lord_Binoy {
void preprocess() {}
void setup() {
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr);
    preprocess();

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}
}
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T> 
using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k . O(logn)
// find_by_order(k) : K-th element in a set (counting from zero). O(logn)
template <typename T, typename R> 
using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 1e5 + 9;

int a[N], ans[N];
struct Dfs {
    vector<vector<int>> g;
    vector<int> sz;
    vector<bool> vis;
    vector<o_set<int>> c;

    Dfs(int n) : sz(n + 1), vis(n + 1), g(n + 1), c(n + 1) {}

    void add_edge(int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u); 
    }
    void dfs(int v, int p = -1) {
        vis[v] = true;
        sz[v] = 1;
        int big = 0, mx = 0;
        for(auto u : g[v]) if(!vis[u]) {
            dfs(u, v);
            sz[v] += sz[u];
            if(sz[u] > mx) {
                mx = sz[u];
                big = u;
            }
        }
        if(big == 0) {
            c[v].insert(a[v]);
        } else {
            c[v] = move(c[big]);
            for(auto u : g[v]) if(u != big) {
                for(auto x : c[u]) {
                    c[v].insert(x);
                }
            }
            c[v].insert(a[v]);
        }
        int nn = sz[v];
        int ase = c[v].order_of_key(nn + 1);
        ans[v] = nn - ase;
    }
};

int32_t main() {
    Dark_Lord_Binoy::setup();
    Testcase_Handler {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        Dfs g(n);
        for (int i = 1; i < n; i++) {
            int u, v;
            cin >> u >> v;
            g.add_edge(u, v);
        }
        g.dfs(1);
        int q;
        cin >> q;
        while(q--) {
            int x;
            cin >> x;
            cout << ans[x] << nl;
        }
    }

    print(_Time_);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1157 Roy and Tree Permutation
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 16:47:33
Judged At
2025-01-02 16:47:33
Judged By
Score
50
Total Time
≥2175ms
Peak Memory
≥256.016 MiB