답안 #308575

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
308575 2020-10-01T14:45:36 Z aZvezda Birthday gift (IZhO18_treearray) C++14
100 / 100
1493 ms 82680 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
template<class T> inline void fix(T &x) {if(labs(x) >= mod) {x %= mod;} if(x < 0) {x += mod;}}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const int MAX_N = 2e5 + 10;

set<int> diff[MAX_N], seq[MAX_N];

int searchIn(set<int> &where, int l, int r) {
    auto it = where.lower_bound(l);
    if(it == where.end() || *it > r) {
        return -1;
    } else {
        return *it;
    }
}

const int LOG = 18;
int par[MAX_N][LOG], d[MAX_N];
vector<int> g[MAX_N];

void dfs(int x, int p) {
    par[x][0] = p;
    d[x] = d[p] + 1;
    for(int i = 1; i < LOG; i ++) {
        par[x][i] = par[par[x][i - 1]][i - 1];
    }
    for(auto it : g[x]) {
        if(it == p) {continue;}
        dfs(it, x);
    }
}

int lca(int a, int b) {
    if(d[a] < d[b]) {swap(a, b);}
    for(int i = LOG - 1; i >= 0; i --) {
        if(d[par[a][i]] >= d[b]) {
            a = par[a][i];
        }
    }
    if(a == b) {return a;}
    for(int i = LOG - 1; i >= 0; i --) {
        if(par[a][i] != par[b][i]) {
            a = par[a][i];
            b = par[b][i];
        }
    }
    return par[a][0];
}

int n, m, q;
int arr[MAX_N];

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    cin >> n >> m >> q;
    for(int i = 0; i < n - 1; i ++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 0);
    for(int i = 1; i <= m; i ++) {
        cin >> arr[i];
        seq[arr[i]].insert(i);
    }
    for(int i = 1; i <= m - 1; i ++) {
        int curr = lca(arr[i], arr[i + 1]);
        diff[curr].insert(i);
    }
    while(q --) {
        int t;
        cin >> t;
        if(t == 1) {
            int a, v;
            cin >> a >> v;
            seq[arr[a]].erase(a);
            seq[v].insert(a);
            if(a != m) {
                int old = lca(arr[a], arr[a + 1]);
                int nw = lca(v, arr[a + 1]);
                diff[old].erase(a);
                diff[nw].insert(a);
            }
            if(a != 1) {
                int old = lca(arr[a], arr[a - 1]);
                int nw = lca(v, arr[a - 1]);
                diff[old].erase(a - 1);
                diff[nw].insert(a - 1);
            }
            arr[a] = v;
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            int ans = searchIn(seq[v], l, r);
            if(ans != -1) {
                cout << ans << " " << ans << endl;
                continue;
            }
            ans = searchIn(diff[v], l, r - 1);
            if(ans != -1) {
                cout << ans << " " << ans + 1 << endl;
                continue;
            }
            cout << -1 << " " << -1 << endl;
        }
    }
    return 0;
}

/*
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
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB n=5
2 Correct 15 ms 23808 KB n=100
3 Correct 15 ms 23936 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 15 ms 23808 KB n=100
6 Correct 15 ms 23808 KB n=100
7 Correct 16 ms 23808 KB n=100
8 Correct 16 ms 23808 KB n=100
9 Correct 15 ms 23808 KB n=100
10 Correct 17 ms 23808 KB n=100
11 Correct 15 ms 23808 KB n=100
12 Correct 17 ms 23808 KB n=100
13 Correct 15 ms 23808 KB n=100
14 Correct 16 ms 23936 KB n=100
15 Correct 16 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 17 ms 23808 KB n=100
19 Correct 15 ms 23900 KB n=100
20 Correct 15 ms 23808 KB n=100
21 Correct 18 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 15 ms 23808 KB n=100
24 Correct 15 ms 23808 KB n=100
25 Correct 15 ms 23808 KB n=100
26 Correct 15 ms 23808 KB n=12
27 Correct 16 ms 23808 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB n=5
2 Correct 15 ms 23808 KB n=100
3 Correct 15 ms 23936 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 15 ms 23808 KB n=100
6 Correct 15 ms 23808 KB n=100
7 Correct 16 ms 23808 KB n=100
8 Correct 16 ms 23808 KB n=100
9 Correct 15 ms 23808 KB n=100
10 Correct 17 ms 23808 KB n=100
11 Correct 15 ms 23808 KB n=100
12 Correct 17 ms 23808 KB n=100
13 Correct 15 ms 23808 KB n=100
14 Correct 16 ms 23936 KB n=100
15 Correct 16 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 17 ms 23808 KB n=100
19 Correct 15 ms 23900 KB n=100
20 Correct 15 ms 23808 KB n=100
21 Correct 18 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 15 ms 23808 KB n=100
24 Correct 15 ms 23808 KB n=100
25 Correct 15 ms 23808 KB n=100
26 Correct 15 ms 23808 KB n=12
27 Correct 16 ms 23808 KB n=100
28 Correct 16 ms 23936 KB n=500
29 Correct 16 ms 23936 KB n=500
30 Correct 16 ms 23936 KB n=500
31 Correct 16 ms 23936 KB n=500
32 Correct 16 ms 23952 KB n=500
33 Correct 17 ms 23936 KB n=500
34 Correct 17 ms 23936 KB n=500
35 Correct 18 ms 23936 KB n=500
36 Correct 16 ms 23936 KB n=500
37 Correct 16 ms 23936 KB n=500
38 Correct 15 ms 23936 KB n=500
39 Correct 16 ms 23936 KB n=500
40 Correct 16 ms 23936 KB n=500
41 Correct 17 ms 23936 KB n=500
42 Correct 16 ms 23936 KB n=500
43 Correct 16 ms 23936 KB n=500
44 Correct 17 ms 23936 KB n=500
45 Correct 16 ms 23936 KB n=500
46 Correct 16 ms 23936 KB n=500
47 Correct 16 ms 23936 KB n=500
48 Correct 16 ms 23936 KB n=500
49 Correct 18 ms 23936 KB n=500
50 Correct 15 ms 23936 KB n=500
51 Correct 15 ms 23936 KB n=500
52 Correct 15 ms 23936 KB n=500
53 Correct 15 ms 23936 KB n=500
54 Correct 20 ms 23936 KB n=500
55 Correct 20 ms 23936 KB n=278
56 Correct 15 ms 23936 KB n=500
57 Correct 21 ms 23936 KB n=500
58 Correct 15 ms 23936 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB n=5
2 Correct 15 ms 23808 KB n=100
3 Correct 15 ms 23936 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 15 ms 23808 KB n=100
6 Correct 15 ms 23808 KB n=100
7 Correct 16 ms 23808 KB n=100
8 Correct 16 ms 23808 KB n=100
9 Correct 15 ms 23808 KB n=100
10 Correct 17 ms 23808 KB n=100
11 Correct 15 ms 23808 KB n=100
12 Correct 17 ms 23808 KB n=100
13 Correct 15 ms 23808 KB n=100
14 Correct 16 ms 23936 KB n=100
15 Correct 16 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 17 ms 23808 KB n=100
19 Correct 15 ms 23900 KB n=100
20 Correct 15 ms 23808 KB n=100
21 Correct 18 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 15 ms 23808 KB n=100
24 Correct 15 ms 23808 KB n=100
25 Correct 15 ms 23808 KB n=100
26 Correct 15 ms 23808 KB n=12
27 Correct 16 ms 23808 KB n=100
28 Correct 16 ms 23936 KB n=500
29 Correct 16 ms 23936 KB n=500
30 Correct 16 ms 23936 KB n=500
31 Correct 16 ms 23936 KB n=500
32 Correct 16 ms 23952 KB n=500
33 Correct 17 ms 23936 KB n=500
34 Correct 17 ms 23936 KB n=500
35 Correct 18 ms 23936 KB n=500
36 Correct 16 ms 23936 KB n=500
37 Correct 16 ms 23936 KB n=500
38 Correct 15 ms 23936 KB n=500
39 Correct 16 ms 23936 KB n=500
40 Correct 16 ms 23936 KB n=500
41 Correct 17 ms 23936 KB n=500
42 Correct 16 ms 23936 KB n=500
43 Correct 16 ms 23936 KB n=500
44 Correct 17 ms 23936 KB n=500
45 Correct 16 ms 23936 KB n=500
46 Correct 16 ms 23936 KB n=500
47 Correct 16 ms 23936 KB n=500
48 Correct 16 ms 23936 KB n=500
49 Correct 18 ms 23936 KB n=500
50 Correct 15 ms 23936 KB n=500
51 Correct 15 ms 23936 KB n=500
52 Correct 15 ms 23936 KB n=500
53 Correct 15 ms 23936 KB n=500
54 Correct 20 ms 23936 KB n=500
55 Correct 20 ms 23936 KB n=278
56 Correct 15 ms 23936 KB n=500
57 Correct 21 ms 23936 KB n=500
58 Correct 15 ms 23936 KB n=500
59 Correct 18 ms 24320 KB n=2000
60 Correct 19 ms 24320 KB n=2000
61 Correct 24 ms 24320 KB n=2000
62 Correct 19 ms 24320 KB n=2000
63 Correct 20 ms 24320 KB n=2000
64 Correct 19 ms 24320 KB n=2000
65 Correct 19 ms 24320 KB n=2000
66 Correct 21 ms 24320 KB n=2000
67 Correct 19 ms 24192 KB n=2000
68 Correct 22 ms 24320 KB n=2000
69 Correct 18 ms 24320 KB n=2000
70 Correct 22 ms 24320 KB n=2000
71 Correct 18 ms 24320 KB n=2000
72 Correct 19 ms 24320 KB n=2000
73 Correct 18 ms 24320 KB n=2000
74 Correct 18 ms 24192 KB n=1844
75 Correct 24 ms 24192 KB n=2000
76 Correct 19 ms 24192 KB n=2000
77 Correct 20 ms 24192 KB n=2000
78 Correct 20 ms 24320 KB n=2000
79 Correct 25 ms 24320 KB n=2000
80 Correct 20 ms 24448 KB n=2000
81 Correct 20 ms 24320 KB n=2000
82 Correct 19 ms 24312 KB n=2000
83 Correct 20 ms 24320 KB n=2000
84 Correct 20 ms 24236 KB n=2000
85 Correct 21 ms 24320 KB n=2000
86 Correct 19 ms 24320 KB n=2000
87 Correct 21 ms 24192 KB n=2000
88 Correct 19 ms 24320 KB n=2000
89 Correct 19 ms 24320 KB n=2000
90 Correct 19 ms 24320 KB n=2000
91 Correct 22 ms 24320 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB n=5
2 Correct 15 ms 23808 KB n=100
3 Correct 15 ms 23936 KB n=100
4 Correct 16 ms 23808 KB n=100
5 Correct 15 ms 23808 KB n=100
6 Correct 15 ms 23808 KB n=100
7 Correct 16 ms 23808 KB n=100
8 Correct 16 ms 23808 KB n=100
9 Correct 15 ms 23808 KB n=100
10 Correct 17 ms 23808 KB n=100
11 Correct 15 ms 23808 KB n=100
12 Correct 17 ms 23808 KB n=100
13 Correct 15 ms 23808 KB n=100
14 Correct 16 ms 23936 KB n=100
15 Correct 16 ms 23808 KB n=100
16 Correct 15 ms 23808 KB n=100
17 Correct 15 ms 23936 KB n=100
18 Correct 17 ms 23808 KB n=100
19 Correct 15 ms 23900 KB n=100
20 Correct 15 ms 23808 KB n=100
21 Correct 18 ms 23808 KB n=100
22 Correct 15 ms 23808 KB n=100
23 Correct 15 ms 23808 KB n=100
24 Correct 15 ms 23808 KB n=100
25 Correct 15 ms 23808 KB n=100
26 Correct 15 ms 23808 KB n=12
27 Correct 16 ms 23808 KB n=100
28 Correct 16 ms 23936 KB n=500
29 Correct 16 ms 23936 KB n=500
30 Correct 16 ms 23936 KB n=500
31 Correct 16 ms 23936 KB n=500
32 Correct 16 ms 23952 KB n=500
33 Correct 17 ms 23936 KB n=500
34 Correct 17 ms 23936 KB n=500
35 Correct 18 ms 23936 KB n=500
36 Correct 16 ms 23936 KB n=500
37 Correct 16 ms 23936 KB n=500
38 Correct 15 ms 23936 KB n=500
39 Correct 16 ms 23936 KB n=500
40 Correct 16 ms 23936 KB n=500
41 Correct 17 ms 23936 KB n=500
42 Correct 16 ms 23936 KB n=500
43 Correct 16 ms 23936 KB n=500
44 Correct 17 ms 23936 KB n=500
45 Correct 16 ms 23936 KB n=500
46 Correct 16 ms 23936 KB n=500
47 Correct 16 ms 23936 KB n=500
48 Correct 16 ms 23936 KB n=500
49 Correct 18 ms 23936 KB n=500
50 Correct 15 ms 23936 KB n=500
51 Correct 15 ms 23936 KB n=500
52 Correct 15 ms 23936 KB n=500
53 Correct 15 ms 23936 KB n=500
54 Correct 20 ms 23936 KB n=500
55 Correct 20 ms 23936 KB n=278
56 Correct 15 ms 23936 KB n=500
57 Correct 21 ms 23936 KB n=500
58 Correct 15 ms 23936 KB n=500
59 Correct 18 ms 24320 KB n=2000
60 Correct 19 ms 24320 KB n=2000
61 Correct 24 ms 24320 KB n=2000
62 Correct 19 ms 24320 KB n=2000
63 Correct 20 ms 24320 KB n=2000
64 Correct 19 ms 24320 KB n=2000
65 Correct 19 ms 24320 KB n=2000
66 Correct 21 ms 24320 KB n=2000
67 Correct 19 ms 24192 KB n=2000
68 Correct 22 ms 24320 KB n=2000
69 Correct 18 ms 24320 KB n=2000
70 Correct 22 ms 24320 KB n=2000
71 Correct 18 ms 24320 KB n=2000
72 Correct 19 ms 24320 KB n=2000
73 Correct 18 ms 24320 KB n=2000
74 Correct 18 ms 24192 KB n=1844
75 Correct 24 ms 24192 KB n=2000
76 Correct 19 ms 24192 KB n=2000
77 Correct 20 ms 24192 KB n=2000
78 Correct 20 ms 24320 KB n=2000
79 Correct 25 ms 24320 KB n=2000
80 Correct 20 ms 24448 KB n=2000
81 Correct 20 ms 24320 KB n=2000
82 Correct 19 ms 24312 KB n=2000
83 Correct 20 ms 24320 KB n=2000
84 Correct 20 ms 24236 KB n=2000
85 Correct 21 ms 24320 KB n=2000
86 Correct 19 ms 24320 KB n=2000
87 Correct 21 ms 24192 KB n=2000
88 Correct 19 ms 24320 KB n=2000
89 Correct 19 ms 24320 KB n=2000
90 Correct 19 ms 24320 KB n=2000
91 Correct 22 ms 24320 KB n=2000
92 Correct 897 ms 73080 KB n=200000
93 Correct 1334 ms 78124 KB n=200000
94 Correct 1306 ms 81520 KB n=200000
95 Correct 903 ms 72884 KB n=200000
96 Correct 803 ms 72852 KB n=200000
97 Correct 1412 ms 77048 KB n=200000
98 Correct 892 ms 72920 KB n=200000
99 Correct 1091 ms 73208 KB n=200000
100 Correct 861 ms 73072 KB n=200000
101 Correct 1245 ms 82680 KB n=200000
102 Correct 523 ms 74104 KB n=200000
103 Correct 523 ms 74240 KB n=200000
104 Correct 521 ms 74104 KB n=200000
105 Correct 534 ms 74488 KB n=200000
106 Correct 513 ms 74488 KB n=200000
107 Correct 522 ms 74488 KB n=200000
108 Correct 995 ms 72952 KB n=200000
109 Correct 998 ms 73044 KB n=200000
110 Correct 1060 ms 73056 KB n=200000
111 Correct 821 ms 72360 KB n=200000
112 Correct 1223 ms 81612 KB n=200000
113 Correct 1343 ms 77108 KB n=200000
114 Correct 897 ms 72548 KB n=200000
115 Correct 1493 ms 75256 KB n=200000
116 Correct 790 ms 73068 KB n=200000
117 Correct 1290 ms 82104 KB n=200000
118 Correct 1383 ms 75916 KB n=200000
119 Correct 773 ms 73192 KB n=200000
120 Correct 1162 ms 81528 KB n=200000
121 Correct 1184 ms 81660 KB n=200000
122 Correct 1149 ms 81916 KB n=200000
123 Correct 534 ms 74360 KB n=200000
124 Correct 270 ms 38520 KB n=25264