답안 #579981

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579981 2022-06-20T12:17:18 Z loggerr Birthday gift (IZhO18_treearray) C++14
56 / 100
170 ms 105732 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include "bits/stdc++.h"

using namespace std;
using ll = int;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
using str = string;
using big_int = __int128_t;

const ld eps = 1e-7;
const ld PI = acos(-1);

#define all(c) (c).begin(), (c).end()
#define rall(c) ((c).rbegin()), ((c).rend())
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define fast ios_base::sync_with_stdio(0); cin.tie(0)
#define forn(i, n) for (ll i = 0; i < n; ++i)
#define sz(a) (ll)a.size()
#define endl '\n'
#define u_map unordered_map
#define mset multiset
//#define x first
//#define y second

#ifdef ONLINE_JUDGE
#define debug(x);
#else
#define debug(x) cerr << #x << ": " << x << endl;
#endif

str IO[2] = {"NO\n", "YES\n"};
str io[2] = {"no\n", "yes\n"};
str Io[2] = {"No\n", "Yes\n"};
//
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnd(1232423);

template<class T> bool inmin(T& x_, T y_) {return y_ < x_ ? (x_ = y_, true) : false;}
template<class T> bool inmax(T& x_, T y_) {return y_ > x_ ? (x_ = y_, true) : false;}

//const ll Mod = 1e9 + 7;
const ll Mod = 998244353;
//const ll Mod = 1e9 + 9;
//const ll Mod = 1234567891;
//const ll INF = 1e18;

template<class T> void add(T& x_, T y_) { x_ = (x_ + y_) % Mod; };
template<class T> void sub(T& x_, T y_) { x_ = (x_ + Mod - y_) % Mod; };
template<class T> void mul(T& x_, T y_) { x_ = (x_ * y_) % Mod; };

const ll Maxn = 2e5 + 10;
const ll Lg = 20;

vll gr[Maxn];

ll app[Maxn];
ll st[Lg][Maxn];
ll ans[Lg][Maxn];
ll d[Maxn];
ll dp[Maxn];
vll pth;

void init() {
    dp[0] = -1;
    for (ll i = 1; i < Maxn; ++i) {
        dp[i] = dp[i / 2] + 1;
    }
    return;
}

void dfs(ll v, ll p) {
    app[v] = sz(pth);
    d[v] = d[p] + 1;
    pth.pb(v);
    for (ll adj : gr[v]) {
        if (adj != p) {
            dfs(adj, v);
            pth.pb(v);
        }
    }
    return;
}

ll lca(ll a, ll b) {
    ll l = min(app[a], app[b]);
    ll r = max(app[a], app[b]);
    ll lg = dp[r - l + 1];
    ll len = (1 << lg);
    ll dep = st[lg][l];
    ll res = ans[lg][l];
    if (st[lg][r - len + 1] < dep) {
        return ans[lg][r - len + 1];
    }
    return res;
}
const ll C = 1500;

struct block {
    u_map<ll, set<ll>> keys;
    ll size;

    void erase(ll x, ll key) {
        keys[key].erase(x);
        if (keys[key].empty())
            keys.erase(key);
    }

    void insert(ll x, ll key) {
        keys[key].insert(x);
    }

    ll get(ll key) {
        return (keys[key].empty() ? -1 : *keys[key].begin());
    }
};

