Submission #944353

# Submission time Handle Problem Language Result Execution time Memory
944353 2024-03-12T15:29:48 Z dilanyan Birthday gift (IZhO18_treearray) C++17
56 / 100
9 ms 5720 KB
//-------------dilanyan------------\\ 
 
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
 
//------------------KarginDefines--------------------\\ 
 
#define ll long long
#define pb push_back
#define all(u) (u).begin(), (u).end()
#define pqueue priority_queue
#define upper upper_bound
#define lower lower_bound
#define umap unordered_map
#define uset unordered_set
#define Kargin ios_base::sync_with_stdio(false);cin.tie(NULL);
#define Usaco freopen(".in", "r", stdin); freopen(".out", "w", stdout);
 
 
//-------------------KarginConstants------------------\\ 
 
const ll mod = 1000000007;
const ll inf = 1e9 + 15;
 
//-------------------KarginCode------------------------\\ 
 
const int N = 100005;

int a[N];
vector<int> g[N];
int tin[N], tout[N], tim = 0;
int up[N][20];

void dfs(int u, int p) {
    tin[u] = tim++;
    up[u][0] = p;
    for (int v : g[u]) {
        if (v != p) dfs(v, u);
    }
    tout[u] = tim;
}

bool anc(int u, int v) {
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int x, int y) {
    if (anc(x, y)) return x;
    if (anc(y, x)) return y;
    for (int i = 19;i >= 0;i--) {
        if (!anc(up[x][i], y)) x = up[x][i];
    }
    return up[x][0];
}

void KarginSolve() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0;i < n - 1;i++) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v), g[v].pb(u);
    }
    for (int i = 1;i <= m;i++) cin >> a[i];
    dfs(1, 1);
    for (int j = 1;j < 20;j++) {
        for (int i = 1;i <= n;i++) {
            up[i][j] = up[up[i][j - 1]][j - 1];
        }
    }
    while (q--) {
        int t; cin >> t;
        if (t == 1) {
            int pos, v;
            cin >> pos >> v;
            a[pos] = v;
        }
        else {
            int l, r, u;
            cin >> l >> r >> u;
            bool flag = false;
            for (int i = l;i <= r;i++) {
                if (a[i] == u) {
                    cout << i << ' ' << i << '\n';
                    flag = true;
                    break;
                }
            }
            if(flag) continue;
            for (int i = l;i <= r;) {
                int s = i, e = i;
                for (int j = i;j <= r;j++) {
                    if (!anc(u, a[j])) break;
                    e = j + 1;
                }
                if (flag) {
                    i = e + 1;
                    continue;
                }
                if(e - s >= 2) {
                    int x = -1, y = -1;
                    for (int v : g[u]) {
                        if (v == up[u][0]) continue;
                        for (int j = s; j < e;j++) {
                            if (anc(v, a[j])) {
                                if (x == -1) x = j;
                                else y = j;
                                break;
                            }
                        }
                        if (y != -1) {
                            if (x > y) swap(x, y);
                            cout << x << ' ' << y << '\n';
                            flag = true;
                            break;
                        }
                    }
                    if (flag) {
                        i = e + 1;
                        continue;
                    }
                }
                i = e + 1;
            }
            if(flag) continue;
            cout << -1 << ' ' << -1 << '\n';
        }
    }
}
 
int main() {
    Kargin;
    int test = 1;
    //cin >> test;
    while (test--) {
        KarginSolve();
    }
    return 0;
}

Compilation message

treearray.cpp:1:1: warning: multi-line comment [-Wcomment]
    1 | //-------------dilanyan------------\\
      | ^
treearray.cpp:8:1: warning: multi-line comment [-Wcomment]
    8 | //------------------KarginDefines--------------------\\
      | ^
treearray.cpp:22:1: warning: multi-line comment [-Wcomment]
   22 | //-------------------KarginConstants------------------\\
      | ^
treearray.cpp:27:1: warning: multi-line comment [-Wcomment]
   27 | //-------------------KarginCode------------------------\\
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 1 ms 2908 KB n=100
3 Correct 1 ms 2908 KB n=100
4 Correct 1 ms 2908 KB n=100
5 Correct 1 ms 2908 KB n=100
6 Correct 1 ms 2908 KB n=100
7 Correct 1 ms 2908 KB n=100
8 Correct 1 ms 3316 KB n=100
9 Correct 1 ms 2904 KB n=100
10 Correct 1 ms 2908 KB n=100
11 Correct 1 ms 2908 KB n=100
12 Correct 1 ms 2908 KB n=100
13 Correct 1 ms 3028 KB n=100
14 Correct 1 ms 2908 KB n=100
15 Correct 1 ms 2908 KB n=100
16 Correct 1 ms 2908 KB n=100
17 Correct 1 ms 2908 KB n=100
18 Correct 1 ms 2908 KB n=100
19 Correct 1 ms 2908 KB n=100
20 Correct 1 ms 2904 KB n=100
21 Correct 1 ms 2908 KB n=100
22 Correct 1 ms 2908 KB n=100
23 Correct 2 ms 2908 KB n=100
24 Correct 1 ms 2908 KB n=100
25 Correct 1 ms 2908 KB n=100
26 Correct 1 ms 2908 KB n=12
27 Correct 1 ms 2904 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 1 ms 2908 KB n=100
3 Correct 1 ms 2908 KB n=100
4 Correct 1 ms 2908 KB n=100
5 Correct 1 ms 2908 KB n=100
6 Correct 1 ms 2908 KB n=100
7 Correct 1 ms 2908 KB n=100
8 Correct 1 ms 3316 KB n=100
9 Correct 1 ms 2904 KB n=100
10 Correct 1 ms 2908 KB n=100
11 Correct 1 ms 2908 KB n=100
12 Correct 1 ms 2908 KB n=100
13 Correct 1 ms 3028 KB n=100
14 Correct 1 ms 2908 KB n=100
15 Correct 1 ms 2908 KB n=100
16 Correct 1 ms 2908 KB n=100
17 Correct 1 ms 2908 KB n=100
18 Correct 1 ms 2908 KB n=100
19 Correct 1 ms 2908 KB n=100
20 Correct 1 ms 2904 KB n=100
21 Correct 1 ms 2908 KB n=100
22 Correct 1 ms 2908 KB n=100
23 Correct 2 ms 2908 KB n=100
24 Correct 1 ms 2908 KB n=100
25 Correct 1 ms 2908 KB n=100
26 Correct 1 ms 2908 KB n=12
27 Correct 1 ms 2904 KB n=100
28 Correct 1 ms 2908 KB n=500
29 Correct 1 ms 2908 KB n=500
30 Correct 2 ms 2908 KB n=500
31 Correct 1 ms 2904 KB n=500
32 Correct 2 ms 2908 KB n=500
33 Correct 1 ms 2908 KB n=500
34 Correct 1 ms 2908 KB n=500
35 Correct 1 ms 2908 KB n=500
36 Correct 2 ms 2908 KB n=500
37 Correct 2 ms 2908 KB n=500
38 Correct 2 ms 2908 KB n=500
39 Correct 2 ms 2908 KB n=500
40 Correct 1 ms 2904 KB n=500
41 Correct 1 ms 2904 KB n=500
42 Correct 1 ms 2908 KB n=500
43 Correct 1 ms 2908 KB n=500
44 Correct 2 ms 2908 KB n=500
45 Correct 1 ms 2904 KB n=500
46 Correct 1 ms 2908 KB n=500
47 Correct 1 ms 2908 KB n=500
48 Correct 1 ms 2908 KB n=500
49 Correct 1 ms 2908 KB n=500
50 Correct 1 ms 2908 KB n=500
51 Correct 1 ms 2908 KB n=500
52 Correct 1 ms 2908 KB n=500
53 Correct 2 ms 2908 KB n=500
54 Correct 2 ms 2908 KB n=500
55 Correct 1 ms 2908 KB n=278
56 Correct 1 ms 2904 KB n=500
57 Correct 1 ms 2916 KB n=500
58 Correct 1 ms 2908 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 1 ms 2908 KB n=100
3 Correct 1 ms 2908 KB n=100
4 Correct 1 ms 2908 KB n=100
5 Correct 1 ms 2908 KB n=100
6 Correct 1 ms 2908 KB n=100
7 Correct 1 ms 2908 KB n=100
8 Correct 1 ms 3316 KB n=100
9 Correct 1 ms 2904 KB n=100
10 Correct 1 ms 2908 KB n=100
11 Correct 1 ms 2908 KB n=100
12 Correct 1 ms 2908 KB n=100
13 Correct 1 ms 3028 KB n=100
14 Correct 1 ms 2908 KB n=100
15 Correct 1 ms 2908 KB n=100
16 Correct 1 ms 2908 KB n=100
17 Correct 1 ms 2908 KB n=100
18 Correct 1 ms 2908 KB n=100
19 Correct 1 ms 2908 KB n=100
20 Correct 1 ms 2904 KB n=100
21 Correct 1 ms 2908 KB n=100
22 Correct 1 ms 2908 KB n=100
23 Correct 2 ms 2908 KB n=100
24 Correct 1 ms 2908 KB n=100
25 Correct 1 ms 2908 KB n=100
26 Correct 1 ms 2908 KB n=12
27 Correct 1 ms 2904 KB n=100
28 Correct 1 ms 2908 KB n=500
29 Correct 1 ms 2908 KB n=500
30 Correct 2 ms 2908 KB n=500
31 Correct 1 ms 2904 KB n=500
32 Correct 2 ms 2908 KB n=500
33 Correct 1 ms 2908 KB n=500
34 Correct 1 ms 2908 KB n=500
35 Correct 1 ms 2908 KB n=500
36 Correct 2 ms 2908 KB n=500
37 Correct 2 ms 2908 KB n=500
38 Correct 2 ms 2908 KB n=500
39 Correct 2 ms 2908 KB n=500
40 Correct 1 ms 2904 KB n=500
41 Correct 1 ms 2904 KB n=500
42 Correct 1 ms 2908 KB n=500
43 Correct 1 ms 2908 KB n=500
44 Correct 2 ms 2908 KB n=500
45 Correct 1 ms 2904 KB n=500
46 Correct 1 ms 2908 KB n=500
47 Correct 1 ms 2908 KB n=500
48 Correct 1 ms 2908 KB n=500
49 Correct 1 ms 2908 KB n=500
50 Correct 1 ms 2908 KB n=500
51 Correct 1 ms 2908 KB n=500
52 Correct 1 ms 2908 KB n=500
53 Correct 2 ms 2908 KB n=500
54 Correct 2 ms 2908 KB n=500
55 Correct 1 ms 2908 KB n=278
56 Correct 1 ms 2904 KB n=500
57 Correct 1 ms 2916 KB n=500
58 Correct 1 ms 2908 KB n=500
59 Correct 3 ms 3164 KB n=2000
60 Correct 3 ms 3416 KB n=2000
61 Correct 3 ms 3164 KB n=2000
62 Correct 4 ms 3372 KB n=2000
63 Correct 5 ms 3296 KB n=2000
64 Correct 4 ms 3160 KB n=2000
65 Correct 5 ms 3164 KB n=2000
66 Correct 3 ms 3164 KB n=2000
67 Correct 4 ms 3164 KB n=2000
68 Correct 4 ms 3164 KB n=2000
69 Correct 9 ms 3160 KB n=2000
70 Correct 8 ms 3160 KB n=2000
71 Correct 8 ms 3164 KB n=2000
72 Correct 4 ms 3164 KB n=2000
73 Correct 4 ms 3128 KB n=2000
74 Correct 3 ms 3164 KB n=1844
75 Correct 4 ms 3164 KB n=2000
76 Correct 6 ms 3304 KB n=2000
77 Correct 5 ms 3160 KB n=2000
78 Correct 6 ms 3164 KB n=2000
79 Correct 2 ms 3164 KB n=2000
80 Correct 3 ms 3416 KB n=2000
81 Correct 3 ms 3164 KB n=2000
82 Correct 2 ms 3160 KB n=2000
83 Correct 3 ms 3164 KB n=2000
84 Correct 4 ms 3164 KB n=2000
85 Correct 4 ms 3164 KB n=2000
86 Correct 5 ms 3164 KB n=2000
87 Correct 5 ms 3164 KB n=2000
88 Correct 5 ms 3432 KB n=2000
89 Correct 5 ms 3420 KB n=2000
90 Correct 5 ms 3420 KB n=2000
91 Correct 6 ms 3160 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2904 KB n=5
2 Correct 1 ms 2908 KB n=100
3 Correct 1 ms 2908 KB n=100
4 Correct 1 ms 2908 KB n=100
5 Correct 1 ms 2908 KB n=100
6 Correct 1 ms 2908 KB n=100
7 Correct 1 ms 2908 KB n=100
8 Correct 1 ms 3316 KB n=100
9 Correct 1 ms 2904 KB n=100
10 Correct 1 ms 2908 KB n=100
11 Correct 1 ms 2908 KB n=100
12 Correct 1 ms 2908 KB n=100
13 Correct 1 ms 3028 KB n=100
14 Correct 1 ms 2908 KB n=100
15 Correct 1 ms 2908 KB n=100
16 Correct 1 ms 2908 KB n=100
17 Correct 1 ms 2908 KB n=100
18 Correct 1 ms 2908 KB n=100
19 Correct 1 ms 2908 KB n=100
20 Correct 1 ms 2904 KB n=100
21 Correct 1 ms 2908 KB n=100
22 Correct 1 ms 2908 KB n=100
23 Correct 2 ms 2908 KB n=100
24 Correct 1 ms 2908 KB n=100
25 Correct 1 ms 2908 KB n=100
26 Correct 1 ms 2908 KB n=12
27 Correct 1 ms 2904 KB n=100
28 Correct 1 ms 2908 KB n=500
29 Correct 1 ms 2908 KB n=500
30 Correct 2 ms 2908 KB n=500
31 Correct 1 ms 2904 KB n=500
32 Correct 2 ms 2908 KB n=500
33 Correct 1 ms 2908 KB n=500
34 Correct 1 ms 2908 KB n=500
35 Correct 1 ms 2908 KB n=500
36 Correct 2 ms 2908 KB n=500
37 Correct 2 ms 2908 KB n=500
38 Correct 2 ms 2908 KB n=500
39 Correct 2 ms 2908 KB n=500
40 Correct 1 ms 2904 KB n=500
41 Correct 1 ms 2904 KB n=500
42 Correct 1 ms 2908 KB n=500
43 Correct 1 ms 2908 KB n=500
44 Correct 2 ms 2908 KB n=500
45 Correct 1 ms 2904 KB n=500
46 Correct 1 ms 2908 KB n=500
47 Correct 1 ms 2908 KB n=500
48 Correct 1 ms 2908 KB n=500
49 Correct 1 ms 2908 KB n=500
50 Correct 1 ms 2908 KB n=500
51 Correct 1 ms 2908 KB n=500
52 Correct 1 ms 2908 KB n=500
53 Correct 2 ms 2908 KB n=500
54 Correct 2 ms 2908 KB n=500
55 Correct 1 ms 2908 KB n=278
56 Correct 1 ms 2904 KB n=500
57 Correct 1 ms 2916 KB n=500
58 Correct 1 ms 2908 KB n=500
59 Correct 3 ms 3164 KB n=2000
60 Correct 3 ms 3416 KB n=2000
61 Correct 3 ms 3164 KB n=2000
62 Correct 4 ms 3372 KB n=2000
63 Correct 5 ms 3296 KB n=2000
64 Correct 4 ms 3160 KB n=2000
65 Correct 5 ms 3164 KB n=2000
66 Correct 3 ms 3164 KB n=2000
67 Correct 4 ms 3164 KB n=2000
68 Correct 4 ms 3164 KB n=2000
69 Correct 9 ms 3160 KB n=2000
70 Correct 8 ms 3160 KB n=2000
71 Correct 8 ms 3164 KB n=2000
72 Correct 4 ms 3164 KB n=2000
73 Correct 4 ms 3128 KB n=2000
74 Correct 3 ms 3164 KB n=1844
75 Correct 4 ms 3164 KB n=2000
76 Correct 6 ms 3304 KB n=2000
77 Correct 5 ms 3160 KB n=2000
78 Correct 6 ms 3164 KB n=2000
79 Correct 2 ms 3164 KB n=2000
80 Correct 3 ms 3416 KB n=2000
81 Correct 3 ms 3164 KB n=2000
82 Correct 2 ms 3160 KB n=2000
83 Correct 3 ms 3164 KB n=2000
84 Correct 4 ms 3164 KB n=2000
85 Correct 4 ms 3164 KB n=2000
86 Correct 5 ms 3164 KB n=2000
87 Correct 5 ms 3164 KB n=2000
88 Correct 5 ms 3432 KB n=2000
89 Correct 5 ms 3420 KB n=2000
90 Correct 5 ms 3420 KB n=2000
91 Correct 6 ms 3160 KB n=2000
92 Runtime error 3 ms 5720 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -