提出 #49672301


ソースコード 拡げる

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using namespace chrono;
#ifdef coderdhanraj
#include "libs/debug.h"
#else
#define debug(...) 
#endif
#define double long double
#define ff first
#define ss second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define size(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
#define SUM(x) accumulate(all(x), 0LL)
#define FIND(x, y) binary_search(all(x), y)
#define MEM(x, y) memset(x, y, sizeof(x))
#define CNT(x) __builtin_popcountll(x)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define bck(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define endl '\n' // remove for interactives
using i64 = int64_t; using u64 = uint64_t; using i128 = __int128_t; using u128 = __uint128_t;
using vi = vector<int>; using mii = map<int, int>; using pii = pair<int, int>; using vii = vector<pii>;
/* --------------------------------------------------------- TEMPLATES --------------------------------------------------- */
class custom_hash{public: static u64 splitmix64(u64 x){ x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31);}
    size_t operator()(u64 x) const {static const u64 FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM);}
    template<typename P, typename Q>
    size_t operator()(const pair<P, Q> &Y) const{static const u64 FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(Y.first * 31ull + Y.second + FIXED_RANDOM);}
};
template<typename P, typename Q> istream& operator>>(istream &in, pair<P, Q> &p){return (in >> p.first >> p.second);}
template<typename P, typename Q> ostream& operator<<(ostream &out, const pair<P, Q> &p){return (out << p.first << " " << p.second); }
template<typename T> istream& operator>>(istream &in, vector<T> &a){for(auto &e : a) cin >> e; return in;}
template<typename T> ostream& operator<<(ostream &out, const vector<T> &a){for (auto &e : a) cout << e << " "; return out;}
template<typename T, size_t N> istream& operator>>(istream &in, array<T, N> &a){for(auto &e : a) cin >> e; return in;}
template<typename T, size_t N> ostream& operator<<(ostream &out, array<T, N> &a){for (auto &e : a) cout << e << " "; return out;}
template<typename T> using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using vec = vector<vector<T>>;
template<typename P, size_t Q> using var = vector<array<P, Q>>;
template<typename P, typename Q> using indexed_map = tree<P, Q, less<P>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename P, typename Q> using gphash = gp_hash_table<P, Q, custom_hash>;
template<typename P, typename Q> using umap = unordered_map<P, Q, custom_hash>;
template<typename P, typename Q> void amax(P &x, Q y){if(x < y) x = y;}
template<typename P, typename Q> void amin(P &x, Q y){if(x > y) x = y;}
template<typename T> void unique(vector<T> &a){sort(all(a)); a.resize(unique(all(a)) - a.begin());}
template<typename T> T ceil(T x, T y){return (x >= 0 ? (x + y - 1) / y : x / y);}
template<typename T> T floor(T x, T y){return x / y - ((x ^ y) < 0 and x % y);}
template<typename T> T gcd(T x, T y){return (x ? gcd(y % x, x) : y);}
template<typename T> T lcm(T x, T y){return x / gcd(x, y) * y;}
template<typename T> T ncr(T n, T k){if (n < k){return 0;} k = min(k, n - k); T ans = 1; rep(i, 1, k + 1){ans *= (n - i + 1), ans /= i;} return ans;}
template<typename P, typename Q> P expo(P x, Q y, u64 m = LLONG_MAX){P res = 1; x = x % m; while (y > 0){if (y & 1) res = (u128)res * x % m; y = y >> 1, x = (u128)x * x % m;} return res;}
template<typename P, typename Q> P bpow(P x, Q y){P res = 1; while (y > 0){if (y & 1) res *= x; y = y >> 1, x *= x;} return res;}
template<typename P, typename Q> P inv(P n, Q m){return expo(n, m - 2, m);}
template<typename T> T sqr(T x){return x * x;}
/* --------------------------------------------------------- UNIVERSAL CONSTANTS --------------------------------------------------- */
const double eps = 1e-9;
const int mod = 1e9 + 7;  // 998244353LL; // 10000000069LL; // 3006703054056749LL;
const i64 inf = 1LL << 60;
const double pi = 3.14159265358979323846264338327950;
const int dx[16] = {1, 0, 0, -1, 1, 1, -1, -1, 2, 2, 1, 1, -1, -1, -2, -2};
const int dy[16] = {0, -1, 1, 0, -1, 1, -1, 1, -1, 1, -2, 2, -2, 2, -1, 1};
/* --------------------------------------------------------- USEFUL FUNCTIONS ---------------------------------------------------- */
int testcase;
bool isOverflow(int x, int y){return (x > LLONG_MAX / y or y > LLONG_MAX / x);}
bool ispow2(int x){return (x ? !(x & (x - 1)) : 0);}
bool Case(){return cout << "Case #" << ++testcase << ": ", true;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
i64 rand(i64 x, i64 y){return uniform_int_distribution<i64>(x, y)(rng);}
#define JAI_SHREE_RAAM ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), cout.precision(20), cout.setf(ios::fixed);
/* ------------------------------------------------------- SOLUTION TO THE PROBLEM ------------------------------------------------- */
namespace Centroid{
    const int N = 2e5 + 5;
    vi adj[N];
    int par[N], sz[N], vis[N];
    int find_size(int u, int p = -1){
        sz[u] = 1; for (auto &v : adj[u]) if (!vis[v] and v != p) sz[u] += find_size(v, u);
        return sz[u];
    }
    int find_centroid(int u, int p, int s){
        for (auto &v : adj[u]) if (!vis[v] and v != p and sz[v] > (s >> 1)) return find_centroid(v, u, s);
        return u;
    }
};
using namespace Centroid;
template <class T> class BIT{
private: int n;
    const T O{};
    vector<T> Tree;
public: 
    void init(int _n){n = _n, Tree.assign(n + 1, O);}
    void op(T &a, T b){a += b;}
    void add(int p, T u){for(; p <= n; p += (p & -p)) op(Tree[p], u);}
    void update(int p, T u){add(p, u - query(p));}
    T _query(int p){T res = O; for(; p > 0; p -= (p & -p)) op(res, Tree[p]); return res;}
    T query(int l, int r){return (l > r ? O : _query(r) - _query(l - 1));}
    T query(int p){return query(p, p);}
};
BIT<int> x, y;
i64 ans[N], pv, tot;
int n;
void op(int u, int p, int k){
    x.add(u + 1, k);
    for (auto &v : adj[u]) if (!vis[v] and v != p) op(v, u, k);
}
void solve(int u, int p, i64 cnt){
    tot += y.query(u + 1, n);
    cnt += x.query(1, u);
    ans[u] += pv + cnt;
    y.add(u + 1, 1);
    for (auto &v : adj[u]) if (!vis[v] and v != p) solve(v, u, cnt);
    y.add(u + 1, -1);
}
void build(int u = 0, int p = -1){
    int s = find_size(u, p);
    u = find_centroid(u, p, s), vis[u] = 1, par[u] = p;
    vi childs; for (auto &v : adj[u]) if (!vis[v]) childs.pb(v);
    y.add(u + 1, 1);
    tot = 0, x.add(u + 1, 1);
    for (auto &v : childs){
        pv = tot;
        solve(v, u, 0);
        op(v, u, 1);
    }
    for (auto & v : childs) op(v, u, -1);
    ans[u] += tot;
    tot = 0, x.add(u + 1, -1);
    reverse(all(childs));
    for (auto &v : childs){
        pv = tot;
        solve(v, u, 0);
        op(v, u, 1);
    }
    for (auto & v : childs) op(v, u, -1);
    y.add(u + 1, -1);
    for (auto &v : childs) build(v, u);
}
void solve(){
    cin >> n;
    rep(i, 1, n){
        int u, v; cin >> u >> v, --u, --v;
        adj[u].pb(v), adj[v].pb(u);
    }
    x.init(n), y.init(n);
    build();
    rep(i, 0, n) cout << ans[i] << ' ';
}
int32_t main(){
#ifdef coderdhanraj
    auto start = high_resolution_clock::now();
    freopen("error.txt", "w", stderr);
#endif
    JAI_SHREE_RAAM
    int ttc = 1;
    // cin >> ttc;
    while (ttc--) solve();
#ifdef coderdhanraj
    auto time =  duration_cast<microseconds>(high_resolution_clock::now() - start).count() / 1000;
    cerr << "Time: " << time << " ms!" << endl;
#endif
    return 0;
}

提出情報

提出日時
問題 G - Tree Inversion
ユーザ coderdhanraj
言語 C++ 20 (gcc 12.2)
得点 600
コード長 8272 Byte
結果 AC
実行時間 1549 ms
メモリ 32020 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 3
AC × 72
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 02_handmade_64.txt, 02_handmade_65.txt, 02_handmade_66.txt, 02_handmade_67.txt, 02_handmade_68.txt, 02_handmade_69.txt, 02_handmade_70.txt, 02_handmade_71.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 2 ms 3500 KiB
00_sample_01.txt AC 2 ms 3536 KiB
00_sample_02.txt AC 2 ms 3540 KiB
01_random_03.txt AC 98 ms 21116 KiB
01_random_04.txt AC 1487 ms 30704 KiB
01_random_05.txt AC 933 ms 19464 KiB
01_random_06.txt AC 615 ms 19976 KiB
01_random_07.txt AC 97 ms 21160 KiB
01_random_08.txt AC 1491 ms 27008 KiB
01_random_09.txt AC 947 ms 19564 KiB
01_random_10.txt AC 665 ms 19812 KiB
01_random_11.txt AC 100 ms 21196 KiB
01_random_12.txt AC 1496 ms 30644 KiB
01_random_13.txt AC 931 ms 19468 KiB
01_random_14.txt AC 631 ms 19848 KiB
01_random_15.txt AC 97 ms 21216 KiB
01_random_16.txt AC 1487 ms 27152 KiB
01_random_17.txt AC 946 ms 19508 KiB
01_random_18.txt AC 648 ms 19876 KiB
01_random_19.txt AC 97 ms 21328 KiB
01_random_20.txt AC 1494 ms 30624 KiB
01_random_21.txt AC 956 ms 19612 KiB
01_random_22.txt AC 641 ms 19812 KiB
01_random_23.txt AC 95 ms 21080 KiB
01_random_24.txt AC 1524 ms 29496 KiB
01_random_25.txt AC 975 ms 19464 KiB
01_random_26.txt AC 663 ms 19872 KiB
01_random_27.txt AC 100 ms 21216 KiB
01_random_28.txt AC 1549 ms 29600 KiB
01_random_29.txt AC 977 ms 19572 KiB
01_random_30.txt AC 617 ms 19808 KiB
01_random_31.txt AC 4 ms 4208 KiB
01_random_32.txt AC 213 ms 9748 KiB
01_random_33.txt AC 472 ms 13004 KiB
01_random_34.txt AC 604 ms 18812 KiB
01_random_35.txt AC 53 ms 13684 KiB
01_random_36.txt AC 940 ms 17788 KiB
01_random_37.txt AC 48 ms 5428 KiB
01_random_38.txt AC 39 ms 11012 KiB
01_random_39.txt AC 99 ms 6568 KiB
01_random_40.txt AC 147 ms 7232 KiB
01_random_41.txt AC 503 ms 17404 KiB
01_random_42.txt AC 33 ms 9824 KiB
01_random_43.txt AC 506 ms 12308 KiB
01_random_44.txt AC 718 ms 18748 KiB
01_random_45.txt AC 17 ms 6644 KiB
01_random_46.txt AC 466 ms 12896 KiB
01_random_47.txt AC 18 ms 4364 KiB
01_random_48.txt AC 610 ms 18596 KiB
01_random_49.txt AC 55 ms 14060 KiB
01_random_50.txt AC 505 ms 12384 KiB
01_random_51.txt AC 255 ms 10304 KiB
01_random_52.txt AC 141 ms 21152 KiB
01_random_53.txt AC 1540 ms 29884 KiB
01_random_54.txt AC 934 ms 19536 KiB
01_random_55.txt AC 633 ms 19836 KiB
01_random_56.txt AC 139 ms 21112 KiB
01_random_57.txt AC 1480 ms 27160 KiB
01_random_58.txt AC 929 ms 19572 KiB
01_random_59.txt AC 661 ms 19788 KiB
01_random_60.txt AC 125 ms 21472 KiB
01_random_61.txt AC 1494 ms 30620 KiB
01_random_62.txt AC 963 ms 19584 KiB
01_random_63.txt AC 701 ms 19808 KiB
02_handmade_64.txt AC 2 ms 3400 KiB
02_handmade_65.txt AC 2 ms 3516 KiB
02_handmade_66.txt AC 886 ms 31924 KiB
02_handmade_67.txt AC 887 ms 31996 KiB
02_handmade_68.txt AC 887 ms 31908 KiB
02_handmade_69.txt AC 885 ms 31936 KiB
02_handmade_70.txt AC 880 ms 31956 KiB
02_handmade_71.txt AC 879 ms 32020 KiB