inline void solve() {
    ll n, m, q;
    cin >> n >> m >> q;
    forn (i, n - 1) {
        ll u, v;
        cin >> u >> v;
        --u, --v;
        gr[u].pb(v), gr[v].pb(u);
    }
    dfs(0, 0);
    dfs(0, 0);
    forn (i, sz(pth)) {
        st[0][i] = d[pth[i]];
        ans[0][i] = pth[i];
    }
    for (ll i = 1; i < Lg; ++i) {
        ll len = (1 << i);
        for (ll j = 0; j + len <= sz(pth); ++j) {
            st[i][j] = st[i - 1][j];
            ans[i][j] = ans[i - 1][j];
            if (st[i][j] > st[i - 1][j + len / 2]) {
                st[i][j] = st[i - 1][j + len / 2];
                ans[i][j] = ans[i - 1][j + len / 2];
            }
        }
    }
    vll que(m);
    forn (i, m) {
        cin >> que[i];
        --que[i];
    }
    vll lcas(m - 1);
    forn (i, m - 1) {
        lcas[i] = lca(que[i], que[i + 1]);
    }
    vector<block> dcL;
    forn (i, sz(lcas)) {
        ll id = i / C;
        if (id == sz(dcL)) dcL.pb({});
        dcL[id].size++;
        dcL[id].insert(i, lcas[i]);
    }
    vector<block> dcV;
    forn (i, sz(que)) {
        ll id = i / C;
        if (id == sz(dcV)) dcV.pb({});
        dcV[id].size++;
        dcV[id].insert(i, que[i]);
    }
    auto query = [&](ll l, ll r, ll key, vector<block> &dc, vector<ll> &arr) {
        ll id = -1;
        while (l <= r) {
            if (l % C == 0 && l + dc[l / C].size - 1 <= r) {
                ll nid = dc[l / C].get(key);
                if (nid != -1) id = nid;
                l += dc[l / C].size;
            } else {
                if (arr[l] == key) id = l;
                ++l;
            }
        }
        return id;
    };
    while (q--) {
        ll tp;
        cin >> tp;
        if (tp == 1) {
            ll pos, v;
            cin >> pos >> v;
            --pos, --v;
            dcV[pos / C].erase(pos, que[pos]);
            if (pos - 1 >= 0) {
                dcL[(pos - 1) / C].erase(pos - 1, lcas[pos - 1]);
            }
            if (pos + 1 < m) {
                dcL[pos / C].erase(pos, lcas[pos]);
            }
            que[pos] = v;
            dcV[pos / C].insert(pos, que[pos]);
            if (pos - 1 >= 0) {
                lcas[pos - 1] = lca(que[pos - 1], que[pos]);
                dcL[(pos - 1) / C].insert(pos - 1, lcas[pos - 1]);
            }
            if (pos + 1 < m) {
                lcas[pos] = lca(que[pos], que[pos + 1]);
                dcL[pos / C].insert(pos, lcas[pos]);
            }
            continue;
        }
        ll l, r, v;
        cin >> l >> r >> v;
        --l, --r, --v;
        ll id = query(l, r, v, dcV, que);
        if (id != -1) {
            cout << id + 1 << ' ' << id + 1 << endl;
            continue;
        }
        if (l == r) {
            cout << -1 << ' ' << -1 << endl;
            continue;
        }
        --r;
        id = query(l, r, v, dcL, lcas);
        if (id == -1) {
            cout << -1 << ' ' << -1 << endl;
            continue;
        }
        ll ql = id + 1;
        ll qr = ql + 1;
        cout << ql << ' ' << qr << endl;
    }
    return;
}

