Submission #495874

# Submission time Handle Problem Language Result Execution time Memory
495874 2021-12-20T06:42:33 Z ergagan Birthday gift (IZhO18_treearray) C++17
100 / 100
1405 ms 156480 KB
//я так много думал, что опять попал
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define f first
#define s second
#define left(v) v + v
#define right(v) v + v + 1
#define ub upper_bound
#define lb lower_bound
#define pll pair<ll, ll>

using namespace std;
typedef long long ll;

//17 SEVENTEEN
const long double Pi = acos(-1.0);
const ll dx[] = {0,0,1,-1};
const ll dy[] = {1,-1,0,0};
const ll N = (ll) 2e5 + 17;
const ll M = (ll) 5e3 + 69;
const ll inf = (ll) 1e14 + 3;
const ll mod = (ll) 1e9 + 7;
ll sq(ll x) { return x * x; }

ll zxc = 1, a[N];
ll h[N], up[N][50];
ll n, m, q, tp, pos, u, v, l, r;

vector<ll> g[N];

set<ll> st[N], ts[N];

void dfs(ll v, ll p, ll lvl) {

	up[v][0] = p, h[v] = lvl;

	for(ll to : g[v]) {
		if(to != p)
			dfs(to, v, lvl + 1);
	}

}

bool bit(ll x, ll p) {
	return x & (1 << p);
}

ll lift(ll v, ll k) {
	for(ll i = 0; i < 26; i++) {
		if(bit(k, i))
			v = up[v][i];
	}
	return v;
}

ll lca(ll a, ll b) {
	if(h[a] < h[b]) swap(a, b);

	a = lift(a, h[a] - h[b]);

	if(a == b) return a;

	for(ll i = 25; i >= 0; i--) {
		if(up[a][i] != up[b][i])
			a = up[a][i], b = up[b][i];
	}

	return up[a][0];
}

ll dis(ll a, ll b) {
    return h[a] + h[b] - h[lca(a, b)] * 2;
}

void solve() {

    cin >> n >> m >> q;

    for(ll i = 1; i < n; i++) {
        cin >> u >> v;
        g[u].pb(v), g[v].pb(u);
    }

    for(ll i = 1; i <= m; i++) {
        cin >> a[i];
        st[a[i]].insert(i);
    }

    dfs(1, -1, 0);

    for(ll i = 1; i <= 25; i++) {
    	for(ll v = 1; v <= n; v++) {
    		up[v][i] = up[up[v][i - 1]][i - 1];
    	}
    }

    for(ll i = 1; i < m; i++) {
        ll x = lca(a[i], a[i + 1]);
        ts[x].insert(i);
    }

    while(q--) {
        cin >> tp;
        if(tp == 1) {
            cin >> pos >> v;
            ll x = lca(a[pos], a[pos + 1]), y = lca(a[pos], a[pos - 1]);

            if(st[a[pos]].count(pos)) st[a[pos]].erase(pos);
            if(pos < m && ts[x].count(pos)) ts[x].erase(pos);
            if(pos > 1 && ts[y].count(pos - 1)) ts[y].erase(pos - 1);

            a[pos] = v;
            x = lca(a[pos], a[pos + 1]), y = lca(a[pos], a[pos - 1]);

            st[a[pos]].insert(pos);
            if(pos < m) ts[x].insert(pos);
            if(pos > 1) ts[y].insert(pos - 1);
        }
        else {

            cin >> l >> r >> v;

            ll x = *st[v].lb(l);

            if(x <= r) { cout << x << " " << x << "\n"; continue; }

            x = *ts[v].lb(l);
            if(x < r) { cout << x << " " << x + 1 << "\n"; continue; }

            cout << "-1 -1\n";
        }
    }
}

/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/

int main(/*Уверенно*/) {
ios_base::sync_with_stdio(0);
    cin.tie(0);
/*
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
*/
//    cin >> zxc;

    for(ll i = 0; i < N; i++)
        st[i].insert(inf), ts[i].insert(inf);

    while(zxc--) {
        solve();
    }
  	return 0;
}
// さよならさ いかなくちゃ
# Verdict Execution time Memory Grader output
1 Correct 36 ms 42564 KB n=5
2 Correct 35 ms 42548 KB n=100
3 Correct 39 ms 42560 KB n=100
4 Correct 34 ms 42572 KB n=100
5 Correct 35 ms 42632 KB n=100
6 Correct 36 ms 42708 KB n=100
7 Correct 34 ms 42596 KB n=100
8 Correct 34 ms 42564 KB n=100
9 Correct 37 ms 42596 KB n=100
10 Correct 35 ms 42572 KB n=100
11 Correct 34 ms 42564 KB n=100
12 Correct 35 ms 42648 KB n=100
13 Correct 38 ms 42632 KB n=100
14 Correct 37 ms 42656 KB n=100
15 Correct 37 ms 42588 KB n=100
16 Correct 34 ms 42652 KB n=100
17 Correct 35 ms 42564 KB n=100
18 Correct 34 ms 42564 KB n=100
19 Correct 35 ms 42612 KB n=100
20 Correct 34 ms 42592 KB n=100
21 Correct 35 ms 42616 KB n=100
22 Correct 35 ms 42564 KB n=100
23 Correct 35 ms 42588 KB n=100
24 Correct 34 ms 42564 KB n=100
25 Correct 37 ms 42640 KB n=100
26 Correct 35 ms 42588 KB n=12
27 Correct 35 ms 42572 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 36 ms 42564 KB n=5
2 Correct 35 ms 42548 KB n=100
3 Correct 39 ms 42560 KB n=100
4 Correct 34 ms 42572 KB n=100
5 Correct 35 ms 42632 KB n=100
6 Correct 36 ms 42708 KB n=100
7 Correct 34 ms 42596 KB n=100
8 Correct 34 ms 42564 KB n=100
9 Correct 37 ms 42596 KB n=100
10 Correct 35 ms 42572 KB n=100
11 Correct 34 ms 42564 KB n=100
12 Correct 35 ms 42648 KB n=100
13 Correct 38 ms 42632 KB n=100
14 Correct 37 ms 42656 KB n=100
15 Correct 37 ms 42588 KB n=100
16 Correct 34 ms 42652 KB n=100
17 Correct 35 ms 42564 KB n=100
18 Correct 34 ms 42564 KB n=100
19 Correct 35 ms 42612 KB n=100
20 Correct 34 ms 42592 KB n=100
21 Correct 35 ms 42616 KB n=100
22 Correct 35 ms 42564 KB n=100
23 Correct 35 ms 42588 KB n=100
24 Correct 34 ms 42564 KB n=100
25 Correct 37 ms 42640 KB n=100
26 Correct 35 ms 42588 KB n=12
27 Correct 35 ms 42572 KB n=100
28 Correct 35 ms 42804 KB n=500
29 Correct 36 ms 42840 KB n=500
30 Correct 42 ms 42848 KB n=500
31 Correct 35 ms 42764 KB n=500
32 Correct 36 ms 42828 KB n=500
33 Correct 36 ms 42848 KB n=500
34 Correct 36 ms 42800 KB n=500
35 Correct 38 ms 42820 KB n=500
36 Correct 37 ms 42956 KB n=500
37 Correct 39 ms 42764 KB n=500
38 Correct 36 ms 42872 KB n=500
39 Correct 35 ms 42828 KB n=500
40 Correct 34 ms 42856 KB n=500
41 Correct 36 ms 42768 KB n=500
42 Correct 37 ms 42820 KB n=500
43 Correct 35 ms 42876 KB n=500
44 Correct 35 ms 42928 KB n=500
45 Correct 35 ms 42864 KB n=500
46 Correct 35 ms 42776 KB n=500
47 Correct 35 ms 42808 KB n=500
48 Correct 36 ms 42812 KB n=500
49 Correct 35 ms 42808 KB n=500
50 Correct 35 ms 42844 KB n=500
51 Correct 35 ms 42788 KB n=500
52 Correct 37 ms 42820 KB n=500
53 Correct 35 ms 42784 KB n=500
54 Correct 35 ms 42860 KB n=500
55 Correct 34 ms 42636 KB n=278
56 Correct 35 ms 42816 KB n=500
57 Correct 35 ms 42812 KB n=500
58 Correct 35 ms 42808 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 36 ms 42564 KB n=5
2 Correct 35 ms 42548 KB n=100
3 Correct 39 ms 42560 KB n=100
4 Correct 34 ms 42572 KB n=100
5 Correct 35 ms 42632 KB n=100
6 Correct 36 ms 42708 KB n=100
7 Correct 34 ms 42596 KB n=100
8 Correct 34 ms 42564 KB n=100
9 Correct 37 ms 42596 KB n=100
10 Correct 35 ms 42572 KB n=100
11 Correct 34 ms 42564 KB n=100
12 Correct 35 ms 42648 KB n=100
13 Correct 38 ms 42632 KB n=100
14 Correct 37 ms 42656 KB n=100
15 Correct 37 ms 42588 KB n=100
16 Correct 34 ms 42652 KB n=100
17 Correct 35 ms 42564 KB n=100
18 Correct 34 ms 42564 KB n=100
19 Correct 35 ms 42612 KB n=100
20 Correct 34 ms 42592 KB n=100
21 Correct 35 ms 42616 KB n=100
22 Correct 35 ms 42564 KB n=100
23 Correct 35 ms 42588 KB n=100
24 Correct 34 ms 42564 KB n=100
25 Correct 37 ms 42640 KB n=100
26 Correct 35 ms 42588 KB n=12
27 Correct 35 ms 42572 KB n=100
28 Correct 35 ms 42804 KB n=500
29 Correct 36 ms 42840 KB n=500
30 Correct 42 ms 42848 KB n=500
31 Correct 35 ms 42764 KB n=500
32 Correct 36 ms 42828 KB n=500
33 Correct 36 ms 42848 KB n=500
34 Correct 36 ms 42800 KB n=500
35 Correct 38 ms 42820 KB n=500
36 Correct 37 ms 42956 KB n=500
37 Correct 39 ms 42764 KB n=500
38 Correct 36 ms 42872 KB n=500
39 Correct 35 ms 42828 KB n=500
40 Correct 34 ms 42856 KB n=500
41 Correct 36 ms 42768 KB n=500
42 Correct 37 ms 42820 KB n=500
43 Correct 35 ms 42876 KB n=500
44 Correct 35 ms 42928 KB n=500
45 Correct 35 ms 42864 KB n=500
46 Correct 35 ms 42776 KB n=500
47 Correct 35 ms 42808 KB n=500
48 Correct 36 ms 42812 KB n=500
49 Correct 35 ms 42808 KB n=500
50 Correct 35 ms 42844 KB n=500
51 Correct 35 ms 42788 KB n=500
52 Correct 37 ms 42820 KB n=500
53 Correct 35 ms 42784 KB n=500
54 Correct 35 ms 42860 KB n=500
55 Correct 34 ms 42636 KB n=278
56 Correct 35 ms 42816 KB n=500
57 Correct 35 ms 42812 KB n=500
58 Correct 35 ms 42808 KB n=500
59 Correct 39 ms 43588 KB n=2000
60 Correct 37 ms 43696 KB n=2000
61 Correct 38 ms 43696 KB n=2000
62 Correct 39 ms 43680 KB n=2000
63 Correct 39 ms 43588 KB n=2000
64 Correct 38 ms 43580 KB n=2000
65 Correct 40 ms 43584 KB n=2000
66 Correct 37 ms 43724 KB n=2000
67 Correct 38 ms 43668 KB n=2000
68 Correct 40 ms 43632 KB n=2000
69 Correct 41 ms 43588 KB n=2000
70 Correct 36 ms 43596 KB n=2000
71 Correct 37 ms 43692 KB n=2000
72 Correct 41 ms 43600 KB n=2000
73 Correct 37 ms 43588 KB n=2000
74 Correct 40 ms 43548 KB n=1844
75 Correct 43 ms 43592 KB n=2000
76 Correct 38 ms 43588 KB n=2000
77 Correct 39 ms 43592 KB n=2000
78 Correct 39 ms 43588 KB n=2000
79 Correct 37 ms 43672 KB n=2000
80 Correct 39 ms 43692 KB n=2000
81 Correct 39 ms 43576 KB n=2000
82 Correct 38 ms 43596 KB n=2000
83 Correct 39 ms 43624 KB n=2000
84 Correct 40 ms 43716 KB n=2000
85 Correct 39 ms 43588 KB n=2000
86 Correct 41 ms 43684 KB n=2000
87 Correct 40 ms 43596 KB n=2000
88 Correct 38 ms 43716 KB n=2000
89 Correct 41 ms 43808 KB n=2000
90 Correct 39 ms 43636 KB n=2000
91 Correct 38 ms 43640 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 36 ms 42564 KB n=5
2 Correct 35 ms 42548 KB n=100
3 Correct 39 ms 42560 KB n=100
4 Correct 34 ms 42572 KB n=100
5 Correct 35 ms 42632 KB n=100
6 Correct 36 ms 42708 KB n=100
7 Correct 34 ms 42596 KB n=100
8 Correct 34 ms 42564 KB n=100
9 Correct 37 ms 42596 KB n=100
10 Correct 35 ms 42572 KB n=100
11 Correct 34 ms 42564 KB n=100
12 Correct 35 ms 42648 KB n=100
13 Correct 38 ms 42632 KB n=100
14 Correct 37 ms 42656 KB n=100
15 Correct 37 ms 42588 KB n=100
16 Correct 34 ms 42652 KB n=100
17 Correct 35 ms 42564 KB n=100
18 Correct 34 ms 42564 KB n=100
19 Correct 35 ms 42612 KB n=100
20 Correct 34 ms 42592 KB n=100
21 Correct 35 ms 42616 KB n=100
22 Correct 35 ms 42564 KB n=100
23 Correct 35 ms 42588 KB n=100
24 Correct 34 ms 42564 KB n=100
25 Correct 37 ms 42640 KB n=100
26 Correct 35 ms 42588 KB n=12
27 Correct 35 ms 42572 KB n=100
28 Correct 35 ms 42804 KB n=500
29 Correct 36 ms 42840 KB n=500
30 Correct 42 ms 42848 KB n=500
31 Correct 35 ms 42764 KB n=500
32 Correct 36 ms 42828 KB n=500
33 Correct 36 ms 42848 KB n=500
34 Correct 36 ms 42800 KB n=500
35 Correct 38 ms 42820 KB n=500
36 Correct 37 ms 42956 KB n=500
37 Correct 39 ms 42764 KB n=500
38 Correct 36 ms 42872 KB n=500
39 Correct 35 ms 42828 KB n=500
40 Correct 34 ms 42856 KB n=500
41 Correct 36 ms 42768 KB n=500
42 Correct 37 ms 42820 KB n=500
43 Correct 35 ms 42876 KB n=500
44 Correct 35 ms 42928 KB n=500
45 Correct 35 ms 42864 KB n=500
46 Correct 35 ms 42776 KB n=500
47 Correct 35 ms 42808 KB n=500
48 Correct 36 ms 42812 KB n=500
49 Correct 35 ms 42808 KB n=500
50 Correct 35 ms 42844 KB n=500
51 Correct 35 ms 42788 KB n=500
52 Correct 37 ms 42820 KB n=500
53 Correct 35 ms 42784 KB n=500
54 Correct 35 ms 42860 KB n=500
55 Correct 34 ms 42636 KB n=278
56 Correct 35 ms 42816 KB n=500
57 Correct 35 ms 42812 KB n=500
58 Correct 35 ms 42808 KB n=500
59 Correct 39 ms 43588 KB n=2000
60 Correct 37 ms 43696 KB n=2000
61 Correct 38 ms 43696 KB n=2000
62 Correct 39 ms 43680 KB n=2000
63 Correct 39 ms 43588 KB n=2000
64 Correct 38 ms 43580 KB n=2000
65 Correct 40 ms 43584 KB n=2000
66 Correct 37 ms 43724 KB n=2000
67 Correct 38 ms 43668 KB n=2000
68 Correct 40 ms 43632 KB n=2000
69 Correct 41 ms 43588 KB n=2000
70 Correct 36 ms 43596 KB n=2000
71 Correct 37 ms 43692 KB n=2000
72 Correct 41 ms 43600 KB n=2000
73 Correct 37 ms 43588 KB n=2000
74 Correct 40 ms 43548 KB n=1844
75 Correct 43 ms 43592 KB n=2000
76 Correct 38 ms 43588 KB n=2000
77 Correct 39 ms 43592 KB n=2000
78 Correct 39 ms 43588 KB n=2000
79 Correct 37 ms 43672 KB n=2000
80 Correct 39 ms 43692 KB n=2000
81 Correct 39 ms 43576 KB n=2000
82 Correct 38 ms 43596 KB n=2000
83 Correct 39 ms 43624 KB n=2000
84 Correct 40 ms 43716 KB n=2000
85 Correct 39 ms 43588 KB n=2000
86 Correct 41 ms 43684 KB n=2000
87 Correct 40 ms 43596 KB n=2000
88 Correct 38 ms 43716 KB n=2000
89 Correct 41 ms 43808 KB n=2000
90 Correct 39 ms 43636 KB n=2000
91 Correct 38 ms 43640 KB n=2000
92 Correct 944 ms 151856 KB n=200000
93 Correct 1200 ms 154124 KB n=200000
94 Correct 1145 ms 155880 KB n=200000
95 Correct 897 ms 151896 KB n=200000
96 Correct 913 ms 151812 KB n=200000
97 Correct 1173 ms 153408 KB n=200000
98 Correct 945 ms 151780 KB n=200000
99 Correct 1086 ms 151376 KB n=200000
100 Correct 949 ms 151772 KB n=200000
101 Correct 1109 ms 156480 KB n=200000
102 Correct 518 ms 152780 KB n=200000
103 Correct 556 ms 152796 KB n=200000
104 Correct 530 ms 152768 KB n=200000
105 Correct 557 ms 152588 KB n=200000
106 Correct 513 ms 152528 KB n=200000
107 Correct 559 ms 152660 KB n=200000
108 Correct 1002 ms 151492 KB n=200000
109 Correct 1010 ms 151560 KB n=200000
110 Correct 1097 ms 151484 KB n=200000
111 Correct 897 ms 151624 KB n=200000
112 Correct 1112 ms 155984 KB n=200000
113 Correct 1266 ms 153484 KB n=200000
114 Correct 879 ms 151636 KB n=200000
115 Correct 1405 ms 152124 KB n=200000
116 Correct 902 ms 151636 KB n=200000
117 Correct 1107 ms 156000 KB n=200000
118 Correct 1233 ms 152776 KB n=200000
119 Correct 877 ms 151456 KB n=200000
120 Correct 1076 ms 155812 KB n=200000
121 Correct 1031 ms 155944 KB n=200000
122 Correct 1061 ms 156440 KB n=200000
123 Correct 567 ms 152276 KB n=200000
124 Correct 264 ms 63636 KB n=25264