답안 #1079501

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079501 2024-08-28T15:53:27 Z Khanhcsp2 Birthday gift (IZhO18_treearray) C++14
56 / 100
2323 ms 83152 KB
#include<bits/stdc++.h>
#define el '\n'
#define fi first
#define sc second
#define int ll
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
const int mod=1e9+7;
const int N=1e5+11;
int n, m, q, up[23][N], h[N], nod[4*N], lc[2003][2003], a[N];
vector<int> adj[N];
void dfs(int u, int pa)
{
    up[0][u]=pa;
    for(int i=1; i<=20; i++) up[i][u]=up[i-1][up[i-1][u]];
    for(int v:adj[u])
    {
        if(v!=pa)
        {
            h[v]=h[u]+1;
            dfs(v, u);
        }
    }
}
int jump(int k, int u)
{
    for(int i=0; i<=20; i++)
    {
        if(k>>i&1) u=up[i][u];
    }
    return u;
}
int lca(int u, int v)
{
    if(h[u]<h[v]) swap(u, v);
    u=jump(h[u]-h[v], u);
    if(u==v) return u;
    for(int i=20; i>=0; i--)
    {
        if(up[i][u]!=up[i][v])
        {
            u=up[i][u];
            v=up[i][v];
        }
    }
    return up[0][u];
}
void glow(int id, int l, int r)
{
    if(l==r)
    {
        nod[id]=a[l];
        return;
    }
    int mid=(l+r)/2;
    glow(id*2, l, mid);
    glow(id*2+1, mid+1, r);
    nod[id]=lc[nod[id*2]][nod[id*2+1]];
}
void update(int id, int l, int r, int pos, int val)
{
    if(r<pos||pos<l) return;
    if(l==r && l==pos)
    {
        nod[id]=val;
        return;
    }
    int mid=(l+r)/2;
    if(pos<=mid) update(id*2, l, mid, pos, val);
    else update(id*2+1, mid+1, r, pos, val);
    nod[id]=lc[nod[id*2]][nod[id*2+1]];
}
int get(int id, int l, int r, int u, int v)
{
    if(r<u || v<l) return n+1;
    if(u<=l && r<=v) return nod[id];
    int mid=(l+r)/2;
    int s1=get(id*2, l, mid, u, v);
    int s2=get(id*2+1, mid+1, r, u, v);
    return lc[s1][s2];
}
void sub1()
{
    while(q--)
    {
        int tt;
        cin >> tt;
        if(tt==1)
        {
            int pos, v;
            cin >> pos >> v;
            update(1, 1, m, pos, v);
            a[pos]=v;
        }
        else
        {
            int l, r, v;
            cin >> l >> r >> v;
            int ch=0, s1, s2;
            for(int i=l; i<=r; i++)
            {
                if(h[a[i]]<h[v] || ch) continue;
                int low=i, high=r;
                while(low<=high)
                {
                    int mid=(low+high)/2;
                    int cc=get(1, 1, m, i, mid);
                    if(h[cc]>h[v]) low=mid+1;
                    else if(h[cc]<h[v]) high=mid-1;
                    else if(cc==v)
                    {
                        ch=1;
                        s1=i;
                        s2=mid;
                        break;
                    }
                    else break;
                }
            }
            if(ch) cout << s1 << ' ' << s2 << el;
            else cout << -1 << ' ' << -1 << el;
        }
    }
}
void sol()
{
    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);
    }
    h[1]=1;
    dfs(1, 0);
    for(int i=1; i<=m; i++) cin >> a[i];
    for(int i=1; i<=n; i++) for(int j=i; j<=n; j++) lc[i][j]=lc[j][i]=lca(i, j);
    for(int i=1; i<=n; i++) lc[n+1][i]=lc[i][n+1]=i;
    glow(1, 1, m);
//    cout << get(1, 1, m, 1, 1) << el;
    sub1();
