답안 #978892

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
978892 2024-05-10T00:49:19 Z irmuun Birthday gift (IZhO18_treearray) C++17
30 / 100
4000 ms 10196 KB
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()

const int N=2e5+5;

int n,m,q,a[N],par[N][20],dep[N];
vector<int>adj[N];
void dfs(int x,int p){
    par[x][0]=p;
    for(int y:adj[x]){
        if(y!=p){
            dep[y]=dep[x]+1;
            dfs(y,x);
        }
    }
}
int up(int x,int d){
    for(int i=19;i>=0;i--){
        if(d&(1<<i)){
            x=par[x][i];
        }
    }
    return x;
}
int lca(int x,int y){
    if(dep[x]<dep[y]) swap(x,y);
    x=up(x,dep[x]-dep[y]);
    for(int i=19;i>=0;i--){
        if(par[x][i]!=par[y][i]){
            x=par[x][i];
            y=par[y][i];
        }
    }
    if(x!=y) x=par[x][0];
    return x;
}

struct segtree{
    int n;
    vector<int>d;
    segtree(int n):n(n){
        d.resize(4*n);
        build(1,1,n);
    }
    void build(int node,int l,int r){
        if(l==r){
            d[node]=0;
            return;
        }
        int mid=(l+r)/2;
        build(node*2,l,mid);
        build(node*2+1,mid+1,r);
        d[node]=0;
    }
    int query(int node,int l,int r,int L,int R){
        if(l>r||r<L||R<l) return 0;
        if(L<=l&&r<=R){
            return d[node];
        }
        int mid=(l+r)/2;
        int le=query(node*2,l,mid,L,R),ri=query(node*2+1,mid+1,r,L,R);
        if(le==0) return ri;
        if(ri==0) return le;
        return lca(le,ri);
    }
    void update(int node,int l,int r,int pos,int val){
        if(r<pos||pos<l) return;
        if(l==r){
            d[node]=val;
            return;
        }
        int mid=(l+r)/2;
        update(node*2,l,mid,pos,val);
        update(node*2+1,mid+1,r,pos,val);
        if(d[node*2]==0) d[node]=d[node*2+1];
        if(d[node*2+1]==0) d[node]=d[node*2];
        d[node]=lca(d[node*2],d[node*2+1]);
    }
}sg(N);

void ask(int l,int r,int u){
    for(int x=l;x<=r;x++){
        int cur=a[x];
        int L=x,R=r;
        while(L<R){
            int mid=(L+R+1)/2;
            int node=sg.query(1,1,N,x,mid);
            if(dep[node]<dep[u]){
                R=mid-1;
            }
            else{
                L=mid;
            }
        }
        if(sg.query(1,1,N,x,L)==u){
            cout<<x<<' '<<L<<"\n";
            return;
        }
    }
    cout<<"-1 -1\n";
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n>>m>>q;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(int i=1;i<=m;i++){
        cin>>a[i];
    }
    dep[1]=0;
    dfs(1,1);
    for(int j=1;j<20;j++){
        for(int i=1;i<=n;i++){
            par[i][j]=par[par[i][j-1]][j-1];
        }
    }
    for(int i=1;i<=m;i++){
        sg.update(1,1,N,i,a[i]);
    }
    while(q--){
        int t;
        cin>>t;
        if(t==1){
            int pos,v;
            cin>>pos>>v;
            a[pos]=v;
            sg.update(1,1,N,pos,v);
        }
        else{
            int l,r,u;
            cin>>l>>r>>u;
            ask(l,r,u);
        }
    }
}

Compilation message

treearray.cpp: In function 'void ask(int, int, int)':
treearray.cpp:91:13: warning: unused variable 'cur' [-Wunused-variable]
   91 |         int cur=a[x];
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9816 KB n=5
2 Correct 5 ms 9820 KB n=100
3 Correct 4 ms 9820 KB n=100
4 Correct 4 ms 9820 KB n=100
5 Correct 5 ms 9820 KB n=100
6 Correct 12 ms 9820 KB n=100
7 Correct 12 ms 9820 KB n=100
8 Correct 5 ms 9820 KB n=100
9 Correct 5 ms 9820 KB n=100
10 Correct 9 ms 9816 KB n=100
11 Correct 9 ms 9820 KB n=100
12 Correct 3 ms 9820 KB n=100
13 Correct 4 ms 9820 KB n=100
14 Correct 4 ms 9820 KB n=100
15 Correct 3 ms 9820 KB n=100
16 Correct 5 ms 9820 KB n=100
17 Correct 5 ms 9820 KB n=100
18 Correct 4 ms 9816 KB n=100
19 Correct 4 ms 9816 KB n=100
20 Correct 5 ms 9820 KB n=100
21 Correct 4 ms 9820 KB n=100
22 Correct 4 ms 9820 KB n=100
23 Correct 11 ms 9820 KB n=100
24 Correct 12 ms 9820 KB n=100
25 Correct 6 ms 9820 KB n=100
26 Correct 3 ms 9820 KB n=12
27 Correct 8 ms 9820 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9816 KB n=5
2 Correct 5 ms 9820 KB n=100
3 Correct 4 ms 9820 KB n=100
4 Correct 4 ms 9820 KB n=100
5 Correct 5 ms 9820 KB n=100
6 Correct 12 ms 9820 KB n=100
7 Correct 12 ms 9820 KB n=100
8 Correct 5 ms 9820 KB n=100
9 Correct 5 ms 9820 KB n=100
10 Correct 9 ms 9816 KB n=100
11 Correct 9 ms 9820 KB n=100
12 Correct 3 ms 9820 KB n=100
13 Correct 4 ms 9820 KB n=100
14 Correct 4 ms 9820 KB n=100
15 Correct 3 ms 9820 KB n=100
16 Correct 5 ms 9820 KB n=100
17 Correct 5 ms 9820 KB n=100
18 Correct 4 ms 9816 KB n=100
19 Correct 4 ms 9816 KB n=100
20 Correct 5 ms 9820 KB n=100
21 Correct 4 ms 9820 KB n=100
22 Correct 4 ms 9820 KB n=100
23 Correct 11 ms 9820 KB n=100
24 Correct 12 ms 9820 KB n=100
25 Correct 6 ms 9820 KB n=100
26 Correct 3 ms 9820 KB n=12
27 Correct 8 ms 9820 KB n=100
28 Correct 7 ms 9820 KB n=500
29 Correct 76 ms 9820 KB n=500
30 Correct 61 ms 10196 KB n=500
31 Correct 66 ms 9816 KB n=500
32 Correct 7 ms 9816 KB n=500
33 Correct 59 ms 9820 KB n=500
34 Correct 6 ms 9820 KB n=500
35 Correct 66 ms 9820 KB n=500
36 Correct 452 ms 9816 KB n=500
37 Correct 435 ms 9944 KB n=500
38 Correct 453 ms 9820 KB n=500
39 Correct 123 ms 9820 KB n=500
40 Correct 125 ms 9948 KB n=500
41 Correct 117 ms 9820 KB n=500
42 Correct 224 ms 9816 KB n=500
43 Correct 239 ms 9820 KB n=500
44 Correct 213 ms 9820 KB n=500
45 Correct 6 ms 9820 KB n=500
46 Correct 78 ms 10068 KB n=500
47 Correct 61 ms 9820 KB n=500
48 Correct 6 ms 9820 KB n=500
49 Correct 57 ms 9820 KB n=500
50 Correct 90 ms 9816 KB n=500
51 Correct 97 ms 9820 KB n=500
52 Correct 132 ms 9816 KB n=500
53 Correct 99 ms 9820 KB n=500
54 Correct 109 ms 9820 KB n=500
55 Correct 15 ms 9820 KB n=278
56 Correct 732 ms 9816 KB n=500
57 Correct 740 ms 9820 KB n=500
58 Correct 253 ms 9972 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9816 KB n=5
2 Correct 5 ms 9820 KB n=100
3 Correct 4 ms 9820 KB n=100
4 Correct 4 ms 9820 KB n=100
5 Correct 5 ms 9820 KB n=100
6 Correct 12 ms 9820 KB n=100
7 Correct 12 ms 9820 KB n=100
8 Correct 5 ms 9820 KB n=100
9 Correct 5 ms 9820 KB n=100
10 Correct 9 ms 9816 KB n=100
11 Correct 9 ms 9820 KB n=100
12 Correct 3 ms 9820 KB n=100
13 Correct 4 ms 9820 KB n=100
14 Correct 4 ms 9820 KB n=100
15 Correct 3 ms 9820 KB n=100
16 Correct 5 ms 9820 KB n=100
17 Correct 5 ms 9820 KB n=100
18 Correct 4 ms 9816 KB n=100
19 Correct 4 ms 9816 KB n=100
20 Correct 5 ms 9820 KB n=100
21 Correct 4 ms 9820 KB n=100
22 Correct 4 ms 9820 KB n=100
23 Correct 11 ms 9820 KB n=100
24 Correct 12 ms 9820 KB n=100
25 Correct 6 ms 9820 KB n=100
26 Correct 3 ms 9820 KB n=12
27 Correct 8 ms 9820 KB n=100
28 Correct 7 ms 9820 KB n=500
29 Correct 76 ms 9820 KB n=500
30 Correct 61 ms 10196 KB n=500
31 Correct 66 ms 9816 KB n=500
32 Correct 7 ms 9816 KB n=500
33 Correct 59 ms 9820 KB n=500
34 Correct 6 ms 9820 KB n=500
35 Correct 66 ms 9820 KB n=500
36 Correct 452 ms 9816 KB n=500
37 Correct 435 ms 9944 KB n=500
38 Correct 453 ms 9820 KB n=500
39 Correct 123 ms 9820 KB n=500
40 Correct 125 ms 9948 KB n=500
41 Correct 117 ms 9820 KB n=500
42 Correct 224 ms 9816 KB n=500
43 Correct 239 ms 9820 KB n=500
44 Correct 213 ms 9820 KB n=500
45 Correct 6 ms 9820 KB n=500
46 Correct 78 ms 10068 KB n=500
47 Correct 61 ms 9820 KB n=500
48 Correct 6 ms 9820 KB n=500
49 Correct 57 ms 9820 KB n=500
50 Correct 90 ms 9816 KB n=500
51 Correct 97 ms 9820 KB n=500
52 Correct 132 ms 9816 KB n=500
53 Correct 99 ms 9820 KB n=500
54 Correct 109 ms 9820 KB n=500
55 Correct 15 ms 9820 KB n=278
56 Correct 732 ms 9816 KB n=500
57 Correct 740 ms 9820 KB n=500
58 Correct 253 ms 9972 KB n=500
59 Correct 28 ms 9820 KB n=2000
60 Correct 1700 ms 10152 KB n=2000
61 Correct 1552 ms 10104 KB n=2000
62 Correct 1031 ms 10076 KB n=2000
63 Correct 40 ms 9820 KB n=2000
64 Correct 1385 ms 10080 KB n=2000
65 Correct 20 ms 9816 KB n=2000
66 Correct 1802 ms 10072 KB n=2000
67 Correct 77 ms 9820 KB n=2000
68 Correct 1444 ms 10072 KB n=2000
69 Execution timed out 4049 ms 10068 KB Time limit exceeded
70 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9816 KB n=5
2 Correct 5 ms 9820 KB n=100
3 Correct 4 ms 9820 KB n=100
4 Correct 4 ms 9820 KB n=100
5 Correct 5 ms 9820 KB n=100
6 Correct 12 ms 9820 KB n=100
7 Correct 12 ms 9820 KB n=100
8 Correct 5 ms 9820 KB n=100
9 Correct 5 ms 9820 KB n=100
10 Correct 9 ms 9816 KB n=100
11 Correct 9 ms 9820 KB n=100
12 Correct 3 ms 9820 KB n=100
13 Correct 4 ms 9820 KB n=100
14 Correct 4 ms 9820 KB n=100
15 Correct 3 ms 9820 KB n=100
16 Correct 5 ms 9820 KB n=100
17 Correct 5 ms 9820 KB n=100
18 Correct 4 ms 9816 KB n=100
19 Correct 4 ms 9816 KB n=100
20 Correct 5 ms 9820 KB n=100
21 Correct 4 ms 9820 KB n=100
22 Correct 4 ms 9820 KB n=100
23 Correct 11 ms 9820 KB n=100
24 Correct 12 ms 9820 KB n=100
25 Correct 6 ms 9820 KB n=100
26 Correct 3 ms 9820 KB n=12
27 Correct 8 ms 9820 KB n=100
28 Correct 7 ms 9820 KB n=500
29 Correct 76 ms 9820 KB n=500
30 Correct 61 ms 10196 KB n=500
31 Correct 66 ms 9816 KB n=500
32 Correct 7 ms 9816 KB n=500
33 Correct 59 ms 9820 KB n=500
34 Correct 6 ms 9820 KB n=500
35 Correct 66 ms 9820 KB n=500
36 Correct 452 ms 9816 KB n=500
37 Correct 435 ms 9944 KB n=500
38 Correct 453 ms 9820 KB n=500
39 Correct 123 ms 9820 KB n=500
40 Correct 125 ms 9948 KB n=500
41 Correct 117 ms 9820 KB n=500
42 Correct 224 ms 9816 KB n=500
43 Correct 239 ms 9820 KB n=500
44 Correct 213 ms 9820 KB n=500
45 Correct 6 ms 9820 KB n=500
46 Correct 78 ms 10068 KB n=500
47 Correct 61 ms 9820 KB n=500
48 Correct 6 ms 9820 KB n=500
49 Correct 57 ms 9820 KB n=500
50 Correct 90 ms 9816 KB n=500
51 Correct 97 ms 9820 KB n=500
52 Correct 132 ms 9816 KB n=500
53 Correct 99 ms 9820 KB n=500
54 Correct 109 ms 9820 KB n=500
55 Correct 15 ms 9820 KB n=278
56 Correct 732 ms 9816 KB n=500
57 Correct 740 ms 9820 KB n=500
58 Correct 253 ms 9972 KB n=500
59 Correct 28 ms 9820 KB n=2000
60 Correct 1700 ms 10152 KB n=2000
61 Correct 1552 ms 10104 KB n=2000
62 Correct 1031 ms 10076 KB n=2000
63 Correct 40 ms 9820 KB n=2000
64 Correct 1385 ms 10080 KB n=2000
65 Correct 20 ms 9816 KB n=2000
66 Correct 1802 ms 10072 KB n=2000
67 Correct 77 ms 9820 KB n=2000
68 Correct 1444 ms 10072 KB n=2000
69 Execution timed out 4049 ms 10068 KB Time limit exceeded
70 Halted 0 ms 0 KB -