int main() {
    init();
    fast;
    //cout << fixed << satprecision(10);
    ll test;
    test = 1;
    //cin >> test;
    for (ll id = 0; id < test; ++id) solve();
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5756 KB n=5
2 Correct 3 ms 5844 KB n=100
3 Correct 4 ms 5844 KB n=100
4 Correct 4 ms 5844 KB n=100
5 Correct 4 ms 5964 KB n=100
6 Correct 4 ms 5844 KB n=100
7 Correct 3 ms 5844 KB n=100
8 Correct 4 ms 5844 KB n=100
9 Correct 3 ms 5844 KB n=100
10 Correct 4 ms 5844 KB n=100
11 Correct 4 ms 5844 KB n=100
12 Correct 5 ms 5936 KB n=100
13 Correct 4 ms 5844 KB n=100
14 Correct 4 ms 5844 KB n=100
15 Correct 4 ms 5844 KB n=100
16 Correct 4 ms 5844 KB n=100
17 Correct 3 ms 5844 KB n=100
18 Correct 5 ms 5948 KB n=100
19 Correct 3 ms 5844 KB n=100
20 Correct 4 ms 5972 KB n=100
21 Correct 4 ms 5972 KB n=100
22 Correct 4 ms 5844 KB n=100
23 Correct 5 ms 5972 KB n=100
24 Correct 3 ms 5844 KB n=100
25 Correct 4 ms 5972 KB n=100
26 Correct 3 ms 5844 KB n=12
27 Correct 4 ms 5844 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5756 KB n=5
2 Correct 3 ms 5844 KB n=100
3 Correct 4 ms 5844 KB n=100
4 Correct 4 ms 5844 KB n=100
5 Correct 4 ms 5964 KB n=100
6 Correct 4 ms 5844 KB n=100
7 Correct 3 ms 5844 KB n=100
8 Correct 4 ms 5844 KB n=100
9 Correct 3 ms 5844 KB n=100
10 Correct 4 ms 5844 KB n=100
11 Correct 4 ms 5844 KB n=100
12 Correct 5 ms 5936 KB n=100
13 Correct 4 ms 5844 KB n=100
14 Correct 4 ms 5844 KB n=100
15 Correct 4 ms 5844 KB n=100
16 Correct 4 ms 5844 KB n=100
17 Correct 3 ms 5844 KB n=100
18 Correct 5 ms 5948 KB n=100
19 Correct 3 ms 5844 KB n=100
20 Correct 4 ms 5972 KB n=100
21 Correct 4 ms 5972 KB n=100
22 Correct 4 ms 5844 KB n=100
23 Correct 5 ms 5972 KB n=100
24 Correct 3 ms 5844 KB n=100
25 Correct 4 ms 5972 KB n=100
26 Correct 3 ms 5844 KB n=12
27 Correct 4 ms 5844 KB n=100
28 Correct 4 ms 6100 KB n=500
29 Correct 5 ms 6228 KB n=500
30 Correct 4 ms 6228 KB n=500
31 Correct 4 ms 6228 KB n=500
32 Correct 5 ms 6100 KB n=500
33 Correct 4 ms 6228 KB n=500
34 Correct 4 ms 6100 KB n=500
35 Correct 4 ms 6228 KB n=500
36 Correct 4 ms 6116 KB n=500
37 Correct 4 ms 6100 KB n=500
38 Correct 4 ms 6100 KB n=500
39 Correct 5 ms 6100 KB n=500
40 Correct 4 ms 6100 KB n=500
41 Correct 4 ms 6100 KB n=500
42 Correct 4 ms 6100 KB n=500
43 Correct 4 ms 6160 KB n=500
44 Correct 4 ms 6100 KB n=500
45 Correct 4 ms 6100 KB n=500
46 Correct 4 ms 6192 KB n=500
47 Correct 4 ms 6228 KB n=500
48 Correct 4 ms 6216 KB n=500
49 Correct 4 ms 6228 KB n=500
50 Correct 4 ms 6216 KB n=500
51 Correct 4 ms 6228 KB n=500
52 Correct 4 ms 6228 KB n=500
53 Correct 4 ms 6228 KB n=500
54 Correct 4 ms 6228 KB n=500
55 Correct 4 ms 6100 KB n=278
56 Correct 4 ms 6228 KB n=500
57 Correct 4 ms 6228 KB n=500
58 Correct 5 ms 6100 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5756 KB n=5
2 Correct 3 ms 5844 KB n=100
3 Correct 4 ms 5844 KB n=100
4 Correct 4 ms 5844 KB n=100
5 Correct 4 ms 5964 KB n=100
6 Correct 4 ms 5844 KB n=100
7 Correct 3 ms 5844 KB n=100
8 Correct 4 ms 5844 KB n=100
9 Correct 3 ms 5844 KB n=100
10 Correct 4 ms 5844 KB n=100
11 Correct 4 ms 5844 KB n=100
12 Correct 5 ms 5936 KB n=100
13 Correct 4 ms 5844 KB n=100
14 Correct 4 ms 5844 KB n=100
15 Correct 4 ms 5844 KB n=100
16 Correct 4 ms 5844 KB n=100
17 Correct 3 ms 5844 KB n=100
18 Correct 5 ms 5948 KB n=100
19 Correct 3 ms 5844 KB n=100
20 Correct 4 ms 5972 KB n=100
21 Correct 4 ms 5972 KB n=100
22 Correct 4 ms 5844 KB n=100
23 Correct 5 ms 5972 KB n=100
24 Correct 3 ms 5844 KB n=100
25 Correct 4 ms 5972 KB n=100
26 Correct 3 ms 5844 KB n=12
27 Correct 4 ms 5844 KB n=100
28 Correct 4 ms 6100 KB n=500
29 Correct 5 ms 6228 KB n=500
30 Correct 4 ms 6228 KB n=500
31 Correct 4 ms 6228 KB n=500
32 Correct 5 ms 6100 KB n=500
33 Correct 4 ms 6228 KB n=500
34 Correct 4 ms 6100 KB n=500
35 Correct 4 ms 6228 KB n=500
36 Correct 4 ms 6116 KB n=500
37 Correct 4 ms 6100 KB n=500
38 Correct 4 ms 6100 KB n=500
39 Correct 5 ms 6100 KB n=500
40 Correct 4 ms 6100 KB n=500
41 Correct 4 ms 6100 KB n=500
42 Correct 4 ms 6100 KB n=500
43 Correct 4 ms 6160 KB n=500
44 Correct 4 ms 6100 KB n=500
45 Correct 4 ms 6100 KB n=500
46 Correct 4 ms 6192 KB n=500
47 Correct 4 ms 6228 KB n=500
48 Correct 4 ms 6216 KB n=500
49 Correct 4 ms 6228 KB n=500
50 Correct 4 ms 6216 KB n=500
51 Correct 4 ms 6228 KB n=500
52 Correct 4 ms 6228 KB n=500
53 Correct 4 ms 6228 KB n=500
54 Correct 4 ms 6228 KB n=500
55 Correct 4 ms 6100 KB n=278
56 Correct 4 ms 6228 KB n=500
57 Correct 4 ms 6228 KB n=500
58 Correct 5 ms 6100 KB n=500
59 Correct 7 ms 7124 KB n=2000
60 Correct 7 ms 7372 KB n=2000
61 Correct 7 ms 7320 KB n=2000
62 Correct 8 ms 7252 KB n=2000
63 Correct 8 ms 7124 KB n=2000
64 Correct 8 ms 7252 KB n=2000
65 Correct 8 ms 7072 KB n=2000
66 Correct 7 ms 7284 KB n=2000
67 Correct 8 ms 7124 KB n=2000
68 Correct 7 ms 7300 KB n=2000
69 Correct 6 ms 7132 KB n=2000
70 Correct 6 ms 7124 KB n=2000
71 Correct 6 ms 7124 KB n=2000
72 Correct 8 ms 7124 KB n=2000
73 Correct 8 ms 7064 KB n=2000
74 Correct 7 ms 6996 KB n=1844
75 Correct 8 ms 7124 KB n=2000
76 Correct 6 ms 7124 KB n=2000
77 Correct 7 ms 7124 KB n=2000
78 Correct 6 ms 7124 KB n=2000
79 Correct 7 ms 7124 KB n=2000
80 Correct 7 ms 7252 KB n=2000
81 Correct 7 ms 7280 KB n=2000
82 Correct 7 ms 7124 KB n=2000
83 Correct 8 ms 7256 KB n=2000
84 Correct 8 ms 7124 KB n=2000
85 Correct 8 ms 7252 KB n=2000
86 Correct 8 ms 7252 KB n=2000
87 Correct 7 ms 7176 KB n=2000
88 Correct 10 ms 7380 KB n=2000
89 Correct 11 ms 7380 KB n=2000
90 Correct 7 ms 7380 KB n=2000
91 Correct 9 ms 7124 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5756 KB n=5
2 Correct 3 ms 5844 KB n=100
3 Correct 4 ms 5844 KB n=100
4 Correct 4 ms 5844 KB n=100
5 Correct 4 ms 5964 KB n=100
6 Correct 4 ms 5844 KB n=100
7 Correct 3 ms 5844 KB n=100
8 Correct 4 ms 5844 KB n=100
9 Correct 3 ms 5844 KB n=100
10 Correct 4 ms 5844 KB n=100
11 Correct 4 ms 5844 KB n=100
12 Correct 5 ms 5936 KB n=100
13 Correct 4 ms 5844 KB n=100
14 Correct 4 ms 5844 KB n=100
15 Correct 4 ms 5844 KB n=100
16 Correct 4 ms 5844 KB n=100
17 Correct 3 ms 5844 KB n=100
18 Correct 5 ms 5948 KB n=100
19 Correct 3 ms 5844 KB n=100
20 Correct 4 ms 5972 KB n=100
21 Correct 4 ms 5972 KB n=100
22 Correct 4 ms 5844 KB n=100
23 Correct 5 ms 5972 KB n=100
24 Correct 3 ms 5844 KB n=100
25 Correct 4 ms 5972 KB n=100
26 Correct 3 ms 5844 KB n=12
27 Correct 4 ms 5844 KB n=100
28 Correct 4 ms 6100 KB n=500
29 Correct 5 ms 6228 KB n=500
30 Correct 4 ms 6228 KB n=500
31 Correct 4 ms 6228 KB n=500
32 Correct 5 ms 6100 KB n=500
33 Correct 4 ms 6228 KB n=500
34 Correct 4 ms 6100 KB n=500
35 Correct 4 ms 6228 KB n=500
36 Correct 4 ms 6116 KB n=500
37 Correct 4 ms 6100 KB n=500
38 Correct 4 ms 6100 KB n=500
39 Correct 5 ms 6100 KB n=500
40 Correct 4 ms 6100 KB n=500
41 Correct 4 ms 6100 KB n=500
42 Correct 4 ms 6100 KB n=500
43 Correct 4 ms 6160 KB n=500
44 Correct 4 ms 6100 KB n=500
45 Correct 4 ms 6100 KB n=500
46 Correct 4 ms 6192 KB n=500
47 Correct 4 ms 6228 KB n=500
48 Correct 4 ms 6216 KB n=500
49 Correct 4 ms 6228 KB n=500
50 Correct 4 ms 6216 KB n=500
51 Correct 4 ms 6228 KB n=500
52 Correct 4 ms 6228 KB n=500
53 Correct 4 ms 6228 KB n=500
54 Correct 4 ms 6228 KB n=500
55 Correct 4 ms 6100 KB n=278
56 Correct 4 ms 6228 KB n=500
57 Correct 4 ms 6228 KB n=500
58 Correct 5 ms 6100 KB n=500
59 Correct 7 ms 7124 KB n=2000
60 Correct 7 ms 7372 KB n=2000
61 Correct 7 ms 7320 KB n=2000
62 Correct 8 ms 7252 KB n=2000
63 Correct 8 ms 7124 KB n=2000
64 Correct 8 ms 7252 KB n=2000
65 Correct 8 ms 7072 KB n=2000
66 Correct 7 ms 7284 KB n=2000
67 Correct 8 ms 7124 KB n=2000
68 Correct 7 ms 7300 KB n=2000
69 Correct 6 ms 7132 KB n=2000
70 Correct 6 ms 7124 KB n=2000
71 Correct 6 ms 7124 KB n=2000
72 Correct 8 ms 7124 KB n=2000
73 Correct 8 ms 7064 KB n=2000
74 Correct 7 ms 6996 KB n=1844
75 Correct 8 ms 7124 KB n=2000
76 Correct 6 ms 7124 KB n=2000
77 Correct 7 ms 7124 KB n=2000
78 Correct 6 ms 7124 KB n=2000
79 Correct 7 ms 7124 KB n=2000
80 Correct 7 ms 7252 KB n=2000
81 Correct 7 ms 7280 KB n=2000
82 Correct 7 ms 7124 KB n=2000
83 Correct 8 ms 7256 KB n=2000
84 Correct 8 ms 7124 KB n=2000
85 Correct 8 ms 7252 KB n=2000
86 Correct 8 ms 7252 KB n=2000
87 Correct 7 ms 7176 KB n=2000
88 Correct 10 ms 7380 KB n=2000
89 Correct 11 ms 7380 KB n=2000
90 Correct 7 ms 7380 KB n=2000
91 Correct 9 ms 7124 KB n=2000
92 Runtime error 170 ms 105732 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -