Submission #919006

# Submission time Handle Problem Language Result Execution time Memory
919006 2024-01-31T03:31:19 Z Whisper Birthday gift (IZhO18_treearray) C++17
100 / 100
1207 ms 107312 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using T = tuple<ll, ll, ll>;

#define int long long
#define Base 41
#define sz(a) (int)a.size()
#define FOR(i, a, b) for ( int i = a ; i <= b ; i++ )
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define all(x) x.begin() , x.end()
#define pii pair<int , int>
#define fi first
#define se second
#define Lg(x) 31 - __builtin_clz(x)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)

constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int MAX = 2e5 + 5;
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void setupIO(){
    #define name "Whisper"
    //Phu Trong from Nguyen Tat Thanh High School for gifted student
    srand(time(NULL));
    cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    cout << fixed << setprecision(10);
}

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
int N, M, Q;
int a[MAX];
vector<int> adj[MAX];

int up[MAX][22], dep[MAX];
void dfs(int u, int p){
    for (auto&v : adj[u]) if(v ^ p){
        up[v][0] = u;
        dep[v] = dep[u] + 1;
        for (int i = 1; i <= 20; ++i){
            up[v][i] = up[up[v][i - 1]][i - 1];
        }
        dfs(v, u);
    }
}

int lca(int u, int v){
    if (dep[u] < dep[v]) swap(u, v); int dis = dep[u] - dep[v];
    for (int i = 0; MASK(i) <= dis; ++i){
        if (BIT(dis, i)) u = up[u][i];
    }
    if (u == v) return u;
    int k = Lg(dep[v]);
    for (int i = k; i >= 0; --i) if(up[u][i] != up[v][i]){
        u = up[u][i], v = up[v][i];
    }
    return up[u][0];
}

set<int> posLca[MAX], posVer[MAX];
void Whisper(){
    cin >> N >> M >> Q;
    for (int i = 1; i < N; ++i){
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= M; ++i) cin >> a[i];
    dfs(1, 0);
    for (int i = 1; i <= M; ++i){
        posVer[a[i]].insert(i);
        if (i > 1){
            int p = lca(a[i], a[i - 1]);
            posLca[p].insert(i);
        }
    }

    for (int i = 1; i <= Q; ++i){
        int cmd; cin >> cmd;
        if(cmd == 2){
            int l, r, p; cin >> l >> r >> p;
            if(posVer[p].size() > 0) {
				auto L = posVer[p].lower_bound(l);
				if(L != posVer[p].end() && *L <= r) {
					cout << *L << ' ' << *L << '\n';
					continue;
				}
			}
			if(posLca[p].size() > 0) {
				auto L = posLca[p].lower_bound(l + 1);
				if(L != posLca[p].end() && *L <= r) {
					cout << (*L) - 1 << ' ' << *L << '\n';
					continue;
				}
			}
			cout << "-1 -1\n";
        }
        else{
            int p, v; cin >> p >> v;
            posVer[a[p]].erase(p);
            if (p > 1) posLca[lca(a[p], a[p - 1])].erase(p);
            if (p < M) posLca[lca(a[p], a[p + 1])].erase(p + 1);
            a[p] = v;
            posVer[a[p]].insert(p);
            if (p > 1) posLca[lca(a[p], a[p - 1])].insert(p);
            if (p < M) posLca[lca(a[p], a[p + 1])].insert(p + 1);
        }
    }
}


signed main(){
    setupIO();
    int Test = 1;
//    cin >> Test;
    for ( int i = 1 ; i <= Test ; i++ ){
        Whisper();
        if (i < Test) cout << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27224 KB n=5
2 Correct 7 ms 27224 KB n=100
3 Correct 7 ms 27228 KB n=100
4 Correct 9 ms 27228 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 8 ms 27228 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 7 ms 27228 KB n=100
10 Correct 8 ms 27224 KB n=100
11 Correct 8 ms 27224 KB n=100
12 Correct 7 ms 27224 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 7 ms 27224 KB n=100
15 Correct 9 ms 27480 KB n=100
16 Correct 7 ms 27224 KB n=100
17 Correct 8 ms 27224 KB n=100
18 Correct 7 ms 27228 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 8 ms 27224 KB n=100
23 Correct 7 ms 27228 KB n=100
24 Correct 8 ms 27224 KB n=100
25 Correct 8 ms 27224 KB n=100
26 Correct 7 ms 27224 KB n=12
27 Correct 7 ms 27224 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27224 KB n=5
2 Correct 7 ms 27224 KB n=100
3 Correct 7 ms 27228 KB n=100
4 Correct 9 ms 27228 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 8 ms 27228 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 7 ms 27228 KB n=100
10 Correct 8 ms 27224 KB n=100
11 Correct 8 ms 27224 KB n=100
12 Correct 7 ms 27224 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 7 ms 27224 KB n=100
15 Correct 9 ms 27480 KB n=100
16 Correct 7 ms 27224 KB n=100
17 Correct 8 ms 27224 KB n=100
18 Correct 7 ms 27228 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 8 ms 27224 KB n=100
23 Correct 7 ms 27228 KB n=100
24 Correct 8 ms 27224 KB n=100
25 Correct 8 ms 27224 KB n=100
26 Correct 7 ms 27224 KB n=12
27 Correct 7 ms 27224 KB n=100
28 Correct 8 ms 27224 KB n=500
29 Correct 8 ms 27224 KB n=500
30 Correct 8 ms 27224 KB n=500
31 Correct 8 ms 27228 KB n=500
32 Correct 7 ms 27224 KB n=500
33 Correct 8 ms 27228 KB n=500
34 Correct 8 ms 27224 KB n=500
35 Correct 8 ms 27224 KB n=500
36 Correct 8 ms 27228 KB n=500
37 Correct 8 ms 27228 KB n=500
38 Correct 9 ms 27224 KB n=500
39 Correct 9 ms 27324 KB n=500
40 Correct 8 ms 27228 KB n=500
41 Correct 10 ms 27228 KB n=500
42 Correct 8 ms 27224 KB n=500
43 Correct 7 ms 27228 KB n=500
44 Correct 8 ms 27224 KB n=500
45 Correct 8 ms 27224 KB n=500
46 Correct 8 ms 27228 KB n=500
47 Correct 7 ms 27228 KB n=500
48 Correct 10 ms 27228 KB n=500
49 Correct 8 ms 27224 KB n=500
50 Correct 9 ms 27228 KB n=500
51 Correct 7 ms 27224 KB n=500
52 Correct 8 ms 27236 KB n=500
53 Correct 8 ms 27224 KB n=500
54 Correct 8 ms 27224 KB n=500
55 Correct 9 ms 27224 KB n=278
56 Correct 8 ms 27224 KB n=500
57 Correct 7 ms 27224 KB n=500
58 Correct 7 ms 27224 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27224 KB n=5
2 Correct 7 ms 27224 KB n=100
3 Correct 7 ms 27228 KB n=100
4 Correct 9 ms 27228 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 8 ms 27228 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 7 ms 27228 KB n=100
10 Correct 8 ms 27224 KB n=100
11 Correct 8 ms 27224 KB n=100
12 Correct 7 ms 27224 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 7 ms 27224 KB n=100
15 Correct 9 ms 27480 KB n=100
16 Correct 7 ms 27224 KB n=100
17 Correct 8 ms 27224 KB n=100
18 Correct 7 ms 27228 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 8 ms 27224 KB n=100
23 Correct 7 ms 27228 KB n=100
24 Correct 8 ms 27224 KB n=100
25 Correct 8 ms 27224 KB n=100
26 Correct 7 ms 27224 KB n=12
27 Correct 7 ms 27224 KB n=100
28 Correct 8 ms 27224 KB n=500
29 Correct 8 ms 27224 KB n=500
30 Correct 8 ms 27224 KB n=500
31 Correct 8 ms 27228 KB n=500
32 Correct 7 ms 27224 KB n=500
33 Correct 8 ms 27228 KB n=500
34 Correct 8 ms 27224 KB n=500
35 Correct 8 ms 27224 KB n=500
36 Correct 8 ms 27228 KB n=500
37 Correct 8 ms 27228 KB n=500
38 Correct 9 ms 27224 KB n=500
39 Correct 9 ms 27324 KB n=500
40 Correct 8 ms 27228 KB n=500
41 Correct 10 ms 27228 KB n=500
42 Correct 8 ms 27224 KB n=500
43 Correct 7 ms 27228 KB n=500
44 Correct 8 ms 27224 KB n=500
45 Correct 8 ms 27224 KB n=500
46 Correct 8 ms 27228 KB n=500
47 Correct 7 ms 27228 KB n=500
48 Correct 10 ms 27228 KB n=500
49 Correct 8 ms 27224 KB n=500
50 Correct 9 ms 27228 KB n=500
51 Correct 7 ms 27224 KB n=500
52 Correct 8 ms 27236 KB n=500
53 Correct 8 ms 27224 KB n=500
54 Correct 8 ms 27224 KB n=500
55 Correct 9 ms 27224 KB n=278
56 Correct 8 ms 27224 KB n=500
57 Correct 7 ms 27224 KB n=500
58 Correct 7 ms 27224 KB n=500
59 Correct 11 ms 29528 KB n=2000
60 Correct 10 ms 29788 KB n=2000
61 Correct 10 ms 29528 KB n=2000
62 Correct 11 ms 29528 KB n=2000
63 Correct 14 ms 29528 KB n=2000
64 Correct 13 ms 29528 KB n=2000
65 Correct 10 ms 29528 KB n=2000
66 Correct 10 ms 29528 KB n=2000
67 Correct 10 ms 29528 KB n=2000
68 Correct 10 ms 29528 KB n=2000
69 Correct 11 ms 29528 KB n=2000
70 Correct 11 ms 29472 KB n=2000
71 Correct 12 ms 29528 KB n=2000
72 Correct 11 ms 29528 KB n=2000
73 Correct 11 ms 29484 KB n=2000
74 Correct 10 ms 27480 KB n=1844
75 Correct 9 ms 29684 KB n=2000
76 Correct 11 ms 29528 KB n=2000
77 Correct 10 ms 29532 KB n=2000
78 Correct 10 ms 29528 KB n=2000
79 Correct 10 ms 29528 KB n=2000
80 Correct 10 ms 29784 KB n=2000
81 Correct 11 ms 29528 KB n=2000
82 Correct 11 ms 29528 KB n=2000
83 Correct 10 ms 29532 KB n=2000
84 Correct 11 ms 29620 KB n=2000
85 Correct 13 ms 29528 KB n=2000
86 Correct 10 ms 29532 KB n=2000
87 Correct 10 ms 29528 KB n=2000
88 Correct 12 ms 29616 KB n=2000
89 Correct 10 ms 29784 KB n=2000
90 Correct 11 ms 29784 KB n=2000
91 Correct 10 ms 29528 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 8 ms 27224 KB n=5
2 Correct 7 ms 27224 KB n=100
3 Correct 7 ms 27228 KB n=100
4 Correct 9 ms 27228 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 8 ms 27228 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 7 ms 27228 KB n=100
10 Correct 8 ms 27224 KB n=100
11 Correct 8 ms 27224 KB n=100
12 Correct 7 ms 27224 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 7 ms 27224 KB n=100
15 Correct 9 ms 27480 KB n=100
16 Correct 7 ms 27224 KB n=100
17 Correct 8 ms 27224 KB n=100
18 Correct 7 ms 27228 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 8 ms 27224 KB n=100
23 Correct 7 ms 27228 KB n=100
24 Correct 8 ms 27224 KB n=100
25 Correct 8 ms 27224 KB n=100
26 Correct 7 ms 27224 KB n=12
27 Correct 7 ms 27224 KB n=100
28 Correct 8 ms 27224 KB n=500
29 Correct 8 ms 27224 KB n=500
30 Correct 8 ms 27224 KB n=500
31 Correct 8 ms 27228 KB n=500
32 Correct 7 ms 27224 KB n=500
33 Correct 8 ms 27228 KB n=500
34 Correct 8 ms 27224 KB n=500
35 Correct 8 ms 27224 KB n=500
36 Correct 8 ms 27228 KB n=500
37 Correct 8 ms 27228 KB n=500
38 Correct 9 ms 27224 KB n=500
39 Correct 9 ms 27324 KB n=500
40 Correct 8 ms 27228 KB n=500
41 Correct 10 ms 27228 KB n=500
42 Correct 8 ms 27224 KB n=500
43 Correct 7 ms 27228 KB n=500
44 Correct 8 ms 27224 KB n=500
45 Correct 8 ms 27224 KB n=500
46 Correct 8 ms 27228 KB n=500
47 Correct 7 ms 27228 KB n=500
48 Correct 10 ms 27228 KB n=500
49 Correct 8 ms 27224 KB n=500
50 Correct 9 ms 27228 KB n=500
51 Correct 7 ms 27224 KB n=500
52 Correct 8 ms 27236 KB n=500
53 Correct 8 ms 27224 KB n=500
54 Correct 8 ms 27224 KB n=500
55 Correct 9 ms 27224 KB n=278
56 Correct 8 ms 27224 KB n=500
57 Correct 7 ms 27224 KB n=500
58 Correct 7 ms 27224 KB n=500
59 Correct 11 ms 29528 KB n=2000
60 Correct 10 ms 29788 KB n=2000
61 Correct 10 ms 29528 KB n=2000
62 Correct 11 ms 29528 KB n=2000
63 Correct 14 ms 29528 KB n=2000
64 Correct 13 ms 29528 KB n=2000
65 Correct 10 ms 29528 KB n=2000
66 Correct 10 ms 29528 KB n=2000
67 Correct 10 ms 29528 KB n=2000
68 Correct 10 ms 29528 KB n=2000
69 Correct 11 ms 29528 KB n=2000
70 Correct 11 ms 29472 KB n=2000
71 Correct 12 ms 29528 KB n=2000
72 Correct 11 ms 29528 KB n=2000
73 Correct 11 ms 29484 KB n=2000
74 Correct 10 ms 27480 KB n=1844
75 Correct 9 ms 29684 KB n=2000
76 Correct 11 ms 29528 KB n=2000
77 Correct 10 ms 29532 KB n=2000
78 Correct 10 ms 29528 KB n=2000
79 Correct 10 ms 29528 KB n=2000
80 Correct 10 ms 29784 KB n=2000
81 Correct 11 ms 29528 KB n=2000
82 Correct 11 ms 29528 KB n=2000
83 Correct 10 ms 29532 KB n=2000
84 Correct 11 ms 29620 KB n=2000
85 Correct 13 ms 29528 KB n=2000
86 Correct 10 ms 29532 KB n=2000
87 Correct 10 ms 29528 KB n=2000
88 Correct 12 ms 29616 KB n=2000
89 Correct 10 ms 29784 KB n=2000
90 Correct 11 ms 29784 KB n=2000
91 Correct 10 ms 29528 KB n=2000
92 Correct 580 ms 95184 KB n=200000
93 Correct 1125 ms 101604 KB n=200000
94 Correct 1187 ms 105664 KB n=200000
95 Correct 610 ms 94908 KB n=200000
96 Correct 563 ms 95092 KB n=200000
97 Correct 1129 ms 100580 KB n=200000
98 Correct 597 ms 95108 KB n=200000
99 Correct 776 ms 95412 KB n=200000
100 Correct 639 ms 95428 KB n=200000
101 Correct 1152 ms 107312 KB n=200000
102 Correct 395 ms 96952 KB n=200000
103 Correct 399 ms 96596 KB n=200000
104 Correct 394 ms 96592 KB n=200000
105 Correct 406 ms 96320 KB n=200000
106 Correct 397 ms 96952 KB n=200000
107 Correct 392 ms 97040 KB n=200000
108 Correct 701 ms 95312 KB n=200000
109 Correct 688 ms 95312 KB n=200000
110 Correct 727 ms 95312 KB n=200000
111 Correct 631 ms 94984 KB n=200000
112 Correct 1207 ms 106244 KB n=200000
113 Correct 1106 ms 100496 KB n=200000
114 Correct 592 ms 94820 KB n=200000
115 Correct 1160 ms 98128 KB n=200000
116 Correct 576 ms 95672 KB n=200000
117 Correct 1184 ms 106776 KB n=200000
118 Correct 1058 ms 98400 KB n=200000
119 Correct 575 ms 95164 KB n=200000
120 Correct 1044 ms 106332 KB n=200000
121 Correct 1097 ms 106148 KB n=200000
122 Correct 1052 ms 106444 KB n=200000
123 Correct 425 ms 97104 KB n=200000
124 Correct 140 ms 44748 KB n=25264