Submission #154849

# Submission time Handle Problem Language Result Execution time Memory
154849 2019-09-25T11:06:38 Z andrew Birthday gift (IZhO18_treearray) C++17
100 / 100
1501 ms 98364 KB
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define sset ordered_set
#define sqr(x) (x)*(x)
#define pw(x) (1ll << x)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
const ll MAXN = 2e5 + 2;
const ll N = 2e5;
const ll inf = 3e18;
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> void vout(T s){cout << s << endl;exit(0);}

set <ll> s[MAXN], s1[MAXN];

vector <ll> v[MAXN];

ll tin[MAXN], tout[MAXN], stn, p[MAXN][19];

void dfs(ll x, ll pr){
    p[x][0] = pr;
    for(int i = 1; i < 19; i++)p[x][i] = p[p[x][i - 1]][i - 1];
    tin[x] = ++stn;
    for(auto to : v[x])if(to != pr)dfs(to, x);
    tout[x] = stn;
}

bool is_less(ll a, ll b){
    return (tin[b] <= tin[a] && tout[a] <= tout[b]);
}

ll lca(ll a, ll b){
    if(is_less(a, b))return b;
    if(is_less(b, a))return a;
    for(int i = 18; i >= 0; i--)if(!is_less(b, p[a][i]))a = p[a][i];
    return p[a][0];
}

int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    tout[0] = 1e18;

    ll n, m, q;
    cin >> n >> m >> q;

    vector <ll> a(m + 1);

    for(int i = 1; i < n; i++){
        ll x, y;
        cin >> x >> y;
        v[x].p_b(y);
        v[y].p_b(x);
    }

    dfs(1ll, 0ll);

    for(int i = 1; i <= m; i++)cin >> a[i];

    for(int i = 1; i <= m; i++)s[a[i]].insert(i);

    for(int i = 1; i < m; i++){
        s1[lca(a[i], a[i + 1])].insert(i);
    }

    while(q--){
        ll t;
        cin >> t;
        if(t == 1){
            ll pos, val;
            cin >> pos >> val;
            s[a[pos]].erase(pos);
            if(pos < m)s1[lca(a[pos], a[pos + 1])].erase(pos);
            if(pos > 1)s1[lca(a[pos], a[pos - 1])].erase(pos - 1);
            a[pos] = val;
            s[a[pos]].insert(pos);
            if(pos < m)s1[lca(a[pos], a[pos + 1])].insert(pos);
            if(pos > 1)s1[lca(a[pos], a[pos - 1])].insert(pos - 1);
        }else{
            ll l, r, v;
            cin >> l >> r >> v;
            ll x, y;
            x = y = -1;
            set <ll> :: iterator it;
            if(!s[v].empty()){
                it = s[v].upper_bound(r);
                if(it != s[v].begin()){
                    --it;
                    if(l <= *it)x = *it, y = *it;
                }
            }

            if(!s1[v].empty()){
                it = s1[v].lower_bound(r);
                if(it != s1[v].begin()){
                    --it;
                    if(l <= *it)x = *it, y = *it + 1;
                }
            }
            cout << x << " " << y << "\n";
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23804 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23928 KB n=100
5 Correct 24 ms 24056 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 24 ms 23928 KB n=100
8 Correct 24 ms 23804 KB n=100
9 Correct 25 ms 23928 KB n=100
10 Correct 29 ms 23904 KB n=100
11 Correct 28 ms 23920 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23912 KB n=100
15 Correct 24 ms 23932 KB n=100
16 Correct 24 ms 23928 KB n=100
17 Correct 24 ms 23928 KB n=100
18 Correct 24 ms 23928 KB n=100
19 Correct 24 ms 23896 KB n=100
20 Correct 24 ms 23800 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 24 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 24 ms 23800 KB n=100
25 Correct 24 ms 23800 KB n=100
26 Correct 24 ms 23892 KB n=12
27 Correct 24 ms 23928 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23804 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23928 KB n=100
5 Correct 24 ms 24056 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 24 ms 23928 KB n=100
8 Correct 24 ms 23804 KB n=100
9 Correct 25 ms 23928 KB n=100
10 Correct 29 ms 23904 KB n=100
11 Correct 28 ms 23920 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23912 KB n=100
15 Correct 24 ms 23932 KB n=100
16 Correct 24 ms 23928 KB n=100
17 Correct 24 ms 23928 KB n=100
18 Correct 24 ms 23928 KB n=100
19 Correct 24 ms 23896 KB n=100
20 Correct 24 ms 23800 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 24 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 24 ms 23800 KB n=100
25 Correct 24 ms 23800 KB n=100
26 Correct 24 ms 23892 KB n=12
27 Correct 24 ms 23928 KB n=100
28 Correct 25 ms 23928 KB n=500
29 Correct 24 ms 24056 KB n=500
30 Correct 25 ms 24008 KB n=500
31 Correct 25 ms 24060 KB n=500
32 Correct 25 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 25 ms 24056 KB n=500
35 Correct 25 ms 24056 KB n=500
36 Correct 25 ms 23928 KB n=500
37 Correct 25 ms 23928 KB n=500
38 Correct 25 ms 24056 KB n=500
39 Correct 25 ms 24056 KB n=500
40 Correct 25 ms 24056 KB n=500
41 Correct 25 ms 23928 KB n=500
42 Correct 25 ms 24056 KB n=500
43 Correct 26 ms 23928 KB n=500
44 Correct 24 ms 23928 KB n=500
45 Correct 25 ms 23928 KB n=500
46 Correct 25 ms 24056 KB n=500
47 Correct 25 ms 24056 KB n=500
48 Correct 25 ms 24056 KB n=500
49 Correct 25 ms 24056 KB n=500
50 Correct 25 ms 23928 KB n=500
51 Correct 26 ms 24056 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 25 ms 23928 KB n=500
54 Correct 25 ms 24056 KB n=500
55 Correct 24 ms 23928 KB n=278
56 Correct 25 ms 24056 KB n=500
57 Correct 25 ms 24056 KB n=500
58 Correct 25 ms 24088 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23804 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23928 KB n=100
5 Correct 24 ms 24056 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 24 ms 23928 KB n=100
8 Correct 24 ms 23804 KB n=100
9 Correct 25 ms 23928 KB n=100
10 Correct 29 ms 23904 KB n=100
11 Correct 28 ms 23920 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23912 KB n=100
15 Correct 24 ms 23932 KB n=100
16 Correct 24 ms 23928 KB n=100
17 Correct 24 ms 23928 KB n=100
18 Correct 24 ms 23928 KB n=100
19 Correct 24 ms 23896 KB n=100
20 Correct 24 ms 23800 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 24 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 24 ms 23800 KB n=100
25 Correct 24 ms 23800 KB n=100
26 Correct 24 ms 23892 KB n=12
27 Correct 24 ms 23928 KB n=100
28 Correct 25 ms 23928 KB n=500
29 Correct 24 ms 24056 KB n=500
30 Correct 25 ms 24008 KB n=500
31 Correct 25 ms 24060 KB n=500
32 Correct 25 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 25 ms 24056 KB n=500
35 Correct 25 ms 24056 KB n=500
36 Correct 25 ms 23928 KB n=500
37 Correct 25 ms 23928 KB n=500
38 Correct 25 ms 24056 KB n=500
39 Correct 25 ms 24056 KB n=500
40 Correct 25 ms 24056 KB n=500
41 Correct 25 ms 23928 KB n=500
42 Correct 25 ms 24056 KB n=500
43 Correct 26 ms 23928 KB n=500
44 Correct 24 ms 23928 KB n=500
45 Correct 25 ms 23928 KB n=500
46 Correct 25 ms 24056 KB n=500
47 Correct 25 ms 24056 KB n=500
48 Correct 25 ms 24056 KB n=500
49 Correct 25 ms 24056 KB n=500
50 Correct 25 ms 23928 KB n=500
51 Correct 26 ms 24056 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 25 ms 23928 KB n=500
54 Correct 25 ms 24056 KB n=500
55 Correct 24 ms 23928 KB n=278
56 Correct 25 ms 24056 KB n=500
57 Correct 25 ms 24056 KB n=500
58 Correct 25 ms 24088 KB n=500
59 Correct 28 ms 24568 KB n=2000
60 Correct 27 ms 24568 KB n=2000
61 Correct 27 ms 24568 KB n=2000
62 Correct 34 ms 24568 KB n=2000
63 Correct 34 ms 24568 KB n=2000
64 Correct 34 ms 24568 KB n=2000
65 Correct 41 ms 24568 KB n=2000
66 Correct 28 ms 24568 KB n=2000
67 Correct 29 ms 24568 KB n=2000
68 Correct 28 ms 24572 KB n=2000
69 Correct 27 ms 24572 KB n=2000
70 Correct 28 ms 24568 KB n=2000
71 Correct 27 ms 24568 KB n=2000
72 Correct 28 ms 24572 KB n=2000
73 Correct 27 ms 24440 KB n=2000
74 Correct 28 ms 24440 KB n=1844
75 Correct 28 ms 24568 KB n=2000
76 Correct 29 ms 24492 KB n=2000
77 Correct 28 ms 24440 KB n=2000
78 Correct 28 ms 24568 KB n=2000
79 Correct 29 ms 24440 KB n=2000
80 Correct 28 ms 24568 KB n=2000
81 Correct 29 ms 24572 KB n=2000
82 Correct 28 ms 24568 KB n=2000
83 Correct 29 ms 24568 KB n=2000
84 Correct 29 ms 24568 KB n=2000
85 Correct 29 ms 24572 KB n=2000
86 Correct 29 ms 24568 KB n=2000
87 Correct 29 ms 24572 KB n=2000
88 Correct 28 ms 24568 KB n=2000
89 Correct 27 ms 24568 KB n=2000
90 Correct 28 ms 24520 KB n=2000
91 Correct 28 ms 24564 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23804 KB n=5
2 Correct 24 ms 23928 KB n=100
3 Correct 24 ms 23928 KB n=100
4 Correct 24 ms 23928 KB n=100
5 Correct 24 ms 24056 KB n=100
6 Correct 24 ms 23928 KB n=100
7 Correct 24 ms 23928 KB n=100
8 Correct 24 ms 23804 KB n=100
9 Correct 25 ms 23928 KB n=100
10 Correct 29 ms 23904 KB n=100
11 Correct 28 ms 23920 KB n=100
12 Correct 24 ms 23800 KB n=100
13 Correct 24 ms 23928 KB n=100
14 Correct 24 ms 23912 KB n=100
15 Correct 24 ms 23932 KB n=100
16 Correct 24 ms 23928 KB n=100
17 Correct 24 ms 23928 KB n=100
18 Correct 24 ms 23928 KB n=100
19 Correct 24 ms 23896 KB n=100
20 Correct 24 ms 23800 KB n=100
21 Correct 24 ms 23800 KB n=100
22 Correct 24 ms 23800 KB n=100
23 Correct 24 ms 23800 KB n=100
24 Correct 24 ms 23800 KB n=100
25 Correct 24 ms 23800 KB n=100
26 Correct 24 ms 23892 KB n=12
27 Correct 24 ms 23928 KB n=100
28 Correct 25 ms 23928 KB n=500
29 Correct 24 ms 24056 KB n=500
30 Correct 25 ms 24008 KB n=500
31 Correct 25 ms 24060 KB n=500
32 Correct 25 ms 23928 KB n=500
33 Correct 25 ms 23928 KB n=500
34 Correct 25 ms 24056 KB n=500
35 Correct 25 ms 24056 KB n=500
36 Correct 25 ms 23928 KB n=500
37 Correct 25 ms 23928 KB n=500
38 Correct 25 ms 24056 KB n=500
39 Correct 25 ms 24056 KB n=500
40 Correct 25 ms 24056 KB n=500
41 Correct 25 ms 23928 KB n=500
42 Correct 25 ms 24056 KB n=500
43 Correct 26 ms 23928 KB n=500
44 Correct 24 ms 23928 KB n=500
45 Correct 25 ms 23928 KB n=500
46 Correct 25 ms 24056 KB n=500
47 Correct 25 ms 24056 KB n=500
48 Correct 25 ms 24056 KB n=500
49 Correct 25 ms 24056 KB n=500
50 Correct 25 ms 23928 KB n=500
51 Correct 26 ms 24056 KB n=500
52 Correct 25 ms 24056 KB n=500
53 Correct 25 ms 23928 KB n=500
54 Correct 25 ms 24056 KB n=500
55 Correct 24 ms 23928 KB n=278
56 Correct 25 ms 24056 KB n=500
57 Correct 25 ms 24056 KB n=500
58 Correct 25 ms 24088 KB n=500
59 Correct 28 ms 24568 KB n=2000
60 Correct 27 ms 24568 KB n=2000
61 Correct 27 ms 24568 KB n=2000
62 Correct 34 ms 24568 KB n=2000
63 Correct 34 ms 24568 KB n=2000
64 Correct 34 ms 24568 KB n=2000
65 Correct 41 ms 24568 KB n=2000
66 Correct 28 ms 24568 KB n=2000
67 Correct 29 ms 24568 KB n=2000
68 Correct 28 ms 24572 KB n=2000
69 Correct 27 ms 24572 KB n=2000
70 Correct 28 ms 24568 KB n=2000
71 Correct 27 ms 24568 KB n=2000
72 Correct 28 ms 24572 KB n=2000
73 Correct 27 ms 24440 KB n=2000
74 Correct 28 ms 24440 KB n=1844
75 Correct 28 ms 24568 KB n=2000
76 Correct 29 ms 24492 KB n=2000
77 Correct 28 ms 24440 KB n=2000
78 Correct 28 ms 24568 KB n=2000
79 Correct 29 ms 24440 KB n=2000
80 Correct 28 ms 24568 KB n=2000
81 Correct 29 ms 24572 KB n=2000
82 Correct 28 ms 24568 KB n=2000
83 Correct 29 ms 24568 KB n=2000
84 Correct 29 ms 24568 KB n=2000
85 Correct 29 ms 24572 KB n=2000
86 Correct 29 ms 24568 KB n=2000
87 Correct 29 ms 24572 KB n=2000
88 Correct 28 ms 24568 KB n=2000
89 Correct 27 ms 24568 KB n=2000
90 Correct 28 ms 24520 KB n=2000
91 Correct 28 ms 24564 KB n=2000
92 Correct 1004 ms 92788 KB n=200000
93 Correct 1305 ms 95968 KB n=200000
94 Correct 850 ms 97892 KB n=200000
95 Correct 964 ms 92648 KB n=200000
96 Correct 1039 ms 92520 KB n=200000
97 Correct 1281 ms 95612 KB n=200000
98 Correct 1049 ms 92624 KB n=200000
99 Correct 1257 ms 93008 KB n=200000
100 Correct 983 ms 92548 KB n=200000
101 Correct 834 ms 98364 KB n=200000
102 Correct 603 ms 94116 KB n=200000
103 Correct 599 ms 94052 KB n=200000
104 Correct 661 ms 94188 KB n=200000
105 Correct 615 ms 94536 KB n=200000
106 Correct 592 ms 94452 KB n=200000
107 Correct 596 ms 94544 KB n=200000
108 Correct 1038 ms 92836 KB n=200000
109 Correct 1044 ms 92760 KB n=200000
110 Correct 1030 ms 92792 KB n=200000
111 Correct 979 ms 92136 KB n=200000
112 Correct 814 ms 98000 KB n=200000
113 Correct 1288 ms 95504 KB n=200000
114 Correct 963 ms 92136 KB n=200000
115 Correct 1501 ms 94252 KB n=200000
116 Correct 921 ms 92648 KB n=200000
117 Correct 783 ms 97988 KB n=200000
118 Correct 1281 ms 94760 KB n=200000
119 Correct 933 ms 92776 KB n=200000
120 Correct 703 ms 97272 KB n=200000
121 Correct 819 ms 97324 KB n=200000
122 Correct 671 ms 97644 KB n=200000
123 Correct 610 ms 94080 KB n=200000
124 Correct 238 ms 40932 KB n=25264