답안 #985293

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
985293 2024-05-17T14:40:53 Z steveonalex Birthday gift (IZhO18_treearray) C++17
100 / 100
860 ms 82528 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)

// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}

ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 2e5 + 69, LOG_N = 18;
int n, m, q; 
vector<int> graph[N];
int h[N], parent[N][LOG_N];

void dfs(int u, int p){
    h[u] = h[p] + 1;
    for(int j = 1; MASK(j) < h[u]; ++j) 
        parent[u][j] = parent[parent[u][j-1]][j-1];
    for(int v: graph[u]) if (v != p){
        parent[v][0] = u;
        dfs(v, u);
    }
}

int LCA(int u, int v){
    if (h[u] < h[v]) swap(u, v);
    int diff = h[u] - h[v];
    for(int j = 0; j < LOG_N; ++j) if (GETBIT(diff, j)) 
        u = parent[u][j];
    if (u == v) return u;
    for(int j = LOG_N - 1; j>=0; --j) if (parent[u][j] != parent[v][j]){
        u = parent[u][j];
        v = parent[v][j];
    }
    return parent[u][0];
}

set<int> S[N];
set<int> val[N];
int a[N];

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> m >> q;
    for(int i = 1; i<n; ++i){
        int u, v; cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }

    dfs(1, 0);

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

    for(int i = 1; i<m; ++i) S[LCA(a[i], a[i+1])].insert(i);
    for(int i = 1; i<=m; ++i) val[a[i]].insert(i);

    while(q--){
        int type; cin >> type;
        if (type == 1){
            int i, v; cin >> i >> v;
            if (i > 1){
                S[LCA(a[i-1], a[i])].erase(i-1);
                S[LCA(a[i-1], v)].insert(i-1);
            }
            if (i < m){
                S[LCA(a[i], a[i+1])].erase(i);
                S[LCA(v, a[i+1])].insert(i);
            }
            val[a[i]].erase(i);
            a[i] = v;
            val[a[i]].insert(i);
        }
        else{
            int l, r, v; cin >> l >> r >> v;
            auto it = S[v].lower_bound(l);
            if (it == S[v].end() || *it >= r) {
                it = val[v].lower_bound(l);
                if (it == val[v].end() || *it > r) cout << "-1 -1\n";
                else cout << *it << " " << *it << "\n";
            }
            else{
                int u = *it;
                cout << u << " " << u+1 << "\n";
            }
        }
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27320 KB n=5
2 Correct 6 ms 26972 KB n=100
3 Correct 6 ms 26972 KB n=100
4 Correct 6 ms 27080 KB n=100
5 Correct 6 ms 26972 KB n=100
6 Correct 6 ms 26972 KB n=100
7 Correct 6 ms 27128 KB n=100
8 Correct 6 ms 26968 KB n=100
9 Correct 7 ms 27304 KB n=100
10 Correct 6 ms 26972 KB n=100
11 Correct 5 ms 26968 KB n=100
12 Correct 5 ms 26972 KB n=100
13 Correct 5 ms 26972 KB n=100
14 Correct 5 ms 26972 KB n=100
15 Correct 6 ms 26972 KB n=100
16 Correct 5 ms 26972 KB n=100
17 Correct 6 ms 26972 KB n=100
18 Correct 5 ms 26972 KB n=100
19 Correct 6 ms 26972 KB n=100
20 Correct 5 ms 26968 KB n=100
21 Correct 5 ms 26972 KB n=100
22 Correct 5 ms 26972 KB n=100
23 Correct 6 ms 26968 KB n=100
24 Correct 5 ms 26972 KB n=100
25 Correct 5 ms 27064 KB n=100
26 Correct 5 ms 26972 KB n=12
27 Correct 6 ms 26972 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27320 KB n=5
2 Correct 6 ms 26972 KB n=100
3 Correct 6 ms 26972 KB n=100
4 Correct 6 ms 27080 KB n=100
5 Correct 6 ms 26972 KB n=100
6 Correct 6 ms 26972 KB n=100
7 Correct 6 ms 27128 KB n=100
8 Correct 6 ms 26968 KB n=100
9 Correct 7 ms 27304 KB n=100
10 Correct 6 ms 26972 KB n=100
11 Correct 5 ms 26968 KB n=100
12 Correct 5 ms 26972 KB n=100
13 Correct 5 ms 26972 KB n=100
14 Correct 5 ms 26972 KB n=100
15 Correct 6 ms 26972 KB n=100
16 Correct 5 ms 26972 KB n=100
17 Correct 6 ms 26972 KB n=100
18 Correct 5 ms 26972 KB n=100
19 Correct 6 ms 26972 KB n=100
20 Correct 5 ms 26968 KB n=100
21 Correct 5 ms 26972 KB n=100
22 Correct 5 ms 26972 KB n=100
23 Correct 6 ms 26968 KB n=100
24 Correct 5 ms 26972 KB n=100
25 Correct 5 ms 27064 KB n=100
26 Correct 5 ms 26972 KB n=12
27 Correct 6 ms 26972 KB n=100
28 Correct 6 ms 26968 KB n=500
29 Correct 6 ms 27224 KB n=500
30 Correct 6 ms 27060 KB n=500
31 Correct 6 ms 26972 KB n=500
32 Correct 7 ms 26972 KB n=500
33 Correct 7 ms 27064 KB n=500
34 Correct 7 ms 26972 KB n=500
35 Correct 6 ms 27144 KB n=500
36 Correct 7 ms 26972 KB n=500
37 Correct 6 ms 26972 KB n=500
38 Correct 6 ms 26972 KB n=500
39 Correct 6 ms 26972 KB n=500
40 Correct 6 ms 26972 KB n=500
41 Correct 6 ms 27060 KB n=500
42 Correct 6 ms 26968 KB n=500
43 Correct 7 ms 26972 KB n=500
44 Correct 6 ms 26972 KB n=500
45 Correct 6 ms 26968 KB n=500
46 Correct 6 ms 27228 KB n=500
47 Correct 6 ms 26972 KB n=500
48 Correct 6 ms 26972 KB n=500
49 Correct 6 ms 27224 KB n=500
50 Correct 6 ms 26972 KB n=500
51 Correct 7 ms 27228 KB n=500
52 Correct 7 ms 27228 KB n=500
53 Correct 6 ms 26972 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 7 ms 26972 KB n=278
56 Correct 6 ms 26972 KB n=500
57 Correct 6 ms 27224 KB n=500
58 Correct 6 ms 26968 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27320 KB n=5
2 Correct 6 ms 26972 KB n=100
3 Correct 6 ms 26972 KB n=100
4 Correct 6 ms 27080 KB n=100
5 Correct 6 ms 26972 KB n=100
6 Correct 6 ms 26972 KB n=100
7 Correct 6 ms 27128 KB n=100
8 Correct 6 ms 26968 KB n=100
9 Correct 7 ms 27304 KB n=100
10 Correct 6 ms 26972 KB n=100
11 Correct 5 ms 26968 KB n=100
12 Correct 5 ms 26972 KB n=100
13 Correct 5 ms 26972 KB n=100
14 Correct 5 ms 26972 KB n=100
15 Correct 6 ms 26972 KB n=100
16 Correct 5 ms 26972 KB n=100
17 Correct 6 ms 26972 KB n=100
18 Correct 5 ms 26972 KB n=100
19 Correct 6 ms 26972 KB n=100
20 Correct 5 ms 26968 KB n=100
21 Correct 5 ms 26972 KB n=100
22 Correct 5 ms 26972 KB n=100
23 Correct 6 ms 26968 KB n=100
24 Correct 5 ms 26972 KB n=100
25 Correct 5 ms 27064 KB n=100
26 Correct 5 ms 26972 KB n=12
27 Correct 6 ms 26972 KB n=100
28 Correct 6 ms 26968 KB n=500
29 Correct 6 ms 27224 KB n=500
30 Correct 6 ms 27060 KB n=500
31 Correct 6 ms 26972 KB n=500
32 Correct 7 ms 26972 KB n=500
33 Correct 7 ms 27064 KB n=500
34 Correct 7 ms 26972 KB n=500
35 Correct 6 ms 27144 KB n=500
36 Correct 7 ms 26972 KB n=500
37 Correct 6 ms 26972 KB n=500
38 Correct 6 ms 26972 KB n=500
39 Correct 6 ms 26972 KB n=500
40 Correct 6 ms 26972 KB n=500
41 Correct 6 ms 27060 KB n=500
42 Correct 6 ms 26968 KB n=500
43 Correct 7 ms 26972 KB n=500
44 Correct 6 ms 26972 KB n=500
45 Correct 6 ms 26968 KB n=500
46 Correct 6 ms 27228 KB n=500
47 Correct 6 ms 26972 KB n=500
48 Correct 6 ms 26972 KB n=500
49 Correct 6 ms 27224 KB n=500
50 Correct 6 ms 26972 KB n=500
51 Correct 7 ms 27228 KB n=500
52 Correct 7 ms 27228 KB n=500
53 Correct 6 ms 26972 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 7 ms 26972 KB n=278
56 Correct 6 ms 26972 KB n=500
57 Correct 6 ms 27224 KB n=500
58 Correct 6 ms 26968 KB n=500
59 Correct 8 ms 27228 KB n=2000
60 Correct 10 ms 27484 KB n=2000
61 Correct 8 ms 27504 KB n=2000
62 Correct 8 ms 27480 KB n=2000
63 Correct 8 ms 27228 KB n=2000
64 Correct 8 ms 27484 KB n=2000
65 Correct 8 ms 27228 KB n=2000
66 Correct 8 ms 27516 KB n=2000
67 Correct 8 ms 27228 KB n=2000
68 Correct 8 ms 27484 KB n=2000
69 Correct 7 ms 27228 KB n=2000
70 Correct 8 ms 27228 KB n=2000
71 Correct 8 ms 27228 KB n=2000
72 Correct 7 ms 27224 KB n=2000
73 Correct 8 ms 27228 KB n=2000
74 Correct 7 ms 27224 KB n=1844
75 Correct 8 ms 27228 KB n=2000
76 Correct 10 ms 27228 KB n=2000
77 Correct 8 ms 27312 KB n=2000
78 Correct 8 ms 27228 KB n=2000
79 Correct 8 ms 27228 KB n=2000
80 Correct 8 ms 27484 KB n=2000
81 Correct 8 ms 27484 KB n=2000
82 Correct 9 ms 27224 KB n=2000
83 Correct 8 ms 27480 KB n=2000
84 Correct 8 ms 27224 KB n=2000
85 Correct 8 ms 27484 KB n=2000
86 Correct 8 ms 27344 KB n=2000
87 Correct 8 ms 27228 KB n=2000
88 Correct 8 ms 27484 KB n=2000
89 Correct 8 ms 27484 KB n=2000
90 Correct 8 ms 27484 KB n=2000
91 Correct 7 ms 27228 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 27320 KB n=5
2 Correct 6 ms 26972 KB n=100
3 Correct 6 ms 26972 KB n=100
4 Correct 6 ms 27080 KB n=100
5 Correct 6 ms 26972 KB n=100
6 Correct 6 ms 26972 KB n=100
7 Correct 6 ms 27128 KB n=100
8 Correct 6 ms 26968 KB n=100
9 Correct 7 ms 27304 KB n=100
10 Correct 6 ms 26972 KB n=100
11 Correct 5 ms 26968 KB n=100
12 Correct 5 ms 26972 KB n=100
13 Correct 5 ms 26972 KB n=100
14 Correct 5 ms 26972 KB n=100
15 Correct 6 ms 26972 KB n=100
16 Correct 5 ms 26972 KB n=100
17 Correct 6 ms 26972 KB n=100
18 Correct 5 ms 26972 KB n=100
19 Correct 6 ms 26972 KB n=100
20 Correct 5 ms 26968 KB n=100
21 Correct 5 ms 26972 KB n=100
22 Correct 5 ms 26972 KB n=100
23 Correct 6 ms 26968 KB n=100
24 Correct 5 ms 26972 KB n=100
25 Correct 5 ms 27064 KB n=100
26 Correct 5 ms 26972 KB n=12
27 Correct 6 ms 26972 KB n=100
28 Correct 6 ms 26968 KB n=500
29 Correct 6 ms 27224 KB n=500
30 Correct 6 ms 27060 KB n=500
31 Correct 6 ms 26972 KB n=500
32 Correct 7 ms 26972 KB n=500
33 Correct 7 ms 27064 KB n=500
34 Correct 7 ms 26972 KB n=500
35 Correct 6 ms 27144 KB n=500
36 Correct 7 ms 26972 KB n=500
37 Correct 6 ms 26972 KB n=500
38 Correct 6 ms 26972 KB n=500
39 Correct 6 ms 26972 KB n=500
40 Correct 6 ms 26972 KB n=500
41 Correct 6 ms 27060 KB n=500
42 Correct 6 ms 26968 KB n=500
43 Correct 7 ms 26972 KB n=500
44 Correct 6 ms 26972 KB n=500
45 Correct 6 ms 26968 KB n=500
46 Correct 6 ms 27228 KB n=500
47 Correct 6 ms 26972 KB n=500
48 Correct 6 ms 26972 KB n=500
49 Correct 6 ms 27224 KB n=500
50 Correct 6 ms 26972 KB n=500
51 Correct 7 ms 27228 KB n=500
52 Correct 7 ms 27228 KB n=500
53 Correct 6 ms 26972 KB n=500
54 Correct 6 ms 27228 KB n=500
55 Correct 7 ms 26972 KB n=278
56 Correct 6 ms 26972 KB n=500
57 Correct 6 ms 27224 KB n=500
58 Correct 6 ms 26968 KB n=500
59 Correct 8 ms 27228 KB n=2000
60 Correct 10 ms 27484 KB n=2000
61 Correct 8 ms 27504 KB n=2000
62 Correct 8 ms 27480 KB n=2000
63 Correct 8 ms 27228 KB n=2000
64 Correct 8 ms 27484 KB n=2000
65 Correct 8 ms 27228 KB n=2000
66 Correct 8 ms 27516 KB n=2000
67 Correct 8 ms 27228 KB n=2000
68 Correct 8 ms 27484 KB n=2000
69 Correct 7 ms 27228 KB n=2000
70 Correct 8 ms 27228 KB n=2000
71 Correct 8 ms 27228 KB n=2000
72 Correct 7 ms 27224 KB n=2000
73 Correct 8 ms 27228 KB n=2000
74 Correct 7 ms 27224 KB n=1844
75 Correct 8 ms 27228 KB n=2000
76 Correct 10 ms 27228 KB n=2000
77 Correct 8 ms 27312 KB n=2000
78 Correct 8 ms 27228 KB n=2000
79 Correct 8 ms 27228 KB n=2000
80 Correct 8 ms 27484 KB n=2000
81 Correct 8 ms 27484 KB n=2000
82 Correct 9 ms 27224 KB n=2000
83 Correct 8 ms 27480 KB n=2000
84 Correct 8 ms 27224 KB n=2000
85 Correct 8 ms 27484 KB n=2000
86 Correct 8 ms 27344 KB n=2000
87 Correct 8 ms 27228 KB n=2000
88 Correct 8 ms 27484 KB n=2000
89 Correct 8 ms 27484 KB n=2000
90 Correct 8 ms 27484 KB n=2000
91 Correct 7 ms 27228 KB n=2000
92 Correct 529 ms 73044 KB n=200000
93 Correct 744 ms 77928 KB n=200000
94 Correct 691 ms 81200 KB n=200000
95 Correct 460 ms 72828 KB n=200000
96 Correct 455 ms 72928 KB n=200000
97 Correct 669 ms 77124 KB n=200000
98 Correct 481 ms 72748 KB n=200000
99 Correct 580 ms 73280 KB n=200000
100 Correct 516 ms 73088 KB n=200000
101 Correct 661 ms 82528 KB n=200000
102 Correct 261 ms 74084 KB n=200000
103 Correct 252 ms 74324 KB n=200000
104 Correct 290 ms 74064 KB n=200000
105 Correct 266 ms 74664 KB n=200000
106 Correct 277 ms 74364 KB n=200000
107 Correct 270 ms 74576 KB n=200000
108 Correct 552 ms 73044 KB n=200000
109 Correct 519 ms 73036 KB n=200000
110 Correct 503 ms 73044 KB n=200000
111 Correct 468 ms 72268 KB n=200000
112 Correct 667 ms 81796 KB n=200000
113 Correct 779 ms 76956 KB n=200000
114 Correct 555 ms 72644 KB n=200000
115 Correct 860 ms 74832 KB n=200000
116 Correct 528 ms 73140 KB n=200000
117 Correct 685 ms 82000 KB n=200000
118 Correct 765 ms 75756 KB n=200000
119 Correct 504 ms 73380 KB n=200000
120 Correct 586 ms 81532 KB n=200000
121 Correct 646 ms 81652 KB n=200000
122 Correct 672 ms 81744 KB n=200000
123 Correct 267 ms 74344 KB n=200000
124 Correct 116 ms 41812 KB n=25264