//    cout << lca(5, 3);
}
signed main()
{
//    freopen("divisor.INP", "r", stdin);
//    freopen("divisor.OUT", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    //cin >> t;
    while(t--)
    {
        sol();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3420 KB n=100
4 Correct 3 ms 3420 KB n=100
5 Correct 2 ms 3420 KB n=100
6 Correct 3 ms 3420 KB n=100
7 Correct 3 ms 3420 KB n=100
8 Correct 3 ms 3420 KB n=100
9 Correct 2 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3416 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 2 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3420 KB n=100
17 Correct 2 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 3 ms 3416 KB n=100
20 Correct 2 ms 3416 KB n=100
21 Correct 2 ms 3420 KB n=100
22 Correct 2 ms 3416 KB n=100
23 Correct 3 ms 3420 KB n=100
24 Correct 3 ms 3420 KB n=100
25 Correct 4 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 3 ms 3420 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3420 KB n=100
4 Correct 3 ms 3420 KB n=100
5 Correct 2 ms 3420 KB n=100
6 Correct 3 ms 3420 KB n=100
7 Correct 3 ms 3420 KB n=100
8 Correct 3 ms 3420 KB n=100
9 Correct 2 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3416 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 2 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3420 KB n=100
17 Correct 2 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 3 ms 3416 KB n=100
20 Correct 2 ms 3416 KB n=100
21 Correct 2 ms 3420 KB n=100
22 Correct 2 ms 3416 KB n=100
23 Correct 3 ms 3420 KB n=100
24 Correct 3 ms 3420 KB n=100
25 Correct 4 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 3 ms 3420 KB n=100
28 Correct 12 ms 7004 KB n=500
29 Correct 15 ms 7004 KB n=500
30 Correct 17 ms 7000 KB n=500
31 Correct 16 ms 6880 KB n=500
32 Correct 12 ms 7000 KB n=500
33 Correct 16 ms 7004 KB n=500
34 Correct 12 ms 7004 KB n=500
35 Correct 15 ms 7004 KB n=500
36 Correct 95 ms 7004 KB n=500
37 Correct 102 ms 7004 KB n=500
38 Correct 108 ms 6956 KB n=500
39 Correct 49 ms 7012 KB n=500
40 Correct 42 ms 7012 KB n=500
41 Correct 42 ms 7004 KB n=500
42 Correct 61 ms 7004 KB n=500
43 Correct 54 ms 7004 KB n=500
44 Correct 74 ms 7004 KB n=500
45 Correct 12 ms 7004 KB n=500
46 Correct 21 ms 7004 KB n=500
47 Correct 16 ms 7008 KB n=500
48 Correct 13 ms 7008 KB n=500
49 Correct 14 ms 7004 KB n=500
50 Correct 41 ms 7000 KB n=500
51 Correct 19 ms 7012 KB n=500
52 Correct 21 ms 7000 KB n=500
53 Correct 27 ms 6840 KB n=500
54 Correct 20 ms 7012 KB n=500
55 Correct 5 ms 4704 KB n=278
56 Correct 88 ms 7024 KB n=500
57 Correct 86 ms 7004 KB n=500
58 Correct 38 ms 7012 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3420 KB n=100
4 Correct 3 ms 3420 KB n=100
5 Correct 2 ms 3420 KB n=100
6 Correct 3 ms 3420 KB n=100
7 Correct 3 ms 3420 KB n=100
8 Correct 3 ms 3420 KB n=100
9 Correct 2 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3416 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 2 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3420 KB n=100
17 Correct 2 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 3 ms 3416 KB n=100
20 Correct 2 ms 3416 KB n=100
21 Correct 2 ms 3420 KB n=100
22 Correct 2 ms 3416 KB n=100
23 Correct 3 ms 3420 KB n=100
24 Correct 3 ms 3420 KB n=100
25 Correct 4 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 3 ms 3420 KB n=100
28 Correct 12 ms 7004 KB n=500
29 Correct 15 ms 7004 KB n=500
30 Correct 17 ms 7000 KB n=500
31 Correct 16 ms 6880 KB n=500
32 Correct 12 ms 7000 KB n=500
33 Correct 16 ms 7004 KB n=500
34 Correct 12 ms 7004 KB n=500
35 Correct 15 ms 7004 KB n=500
36 Correct 95 ms 7004 KB n=500
37 Correct 102 ms 7004 KB n=500
38 Correct 108 ms 6956 KB n=500
39 Correct 49 ms 7012 KB n=500
40 Correct 42 ms 7012 KB n=500
41 Correct 42 ms 7004 KB n=500
42 Correct 61 ms 7004 KB n=500
43 Correct 54 ms 7004 KB n=500
44 Correct 74 ms 7004 KB n=500
45 Correct 12 ms 7004 KB n=500
46 Correct 21 ms 7004 KB n=500
47 Correct 16 ms 7008 KB n=500
48 Correct 13 ms 7008 KB n=500
49 Correct 14 ms 7004 KB n=500
50 Correct 41 ms 7000 KB n=500
51 Correct 19 ms 7012 KB n=500
52 Correct 21 ms 7000 KB n=500
53 Correct 27 ms 6840 KB n=500
54 Correct 20 ms 7012 KB n=500
55 Correct 5 ms 4704 KB n=278
56 Correct 88 ms 7024 KB n=500
57 Correct 86 ms 7004 KB n=500
58 Correct 38 ms 7012 KB n=500
59 Correct 171 ms 34648 KB n=2000
60 Correct 369 ms 34896 KB n=2000
61 Correct 339 ms 34896 KB n=2000
62 Correct 286 ms 34816 KB n=2000
63 Correct 209 ms 34628 KB n=2000
64 Correct 320 ms 34636 KB n=2000
65 Correct 184 ms 34568 KB n=2000
66 Correct 367 ms 34792 KB n=2000
67 Correct 206 ms 34744 KB n=2000
68 Correct 335 ms 34836 KB n=2000
69 Correct 2323 ms 34800 KB n=2000
70 Correct 2241 ms 34700 KB n=2000
71 Correct 2306 ms 34796 KB n=2000
72 Correct 898 ms 34804 KB n=2000
73 Correct 914 ms 34712 KB n=2000
74 Correct 132 ms 32284 KB n=1844
75 Correct 903 ms 34800 KB n=2000
76 Correct 1228 ms 34640 KB n=2000
77 Correct 1211 ms 34884 KB n=2000
78 Correct 1254 ms 34628 KB n=2000
79 Correct 158 ms 34552 KB n=2000
80 Correct 307 ms 34644 KB n=2000
81 Correct 298 ms 34644 KB n=2000
82 Correct 161 ms 34696 KB n=2000
83 Correct 311 ms 34644 KB n=2000
84 Correct 541 ms 35068 KB n=2000
85 Correct 385 ms 34676 KB n=2000
86 Correct 388 ms 34920 KB n=2000
87 Correct 550 ms 35052 KB n=2000
88 Correct 2312 ms 34856 KB n=2000
89 Correct 2181 ms 34648 KB n=2000
90 Correct 440 ms 34872 KB n=2000
91 Correct 1267 ms 34900 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3420 KB n=100
4 Correct 3 ms 3420 KB n=100
5 Correct 2 ms 3420 KB n=100
6 Correct 3 ms 3420 KB n=100
7 Correct 3 ms 3420 KB n=100
8 Correct 3 ms 3420 KB n=100
9 Correct 2 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3416 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 2 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3420 KB n=100
17 Correct 2 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 3 ms 3416 KB n=100
20 Correct 2 ms 3416 KB n=100
21 Correct 2 ms 3420 KB n=100
22 Correct 2 ms 3416 KB n=100
23 Correct 3 ms 3420 KB n=100
24 Correct 3 ms 3420 KB n=100
25 Correct 4 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 3 ms 3420 KB n=100
28 Correct 12 ms 7004 KB n=500
29 Correct 15 ms 7004 KB n=500
30 Correct 17 ms 7000 KB n=500
31 Correct 16 ms 6880 KB n=500
32 Correct 12 ms 7000 KB n=500
33 Correct 16 ms 7004 KB n=500
34 Correct 12 ms 7004 KB n=500
35 Correct 15 ms 7004 KB n=500
36 Correct 95 ms 7004 KB n=500
37 Correct 102 ms 7004 KB n=500
38 Correct 108 ms 6956 KB n=500
39 Correct 49 ms 7012 KB n=500
40 Correct 42 ms 7012 KB n=500
41 Correct 42 ms 7004 KB n=500
42 Correct 61 ms 7004 KB n=500
43 Correct 54 ms 7004 KB n=500
44 Correct 74 ms 7004 KB n=500
45 Correct 12 ms 7004 KB n=500
46 Correct 21 ms 7004 KB n=500
47 Correct 16 ms 7008 KB n=500
48 Correct 13 ms 7008 KB n=500
49 Correct 14 ms 7004 KB n=500
50 Correct 41 ms 7000 KB n=500
51 Correct 19 ms 7012 KB n=500
52 Correct 21 ms 7000 KB n=500
53 Correct 27 ms 6840 KB n=500
54 Correct 20 ms 7012 KB n=500
55 Correct 5 ms 4704 KB n=278
56 Correct 88 ms 7024 KB n=500
57 Correct 86 ms 7004 KB n=500
58 Correct 38 ms 7012 KB n=500
59 Correct 171 ms 34648 KB n=2000
60 Correct 369 ms 34896 KB n=2000
61 Correct 339 ms 34896 KB n=2000
62 Correct 286 ms 34816 KB n=2000
63 Correct 209 ms 34628 KB n=2000
64 Correct 320 ms 34636 KB n=2000
65 Correct 184 ms 34568 KB n=2000
66 Correct 367 ms 34792 KB n=2000
67 Correct 206 ms 34744 KB n=2000
68 Correct 335 ms 34836 KB n=2000
69 Correct 2323 ms 34800 KB n=2000
70 Correct 2241 ms 34700 KB n=2000
71 Correct 2306 ms 34796 KB n=2000
72 Correct 898 ms 34804 KB n=2000
73 Correct 914 ms 34712 KB n=2000
74 Correct 132 ms 32284 KB n=1844
75 Correct 903 ms 34800 KB n=2000
76 Correct 1228 ms 34640 KB n=2000
77 Correct 1211 ms 34884 KB n=2000
78 Correct 1254 ms 34628 KB n=2000
79 Correct 158 ms 34552 KB n=2000
80 Correct 307 ms 34644 KB n=2000
81 Correct 298 ms 34644 KB n=2000
82 Correct 161 ms 34696 KB n=2000
83 Correct 311 ms 34644 KB n=2000
84 Correct 541 ms 35068 KB n=2000
85 Correct 385 ms 34676 KB n=2000
86 Correct 388 ms 34920 KB n=2000
87 Correct 550 ms 35052 KB n=2000
88 Correct 2312 ms 34856 KB n=2000
89 Correct 2181 ms 34648 KB n=2000
90 Correct 440 ms 34872 KB n=2000
91 Correct 1267 ms 34900 KB n=2000
92 Runtime error 148 ms 83152 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -