답안 #683292

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
683292 2023-01-18T06:25:50 Z Dima_Borchuk Birthday gift (IZhO18_treearray) C++17
0 / 100
16 ms 35540 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) int32_t(x.size())
#define pii pair<int,int>
#include<bits/stdc++.h>
#define ld long double
#define ll long long
#define _ << ' ' <<
//#define int ll

using namespace std;
using namespace __gnu_pbds;

const int p = 1009;
const int L = 20;
const int N = (int)3e5 + 5;
const int INF = (int)2e18 + 6;
const int mod = (int)998244353;

mt19937 gen(chrono::system_clock::now().time_since_epoch().count());

template <typename T>
using ordered_set = tree < T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update >;

int a[N], up[N][20], tin[N], tout[N];

vector < int > g[N];

int timer = 0;

void dfs(int v, int anc) {
    tin[v] = ++timer;
    up[v][0] = anc;
    for (int i = 1; i < 20; i++) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }

    for (auto to : g[v]) {
        if (to == anc) continue;
        dfs(to, v);
    }

    tout[v] = ++timer;
}

bool upper(int a, int b) {
    return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}

int lca(int a, int b) {
    if (upper(a, b)) return a;
    if (upper(b, a)) return b;
    for (int i = 19; i >= 0; i--) {
        if (up[a][i] && !upper(up[a][i], b)) {
            a = up[a][i];
        }
    }
    return up[a][0];
}

set < int > s[N], t[N];

int32_t main() {
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // LOCAL

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
        s[a[i]].insert(i);
    }
    dfs(1, 0);
    for (int i = 2; i <= m; i++) {
        t[lca(a[i], a[i - 1])].insert(i);
    }
    for (int i = 1; i <= n; i++) {
        s[i].insert(m + 1); t[i].insert(m + 1);
    }

    while (q--) {
        int type;
        cin >> type;
        if (type == 2) {
            int l, r, v;
            cin >> l >> r >> v;
            int d = *s[v].lower_bound(l), e = *t[v].lower_bound(l + 1);
            if (d <= r) {
                cout << d << ' ' << d << '\n';
            }
            else {
                if (e <= r) {
                    cout << e - 1 << ' ' << e << '\n';
                }
                else {
                    cout << "-1 -1\n";
                }
            }
        }
        else {
            int pos, v;
            cin >> pos >> v;

            s[a[pos]].erase(pos);
            s[v].insert(pos);
            if (pos > 1) t[lca(a[pos], a[pos - 1])].erase(pos);
            a[pos] = v;
            if (pos > 1) t[lca(a[pos], a[pos - 1])].insert(pos);
        }
    }
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35540 KB n=5
2 Correct 16 ms 35540 KB n=100
3 Incorrect 16 ms 35540 KB Wrong range.
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35540 KB n=5
2 Correct 16 ms 35540 KB n=100
3 Incorrect 16 ms 35540 KB Wrong range.
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35540 KB n=5
2 Correct 16 ms 35540 KB n=100
3 Incorrect 16 ms 35540 KB Wrong range.
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 35540 KB n=5
2 Correct 16 ms 35540 KB n=100
3 Incorrect 16 ms 35540 KB Wrong range.
4 Halted 0 ms 0 KB -