답안 #919003

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
919003 2024-01-31T03:23:42 Z Whisper Birthday gift (IZhO18_treearray) C++17
100 / 100
1524 ms 102792 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][21], dep[MAX];
void dfs(int s, int p) {
	up[s][0] = p;
	for(int i = 1; i <= 20; i++) {
		up[s][i] = up[up[s][i - 1]][i - 1];
	}
	for(auto u : adj[s]) {
		if(u == p) continue;
		dep[u] = dep[s] + 1;
		dfs(u, s);
	}
}

int lca(int u, int v) {
	if(dep[u] < dep[v]) swap(u, v);
	for(int i = 20; i >= 0; i--) if(dep[u] - dep[v] >= (1 << i)) u = up[u][i];
	if(u == v) return u;
	for(int i = 20; 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';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27480 KB n=5
2 Correct 7 ms 27480 KB n=100
3 Correct 7 ms 27480 KB n=100
4 Correct 9 ms 27640 KB n=100
5 Correct 7 ms 27480 KB n=100
6 Correct 8 ms 27480 KB n=100
7 Correct 7 ms 27480 KB n=100
8 Correct 8 ms 27480 KB n=100
9 Correct 9 ms 27832 KB n=100
10 Correct 7 ms 27480 KB n=100
11 Correct 8 ms 27480 KB n=100
12 Correct 9 ms 27616 KB n=100
13 Correct 8 ms 27736 KB n=100
14 Correct 7 ms 27480 KB n=100
15 Correct 8 ms 27480 KB n=100
16 Correct 7 ms 27480 KB n=100
17 Correct 8 ms 27480 KB n=100
18 Correct 7 ms 27484 KB n=100
19 Correct 7 ms 27480 KB n=100
20 Correct 8 ms 27480 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 9 ms 27484 KB n=100
23 Correct 7 ms 27480 KB n=100
24 Correct 8 ms 27480 KB n=100
25 Correct 9 ms 27880 KB n=100
26 Correct 11 ms 27736 KB n=12
27 Correct 7 ms 27480 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27480 KB n=5
2 Correct 7 ms 27480 KB n=100
3 Correct 7 ms 27480 KB n=100
4 Correct 9 ms 27640 KB n=100
5 Correct 7 ms 27480 KB n=100
6 Correct 8 ms 27480 KB n=100
7 Correct 7 ms 27480 KB n=100
8 Correct 8 ms 27480 KB n=100
9 Correct 9 ms 27832 KB n=100
10 Correct 7 ms 27480 KB n=100
11 Correct 8 ms 27480 KB n=100
12 Correct 9 ms 27616 KB n=100
13 Correct 8 ms 27736 KB n=100
14 Correct 7 ms 27480 KB n=100
15 Correct 8 ms 27480 KB n=100
16 Correct 7 ms 27480 KB n=100
17 Correct 8 ms 27480 KB n=100
18 Correct 7 ms 27484 KB n=100
19 Correct 7 ms 27480 KB n=100
20 Correct 8 ms 27480 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 9 ms 27484 KB n=100
23 Correct 7 ms 27480 KB n=100
24 Correct 8 ms 27480 KB n=100
25 Correct 9 ms 27880 KB n=100
26 Correct 11 ms 27736 KB n=12
27 Correct 7 ms 27480 KB n=100
28 Correct 8 ms 27480 KB n=500
29 Correct 9 ms 27480 KB n=500
30 Correct 9 ms 27484 KB n=500
31 Correct 8 ms 27480 KB n=500
32 Correct 8 ms 27480 KB n=500
33 Correct 8 ms 27484 KB n=500
34 Correct 8 ms 27480 KB n=500
35 Correct 9 ms 27480 KB n=500
36 Correct 7 ms 27480 KB n=500
37 Correct 7 ms 27480 KB n=500
38 Correct 10 ms 27992 KB n=500
39 Correct 8 ms 27480 KB n=500
40 Correct 8 ms 27480 KB n=500
41 Correct 8 ms 27480 KB n=500
42 Correct 8 ms 27484 KB n=500
43 Correct 8 ms 27480 KB n=500
44 Correct 8 ms 27480 KB n=500
45 Correct 8 ms 27480 KB n=500
46 Correct 8 ms 27484 KB n=500
47 Correct 8 ms 27736 KB n=500
48 Correct 8 ms 27480 KB n=500
49 Correct 8 ms 27480 KB n=500
50 Correct 8 ms 27620 KB n=500
51 Correct 8 ms 27480 KB n=500
52 Correct 8 ms 27480 KB n=500
53 Correct 8 ms 27480 KB n=500
54 Correct 8 ms 27480 KB n=500
55 Correct 8 ms 27480 KB n=278
56 Correct 8 ms 27480 KB n=500
57 Correct 8 ms 27628 KB n=500
58 Correct 8 ms 27480 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27480 KB n=5
2 Correct 7 ms 27480 KB n=100
3 Correct 7 ms 27480 KB n=100
4 Correct 9 ms 27640 KB n=100
5 Correct 7 ms 27480 KB n=100
6 Correct 8 ms 27480 KB n=100
7 Correct 7 ms 27480 KB n=100
8 Correct 8 ms 27480 KB n=100
9 Correct 9 ms 27832 KB n=100
10 Correct 7 ms 27480 KB n=100
11 Correct 8 ms 27480 KB n=100
12 Correct 9 ms 27616 KB n=100
13 Correct 8 ms 27736 KB n=100
14 Correct 7 ms 27480 KB n=100
15 Correct 8 ms 27480 KB n=100
16 Correct 7 ms 27480 KB n=100
17 Correct 8 ms 27480 KB n=100
18 Correct 7 ms 27484 KB n=100
19 Correct 7 ms 27480 KB n=100
20 Correct 8 ms 27480 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 9 ms 27484 KB n=100
23 Correct 7 ms 27480 KB n=100
24 Correct 8 ms 27480 KB n=100
25 Correct 9 ms 27880 KB n=100
26 Correct 11 ms 27736 KB n=12
27 Correct 7 ms 27480 KB n=100
28 Correct 8 ms 27480 KB n=500
29 Correct 9 ms 27480 KB n=500
30 Correct 9 ms 27484 KB n=500
31 Correct 8 ms 27480 KB n=500
32 Correct 8 ms 27480 KB n=500
33 Correct 8 ms 27484 KB n=500
34 Correct 8 ms 27480 KB n=500
35 Correct 9 ms 27480 KB n=500
36 Correct 7 ms 27480 KB n=500
37 Correct 7 ms 27480 KB n=500
38 Correct 10 ms 27992 KB n=500
39 Correct 8 ms 27480 KB n=500
40 Correct 8 ms 27480 KB n=500
41 Correct 8 ms 27480 KB n=500
42 Correct 8 ms 27484 KB n=500
43 Correct 8 ms 27480 KB n=500
44 Correct 8 ms 27480 KB n=500
45 Correct 8 ms 27480 KB n=500
46 Correct 8 ms 27484 KB n=500
47 Correct 8 ms 27736 KB n=500
48 Correct 8 ms 27480 KB n=500
49 Correct 8 ms 27480 KB n=500
50 Correct 8 ms 27620 KB n=500
51 Correct 8 ms 27480 KB n=500
52 Correct 8 ms 27480 KB n=500
53 Correct 8 ms 27480 KB n=500
54 Correct 8 ms 27480 KB n=500
55 Correct 8 ms 27480 KB n=278
56 Correct 8 ms 27480 KB n=500
57 Correct 8 ms 27628 KB n=500
58 Correct 8 ms 27480 KB n=500
59 Correct 10 ms 27736 KB n=2000
60 Correct 11 ms 27992 KB n=2000
61 Correct 11 ms 27996 KB n=2000
62 Correct 10 ms 27736 KB n=2000
63 Correct 10 ms 27736 KB n=2000
64 Correct 11 ms 27736 KB n=2000
65 Correct 10 ms 27736 KB n=2000
66 Correct 10 ms 27992 KB n=2000
67 Correct 11 ms 27736 KB n=2000
68 Correct 10 ms 27992 KB n=2000
69 Correct 10 ms 27736 KB n=2000
70 Correct 10 ms 27736 KB n=2000
71 Correct 9 ms 27736 KB n=2000
72 Correct 9 ms 27736 KB n=2000
73 Correct 9 ms 27736 KB n=2000
74 Correct 9 ms 27940 KB n=1844
75 Correct 11 ms 27736 KB n=2000
76 Correct 10 ms 27736 KB n=2000
77 Correct 12 ms 27736 KB n=2000
78 Correct 10 ms 27736 KB n=2000
79 Correct 10 ms 27736 KB n=2000
80 Correct 12 ms 27992 KB n=2000
81 Correct 11 ms 27740 KB n=2000
82 Correct 11 ms 27912 KB n=2000
83 Correct 10 ms 27992 KB n=2000
84 Correct 11 ms 27740 KB n=2000
85 Correct 10 ms 27736 KB n=2000
86 Correct 11 ms 27736 KB n=2000
87 Correct 10 ms 27992 KB n=2000
88 Correct 9 ms 27996 KB n=2000
89 Correct 10 ms 27992 KB n=2000
90 Correct 9 ms 27992 KB n=2000
91 Correct 10 ms 27740 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27480 KB n=5
2 Correct 7 ms 27480 KB n=100
3 Correct 7 ms 27480 KB n=100
4 Correct 9 ms 27640 KB n=100
5 Correct 7 ms 27480 KB n=100
6 Correct 8 ms 27480 KB n=100
7 Correct 7 ms 27480 KB n=100
8 Correct 8 ms 27480 KB n=100
9 Correct 9 ms 27832 KB n=100
10 Correct 7 ms 27480 KB n=100
11 Correct 8 ms 27480 KB n=100
12 Correct 9 ms 27616 KB n=100
13 Correct 8 ms 27736 KB n=100
14 Correct 7 ms 27480 KB n=100
15 Correct 8 ms 27480 KB n=100
16 Correct 7 ms 27480 KB n=100
17 Correct 8 ms 27480 KB n=100
18 Correct 7 ms 27484 KB n=100
19 Correct 7 ms 27480 KB n=100
20 Correct 8 ms 27480 KB n=100
21 Correct 7 ms 27480 KB n=100
22 Correct 9 ms 27484 KB n=100
23 Correct 7 ms 27480 KB n=100
24 Correct 8 ms 27480 KB n=100
25 Correct 9 ms 27880 KB n=100
26 Correct 11 ms 27736 KB n=12
27 Correct 7 ms 27480 KB n=100
28 Correct 8 ms 27480 KB n=500
29 Correct 9 ms 27480 KB n=500
30 Correct 9 ms 27484 KB n=500
31 Correct 8 ms 27480 KB n=500
32 Correct 8 ms 27480 KB n=500
33 Correct 8 ms 27484 KB n=500
34 Correct 8 ms 27480 KB n=500
35 Correct 9 ms 27480 KB n=500
36 Correct 7 ms 27480 KB n=500
37 Correct 7 ms 27480 KB n=500
38 Correct 10 ms 27992 KB n=500
39 Correct 8 ms 27480 KB n=500
40 Correct 8 ms 27480 KB n=500
41 Correct 8 ms 27480 KB n=500
42 Correct 8 ms 27484 KB n=500
43 Correct 8 ms 27480 KB n=500
44 Correct 8 ms 27480 KB n=500
45 Correct 8 ms 27480 KB n=500
46 Correct 8 ms 27484 KB n=500
47 Correct 8 ms 27736 KB n=500
48 Correct 8 ms 27480 KB n=500
49 Correct 8 ms 27480 KB n=500
50 Correct 8 ms 27620 KB n=500
51 Correct 8 ms 27480 KB n=500
52 Correct 8 ms 27480 KB n=500
53 Correct 8 ms 27480 KB n=500
54 Correct 8 ms 27480 KB n=500
55 Correct 8 ms 27480 KB n=278
56 Correct 8 ms 27480 KB n=500
57 Correct 8 ms 27628 KB n=500
58 Correct 8 ms 27480 KB n=500
59 Correct 10 ms 27736 KB n=2000
60 Correct 11 ms 27992 KB n=2000
61 Correct 11 ms 27996 KB n=2000
62 Correct 10 ms 27736 KB n=2000
63 Correct 10 ms 27736 KB n=2000
64 Correct 11 ms 27736 KB n=2000
65 Correct 10 ms 27736 KB n=2000
66 Correct 10 ms 27992 KB n=2000
67 Correct 11 ms 27736 KB n=2000
68 Correct 10 ms 27992 KB n=2000
69 Correct 10 ms 27736 KB n=2000
70 Correct 10 ms 27736 KB n=2000
71 Correct 9 ms 27736 KB n=2000
72 Correct 9 ms 27736 KB n=2000
73 Correct 9 ms 27736 KB n=2000
74 Correct 9 ms 27940 KB n=1844
75 Correct 11 ms 27736 KB n=2000
76 Correct 10 ms 27736 KB n=2000
77 Correct 12 ms 27736 KB n=2000
78 Correct 10 ms 27736 KB n=2000
79 Correct 10 ms 27736 KB n=2000
80 Correct 12 ms 27992 KB n=2000
81 Correct 11 ms 27740 KB n=2000
82 Correct 11 ms 27912 KB n=2000
83 Correct 10 ms 27992 KB n=2000
84 Correct 11 ms 27740 KB n=2000
85 Correct 10 ms 27736 KB n=2000
86 Correct 11 ms 27736 KB n=2000
87 Correct 10 ms 27992 KB n=2000
88 Correct 9 ms 27996 KB n=2000
89 Correct 10 ms 27992 KB n=2000
90 Correct 9 ms 27992 KB n=2000
91 Correct 10 ms 27740 KB n=2000
92 Correct 807 ms 94252 KB n=200000
93 Correct 1524 ms 99208 KB n=200000
94 Correct 1249 ms 101968 KB n=200000
95 Correct 805 ms 94128 KB n=200000
96 Correct 755 ms 94100 KB n=200000
97 Correct 1462 ms 98340 KB n=200000
98 Correct 815 ms 94420 KB n=200000
99 Correct 1012 ms 94588 KB n=200000
100 Correct 772 ms 94144 KB n=200000
101 Correct 1349 ms 102792 KB n=200000
102 Correct 444 ms 95568 KB n=200000
103 Correct 429 ms 95312 KB n=200000
104 Correct 416 ms 95312 KB n=200000
105 Correct 424 ms 95684 KB n=200000
106 Correct 437 ms 96208 KB n=200000
107 Correct 443 ms 96040 KB n=200000
108 Correct 852 ms 94224 KB n=200000
109 Correct 890 ms 94428 KB n=200000
110 Correct 868 ms 94452 KB n=200000
111 Correct 740 ms 93368 KB n=200000
112 Correct 1339 ms 102300 KB n=200000
113 Correct 1377 ms 98236 KB n=200000
114 Correct 735 ms 93624 KB n=200000
115 Correct 1248 ms 96312 KB n=200000
116 Correct 769 ms 94384 KB n=200000
117 Correct 1356 ms 102556 KB n=200000
118 Correct 1311 ms 97224 KB n=200000
119 Correct 741 ms 94216 KB n=200000
120 Correct 1333 ms 101940 KB n=200000
121 Correct 1280 ms 101740 KB n=200000
122 Correct 1349 ms 102520 KB n=200000
123 Correct 440 ms 95532 KB n=200000
124 Correct 175 ms 44880 KB n=25264