답안 #1096235

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1096235 2024-10-04T06:51:48 Z Zero_OP Birthday gift (IZhO18_treearray) C++14
0 / 100
14 ms 23948 KB
#include "bits/stdc++.h"

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define rall(v) rbegin(v), rend(v)

const int MAX = 2e5 + 5;

int n, m, q, timerdfs, a[MAX], adjLCA[MAX], depth[MAX], pos[MAX], st[20][MAX];
vector<int> adj[MAX];
set<int> one[MAX], two[MAX];

int query(set<int>& S, int l, int r){
    auto it = S.lower_bound(l);
    if(it == S.end()) return -1;
    if(*it <= r) return *it;
    return -1;
}

void dfs(int u){
    st[0][pos[u] = timerdfs++] = u;
    for(int v : adj[u]){
        adj[v].erase(find(all(adj[v]), u));
        depth[v] = depth[u] + 1;
        dfs(v);
        st[0][timerdfs++] = u;
    }
}

int f(int u, int v){
    return depth[u] < depth[v] ? u : v;
}

void prepareRMQ(){
    for(int i = 1; (1 << i) <= timerdfs; ++i){
        for(int j = 0; j + (1 << i) <= timerdfs; ++j){
            st[i][j] = f(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
        }
    }
}

int getLCA(int u, int v){
    u = pos[u]; v = pos[v];
    if(u > v) swap(u, v);
    int k = 31 - __builtin_clz(v - u + 1);
    return f(st[k][u], st[k][v - (1 << k) + 1]);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    if(fopen("task.inp", "r")){
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
    }

    cin >> n >> m >> q;
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v); adj[v].push_back(u);
    }

    for(int i = 1; i <= m; ++i){
        cin >> a[i];
    }

    dfs(1);
    prepareRMQ();

    for(int i = 1; i <= m; ++i){
        one[a[i]].insert(i);
        if(i + 1 < m) {
            adjLCA[i] = getLCA(a[i], a[i + 1]);
            two[adjLCA[i]].insert(i);
        }
    }

    while(q--){
        int type;
        cin >> type;
        if(type == 1){
            int pos, val;
            cin >> pos >> val;

            if(pos + 1 < m){
                two[adjLCA[pos]].erase(pos);
                adjLCA[pos] = getLCA(val, a[pos + 1]);
                two[adjLCA[pos]].insert(pos);
            }

            if(pos > 1){
                two[adjLCA[pos - 1]].erase(pos - 1);
                adjLCA[pos - 1] = getLCA(val, a[pos - 1]);
                two[adjLCA[pos - 1]].insert(pos - 1);
            }

            one[a[pos]].erase(pos);
            a[pos] = val;
            one[a[pos]].insert(pos);
        } else{
            int l, r, x;
            cin >> l >> r >> x;

            int q1 = query(one[x], l, r);
            if(q1 != -1){
                cout << q1 << ' ' << q1 << '\n';
                continue;
            }

            int q2 = query(two[x], l, r - 1);
            if(q2 != -1){
                cout << q2 << ' ' << q2 + 1 << '\n';
                continue;
            } else{
                cout << -1 << ' ' << -1 << '\n';
            }
        }
    }

    return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         freopen("task.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23948 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Incorrect 10 ms 23900 KB Jury has the answer but participant has not
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23948 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Incorrect 10 ms 23900 KB Jury has the answer but participant has not
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23948 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Incorrect 10 ms 23900 KB Jury has the answer but participant has not
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23948 KB n=5
2 Correct 10 ms 23900 KB n=100
3 Correct 10 ms 23900 KB n=100
4 Incorrect 10 ms 23900 KB Jury has the answer but participant has not
5 Halted 0 ms 0 KB -