답안 #446184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446184 2021-07-21T07:42:54 Z ak2006 Birthday gift (IZhO18_treearray) C++14
100 / 100
1289 ms 92588 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
const ll mod = 1e9 + 7,inf = 1e18;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void setIO()
{
    fast;
}

int n = 2e5 + 5,m,q,TIMER;
vvi adj(n);
vvi anc(n,vi(21));
vi in(n),out(n);

void dfs(int u,int p)
{

    in[u] = ++TIMER;
    anc[u][0] = p;
    for (int i = 1;i<=20;i++)anc[u][i] = anc[anc[u][i - 1]][i - 1];
    for (int v:adj[u]){
        if (v == p)continue;
        dfs(v,u);
    }
    out[u] = ++TIMER;
}
bool is_ancestor(int u,int v)
{
    return in[u] <= in[v] && out[u] >= out[v];
}
int LCA(int u,int v)
{
    if (in[u] > in[v])swap(u,v);
    if (is_ancestor(u,v))return u;
    //cout<<u<<" "<<v<<endl;
    for (int i = 20;i>=0;i--){
        if (!is_ancestor(anc[v][i],u))v = anc[v][i];
    }
    return anc[v][0];
}

int main()
{
    setIO();
    cin>>n>>m>>q;
    vi a(m + 1);
    vector<set<int>>lcas(n + 1);
    vector<set<int>>positions(n + 1);

    for (int i = 1;i<=n - 1;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],positions[a[i]].insert(i);
    dfs(1,1);
    
    for (int i = 1;i<=m - 1;i++)
        lcas[LCA(a[i],a[i + 1])].insert(i);

    // for (int i = 1;i<=n;i++){
    //     cout<<lcas[i].size()<<endl;
    //     for (auto it:lcas[i])cout<<it<<" ";
    //     cout<<endl;
    // }
    
    while (q--){
        int type,v;
        cin>>type;

        if (type == 1){
            int pos;
            cin>>pos>>v;

            if (pos >= 2)lcas[LCA(a[pos - 1],a[pos])].erase(pos - 1);
            if (pos + 1 <= m)lcas[LCA(a[pos],a[pos + 1])].erase(pos);

            positions[a[pos]].erase(pos);
            a[pos] = v;
            positions[a[pos]].insert(pos);


            if (pos >= 2)lcas[LCA(a[pos - 1],a[pos])].insert(pos - 1);
            if (pos + 1 <= m)lcas[LCA(a[pos],a[pos + 1])].insert(pos);

        }
        else{
            int l,r;
            cin>>l>>r>>v;

            auto it = positions[v].lower_bound(l);
            auto it2 = lcas[v].lower_bound(l);
            
            if (it != positions[v].end() && *it<= r)
                cout<<*it<<" "<<*it<<'\n';
            else if (it2 != lcas[v].end() && *it2 + 1 <= r)
                cout<<*it2<<" "<<*it2 + 1<<'\n';
            else cout<<"-1 -1"<<'\n';
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 30028 KB n=5
2 Correct 26 ms 29988 KB n=100
3 Correct 25 ms 30032 KB n=100
4 Correct 25 ms 30024 KB n=100
5 Correct 25 ms 30048 KB n=100
6 Correct 28 ms 29972 KB n=100
7 Correct 25 ms 30028 KB n=100
8 Correct 25 ms 30072 KB n=100
9 Correct 25 ms 30028 KB n=100
10 Correct 25 ms 30020 KB n=100
11 Correct 25 ms 30040 KB n=100
12 Correct 25 ms 29968 KB n=100
13 Correct 25 ms 30084 KB n=100
14 Correct 24 ms 30068 KB n=100
15 Correct 25 ms 30028 KB n=100
16 Correct 25 ms 30064 KB n=100
17 Correct 25 ms 29988 KB n=100
18 Correct 25 ms 30072 KB n=100
19 Correct 25 ms 30028 KB n=100
20 Correct 25 ms 30088 KB n=100
21 Correct 25 ms 30044 KB n=100
22 Correct 25 ms 30012 KB n=100
23 Correct 25 ms 30028 KB n=100
24 Correct 25 ms 29976 KB n=100
25 Correct 25 ms 30056 KB n=100
26 Correct 25 ms 30028 KB n=12
27 Correct 26 ms 30028 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 30028 KB n=5
2 Correct 26 ms 29988 KB n=100
3 Correct 25 ms 30032 KB n=100
4 Correct 25 ms 30024 KB n=100
5 Correct 25 ms 30048 KB n=100
6 Correct 28 ms 29972 KB n=100
7 Correct 25 ms 30028 KB n=100
8 Correct 25 ms 30072 KB n=100
9 Correct 25 ms 30028 KB n=100
10 Correct 25 ms 30020 KB n=100
11 Correct 25 ms 30040 KB n=100
12 Correct 25 ms 29968 KB n=100
13 Correct 25 ms 30084 KB n=100
14 Correct 24 ms 30068 KB n=100
15 Correct 25 ms 30028 KB n=100
16 Correct 25 ms 30064 KB n=100
17 Correct 25 ms 29988 KB n=100
18 Correct 25 ms 30072 KB n=100
19 Correct 25 ms 30028 KB n=100
20 Correct 25 ms 30088 KB n=100
21 Correct 25 ms 30044 KB n=100
22 Correct 25 ms 30012 KB n=100
23 Correct 25 ms 30028 KB n=100
24 Correct 25 ms 29976 KB n=100
25 Correct 25 ms 30056 KB n=100
26 Correct 25 ms 30028 KB n=12
27 Correct 26 ms 30028 KB n=100
28 Correct 26 ms 30128 KB n=500
29 Correct 26 ms 30220 KB n=500
30 Correct 25 ms 30092 KB n=500
31 Correct 26 ms 30144 KB n=500
32 Correct 32 ms 30084 KB n=500
33 Correct 26 ms 30080 KB n=500
34 Correct 25 ms 30096 KB n=500
35 Correct 26 ms 30148 KB n=500
36 Correct 26 ms 30120 KB n=500
37 Correct 26 ms 30092 KB n=500
38 Correct 26 ms 30112 KB n=500
39 Correct 26 ms 30100 KB n=500
40 Correct 26 ms 30080 KB n=500
41 Correct 27 ms 30088 KB n=500
42 Correct 31 ms 30116 KB n=500
43 Correct 31 ms 30156 KB n=500
44 Correct 30 ms 30096 KB n=500
45 Correct 26 ms 30108 KB n=500
46 Correct 25 ms 30212 KB n=500
47 Correct 25 ms 30148 KB n=500
48 Correct 27 ms 30084 KB n=500
49 Correct 26 ms 30216 KB n=500
50 Correct 26 ms 30100 KB n=500
51 Correct 26 ms 30208 KB n=500
52 Correct 25 ms 30216 KB n=500
53 Correct 26 ms 30148 KB n=500
54 Correct 27 ms 30156 KB n=500
55 Correct 25 ms 30080 KB n=278
56 Correct 27 ms 30128 KB n=500
57 Correct 25 ms 30156 KB n=500
58 Correct 25 ms 30088 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 30028 KB n=5
2 Correct 26 ms 29988 KB n=100
3 Correct 25 ms 30032 KB n=100
4 Correct 25 ms 30024 KB n=100
5 Correct 25 ms 30048 KB n=100
6 Correct 28 ms 29972 KB n=100
7 Correct 25 ms 30028 KB n=100
8 Correct 25 ms 30072 KB n=100
9 Correct 25 ms 30028 KB n=100
10 Correct 25 ms 30020 KB n=100
11 Correct 25 ms 30040 KB n=100
12 Correct 25 ms 29968 KB n=100
13 Correct 25 ms 30084 KB n=100
14 Correct 24 ms 30068 KB n=100
15 Correct 25 ms 30028 KB n=100
16 Correct 25 ms 30064 KB n=100
17 Correct 25 ms 29988 KB n=100
18 Correct 25 ms 30072 KB n=100
19 Correct 25 ms 30028 KB n=100
20 Correct 25 ms 30088 KB n=100
21 Correct 25 ms 30044 KB n=100
22 Correct 25 ms 30012 KB n=100
23 Correct 25 ms 30028 KB n=100
24 Correct 25 ms 29976 KB n=100
25 Correct 25 ms 30056 KB n=100
26 Correct 25 ms 30028 KB n=12
27 Correct 26 ms 30028 KB n=100
28 Correct 26 ms 30128 KB n=500
29 Correct 26 ms 30220 KB n=500
30 Correct 25 ms 30092 KB n=500
31 Correct 26 ms 30144 KB n=500
32 Correct 32 ms 30084 KB n=500
33 Correct 26 ms 30080 KB n=500
34 Correct 25 ms 30096 KB n=500
35 Correct 26 ms 30148 KB n=500
36 Correct 26 ms 30120 KB n=500
37 Correct 26 ms 30092 KB n=500
38 Correct 26 ms 30112 KB n=500
39 Correct 26 ms 30100 KB n=500
40 Correct 26 ms 30080 KB n=500
41 Correct 27 ms 30088 KB n=500
42 Correct 31 ms 30116 KB n=500
43 Correct 31 ms 30156 KB n=500
44 Correct 30 ms 30096 KB n=500
45 Correct 26 ms 30108 KB n=500
46 Correct 25 ms 30212 KB n=500
47 Correct 25 ms 30148 KB n=500
48 Correct 27 ms 30084 KB n=500
49 Correct 26 ms 30216 KB n=500
50 Correct 26 ms 30100 KB n=500
51 Correct 26 ms 30208 KB n=500
52 Correct 25 ms 30216 KB n=500
53 Correct 26 ms 30148 KB n=500
54 Correct 27 ms 30156 KB n=500
55 Correct 25 ms 30080 KB n=278
56 Correct 27 ms 30128 KB n=500
57 Correct 25 ms 30156 KB n=500
58 Correct 25 ms 30088 KB n=500
59 Correct 28 ms 30540 KB n=2000
60 Correct 28 ms 30612 KB n=2000
61 Correct 28 ms 30532 KB n=2000
62 Correct 29 ms 30540 KB n=2000
63 Correct 32 ms 30708 KB n=2000
64 Correct 30 ms 30540 KB n=2000
65 Correct 29 ms 30532 KB n=2000
66 Correct 28 ms 30600 KB n=2000
67 Correct 29 ms 30472 KB n=2000
68 Correct 29 ms 30624 KB n=2000
69 Correct 28 ms 30484 KB n=2000
70 Correct 30 ms 30512 KB n=2000
71 Correct 30 ms 30572 KB n=2000
72 Correct 28 ms 30476 KB n=2000
73 Correct 28 ms 30480 KB n=2000
74 Correct 28 ms 30528 KB n=1844
75 Correct 28 ms 30596 KB n=2000
76 Correct 32 ms 30508 KB n=2000
77 Correct 29 ms 30564 KB n=2000
78 Correct 29 ms 30484 KB n=2000
79 Correct 29 ms 30496 KB n=2000
80 Correct 30 ms 30608 KB n=2000
81 Correct 29 ms 30600 KB n=2000
82 Correct 29 ms 30536 KB n=2000
83 Correct 28 ms 30620 KB n=2000
84 Correct 29 ms 30592 KB n=2000
85 Correct 28 ms 30516 KB n=2000
86 Correct 28 ms 30532 KB n=2000
87 Correct 28 ms 30532 KB n=2000
88 Correct 27 ms 30600 KB n=2000
89 Correct 28 ms 30600 KB n=2000
90 Correct 28 ms 30560 KB n=2000
91 Correct 30 ms 30484 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 30028 KB n=5
2 Correct 26 ms 29988 KB n=100
3 Correct 25 ms 30032 KB n=100
4 Correct 25 ms 30024 KB n=100
5 Correct 25 ms 30048 KB n=100
6 Correct 28 ms 29972 KB n=100
7 Correct 25 ms 30028 KB n=100
8 Correct 25 ms 30072 KB n=100
9 Correct 25 ms 30028 KB n=100
10 Correct 25 ms 30020 KB n=100
11 Correct 25 ms 30040 KB n=100
12 Correct 25 ms 29968 KB n=100
13 Correct 25 ms 30084 KB n=100
14 Correct 24 ms 30068 KB n=100
15 Correct 25 ms 30028 KB n=100
16 Correct 25 ms 30064 KB n=100
17 Correct 25 ms 29988 KB n=100
18 Correct 25 ms 30072 KB n=100
19 Correct 25 ms 30028 KB n=100
20 Correct 25 ms 30088 KB n=100
21 Correct 25 ms 30044 KB n=100
22 Correct 25 ms 30012 KB n=100
23 Correct 25 ms 30028 KB n=100
24 Correct 25 ms 29976 KB n=100
25 Correct 25 ms 30056 KB n=100
26 Correct 25 ms 30028 KB n=12
27 Correct 26 ms 30028 KB n=100
28 Correct 26 ms 30128 KB n=500
29 Correct 26 ms 30220 KB n=500
30 Correct 25 ms 30092 KB n=500
31 Correct 26 ms 30144 KB n=500
32 Correct 32 ms 30084 KB n=500
33 Correct 26 ms 30080 KB n=500
34 Correct 25 ms 30096 KB n=500
35 Correct 26 ms 30148 KB n=500
36 Correct 26 ms 30120 KB n=500
37 Correct 26 ms 30092 KB n=500
38 Correct 26 ms 30112 KB n=500
39 Correct 26 ms 30100 KB n=500
40 Correct 26 ms 30080 KB n=500
41 Correct 27 ms 30088 KB n=500
42 Correct 31 ms 30116 KB n=500
43 Correct 31 ms 30156 KB n=500
44 Correct 30 ms 30096 KB n=500
45 Correct 26 ms 30108 KB n=500
46 Correct 25 ms 30212 KB n=500
47 Correct 25 ms 30148 KB n=500
48 Correct 27 ms 30084 KB n=500
49 Correct 26 ms 30216 KB n=500
50 Correct 26 ms 30100 KB n=500
51 Correct 26 ms 30208 KB n=500
52 Correct 25 ms 30216 KB n=500
53 Correct 26 ms 30148 KB n=500
54 Correct 27 ms 30156 KB n=500
55 Correct 25 ms 30080 KB n=278
56 Correct 27 ms 30128 KB n=500
57 Correct 25 ms 30156 KB n=500
58 Correct 25 ms 30088 KB n=500
59 Correct 28 ms 30540 KB n=2000
60 Correct 28 ms 30612 KB n=2000
61 Correct 28 ms 30532 KB n=2000
62 Correct 29 ms 30540 KB n=2000
63 Correct 32 ms 30708 KB n=2000
64 Correct 30 ms 30540 KB n=2000
65 Correct 29 ms 30532 KB n=2000
66 Correct 28 ms 30600 KB n=2000
67 Correct 29 ms 30472 KB n=2000
68 Correct 29 ms 30624 KB n=2000
69 Correct 28 ms 30484 KB n=2000
70 Correct 30 ms 30512 KB n=2000
71 Correct 30 ms 30572 KB n=2000
72 Correct 28 ms 30476 KB n=2000
73 Correct 28 ms 30480 KB n=2000
74 Correct 28 ms 30528 KB n=1844
75 Correct 28 ms 30596 KB n=2000
76 Correct 32 ms 30508 KB n=2000
77 Correct 29 ms 30564 KB n=2000
78 Correct 29 ms 30484 KB n=2000
79 Correct 29 ms 30496 KB n=2000
80 Correct 30 ms 30608 KB n=2000
81 Correct 29 ms 30600 KB n=2000
82 Correct 29 ms 30536 KB n=2000
83 Correct 28 ms 30620 KB n=2000
84 Correct 29 ms 30592 KB n=2000
85 Correct 28 ms 30516 KB n=2000
86 Correct 28 ms 30532 KB n=2000
87 Correct 28 ms 30532 KB n=2000
88 Correct 27 ms 30600 KB n=2000
89 Correct 28 ms 30600 KB n=2000
90 Correct 28 ms 30560 KB n=2000
91 Correct 30 ms 30484 KB n=2000
92 Correct 832 ms 83144 KB n=200000
93 Correct 939 ms 87960 KB n=200000
94 Correct 740 ms 91320 KB n=200000
95 Correct 797 ms 82872 KB n=200000
96 Correct 821 ms 83012 KB n=200000
97 Correct 1055 ms 87208 KB n=200000
98 Correct 847 ms 82964 KB n=200000
99 Correct 983 ms 83140 KB n=200000
100 Correct 849 ms 83004 KB n=200000
101 Correct 620 ms 92588 KB n=200000
102 Correct 487 ms 84316 KB n=200000
103 Correct 502 ms 84164 KB n=200000
104 Correct 518 ms 84220 KB n=200000
105 Correct 498 ms 84548 KB n=200000
106 Correct 506 ms 84552 KB n=200000
107 Correct 507 ms 84676 KB n=200000
108 Correct 859 ms 83164 KB n=200000
109 Correct 859 ms 83156 KB n=200000
110 Correct 881 ms 83028 KB n=200000
111 Correct 827 ms 82520 KB n=200000
112 Correct 661 ms 91724 KB n=200000
113 Correct 1000 ms 87024 KB n=200000
114 Correct 855 ms 82468 KB n=200000
115 Correct 1289 ms 85036 KB n=200000
116 Correct 786 ms 83336 KB n=200000
117 Correct 630 ms 92128 KB n=200000
118 Correct 1111 ms 85976 KB n=200000
119 Correct 778 ms 83128 KB n=200000
120 Correct 563 ms 91664 KB n=200000
121 Correct 562 ms 91624 KB n=200000
122 Correct 613 ms 92012 KB n=200000
123 Correct 532 ms 84308 KB n=200000
124 Correct 188 ms 45252 KB n=25264