답안 #751439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
751439 2023-05-31T15:00:30 Z sofija6 Birthday gift (IZhO18_treearray) C++14
100 / 100
1160 ms 99760 KB
#include <bits/stdc++.h>
#define ll long long
#define MAXN 200010
#define logn 18
using namespace std;
vector<ll> G[MAXN];
set<ll> poslca[MAXN],posv[MAXN];
ll a[MAXN],up[MAXN][logn],in[MAXN],out[MAXN],t;
void DFS(ll i,ll p)
{
    in[i]=t++;
    up[i][0]=p;
    for (ll j=1;j<logn;j++)
        up[i][j]=up[up[i][j-1]][j-1];
    for (ll next : G[i])
    {
        if (next!=p)
            DFS(next,i);
    }
    out[i]=t++;
}
bool Is_Ancestor(ll u,ll v)
{
    return in[u]<=in[v] && out[u]>=out[v];
}
ll LCA(ll u,ll v)
{
    if (Is_Ancestor(u,v))
        return u;
    if (Is_Ancestor(v,u))
        return v;
    for (ll i=logn-1;i>=0;i--)
    {
        if (!Is_Ancestor(up[u][i],v))
            u=up[u][i];
    }
    return up[u][0];
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,m,q,u,v,type,pos,l,r;
    cin >> n >> m >> q;
    for (ll i=1;i<n;i++)
    {
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    DFS(1,1);
    for (ll i=1;i<=m;i++)
    {
        cin >> a[i];
        if (i>1)
            poslca[LCA(a[i-1],a[i])].insert(i-1);
        posv[a[i]].insert(i);
    }
    while (q--)
    {
        cin >> type;
        if (type==1)
        {
            cin >> pos >> v;
            if (pos>1)
            {
                poslca[LCA(a[pos-1],a[pos])].erase(pos-1);
                poslca[LCA(a[pos-1],v)].insert(pos-1);
            }
            if (pos<m)
            {
                poslca[LCA(a[pos],a[pos+1])].erase(pos);
                poslca[LCA(v,a[pos+1])].insert(pos);
            }
            posv[a[pos]].erase(pos);
            posv[v].insert(pos);
            a[pos]=v;
            continue;
        }
        cin >> l >> r >> v;
        auto it=posv[v].lower_bound(l);
        if (it!=posv[v].end() && *it<=r)
        {
            cout << *it << " " << *it << "\n";
            continue;
        }
        it=poslca[v].lower_bound(l);
        if (it==poslca[v].end() || *it>=r)
            cout << -1 << " " << -1 << "\n";
        else
            cout << *it << " " << *it+1 << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 13 ms 23868 KB n=100
4 Correct 11 ms 23812 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23812 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23824 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23792 KB n=100
11 Correct 15 ms 23816 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23812 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 14 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 11 ms 23760 KB n=100
18 Correct 14 ms 23764 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 16 ms 23816 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 12 ms 23856 KB n=100
24 Correct 12 ms 23800 KB n=100
25 Correct 16 ms 23820 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 13 ms 23868 KB n=100
4 Correct 11 ms 23812 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23812 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23824 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23792 KB n=100
11 Correct 15 ms 23816 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23812 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 14 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 11 ms 23760 KB n=100
18 Correct 14 ms 23764 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 16 ms 23816 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 12 ms 23856 KB n=100
24 Correct 12 ms 23800 KB n=100
25 Correct 16 ms 23820 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 13 ms 23952 KB n=500
29 Correct 14 ms 23956 KB n=500
30 Correct 14 ms 23952 KB n=500
31 Correct 15 ms 23936 KB n=500
32 Correct 15 ms 23892 KB n=500
33 Correct 14 ms 23904 KB n=500
34 Correct 14 ms 23892 KB n=500
35 Correct 13 ms 23984 KB n=500
36 Correct 14 ms 23892 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 14 ms 23892 KB n=500
39 Correct 17 ms 23944 KB n=500
40 Correct 22 ms 23956 KB n=500
41 Correct 15 ms 23892 KB n=500
42 Correct 14 ms 23944 KB n=500
43 Correct 13 ms 23956 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 13 ms 23892 KB n=500
46 Correct 13 ms 23956 KB n=500
47 Correct 14 ms 23904 KB n=500
48 Correct 13 ms 23892 KB n=500
49 Correct 14 ms 23892 KB n=500
50 Correct 14 ms 23952 KB n=500
51 Correct 18 ms 23896 KB n=500
52 Correct 14 ms 23904 KB n=500
53 Correct 15 ms 23960 KB n=500
54 Correct 13 ms 23948 KB n=500
55 Correct 18 ms 23944 KB n=278
56 Correct 17 ms 23896 KB n=500
57 Correct 12 ms 23948 KB n=500
58 Correct 14 ms 23892 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 13 ms 23868 KB n=100
4 Correct 11 ms 23812 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23812 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23824 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23792 KB n=100
11 Correct 15 ms 23816 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23812 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 14 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 11 ms 23760 KB n=100
18 Correct 14 ms 23764 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 16 ms 23816 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 12 ms 23856 KB n=100
24 Correct 12 ms 23800 KB n=100
25 Correct 16 ms 23820 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 13 ms 23952 KB n=500
29 Correct 14 ms 23956 KB n=500
30 Correct 14 ms 23952 KB n=500
31 Correct 15 ms 23936 KB n=500
32 Correct 15 ms 23892 KB n=500
33 Correct 14 ms 23904 KB n=500
34 Correct 14 ms 23892 KB n=500
35 Correct 13 ms 23984 KB n=500
36 Correct 14 ms 23892 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 14 ms 23892 KB n=500
39 Correct 17 ms 23944 KB n=500
40 Correct 22 ms 23956 KB n=500
41 Correct 15 ms 23892 KB n=500
42 Correct 14 ms 23944 KB n=500
43 Correct 13 ms 23956 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 13 ms 23892 KB n=500
46 Correct 13 ms 23956 KB n=500
47 Correct 14 ms 23904 KB n=500
48 Correct 13 ms 23892 KB n=500
49 Correct 14 ms 23892 KB n=500
50 Correct 14 ms 23952 KB n=500
51 Correct 18 ms 23896 KB n=500
52 Correct 14 ms 23904 KB n=500
53 Correct 15 ms 23960 KB n=500
54 Correct 13 ms 23948 KB n=500
55 Correct 18 ms 23944 KB n=278
56 Correct 17 ms 23896 KB n=500
57 Correct 12 ms 23948 KB n=500
58 Correct 14 ms 23892 KB n=500
59 Correct 15 ms 24468 KB n=2000
60 Correct 16 ms 24532 KB n=2000
61 Correct 15 ms 24532 KB n=2000
62 Correct 16 ms 24404 KB n=2000
63 Correct 17 ms 24380 KB n=2000
64 Correct 16 ms 24404 KB n=2000
65 Correct 18 ms 24424 KB n=2000
66 Correct 18 ms 24524 KB n=2000
67 Correct 20 ms 24428 KB n=2000
68 Correct 16 ms 24532 KB n=2000
69 Correct 16 ms 24460 KB n=2000
70 Correct 15 ms 24424 KB n=2000
71 Correct 15 ms 24416 KB n=2000
72 Correct 14 ms 24404 KB n=2000
73 Correct 14 ms 24460 KB n=2000
74 Correct 15 ms 24356 KB n=1844
75 Correct 19 ms 24464 KB n=2000
76 Correct 16 ms 24416 KB n=2000
77 Correct 14 ms 24404 KB n=2000
78 Correct 16 ms 24372 KB n=2000
79 Correct 16 ms 24488 KB n=2000
80 Correct 15 ms 24532 KB n=2000
81 Correct 15 ms 24472 KB n=2000
82 Correct 17 ms 24404 KB n=2000
83 Correct 15 ms 24532 KB n=2000
84 Correct 17 ms 24464 KB n=2000
85 Correct 25 ms 24440 KB n=2000
86 Correct 19 ms 24424 KB n=2000
87 Correct 18 ms 24468 KB n=2000
88 Correct 15 ms 24532 KB n=2000
89 Correct 15 ms 24532 KB n=2000
90 Correct 17 ms 24532 KB n=2000
91 Correct 15 ms 24404 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB n=5
2 Correct 11 ms 23764 KB n=100
3 Correct 13 ms 23868 KB n=100
4 Correct 11 ms 23812 KB n=100
5 Correct 12 ms 23764 KB n=100
6 Correct 12 ms 23812 KB n=100
7 Correct 12 ms 23816 KB n=100
8 Correct 12 ms 23824 KB n=100
9 Correct 12 ms 23764 KB n=100
10 Correct 12 ms 23792 KB n=100
11 Correct 15 ms 23816 KB n=100
12 Correct 13 ms 23820 KB n=100
13 Correct 12 ms 23812 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 14 ms 23764 KB n=100
16 Correct 12 ms 23764 KB n=100
17 Correct 11 ms 23760 KB n=100
18 Correct 14 ms 23764 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 12 ms 23764 KB n=100
21 Correct 16 ms 23816 KB n=100
22 Correct 13 ms 23764 KB n=100
23 Correct 12 ms 23856 KB n=100
24 Correct 12 ms 23800 KB n=100
25 Correct 16 ms 23820 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 13 ms 23952 KB n=500
29 Correct 14 ms 23956 KB n=500
30 Correct 14 ms 23952 KB n=500
31 Correct 15 ms 23936 KB n=500
32 Correct 15 ms 23892 KB n=500
33 Correct 14 ms 23904 KB n=500
34 Correct 14 ms 23892 KB n=500
35 Correct 13 ms 23984 KB n=500
36 Correct 14 ms 23892 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 14 ms 23892 KB n=500
39 Correct 17 ms 23944 KB n=500
40 Correct 22 ms 23956 KB n=500
41 Correct 15 ms 23892 KB n=500
42 Correct 14 ms 23944 KB n=500
43 Correct 13 ms 23956 KB n=500
44 Correct 13 ms 23892 KB n=500
45 Correct 13 ms 23892 KB n=500
46 Correct 13 ms 23956 KB n=500
47 Correct 14 ms 23904 KB n=500
48 Correct 13 ms 23892 KB n=500
49 Correct 14 ms 23892 KB n=500
50 Correct 14 ms 23952 KB n=500
51 Correct 18 ms 23896 KB n=500
52 Correct 14 ms 23904 KB n=500
53 Correct 15 ms 23960 KB n=500
54 Correct 13 ms 23948 KB n=500
55 Correct 18 ms 23944 KB n=278
56 Correct 17 ms 23896 KB n=500
57 Correct 12 ms 23948 KB n=500
58 Correct 14 ms 23892 KB n=500
59 Correct 15 ms 24468 KB n=2000
60 Correct 16 ms 24532 KB n=2000
61 Correct 15 ms 24532 KB n=2000
62 Correct 16 ms 24404 KB n=2000
63 Correct 17 ms 24380 KB n=2000
64 Correct 16 ms 24404 KB n=2000
65 Correct 18 ms 24424 KB n=2000
66 Correct 18 ms 24524 KB n=2000
67 Correct 20 ms 24428 KB n=2000
68 Correct 16 ms 24532 KB n=2000
69 Correct 16 ms 24460 KB n=2000
70 Correct 15 ms 24424 KB n=2000
71 Correct 15 ms 24416 KB n=2000
72 Correct 14 ms 24404 KB n=2000
73 Correct 14 ms 24460 KB n=2000
74 Correct 15 ms 24356 KB n=1844
75 Correct 19 ms 24464 KB n=2000
76 Correct 16 ms 24416 KB n=2000
77 Correct 14 ms 24404 KB n=2000
78 Correct 16 ms 24372 KB n=2000
79 Correct 16 ms 24488 KB n=2000
80 Correct 15 ms 24532 KB n=2000
81 Correct 15 ms 24472 KB n=2000
82 Correct 17 ms 24404 KB n=2000
83 Correct 15 ms 24532 KB n=2000
84 Correct 17 ms 24464 KB n=2000
85 Correct 25 ms 24440 KB n=2000
86 Correct 19 ms 24424 KB n=2000
87 Correct 18 ms 24468 KB n=2000
88 Correct 15 ms 24532 KB n=2000
89 Correct 15 ms 24532 KB n=2000
90 Correct 17 ms 24532 KB n=2000
91 Correct 15 ms 24404 KB n=2000
92 Correct 784 ms 91076 KB n=200000
93 Correct 911 ms 95924 KB n=200000
94 Correct 636 ms 98776 KB n=200000
95 Correct 747 ms 90748 KB n=200000
96 Correct 948 ms 90732 KB n=200000
97 Correct 999 ms 95088 KB n=200000
98 Correct 794 ms 90752 KB n=200000
99 Correct 891 ms 91240 KB n=200000
100 Correct 693 ms 90896 KB n=200000
101 Correct 577 ms 99760 KB n=200000
102 Correct 472 ms 92216 KB n=200000
103 Correct 636 ms 92176 KB n=200000
104 Correct 449 ms 92188 KB n=200000
105 Correct 462 ms 92592 KB n=200000
106 Correct 516 ms 92624 KB n=200000
107 Correct 456 ms 92628 KB n=200000
108 Correct 780 ms 91132 KB n=200000
109 Correct 836 ms 91072 KB n=200000
110 Correct 827 ms 91188 KB n=200000
111 Correct 746 ms 90328 KB n=200000
112 Correct 615 ms 98908 KB n=200000
113 Correct 917 ms 95032 KB n=200000
114 Correct 733 ms 90388 KB n=200000
115 Correct 1160 ms 93020 KB n=200000
116 Correct 711 ms 90872 KB n=200000
117 Correct 573 ms 99236 KB n=200000
118 Correct 1066 ms 93916 KB n=200000
119 Correct 694 ms 91200 KB n=200000
120 Correct 483 ms 98676 KB n=200000
121 Correct 486 ms 98780 KB n=200000
122 Correct 483 ms 98968 KB n=200000
123 Correct 474 ms 92352 KB n=200000
124 Correct 158 ms 40884 KB n=25264