답안 #999913

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999913 2024-06-16T09:41:39 Z vjudge1 Construction of Highway (JOI18_construction) C++17
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"

using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 4005;
struct SegTree
{
    ll n;
    vector<ll> st;
    SegTree(ll sz = 0, bool input = false)
    {
        n = sz;
        st.resize((n + 1) << 1, 0);
    }
    void set(ll i, ll val)
    {
        st[i + n - 1] = val;
    }
    void build()
    {
        for (ll i = n - 1; i >= 1; i--)
            st[i] = st[i << 1] + st[i << 1 | 1];
    }
    void modify(ll p, ll val)
    {
        for (st[p += n - 1] = val; p > 1; p >>= 1)
            st[p >> 1] = st[p] + st[p ^ 1];
    }
    ll query(ll l, ll r)
    {
        ll ans = 0;
        for (l += n - 1, r += n; l < r; l >>= 1, r >>= 1)
        {
            if (l & 1)
                ans += st[l++];
            if (r & 1)
                ans += st[--r];
        }
        return ans;
    }
};
SegTree st;
ll c[N], p[N];
void solve()
{
    ll n;
    cin >> n;
    for (ll i = 1; i <= n; i++) cin >> c[i];
    {
        map<ll, ll> mp;
        for (ll i = 1; i <= n; i++) mp[c[i]] = 1;
        ll cur = 1;
        for (auto i : mp)
            mp[i.first] = cur++;
        for (ll i = 1; i <= n; i++) c[i] = mp[c[i]];
    }
    for (ll i = 1; i < n; i++)
    {
        ll a, b;
        cin >> a >> b;
        st = SegTree(n);
        p[b] = a;
        vector<ll> vec;
        ll v = a;
        while (v) vec.push_back(c[v]), v = p[v];
        ll ans = 0;
        for (ll i : vec)
            ans += st.query(1, i - 1), st.modify(i, 1);
        v = b;
        while (v) c[v] = c[b], v = p[v];
        cout << ans << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll t = 1;
    // precomp();
    //cin >> t;
    for (ll cs = 1; cs <= t; cs++)
        solve();
    // cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -