Submission #945450

# Submission time Handle Problem Language Result Execution time Memory
945450 2024-03-13T19:47:24 Z VahanAbraham Birthday gift (IZhO18_treearray) C++17
100 / 100
811 ms 109528 KB
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <math.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <iomanip>
#include <random>
#include <chrono>
#include <cassert>
using namespace std;

#define ll long long
#define fr first
#define sc second
#define pb push_back
#define US freopen("cowmbat.in", "r", stdin); freopen("cowmbat.out", "w", stdout);

ll gcd(ll a, ll b)
{
    if (a == 0 || b == 0) {
        return  max(a, b);
    }
    if (a <= b) {
        return gcd(a, b % a);
    }
    else {
        return gcd(a % b, b);
    }
}
ll lcm(ll a, ll b) {
    return (a / gcd(a, b)) * b;
}

const int N = 300005;
const ll oo = 1000000000000000000, MOD = 1000000007;

set<int> s[N], s1[N];
int timer = 0, a[N], b[N], up[N][25], tin[N], tout[N];
vector<int> g[N];


void dfs(int u, int p) {
    ++timer;
    tin[u] = timer;
    up[u][0] = p;
    for (int i = 1; i <= 20; ++i) {
        up[u][i] = up[up[u][i - 1]][i - 1];
    }
    for (int i = 0; i < g[u].size(); ++i) {
        int h = g[u][i];
        if (p != h) {
            dfs(h, u);
        }
    }
    ++timer;
    tout[u] = timer;
}

bool upper(int a, int b) {
    return tin[a] <= tin[b] && tout[b] <= tout[a];
}

