답안 #340710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
340710 2020-12-28T09:03:57 Z Dilshod_Imomov Birthday gift (IZhO18_treearray) C++17
100 / 100
1252 ms 100588 KB
# include <bits/stdc++.h>
# define speed ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
# define inht long long
# define fi first
# define se second
 
using namespace std;
 
const int N = 2e5 + 7;
const int mod = 1e9 + 7;

int a[N], up[N][40], tin[N], tout[N], timer = 1, lg, x, y;
vector < int > adj[N];
set < int > st[N][2];

void dfs( int v, int p ) {
    up[v][0] = p;
    for ( int i = 1; i <= lg; i++ ) {
        up[v][i] = up[ up[v][i - 1] ][i - 1];
    }
    tin[v] = timer++;
    for ( auto u: adj[v] ) {
        if ( u != p ) {
            dfs( u, v );
        }
    }
    tout[v] = timer++;
}

bool is_upper( int a, int b ) {
    return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int LCA( int a, int b ) {
    if ( is_upper( a, b ) ) {
        return a;
    }
    if ( is_upper( b, a ) ) {
        return b;
    }
    for ( int i = lg; i >= 0; i-- ) {
        if ( !is_upper( up[a][i], b ) ) {
            a = up[a][i];
        }
    }
    return up[a][0];
}

pair < int, int > solve( int l, int r, int v ) {
    auto s = st[v];
    auto it = s[0].lower_bound(l);
    if ( it != s[0].end() && *it <= r ) {
        return { *it, *it };
    }
    it = s[1].lower_bound(l);
    if ( it != s[1].end() && *it + 1 <= r ) {
        return { *it, *it + 1 };
    }
    return {-1, -1};
}

int32_t main() {
    speed;
    int n, m, q;
    cin >> n >> m >> q;
    lg = log2(n) + 1;
    for ( int i = 1; i < n; i++ ) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs( 1, 1 );
    for ( int i = 1; i <= m; i++ ) {
        cin >> a[i];
    }
    for ( int i = 1; i <= m; i++ ) {
        st[ a[i] ][0].insert(i);
        if ( i + 1 <= m ) {
            st[ LCA( a[i], a[i + 1] ) ][1].insert(i);
        }
    }
    for ( int i = 1; i <= q; i++ ) {
        int t;
        cin >> t;
        if ( t == 1 ) {
            int pos, val;
            cin >> pos >> val;
            st[ a[pos] ][0].erase(pos);
            if ( pos + 1 <= m ) {
                st[ LCA( a[pos], a[pos + 1] ) ][1].erase(pos);
            }
            if ( pos - 1 > 0 ) {
                st[ LCA( a[pos - 1], a[pos] ) ][1].erase(pos - 1);
            }
            a[pos] = val;
            st[ a[pos] ][0].insert(pos);
            if ( pos + 1 <= m ) {
                st[ LCA( a[pos], a[pos + 1] ) ][1].insert(pos);
            }
            if ( pos - 1 > 0 ) {
                st[ LCA( a[pos - 1], a[pos] ) ][1].insert(pos - 1);
            }
        }
        else {
            int l, r, v;
            cin >> l >> r >> v;
            auto pr = solve( l, r, v );
            cout << pr.fi << ' ' << pr.se << '\n';
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23796 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 14 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 14 ms 23916 KB n=100
9 Correct 18 ms 23916 KB n=100
10 Correct 15 ms 23916 KB n=100
11 Correct 14 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 16 ms 24044 KB n=100
14 Correct 16 ms 23872 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 14 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 14 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23796 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 14 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 14 ms 23916 KB n=100
9 Correct 18 ms 23916 KB n=100
10 Correct 15 ms 23916 KB n=100
11 Correct 14 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 16 ms 24044 KB n=100
14 Correct 16 ms 23872 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 14 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 14 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
28 Correct 15 ms 24044 KB n=500
29 Correct 16 ms 24044 KB n=500
30 Correct 16 ms 24044 KB n=500
31 Correct 15 ms 24044 KB n=500
32 Correct 16 ms 24044 KB n=500
33 Correct 15 ms 24044 KB n=500
34 Correct 15 ms 24044 KB n=500
35 Correct 15 ms 24044 KB n=500
36 Correct 15 ms 24044 KB n=500
37 Correct 15 ms 24044 KB n=500
38 Correct 15 ms 24044 KB n=500
39 Correct 15 ms 24044 KB n=500
40 Correct 16 ms 24172 KB n=500
41 Correct 15 ms 24044 KB n=500
42 Correct 15 ms 24044 KB n=500
43 Correct 15 ms 24064 KB n=500
44 Correct 15 ms 24044 KB n=500
45 Correct 15 ms 24044 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 24044 KB n=500
49 Correct 15 ms 24044 KB n=500
50 Correct 15 ms 23936 KB n=500
51 Correct 15 ms 24044 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 15 ms 24044 KB n=500
54 Correct 15 ms 24044 KB n=500
55 Correct 14 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 15 ms 24044 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23796 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 14 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 14 ms 23916 KB n=100
9 Correct 18 ms 23916 KB n=100
10 Correct 15 ms 23916 KB n=100
11 Correct 14 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 16 ms 24044 KB n=100
14 Correct 16 ms 23872 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 14 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 14 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
28 Correct 15 ms 24044 KB n=500
29 Correct 16 ms 24044 KB n=500
30 Correct 16 ms 24044 KB n=500
31 Correct 15 ms 24044 KB n=500
32 Correct 16 ms 24044 KB n=500
33 Correct 15 ms 24044 KB n=500
34 Correct 15 ms 24044 KB n=500
35 Correct 15 ms 24044 KB n=500
36 Correct 15 ms 24044 KB n=500
37 Correct 15 ms 24044 KB n=500
38 Correct 15 ms 24044 KB n=500
39 Correct 15 ms 24044 KB n=500
40 Correct 16 ms 24172 KB n=500
41 Correct 15 ms 24044 KB n=500
42 Correct 15 ms 24044 KB n=500
43 Correct 15 ms 24064 KB n=500
44 Correct 15 ms 24044 KB n=500
45 Correct 15 ms 24044 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 24044 KB n=500
49 Correct 15 ms 24044 KB n=500
50 Correct 15 ms 23936 KB n=500
51 Correct 15 ms 24044 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 15 ms 24044 KB n=500
54 Correct 15 ms 24044 KB n=500
55 Correct 14 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 15 ms 24044 KB n=500
59 Correct 18 ms 24440 KB n=2000
60 Correct 18 ms 24556 KB n=2000
61 Correct 18 ms 24428 KB n=2000
62 Correct 18 ms 24428 KB n=2000
63 Correct 21 ms 24428 KB n=2000
64 Correct 18 ms 24448 KB n=2000
65 Correct 18 ms 24428 KB n=2000
66 Correct 18 ms 24556 KB n=2000
67 Correct 19 ms 24556 KB n=2000
68 Correct 18 ms 24428 KB n=2000
69 Correct 17 ms 24428 KB n=2000
70 Correct 17 ms 24428 KB n=2000
71 Correct 17 ms 24428 KB n=2000
72 Correct 17 ms 24428 KB n=2000
73 Correct 18 ms 24428 KB n=2000
74 Correct 21 ms 24576 KB n=1844
75 Correct 17 ms 24428 KB n=2000
76 Correct 18 ms 24556 KB n=2000
77 Correct 19 ms 24556 KB n=2000
78 Correct 18 ms 24556 KB n=2000
79 Correct 18 ms 24428 KB n=2000
80 Correct 17 ms 24556 KB n=2000
81 Correct 18 ms 24428 KB n=2000
82 Correct 18 ms 24428 KB n=2000
83 Correct 18 ms 24556 KB n=2000
84 Correct 18 ms 24428 KB n=2000
85 Correct 17 ms 24428 KB n=2000
86 Correct 18 ms 24428 KB n=2000
87 Correct 18 ms 24428 KB n=2000
88 Correct 18 ms 24556 KB n=2000
89 Correct 17 ms 24556 KB n=2000
90 Correct 18 ms 24684 KB n=2000
91 Correct 17 ms 24428 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23788 KB n=5
2 Correct 14 ms 23916 KB n=100
3 Correct 14 ms 23916 KB n=100
4 Correct 15 ms 23796 KB n=100
5 Correct 14 ms 23916 KB n=100
6 Correct 14 ms 23916 KB n=100
7 Correct 15 ms 23916 KB n=100
8 Correct 14 ms 23916 KB n=100
9 Correct 18 ms 23916 KB n=100
10 Correct 15 ms 23916 KB n=100
11 Correct 14 ms 23916 KB n=100
12 Correct 15 ms 23916 KB n=100
13 Correct 16 ms 24044 KB n=100
14 Correct 16 ms 23872 KB n=100
15 Correct 14 ms 23916 KB n=100
16 Correct 15 ms 23916 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 14 ms 23916 KB n=100
19 Correct 14 ms 23916 KB n=100
20 Correct 14 ms 23916 KB n=100
21 Correct 14 ms 23916 KB n=100
22 Correct 14 ms 23916 KB n=100
23 Correct 14 ms 23916 KB n=100
24 Correct 14 ms 23916 KB n=100
25 Correct 15 ms 23916 KB n=100
26 Correct 14 ms 23788 KB n=12
27 Correct 14 ms 23916 KB n=100
28 Correct 15 ms 24044 KB n=500
29 Correct 16 ms 24044 KB n=500
30 Correct 16 ms 24044 KB n=500
31 Correct 15 ms 24044 KB n=500
32 Correct 16 ms 24044 KB n=500
33 Correct 15 ms 24044 KB n=500
34 Correct 15 ms 24044 KB n=500
35 Correct 15 ms 24044 KB n=500
36 Correct 15 ms 24044 KB n=500
37 Correct 15 ms 24044 KB n=500
38 Correct 15 ms 24044 KB n=500
39 Correct 15 ms 24044 KB n=500
40 Correct 16 ms 24172 KB n=500
41 Correct 15 ms 24044 KB n=500
42 Correct 15 ms 24044 KB n=500
43 Correct 15 ms 24064 KB n=500
44 Correct 15 ms 24044 KB n=500
45 Correct 15 ms 24044 KB n=500
46 Correct 15 ms 24044 KB n=500
47 Correct 15 ms 24044 KB n=500
48 Correct 15 ms 24044 KB n=500
49 Correct 15 ms 24044 KB n=500
50 Correct 15 ms 23936 KB n=500
51 Correct 15 ms 24044 KB n=500
52 Correct 15 ms 24044 KB n=500
53 Correct 15 ms 24044 KB n=500
54 Correct 15 ms 24044 KB n=500
55 Correct 14 ms 23916 KB n=278
56 Correct 15 ms 24044 KB n=500
57 Correct 15 ms 24044 KB n=500
58 Correct 15 ms 24044 KB n=500
59 Correct 18 ms 24440 KB n=2000
60 Correct 18 ms 24556 KB n=2000
61 Correct 18 ms 24428 KB n=2000
62 Correct 18 ms 24428 KB n=2000
63 Correct 21 ms 24428 KB n=2000
64 Correct 18 ms 24448 KB n=2000
65 Correct 18 ms 24428 KB n=2000
66 Correct 18 ms 24556 KB n=2000
67 Correct 19 ms 24556 KB n=2000
68 Correct 18 ms 24428 KB n=2000
69 Correct 17 ms 24428 KB n=2000
70 Correct 17 ms 24428 KB n=2000
71 Correct 17 ms 24428 KB n=2000
72 Correct 17 ms 24428 KB n=2000
73 Correct 18 ms 24428 KB n=2000
74 Correct 21 ms 24576 KB n=1844
75 Correct 17 ms 24428 KB n=2000
76 Correct 18 ms 24556 KB n=2000
77 Correct 19 ms 24556 KB n=2000
78 Correct 18 ms 24556 KB n=2000
79 Correct 18 ms 24428 KB n=2000
80 Correct 17 ms 24556 KB n=2000
81 Correct 18 ms 24428 KB n=2000
82 Correct 18 ms 24428 KB n=2000
83 Correct 18 ms 24556 KB n=2000
84 Correct 18 ms 24428 KB n=2000
85 Correct 17 ms 24428 KB n=2000
86 Correct 18 ms 24428 KB n=2000
87 Correct 18 ms 24428 KB n=2000
88 Correct 18 ms 24556 KB n=2000
89 Correct 17 ms 24556 KB n=2000
90 Correct 18 ms 24684 KB n=2000
91 Correct 17 ms 24428 KB n=2000
92 Correct 869 ms 84796 KB n=200000
93 Correct 937 ms 92272 KB n=200000
94 Correct 699 ms 99336 KB n=200000
95 Correct 871 ms 90848 KB n=200000
96 Correct 812 ms 90848 KB n=200000
97 Correct 1016 ms 95052 KB n=200000
98 Correct 834 ms 91236 KB n=200000
99 Correct 1027 ms 91244 KB n=200000
100 Correct 848 ms 90980 KB n=200000
101 Correct 592 ms 100588 KB n=200000
102 Correct 547 ms 92140 KB n=200000
103 Correct 534 ms 92288 KB n=200000
104 Correct 533 ms 92076 KB n=200000
105 Correct 528 ms 92652 KB n=200000
106 Correct 530 ms 92396 KB n=200000
107 Correct 533 ms 92652 KB n=200000
108 Correct 939 ms 90988 KB n=200000
109 Correct 951 ms 90988 KB n=200000
110 Correct 954 ms 90988 KB n=200000
111 Correct 860 ms 90472 KB n=200000
112 Correct 627 ms 99564 KB n=200000
113 Correct 988 ms 95084 KB n=200000
114 Correct 861 ms 90592 KB n=200000
115 Correct 1252 ms 93164 KB n=200000
116 Correct 800 ms 91104 KB n=200000
117 Correct 606 ms 99820 KB n=200000
118 Correct 1094 ms 94060 KB n=200000
119 Correct 787 ms 90976 KB n=200000
120 Correct 538 ms 99436 KB n=200000
121 Correct 568 ms 99556 KB n=200000
122 Correct 553 ms 99820 KB n=200000
123 Correct 565 ms 92268 KB n=200000
124 Correct 187 ms 40812 KB n=25264