답안 #1110955

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1110955 2024-11-11T06:00:44 Z KasymK Birthday gift (IZhO18_treearray) C++17
0 / 100
9 ms 11344 KB
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 2e5+5;
const int LOG = 20;
vector<int> adj[N];
int v[N], A[N][LOG], d[N];

void dfs(int x, int pr = -1){
    A[x][0] = pr;
    for(int i = 1; i < LOG; ++i)
        A[x][i] = A[A[x][i-1]][i-1];
    tr(it, adj[x])
        if(*it!=pr){
            d[*it] = d[x]+1;
            dfs(*it, x);
        }
}

int al(int x, int k){
    if(d[x]<k)
        return -1;
    for(int i = LOG-1; i >= 0; --i)
        if(k>>i&1)
            x = A[x][i];
    return x;
}

int lca(int a, int b){
    if(d[b]>d[a])
        swap(a, b);
    a = al(a, d[a]-d[b]);
    if(a==b)
        return a;
    for(int i = LOG-1; i >= 1; --i)
        if(al(a, i) != al(b, i))
            a = A[a][i], b = A[b][i];
    return A[a][0];
}

int main(){
    freopen("file.txt", "r", stdin);
    int n, m, q;
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i < n; ++i){
        int a, b;
        scanf("%d%d", &a, &b);
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(1);
    for(int i = 1; i <= m; ++i){
        int x;
        scanf("%d", &x);
        v[i] = x;
    }
    while(q--){
        int op;
        scanf("%d", &op);
        if(op==1){
            int k, u;
            scanf("%d%d", &k, &u);
            v[k] = u;
            continue;
        }
        int l, r, val;
        scanf("%d%d%d", &l, &r, &val);
        bool done = 0;
        for(int i = l; i <= r and !done; ++i)
            if(v[i]==val)
                printf("%d %d\n", i, i), done = 1;
        for(int i = l; i < r and !done; ++i)
            if(lca(v[i], v[i+1])==val)
                printf("%d %d\n", i, i+1), done = 1;
        if(!done)
            printf("-1 -1\n");
    }
    return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |     freopen("file.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |     scanf("%d%d%d", &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         scanf("%d%d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
treearray.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         scanf("%d", &op);
      |         ~~~~~^~~~~~~~~~~
treearray.cpp:73:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |             scanf("%d%d", &k, &u);
      |             ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |         scanf("%d%d%d", &l, &r, &val);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 11344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 11344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 11344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 11344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -