답안 #91021

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91021 2018-12-25T15:44:32 Z rkocharyan Birthday gift (IZhO18_treearray) C++14
0 / 100
23 ms 23800 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 7;

int n, m, q;
vector <int> g[N];
int up[N][22], tin[N], tout[N];
int timer, l = 1;
set <int> f[N], s[N];

void dfs(int v, int p) {
    tin[v] = ++timer;
    up[v][0] = p;
    for(int i = 1; i <= l; i++) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for(int u : g[v]) {
        if(u == p) continue;
        dfs(u, v);
    }
    tout[v] = ++timer;
}

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

int lca(int u, int v) {
    if(upper(u, v)) return u;
    if(upper(v, u)) return v;
    for(int i = l; i >= 0; i--) {
        if(!upper(up[u][i], v)) {
            u = up[u][i];
        }
    }
    return up[u][0];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> q;
    for(int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    while((1 << l) <= n) l++;
    dfs(1, 1);
    vector <int> a(m);
    for(int i = 0; i < m; i++) {
        cin >> a[i];
        f[a[i]].insert(i);
    }
    for(int i = 0; i < m - 1; i++) {
        s[lca(a[i], a[i + 1])].insert(i);
    }
    for(; q; --q) {
        int t;
        cin >> t;
        if(t == 1) {
            int pos, val;
            cin >> pos >> val;
            pos--;
            f[a[pos]].erase(pos);
            f[val].insert(pos);
            if(pos > 0) s[lca(a[pos], a[pos - 1])].erase(pos - 1);
            if(pos < m - 1) s[lca(a[pos], a[pos + 1])].erase(pos);
            a[pos] = val;
            if(pos > 0) s[lca(a[pos], a[pos - 1])].insert(pos - 1);
            if(pos < m - 1) s[lca(a[pos], a[pos + 1])].insert(pos);
        }
        else {
            int l, r, val;
            cin >> l >> r >> val;
            l--, r--;
            if(l > r) swap(l, r);
            pair <int, int> ans = { -1, -1 };
            if(!f[val].empty()) {
                auto it = f[val].lower_bound(l);
                if(it != f[val].end() && *it + 1 <= r) {
                    ans = { *it, *it };
                }
            }
            if(!s[val].empty()) {
                auto it = s[val].lower_bound(l);
                if(it != s[val].end() && *it + 1 <= r) {
                    ans = { *it, *it + 1 };
                }
            }
            if(ans.first != -1) {
                ans.first++;
                ans.second++;
            }
            cout << ans.first << ' ' << ans.second << '\n';
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Incorrect 22 ms 23800 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Incorrect 22 ms 23800 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Incorrect 22 ms 23800 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB n=5
2 Incorrect 22 ms 23800 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -