Submission #919004

# Submission time Handle Problem Language Result Execution time Memory
919004 2024-01-31T03:26:49 Z Whisper Birthday gift (IZhO18_treearray) C++17
100 / 100
1271 ms 106664 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 7 ms 27224 KB n=5
2 Correct 8 ms 27224 KB n=100
3 Correct 8 ms 27228 KB n=100
4 Correct 7 ms 27308 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 7 ms 27224 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 8 ms 27224 KB n=100
10 Correct 7 ms 27224 KB n=100
11 Correct 7 ms 27224 KB n=100
12 Correct 7 ms 27228 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 8 ms 27224 KB n=100
15 Correct 8 ms 27228 KB n=100
16 Correct 8 ms 27228 KB n=100
17 Correct 7 ms 27224 KB n=100
18 Correct 10 ms 27224 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27224 KB n=100
22 Correct 7 ms 27224 KB n=100
23 Correct 8 ms 27224 KB n=100
24 Correct 8 ms 27516 KB n=100
25 Correct 7 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 7 ms 27224 KB n=5
2 Correct 8 ms 27224 KB n=100
3 Correct 8 ms 27228 KB n=100
4 Correct 7 ms 27308 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 7 ms 27224 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 8 ms 27224 KB n=100
10 Correct 7 ms 27224 KB n=100
11 Correct 7 ms 27224 KB n=100
12 Correct 7 ms 27228 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 8 ms 27224 KB n=100
15 Correct 8 ms 27228 KB n=100
16 Correct 8 ms 27228 KB n=100
17 Correct 7 ms 27224 KB n=100
18 Correct 10 ms 27224 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27224 KB n=100
22 Correct 7 ms 27224 KB n=100
23 Correct 8 ms 27224 KB n=100
24 Correct 8 ms 27516 KB n=100
25 Correct 7 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 7 ms 27224 KB n=500
30 Correct 7 ms 27228 KB n=500
31 Correct 7 ms 27224 KB n=500
32 Correct 8 ms 27224 KB n=500
33 Correct 8 ms 27228 KB n=500
34 Correct 7 ms 27224 KB n=500
35 Correct 8 ms 27228 KB n=500
36 Correct 8 ms 27480 KB n=500
37 Correct 8 ms 27224 KB n=500
38 Correct 7 ms 27224 KB n=500
39 Correct 8 ms 27228 KB n=500
40 Correct 8 ms 27224 KB n=500
41 Correct 8 ms 27228 KB n=500
42 Correct 8 ms 27224 KB n=500
43 Correct 8 ms 27224 KB n=500
44 Correct 8 ms 27224 KB n=500
45 Correct 8 ms 27228 KB n=500
46 Correct 8 ms 27224 KB n=500
47 Correct 8 ms 27224 KB n=500
48 Correct 8 ms 27224 KB n=500
49 Correct 8 ms 27224 KB n=500
50 Correct 8 ms 27224 KB n=500
51 Correct 7 ms 27224 KB n=500
52 Correct 8 ms 27224 KB n=500
53 Correct 8 ms 27224 KB n=500
54 Correct 7 ms 27224 KB n=500
55 Correct 7 ms 27228 KB n=278
56 Correct 8 ms 27224 KB n=500
57 Correct 8 ms 27228 KB n=500
58 Correct 7 ms 27224 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 7 ms 27224 KB n=5
2 Correct 8 ms 27224 KB n=100
3 Correct 8 ms 27228 KB n=100
4 Correct 7 ms 27308 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 7 ms 27224 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 8 ms 27224 KB n=100
10 Correct 7 ms 27224 KB n=100
11 Correct 7 ms 27224 KB n=100
12 Correct 7 ms 27228 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 8 ms 27224 KB n=100
15 Correct 8 ms 27228 KB n=100
16 Correct 8 ms 27228 KB n=100
17 Correct 7 ms 27224 KB n=100
18 Correct 10 ms 27224 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27224 KB n=100
22 Correct 7 ms 27224 KB n=100
23 Correct 8 ms 27224 KB n=100
24 Correct 8 ms 27516 KB n=100
25 Correct 7 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 7 ms 27224 KB n=500
30 Correct 7 ms 27228 KB n=500
31 Correct 7 ms 27224 KB n=500
32 Correct 8 ms 27224 KB n=500
33 Correct 8 ms 27228 KB n=500
34 Correct 7 ms 27224 KB n=500
35 Correct 8 ms 27228 KB n=500
36 Correct 8 ms 27480 KB n=500
37 Correct 8 ms 27224 KB n=500
38 Correct 7 ms 27224 KB n=500
39 Correct 8 ms 27228 KB n=500
40 Correct 8 ms 27224 KB n=500
41 Correct 8 ms 27228 KB n=500
42 Correct 8 ms 27224 KB n=500
43 Correct 8 ms 27224 KB n=500
44 Correct 8 ms 27224 KB n=500
45 Correct 8 ms 27228 KB n=500
46 Correct 8 ms 27224 KB n=500
47 Correct 8 ms 27224 KB n=500
48 Correct 8 ms 27224 KB n=500
49 Correct 8 ms 27224 KB n=500
50 Correct 8 ms 27224 KB n=500
51 Correct 7 ms 27224 KB n=500
52 Correct 8 ms 27224 KB n=500
53 Correct 8 ms 27224 KB n=500
54 Correct 7 ms 27224 KB n=500
55 Correct 7 ms 27228 KB n=278
56 Correct 8 ms 27224 KB n=500
57 Correct 8 ms 27228 KB n=500
58 Correct 7 ms 27224 KB n=500
59 Correct 10 ms 29528 KB n=2000
60 Correct 11 ms 29784 KB n=2000
61 Correct 11 ms 29748 KB n=2000
62 Correct 11 ms 29532 KB n=2000
63 Correct 11 ms 29528 KB n=2000
64 Correct 10 ms 29532 KB n=2000
65 Correct 10 ms 29528 KB n=2000
66 Correct 11 ms 29528 KB n=2000
67 Correct 10 ms 29528 KB n=2000
68 Correct 10 ms 29528 KB n=2000
69 Correct 10 ms 29528 KB n=2000
70 Correct 10 ms 29528 KB n=2000
71 Correct 10 ms 29528 KB n=2000
72 Correct 10 ms 29528 KB n=2000
73 Correct 9 ms 29528 KB n=2000
74 Correct 9 ms 27480 KB n=1844
75 Correct 10 ms 29528 KB n=2000
76 Correct 10 ms 29528 KB n=2000
77 Correct 12 ms 29528 KB n=2000
78 Correct 10 ms 29532 KB n=2000
79 Correct 10 ms 29528 KB n=2000
80 Correct 10 ms 29784 KB n=2000
81 Correct 10 ms 29528 KB n=2000
82 Correct 10 ms 29532 KB n=2000
83 Correct 11 ms 29528 KB n=2000
84 Correct 13 ms 29528 KB n=2000
85 Correct 10 ms 29528 KB n=2000
86 Correct 10 ms 29528 KB n=2000
87 Correct 11 ms 29528 KB n=2000
88 Correct 10 ms 29784 KB n=2000
89 Correct 9 ms 29784 KB n=2000
90 Correct 11 ms 29784 KB n=2000
91 Correct 9 ms 29528 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 7 ms 27224 KB n=5
2 Correct 8 ms 27224 KB n=100
3 Correct 8 ms 27228 KB n=100
4 Correct 7 ms 27308 KB n=100
5 Correct 7 ms 27224 KB n=100
6 Correct 7 ms 27228 KB n=100
7 Correct 7 ms 27224 KB n=100
8 Correct 7 ms 27224 KB n=100
9 Correct 8 ms 27224 KB n=100
10 Correct 7 ms 27224 KB n=100
11 Correct 7 ms 27224 KB n=100
12 Correct 7 ms 27228 KB n=100
13 Correct 7 ms 27228 KB n=100
14 Correct 8 ms 27224 KB n=100
15 Correct 8 ms 27228 KB n=100
16 Correct 8 ms 27228 KB n=100
17 Correct 7 ms 27224 KB n=100
18 Correct 10 ms 27224 KB n=100
19 Correct 7 ms 27224 KB n=100
20 Correct 7 ms 27224 KB n=100
21 Correct 7 ms 27224 KB n=100
22 Correct 7 ms 27224 KB n=100
23 Correct 8 ms 27224 KB n=100
24 Correct 8 ms 27516 KB n=100
25 Correct 7 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 7 ms 27224 KB n=500
30 Correct 7 ms 27228 KB n=500
31 Correct 7 ms 27224 KB n=500
32 Correct 8 ms 27224 KB n=500
33 Correct 8 ms 27228 KB n=500
34 Correct 7 ms 27224 KB n=500
35 Correct 8 ms 27228 KB n=500
36 Correct 8 ms 27480 KB n=500
37 Correct 8 ms 27224 KB n=500
38 Correct 7 ms 27224 KB n=500
39 Correct 8 ms 27228 KB n=500
40 Correct 8 ms 27224 KB n=500
41 Correct 8 ms 27228 KB n=500
42 Correct 8 ms 27224 KB n=500
43 Correct 8 ms 27224 KB n=500
44 Correct 8 ms 27224 KB n=500
45 Correct 8 ms 27228 KB n=500
46 Correct 8 ms 27224 KB n=500
47 Correct 8 ms 27224 KB n=500
48 Correct 8 ms 27224 KB n=500
49 Correct 8 ms 27224 KB n=500
50 Correct 8 ms 27224 KB n=500
51 Correct 7 ms 27224 KB n=500
52 Correct 8 ms 27224 KB n=500
53 Correct 8 ms 27224 KB n=500
54 Correct 7 ms 27224 KB n=500
55 Correct 7 ms 27228 KB n=278
56 Correct 8 ms 27224 KB n=500
57 Correct 8 ms 27228 KB n=500
58 Correct 7 ms 27224 KB n=500
59 Correct 10 ms 29528 KB n=2000
60 Correct 11 ms 29784 KB n=2000
61 Correct 11 ms 29748 KB n=2000
62 Correct 11 ms 29532 KB n=2000
63 Correct 11 ms 29528 KB n=2000
64 Correct 10 ms 29532 KB n=2000
65 Correct 10 ms 29528 KB n=2000
66 Correct 11 ms 29528 KB n=2000
67 Correct 10 ms 29528 KB n=2000
68 Correct 10 ms 29528 KB n=2000
69 Correct 10 ms 29528 KB n=2000
70 Correct 10 ms 29528 KB n=2000
71 Correct 10 ms 29528 KB n=2000
72 Correct 10 ms 29528 KB n=2000
73 Correct 9 ms 29528 KB n=2000
74 Correct 9 ms 27480 KB n=1844
75 Correct 10 ms 29528 KB n=2000
76 Correct 10 ms 29528 KB n=2000
77 Correct 12 ms 29528 KB n=2000
78 Correct 10 ms 29532 KB n=2000
79 Correct 10 ms 29528 KB n=2000
80 Correct 10 ms 29784 KB n=2000
81 Correct 10 ms 29528 KB n=2000
82 Correct 10 ms 29532 KB n=2000
83 Correct 11 ms 29528 KB n=2000
84 Correct 13 ms 29528 KB n=2000
85 Correct 10 ms 29528 KB n=2000
86 Correct 10 ms 29528 KB n=2000
87 Correct 11 ms 29528 KB n=2000
88 Correct 10 ms 29784 KB n=2000
89 Correct 9 ms 29784 KB n=2000
90 Correct 11 ms 29784 KB n=2000
91 Correct 9 ms 29528 KB n=2000
92 Correct 640 ms 95524 KB n=200000
93 Correct 1147 ms 101788 KB n=200000
94 Correct 1137 ms 105716 KB n=200000
95 Correct 635 ms 95256 KB n=200000
96 Correct 609 ms 95280 KB n=200000
97 Correct 1172 ms 100528 KB n=200000
98 Correct 642 ms 95420 KB n=200000
99 Correct 815 ms 95616 KB n=200000
100 Correct 655 ms 95596 KB n=200000
101 Correct 1129 ms 106188 KB n=200000
102 Correct 418 ms 95636 KB n=200000
103 Correct 415 ms 95552 KB n=200000
104 Correct 427 ms 95728 KB n=200000
105 Correct 414 ms 95840 KB n=200000
106 Correct 379 ms 96848 KB n=200000
107 Correct 394 ms 97192 KB n=200000
108 Correct 760 ms 95436 KB n=200000
109 Correct 736 ms 95472 KB n=200000
110 Correct 725 ms 95444 KB n=200000
111 Correct 620 ms 94928 KB n=200000
112 Correct 1271 ms 106196 KB n=200000
113 Correct 1175 ms 100596 KB n=200000
114 Correct 624 ms 94320 KB n=200000
115 Correct 1169 ms 96648 KB n=200000
116 Correct 640 ms 94088 KB n=200000
117 Correct 1139 ms 105276 KB n=200000
118 Correct 1139 ms 98212 KB n=200000
119 Correct 601 ms 95420 KB n=200000
120 Correct 1093 ms 106248 KB n=200000
121 Correct 1180 ms 106664 KB n=200000
122 Correct 1121 ms 106472 KB n=200000
123 Correct 421 ms 96908 KB n=200000
124 Correct 151 ms 44884 KB n=25264