답안 #1084319

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1084319 2024-09-05T20:36:56 Z kokoue Birthday gift (IZhO18_treearray) C++14
56 / 100
2598 ms 16848 KB
#include<bits/stdc++.h>
#define maxn 2010
#define f first
#define s second
using namespace std;
int n,m,q;
vector<int> edges[maxn];
int lc[maxn][maxn];
int st[15][maxn];
int timer=0;
int tin[maxn],tout[maxn],depth[maxn];
int seq[maxn];
int tree[4*maxn];
void dfs(int start,int par)
{
    st[0][start]=par;
    tin[start]=timer++;
    depth[start]=depth[par]+1;
    for(auto e:edges[start])
    {
        if(e!=par)
        {
            dfs(e,start);
        }
    }
    tout[start]=timer++;
}
bool is_ancestor(int u,int v)
{
    if(tin[u]<tin[v] && tout[u]>tout[v]) return true;
    return false;
}
bool is_low(int u,int v)
{
    if(u==v) return true;
    if(tin[u]<tin[v] && tout[u]>tout[v]) return true;
    return false;
}
int lca(int u,int v)
{
    if(u==v) return u;
    if(is_ancestor(u,v)) return u;
    if(is_ancestor(v,u)) return v;
    if(depth[u]>depth[v]) swap(u,v);
    for(int i=14;i>=0;i--)
    {
        if(!is_ancestor(st[i][u],v))
        {
            u=st[i][u];
        }
    }
    u=st[0][u];
    return u;
}
void build(int node=1,int l=1,int r=m)
{
    if(l==r)
    {
        tree[node]=seq[l];
        return;
    }
    int mid=(l+r)/2;
    int left=node<<1,right=left+1;
    build(left,l,mid);
    build(right,mid+1,r);
    tree[node]=lc[tree[left]][tree[right]];
}

void update(int idx,int val,int node=1,int l=1,int r=m)
{
    if(idx<l || r<idx) return;
    if(l==r)
    {
        tree[node]=val;
        return;
    }

    int mid=(l+r)/2;
    int left=node<<1,right=left+1;

    update(idx,val,left,l,mid);
    update(idx,val,right,mid+1,r);

    tree[node]=lc[tree[left]][tree[right]];
}