int lca(int a, int b) {
    if (upper(a, b)) {
        return a;
    }
    if (upper(b, a)) {
        return b;
    }
    for (int i = 20; i >= 0; --i) {
        if (upper(up[a][i], b) != 1) {
            a = up[a][i];
        }
    }
    return up[a][0];
}

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 1; i <= n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1, 1);
    for (int i = 1; i <= m; ++i) {
        cin >> a[i];
        s1[a[i]].insert(i);
    }
    for (int i = 1; i <= m - 1; ++i) {
        b[i] = lca(a[i], a[i + 1]);
        s[b[i]].insert(i);
    }
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int pos, v;
            cin >> pos >> v;
            s1[a[pos]].erase(pos);
            s1[v].insert(pos);
            int ind = pos - 1;
            if (ind >= 1) {
                s[b[ind]].erase(ind);
            }
            if (pos <= m - 1) {
                s[b[pos]].erase(pos);
            }
            a[pos] = v;
            if (pos > 1) {
                b[pos - 1] = lca(a[pos - 1], a[pos]);
                s[b[pos - 1]].insert(pos - 1);
            }
            if (pos <= m - 1) {
                b[pos] = lca(a[pos + 1], a[pos]);
                s[b[pos]].insert(pos);
            }
        }
        else {
            int l, r, v;
            cin >> l >> r >> v;
            auto it1 = s1[v].lower_bound(l);
            if (it1 != s1[v].end() && *it1 <= r) {
                cout << *it1 << " " << *it1 << endl;
            }
            else {
                auto it = s[v].lower_bound(l);
                if (it != s[v].end()) {
                    if (*it >= r) {
                        cout << -1 << " " << -1 << endl;
                    }
                    else {
                        cout << (*it) << " " << *(it)+1 << endl;
                    }
                }
                else {
                    cout << -1 << " " << -1 << endl;
                }
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    //US
    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
    }
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0; i < g[u].size(); ++i) {
      |                     ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 42844 KB n=5
2 Correct 10 ms 43096 KB n=100
3 Correct 9 ms 42844 KB n=100
4 Correct 9 ms 42844 KB n=100
5 Correct 9 ms 42840 KB n=100
6 Correct 9 ms 42844 KB n=100
7 Correct 9 ms 42844 KB n=100
8 Correct 9 ms 42844 KB n=100
9 Correct 9 ms 42844 KB n=100
10 Correct 12 ms 42844 KB n=100
11 Correct 9 ms 42844 KB n=100
12 Correct 9 ms 42844 KB n=100
13 Correct 9 ms 42840 KB n=100
14 Correct 9 ms 42844 KB n=100
15 Correct 9 ms 42840 KB n=100
16 Correct 9 ms 42840 KB n=100
17 Correct 9 ms 42844 KB n=100
18 Correct 9 ms 42844 KB n=100
19 Correct 11 ms 42844 KB n=100
20 Correct 8 ms 42844 KB n=100
21 Correct 8 ms 42844 KB n=100
22 Correct 9 ms 43020 KB n=100
23 Correct 9 ms 42840 KB n=100
24 Correct 9 ms 42844 KB n=100
25 Correct 9 ms 42844 KB n=100
26 Correct 9 ms 43008 KB n=12
27 Correct 9 ms 42844 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 13 ms 42844 KB n=5
2 Correct 10 ms 43096 KB n=100
3 Correct 9 ms 42844 KB n=100
4 Correct 9 ms 42844 KB n=100
5 Correct 9 ms 42840 KB n=100
6 Correct 9 ms 42844 KB n=100
7 Correct 9 ms 42844 KB n=100
8 Correct 9 ms 42844 KB n=100
9 Correct 9 ms 42844 KB n=100
10 Correct 12 ms 42844 KB n=100
11 Correct 9 ms 42844 KB n=100
12 Correct 9 ms 42844 KB n=100
13 Correct 9 ms 42840 KB n=100
14 Correct 9 ms 42844 KB n=100
15 Correct 9 ms 42840 KB n=100
16 Correct 9 ms 42840 KB n=100
17 Correct 9 ms 42844 KB n=100
18 Correct 9 ms 42844 KB n=100
19 Correct 11 ms 42844 KB n=100
20 Correct 8 ms 42844 KB n=100
21 Correct 8 ms 42844 KB n=100
22 Correct 9 ms 43020 KB n=100
23 Correct 9 ms 42840 KB n=100
24 Correct 9 ms 42844 KB n=100
25 Correct 9 ms 42844 KB n=100
26 Correct 9 ms 43008 KB n=12
27 Correct 9 ms 42844 KB n=100
28 Correct 9 ms 43100 KB n=500
29 Correct 9 ms 43096 KB n=500
30 Correct 10 ms 43296 KB n=500
31 Correct 10 ms 43100 KB n=500
32 Correct 9 ms 43096 KB n=500
33 Correct 10 ms 43100 KB n=500
34 Correct 12 ms 43100 KB n=500
35 Correct 9 ms 43096 KB n=500
36 Correct 9 ms 43100 KB n=500
37 Correct 10 ms 43096 KB n=500
38 Correct 10 ms 43100 KB n=500
39 Correct 10 ms 43100 KB n=500
40 Correct 10 ms 43096 KB n=500
41 Correct 9 ms 43100 KB n=500
42 Correct 9 ms 43060 KB n=500
43 Correct 9 ms 43100 KB n=500
44 Correct 9 ms 43100 KB n=500
45 Correct 10 ms 43100 KB n=500
46 Correct 9 ms 43100 KB n=500
47 Correct 9 ms 43096 KB n=500
48 Correct 10 ms 43096 KB n=500
49 Correct 9 ms 43100 KB n=500
50 Correct 10 ms 43060 KB n=500
51 Correct 11 ms 43100 KB n=500
52 Correct 10 ms 43096 KB n=500
53 Correct 9 ms 43100 KB n=500
54 Correct 10 ms 43100 KB n=500
55 Correct 10 ms 43100 KB n=278
56 Correct 9 ms 43100 KB n=500
57 Correct 9 ms 43100 KB n=500
58 Correct 10 ms 43096 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 13 ms 42844 KB n=5
2 Correct 10 ms 43096 KB n=100
3 Correct 9 ms 42844 KB n=100
4 Correct 9 ms 42844 KB n=100
5 Correct 9 ms 42840 KB n=100
6 Correct 9 ms 42844 KB n=100
7 Correct 9 ms 42844 KB n=100
8 Correct 9 ms 42844 KB n=100
9 Correct 9 ms 42844 KB n=100
10 Correct 12 ms 42844 KB n=100
11 Correct 9 ms 42844 KB n=100
12 Correct 9 ms 42844 KB n=100
13 Correct 9 ms 42840 KB n=100
14 Correct 9 ms 42844 KB n=100
15 Correct 9 ms 42840 KB n=100
16 Correct 9 ms 42840 KB n=100
17 Correct 9 ms 42844 KB n=100
18 Correct 9 ms 42844 KB n=100
19 Correct 11 ms 42844 KB n=100
20 Correct 8 ms 42844 KB n=100
21 Correct 8 ms 42844 KB n=100
22 Correct 9 ms 43020 KB n=100
23 Correct 9 ms 42840 KB n=100
24 Correct 9 ms 42844 KB n=100
25 Correct 9 ms 42844 KB n=100
26 Correct 9 ms 43008 KB n=12
27 Correct 9 ms 42844 KB n=100
28 Correct 9 ms 43100 KB n=500
29 Correct 9 ms 43096 KB n=500
30 Correct 10 ms 43296 KB n=500
31 Correct 10 ms 43100 KB n=500
32 Correct 9 ms 43096 KB n=500
33 Correct 10 ms 43100 KB n=500
34 Correct 12 ms 43100 KB n=500
35 Correct 9 ms 43096 KB n=500
36 Correct 9 ms 43100 KB n=500
37 Correct 10 ms 43096 KB n=500
38 Correct 10 ms 43100 KB n=500
39 Correct 10 ms 43100 KB n=500
40 Correct 10 ms 43096 KB n=500
41 Correct 9 ms 43100 KB n=500
42 Correct 9 ms 43060 KB n=500
43 Correct 9 ms 43100 KB n=500
44 Correct 9 ms 43100 KB n=500
45 Correct 10 ms 43100 KB n=500
46 Correct 9 ms 43100 KB n=500
47 Correct 9 ms 43096 KB n=500
48 Correct 10 ms 43096 KB n=500
49 Correct 9 ms 43100 KB n=500
50 Correct 10 ms 43060 KB n=500
51 Correct 11 ms 43100 KB n=500
52 Correct 10 ms 43096 KB n=500
53 Correct 9 ms 43100 KB n=500
54 Correct 10 ms 43100 KB n=500
55 Correct 10 ms 43100 KB n=278
56 Correct 9 ms 43100 KB n=500
57 Correct 9 ms 43100 KB n=500
58 Correct 10 ms 43096 KB n=500
59 Correct 12 ms 43352 KB n=2000
60 Correct 12 ms 43356 KB n=2000
61 Correct 12 ms 43352 KB n=2000
62 Correct 13 ms 43356 KB n=2000
63 Correct 13 ms 43356 KB n=2000
64 Correct 13 ms 43316 KB n=2000
65 Correct 13 ms 43356 KB n=2000
66 Correct 12 ms 43356 KB n=2000
67 Correct 12 ms 43356 KB n=2000
68 Correct 13 ms 43356 KB n=2000
69 Correct 13 ms 43352 KB n=2000
70 Correct 13 ms 43356 KB n=2000
71 Correct 13 ms 43356 KB n=2000
72 Correct 13 ms 43356 KB n=2000
73 Correct 12 ms 43356 KB n=2000
74 Correct 12 ms 43356 KB n=1844
75 Correct 13 ms 43356 KB n=2000
76 Correct 13 ms 43356 KB n=2000
77 Correct 13 ms 43164 KB n=2000
78 Correct 13 ms 43356 KB n=2000
79 Correct 12 ms 43352 KB n=2000
80 Correct 12 ms 43336 KB n=2000
81 Correct 12 ms 43356 KB n=2000
82 Correct 13 ms 43356 KB n=2000
83 Correct 14 ms 43596 KB n=2000
84 Correct 12 ms 43352 KB n=2000
85 Correct 12 ms 43356 KB n=2000
86 Correct 12 ms 43352 KB n=2000
87 Correct 13 ms 43356 KB n=2000
88 Correct 12 ms 43356 KB n=2000
89 Correct 12 ms 43388 KB n=2000
90 Correct 11 ms 43356 KB n=2000
91 Correct 13 ms 43316 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 13 ms 42844 KB n=5
2 Correct 10 ms 43096 KB n=100
3 Correct 9 ms 42844 KB n=100
4 Correct 9 ms 42844 KB n=100
5 Correct 9 ms 42840 KB n=100
6 Correct 9 ms 42844 KB n=100
7 Correct 9 ms 42844 KB n=100
8 Correct 9 ms 42844 KB n=100
9 Correct 9 ms 42844 KB n=100
10 Correct 12 ms 42844 KB n=100
11 Correct 9 ms 42844 KB n=100
12 Correct 9 ms 42844 KB n=100
13 Correct 9 ms 42840 KB n=100
14 Correct 9 ms 42844 KB n=100
15 Correct 9 ms 42840 KB n=100
16 Correct 9 ms 42840 KB n=100
17 Correct 9 ms 42844 KB n=100
18 Correct 9 ms 42844 KB n=100
19 Correct 11 ms 42844 KB n=100
20 Correct 8 ms 42844 KB n=100
21 Correct 8 ms 42844 KB n=100
22 Correct 9 ms 43020 KB n=100
23 Correct 9 ms 42840 KB n=100
24 Correct 9 ms 42844 KB n=100
25 Correct 9 ms 42844 KB n=100
26 Correct 9 ms 43008 KB n=12
27 Correct 9 ms 42844 KB n=100
28 Correct 9 ms 43100 KB n=500
29 Correct 9 ms 43096 KB n=500
30 Correct 10 ms 43296 KB n=500
31 Correct 10 ms 43100 KB n=500
32 Correct 9 ms 43096 KB n=500
33 Correct 10 ms 43100 KB n=500
34 Correct 12 ms 43100 KB n=500
35 Correct 9 ms 43096 KB n=500
36 Correct 9 ms 43100 KB n=500
37 Correct 10 ms 43096 KB n=500
38 Correct 10 ms 43100 KB n=500
39 Correct 10 ms 43100 KB n=500
40 Correct 10 ms 43096 KB n=500
41 Correct 9 ms 43100 KB n=500
42 Correct 9 ms 43060 KB n=500
43 Correct 9 ms 43100 KB n=500
44 Correct 9 ms 43100 KB n=500
45 Correct 10 ms 43100 KB n=500
46 Correct 9 ms 43100 KB n=500
47 Correct 9 ms 43096 KB n=500
48 Correct 10 ms 43096 KB n=500
49 Correct 9 ms 43100 KB n=500
50 Correct 10 ms 43060 KB n=500
51 Correct 11 ms 43100 KB n=500
52 Correct 10 ms 43096 KB n=500
53 Correct 9 ms 43100 KB n=500
54 Correct 10 ms 43100 KB n=500
55 Correct 10 ms 43100 KB n=278
56 Correct 9 ms 43100 KB n=500
57 Correct 9 ms 43100 KB n=500
58 Correct 10 ms 43096 KB n=500
59 Correct 12 ms 43352 KB n=2000
60 Correct 12 ms 43356 KB n=2000
61 Correct 12 ms 43352 KB n=2000
62 Correct 13 ms 43356 KB n=2000
63 Correct 13 ms 43356 KB n=2000
64 Correct 13 ms 43316 KB n=2000
65 Correct 13 ms 43356 KB n=2000
66 Correct 12 ms 43356 KB n=2000
67 Correct 12 ms 43356 KB n=2000
68 Correct 13 ms 43356 KB n=2000
69 Correct 13 ms 43352 KB n=2000
70 Correct 13 ms 43356 KB n=2000
71 Correct 13 ms 43356 KB n=2000
72 Correct 13 ms 43356 KB n=2000
73 Correct 12 ms 43356 KB n=2000
74 Correct 12 ms 43356 KB n=1844
75 Correct 13 ms 43356 KB n=2000
76 Correct 13 ms 43356 KB n=2000
77 Correct 13 ms 43164 KB n=2000
78 Correct 13 ms 43356 KB n=2000
79 Correct 12 ms 43352 KB n=2000
80 Correct 12 ms 43336 KB n=2000
81 Correct 12 ms 43356 KB n=2000
82 Correct 13 ms 43356 KB n=2000
83 Correct 14 ms 43596 KB n=2000
84 Correct 12 ms 43352 KB n=2000
85 Correct 12 ms 43356 KB n=2000
86 Correct 12 ms 43352 KB n=2000
87 Correct 13 ms 43356 KB n=2000
88 Correct 12 ms 43356 KB n=2000
89 Correct 12 ms 43388 KB n=2000
90 Correct 11 ms 43356 KB n=2000
91 Correct 13 ms 43316 KB n=2000
92 Correct 590 ms 96848 KB n=200000
93 Correct 659 ms 103508 KB n=200000
94 Correct 525 ms 108024 KB n=200000
95 Correct 607 ms 96704 KB n=200000
96 Correct 577 ms 96968 KB n=200000
97 Correct 676 ms 102252 KB n=200000
98 Correct 595 ms 96964 KB n=200000
99 Correct 644 ms 96980 KB n=200000
100 Correct 567 ms 96968 KB n=200000
101 Correct 503 ms 109528 KB n=200000
102 Correct 514 ms 97876 KB n=200000
103 Correct 517 ms 97864 KB n=200000
104 Correct 522 ms 97896 KB n=200000
105 Correct 504 ms 98284 KB n=200000
106 Correct 507 ms 98640 KB n=200000
107 Correct 513 ms 98564 KB n=200000
108 Correct 613 ms 96912 KB n=200000
109 Correct 624 ms 96940 KB n=200000
110 Correct 604 ms 96984 KB n=200000
111 Correct 634 ms 96544 KB n=200000
112 Correct 530 ms 108228 KB n=200000
113 Correct 660 ms 102224 KB n=200000
114 Correct 605 ms 96412 KB n=200000
115 Correct 763 ms 99412 KB n=200000
116 Correct 603 ms 97192 KB n=200000
117 Correct 544 ms 109044 KB n=200000
118 Correct 811 ms 100760 KB n=200000
119 Correct 626 ms 97024 KB n=200000
120 Correct 498 ms 108572 KB n=200000
121 Correct 504 ms 108652 KB n=200000
122 Correct 494 ms 108884 KB n=200000
123 Correct 560 ms 98268 KB n=200000
124 Correct 166 ms 57680 KB n=25264