int query(int ql,int qr,int node=1,int l=1,int r=m)
{
    if(qr<l || r<ql) return 0;

    if(ql<=l && r<=qr) return tree[node];

    int mid=(l+r)/2;
    int left=node<<1,right=left+1;

    int ans=query(ql,qr,left,l,mid);
    int rans=query(ql,qr,right,mid+1,r);

    return lc[ans][rans];
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    for(int i=0;i<n-1;i++)
    {
        int a,b;
        cin>>a>>b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    dfs(1,1);
    for(int i=1;i<15;i++)
    {
        for(int j=1;j<=n;j++)
        {
            st[i][j]=st[i-1][st[i-1][j]];
        }
    }
    for(int i=1;i<=m;i++)
    {
        cin>>seq[i];
    }
    for(int i=1;i<=n;i++) lc[0][i]=lc[i][0]=lc[i][i]=i;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            lc[i][j]=lc[j][i]=lca(i,j);
        }
    }
    build();
    for(int i=0;i<q;i++)
    {
        int t;
        cin>>t;
        if(t==1)
        {
            int idx,val;
            cin>>idx>>val;
            seq[idx]=val;
            update(idx,val);
        }
        if(t==2)
        {
            int l,r,hole;
            cin>>l>>r>>hole;
            int idxl=-1,idxr=-1;
            bool fl=0;
            for(int j=l;j<=r;j++)
            {
                if(is_ancestor(seq[j],hole) || fl) continue;
                int x=j;
                for(int b=r-j;b>0 && x<r;b/=2)
                {
                    while(x+b<=r && is_low(hole,query(j,x+b)))
                    {
                        x+=b;
                    }
                }
                if(query(j,x)==hole)
                {
                    if(idxl==-1) {idxl=j;idxr=x;continue;}
                    if(idxr-idxl<x-j) {idxl=j;idxr=x;}
                    fl=1;
                }
            }
            cout<<idxl<<" "<<idxr<<"\n";
        }
    }
}
/*
5 4 5
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
2 1 4 1
1 3 5
2 3 4 5
2 1 3 1

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB n=5
2 Correct 2 ms 860 KB n=100
3 Correct 2 ms 856 KB n=100
4 Correct 1 ms 856 KB n=100
5 Correct 1 ms 860 KB n=100
6 Correct 3 ms 860 KB n=100
7 Correct 3 ms 856 KB n=100
8 Correct 2 ms 860 KB n=100
9 Correct 1 ms 860 KB n=100
10 Correct 2 ms 860 KB n=100
11 Correct 2 ms 860 KB n=100
12 Correct 1 ms 860 KB n=100
13 Correct 1 ms 856 KB n=100
14 Correct 1 ms 856 KB n=100
15 Correct 1 ms 860 KB n=100
16 Correct 1 ms 856 KB n=100
17 Correct 2 ms 1116 KB n=100
18 Correct 1 ms 856 KB n=100
19 Correct 1 ms 860 KB n=100
20 Correct 2 ms 860 KB n=100
21 Correct 1 ms 860 KB n=100
22 Correct 1 ms 860 KB n=100
23 Correct 1 ms 856 KB n=100
24 Correct 1 ms 856 KB n=100
25 Correct 1 ms 860 KB n=100
26 Correct 0 ms 600 KB n=12
27 Correct 4 ms 860 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB n=5
2 Correct 2 ms 860 KB n=100
3 Correct 2 ms 856 KB n=100
4 Correct 1 ms 856 KB n=100
5 Correct 1 ms 860 KB n=100
6 Correct 3 ms 860 KB n=100
7 Correct 3 ms 856 KB n=100
8 Correct 2 ms 860 KB n=100
9 Correct 1 ms 860 KB n=100
10 Correct 2 ms 860 KB n=100
11 Correct 2 ms 860 KB n=100
12 Correct 1 ms 860 KB n=100
13 Correct 1 ms 856 KB n=100
14 Correct 1 ms 856 KB n=100
15 Correct 1 ms 860 KB n=100
16 Correct 1 ms 856 KB n=100
17 Correct 2 ms 1116 KB n=100
18 Correct 1 ms 856 KB n=100
19 Correct 1 ms 860 KB n=100
20 Correct 2 ms 860 KB n=100
21 Correct 1 ms 860 KB n=100
22 Correct 1 ms 860 KB n=100
23 Correct 1 ms 856 KB n=100
24 Correct 1 ms 856 KB n=100
25 Correct 1 ms 860 KB n=100
26 Correct 0 ms 600 KB n=12
27 Correct 4 ms 860 KB n=100
28 Correct 8 ms 3420 KB n=500
29 Correct 18 ms 3416 KB n=500
30 Correct 18 ms 3416 KB n=500
31 Correct 18 ms 3596 KB n=500
32 Correct 9 ms 3420 KB n=500
33 Correct 20 ms 3420 KB n=500
34 Correct 7 ms 3416 KB n=500
35 Correct 19 ms 3416 KB n=500
36 Correct 109 ms 3672 KB n=500
37 Correct 108 ms 3416 KB n=500
38 Correct 114 ms 3416 KB n=500
39 Correct 45 ms 3420 KB n=500
40 Correct 43 ms 3648 KB n=500
41 Correct 44 ms 3420 KB n=500
42 Correct 73 ms 3616 KB n=500
43 Correct 59 ms 3420 KB n=500
44 Correct 58 ms 3416 KB n=500
45 Correct 11 ms 3420 KB n=500
46 Correct 22 ms 3420 KB n=500
47 Correct 16 ms 3420 KB n=500
48 Correct 7 ms 3420 KB n=500
49 Correct 13 ms 3420 KB n=500
50 Correct 35 ms 3420 KB n=500
51 Correct 27 ms 3420 KB n=500
52 Correct 20 ms 3420 KB n=500
53 Correct 27 ms 3416 KB n=500
54 Correct 5 ms 3416 KB n=500
55 Correct 5 ms 1884 KB n=278
56 Correct 22 ms 3420 KB n=500
57 Correct 23 ms 3416 KB n=500
58 Correct 94 ms 3620 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB n=5
2 Correct 2 ms 860 KB n=100
3 Correct 2 ms 856 KB n=100
4 Correct 1 ms 856 KB n=100
5 Correct 1 ms 860 KB n=100
6 Correct 3 ms 860 KB n=100
7 Correct 3 ms 856 KB n=100
8 Correct 2 ms 860 KB n=100
9 Correct 1 ms 860 KB n=100
10 Correct 2 ms 860 KB n=100
11 Correct 2 ms 860 KB n=100
12 Correct 1 ms 860 KB n=100
13 Correct 1 ms 856 KB n=100
14 Correct 1 ms 856 KB n=100
15 Correct 1 ms 860 KB n=100
16 Correct 1 ms 856 KB n=100
17 Correct 2 ms 1116 KB n=100
18 Correct 1 ms 856 KB n=100
19 Correct 1 ms 860 KB n=100
20 Correct 2 ms 860 KB n=100
21 Correct 1 ms 860 KB n=100
22 Correct 1 ms 860 KB n=100
23 Correct 1 ms 856 KB n=100
24 Correct 1 ms 856 KB n=100
25 Correct 1 ms 860 KB n=100
26 Correct 0 ms 600 KB n=12
27 Correct 4 ms 860 KB n=100
28 Correct 8 ms 3420 KB n=500
29 Correct 18 ms 3416 KB n=500
30 Correct 18 ms 3416 KB n=500
31 Correct 18 ms 3596 KB n=500
32 Correct 9 ms 3420 KB n=500
33 Correct 20 ms 3420 KB n=500
34 Correct 7 ms 3416 KB n=500
35 Correct 19 ms 3416 KB n=500
36 Correct 109 ms 3672 KB n=500
37 Correct 108 ms 3416 KB n=500
38 Correct 114 ms 3416 KB n=500
39 Correct 45 ms 3420 KB n=500
40 Correct 43 ms 3648 KB n=500
41 Correct 44 ms 3420 KB n=500
42 Correct 73 ms 3616 KB n=500
43 Correct 59 ms 3420 KB n=500
44 Correct 58 ms 3416 KB n=500
45 Correct 11 ms 3420 KB n=500
46 Correct 22 ms 3420 KB n=500
47 Correct 16 ms 3420 KB n=500
48 Correct 7 ms 3420 KB n=500
49 Correct 13 ms 3420 KB n=500
50 Correct 35 ms 3420 KB n=500
51 Correct 27 ms 3420 KB n=500
52 Correct 20 ms 3420 KB n=500
53 Correct 27 ms 3416 KB n=500
54 Correct 5 ms 3416 KB n=500
55 Correct 5 ms 1884 KB n=278
56 Correct 22 ms 3420 KB n=500
57 Correct 23 ms 3416 KB n=500
58 Correct 94 ms 3620 KB n=500
59 Correct 103 ms 16324 KB n=2000
60 Correct 369 ms 16476 KB n=2000
61 Correct 393 ms 16472 KB n=2000
62 Correct 350 ms 16416 KB n=2000
63 Correct 107 ms 16464 KB n=2000
64 Correct 391 ms 16320 KB n=2000
65 Correct 111 ms 16468 KB n=2000
66 Correct 410 ms 16476 KB n=2000
67 Correct 149 ms 16468 KB n=2000
68 Correct 382 ms 16464 KB n=2000
69 Correct 2495 ms 16540 KB n=2000
70 Correct 2529 ms 16468 KB n=2000
71 Correct 2598 ms 16532 KB n=2000
72 Correct 1014 ms 16464 KB n=2000
73 Correct 1039 ms 16356 KB n=2000
74 Correct 83 ms 15292 KB n=1844
75 Correct 1120 ms 16280 KB n=2000
76 Correct 1374 ms 16520 KB n=2000
77 Correct 1342 ms 16416 KB n=2000
78 Correct 1375 ms 16520 KB n=2000
79 Correct 121 ms 16464 KB n=2000
80 Correct 328 ms 16848 KB n=2000
81 Correct 364 ms 16476 KB n=2000
82 Correct 95 ms 16464 KB n=2000
83 Correct 332 ms 16560 KB n=2000
84 Correct 844 ms 16536 KB n=2000
85 Correct 633 ms 16552 KB n=2000
86 Correct 707 ms 16528 KB n=2000
87 Correct 854 ms 16724 KB n=2000
88 Correct 481 ms 16604 KB n=2000
89 Correct 464 ms 16720 KB n=2000
90 Correct 89 ms 16560 KB n=2000
91 Correct 2163 ms 16584 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 600 KB n=5
2 Correct 2 ms 860 KB n=100
3 Correct 2 ms 856 KB n=100
4 Correct 1 ms 856 KB n=100
5 Correct 1 ms 860 KB n=100
6 Correct 3 ms 860 KB n=100
7 Correct 3 ms 856 KB n=100
8 Correct 2 ms 860 KB n=100
9 Correct 1 ms 860 KB n=100
10 Correct 2 ms 860 KB n=100
11 Correct 2 ms 860 KB n=100
12 Correct 1 ms 860 KB n=100
13 Correct 1 ms 856 KB n=100
14 Correct 1 ms 856 KB n=100
15 Correct 1 ms 860 KB n=100
16 Correct 1 ms 856 KB n=100
17 Correct 2 ms 1116 KB n=100
18 Correct 1 ms 856 KB n=100
19 Correct 1 ms 860 KB n=100
20 Correct 2 ms 860 KB n=100
21 Correct 1 ms 860 KB n=100
22 Correct 1 ms 860 KB n=100
23 Correct 1 ms 856 KB n=100
24 Correct 1 ms 856 KB n=100
25 Correct 1 ms 860 KB n=100
26 Correct 0 ms 600 KB n=12
27 Correct 4 ms 860 KB n=100
28 Correct 8 ms 3420 KB n=500
29 Correct 18 ms 3416 KB n=500
30 Correct 18 ms 3416 KB n=500
31 Correct 18 ms 3596 KB n=500
32 Correct 9 ms 3420 KB n=500
33 Correct 20 ms 3420 KB n=500
34 Correct 7 ms 3416 KB n=500
35 Correct 19 ms 3416 KB n=500
36 Correct 109 ms 3672 KB n=500
37 Correct 108 ms 3416 KB n=500
38 Correct 114 ms 3416 KB n=500
39 Correct 45 ms 3420 KB n=500
40 Correct 43 ms 3648 KB n=500
41 Correct 44 ms 3420 KB n=500
42 Correct 73 ms 3616 KB n=500
43 Correct 59 ms 3420 KB n=500
44 Correct 58 ms 3416 KB n=500
45 Correct 11 ms 3420 KB n=500
46 Correct 22 ms 3420 KB n=500
47 Correct 16 ms 3420 KB n=500
48 Correct 7 ms 3420 KB n=500
49 Correct 13 ms 3420 KB n=500
50 Correct 35 ms 3420 KB n=500
51 Correct 27 ms 3420 KB n=500
52 Correct 20 ms 3420 KB n=500
53 Correct 27 ms 3416 KB n=500
54 Correct 5 ms 3416 KB n=500
55 Correct 5 ms 1884 KB n=278
56 Correct 22 ms 3420 KB n=500
57 Correct 23 ms 3416 KB n=500
58 Correct 94 ms 3620 KB n=500
59 Correct 103 ms 16324 KB n=2000
60 Correct 369 ms 16476 KB n=2000
61 Correct 393 ms 16472 KB n=2000
62 Correct 350 ms 16416 KB n=2000
63 Correct 107 ms 16464 KB n=2000
64 Correct 391 ms 16320 KB n=2000
65 Correct 111 ms 16468 KB n=2000
66 Correct 410 ms 16476 KB n=2000
67 Correct 149 ms 16468 KB n=2000
68 Correct 382 ms 16464 KB n=2000
69 Correct 2495 ms 16540 KB n=2000
70 Correct 2529 ms 16468 KB n=2000
71 Correct 2598 ms 16532 KB n=2000
72 Correct 1014 ms 16464 KB n=2000
73 Correct 1039 ms 16356 KB n=2000
74 Correct 83 ms 15292 KB n=1844
75 Correct 1120 ms 16280 KB n=2000
76 Correct 1374 ms 16520 KB n=2000
77 Correct 1342 ms 16416 KB n=2000
78 Correct 1375 ms 16520 KB n=2000
79 Correct 121 ms 16464 KB n=2000
80 Correct 328 ms 16848 KB n=2000
81 Correct 364 ms 16476 KB n=2000
82 Correct 95 ms 16464 KB n=2000
83 Correct 332 ms 16560 KB n=2000
84 Correct 844 ms 16536 KB n=2000
85 Correct 633 ms 16552 KB n=2000
86 Correct 707 ms 16528 KB n=2000
87 Correct 854 ms 16724 KB n=2000
88 Correct 481 ms 16604 KB n=2000
89 Correct 464 ms 16720 KB n=2000
90 Correct 89 ms 16560 KB n=2000
91 Correct 2163 ms 16584 KB n=2000
92 Runtime error 1 ms 600 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -