Submission #500363

# Submission time Handle Problem Language Result Execution time Memory
500363 2021-12-30T19:30:07 Z RedBoy Birthday gift (IZhO18_treearray) C++11
100 / 100
964 ms 80392 KB
#include <iostream>
#include <vector>
#include <set>
#include <bitset>

using namespace std;

const int N_MAX = 2e5 + 5;
const int LOG = 20;

int n, m, q;
int v[N_MAX], depth[N_MAX], p[N_MAX][LOG + 5];
vector<int> adj[N_MAX];
set<int> a[N_MAX], b[N_MAX];

void scan()
{
    cin >> n >> m >> q;

    for(int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

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

void dfs(int node, int father = 1)
{
    depth[node] = depth[father] + 1;
    p[node][0] = father;
    for(int j = 1; j <= LOG; j++)
        p[node][j] = p[p[node][j - 1]][j - 1];

    for(auto it: adj[node])
    {
        if(it == father)
            continue;
        dfs(it, node);
    }
}

int lca(int x, int y)
{
    if(depth[x] < depth[y])
        swap(x, y);

    int k = depth[x] - depth[y];
    for(int j = LOG; j >= 0; j--)
        if(k & (1 << j))
            x = p[x][j];

    if(x == y)
        return x;

    for(int j = LOG; j >= 0; j--)
    {
        if(p[x][j] != p[y][j])
        {
            x = p[x][j];
            y = p[y][j];
        }
    }

    return p[x][0];
}

void update(int pos, int val)
{
    if(pos > 1)
    {
        a[lca(v[pos - 1], v[pos])].erase(pos - 1);
        a[lca(v[pos - 1], val)].insert(pos - 1);
    }
    if(pos < m)
    {
        a[lca(v[pos], v[pos + 1])].erase(pos);
        a[lca(val, v[pos + 1])].insert(pos);
    }

    b[v[pos]].erase(pos);
    b[val].insert(pos);

    v[pos] = val;
}

pair<int, int> query(int l, int r, int val)
{
    //cout << "Q: " << val << " - ";

    auto x = b[val].lower_bound(l);
    //cout << *x << '\n';

    if(x != b[val].end() && *x <= r)
        return {*x, *x};

    x = a[val].lower_bound(l);
    if(x != a[val].end() && *x < r)
        return {*x, *x + 1};

    return {-1, -1};
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    scan();
    dfs(1);

    for(int i = 1; i <= m; i++)
    {
        if(i < m)
            a[lca(v[i], v[i + 1])].insert(i);
        b[v[i]].insert(i);
    }

    while(q--)
    {
        int op;
        cin >> op;
        if(op == 1)
        {
            int pos, val;
            cin >> pos >> val;
            update(pos, val);
        }
        else
        {
            int l, r, val;
            cin >> l >> r >> val;
            pair<int, int> ans = query(l, r, val);
            cout << ans.first << ' ' << ans.second << '\n';
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 14 ms 23756 KB n=100
4 Correct 12 ms 23756 KB n=100
5 Correct 14 ms 23836 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 11 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23756 KB n=100
10 Correct 14 ms 23756 KB n=100
11 Correct 11 ms 23760 KB n=100
12 Correct 12 ms 23768 KB n=100
13 Correct 12 ms 23784 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23720 KB n=100
16 Correct 11 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 11 ms 23832 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23756 KB n=100
21 Correct 12 ms 23756 KB n=100
22 Correct 12 ms 23836 KB n=100
23 Correct 12 ms 23832 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23808 KB n=100
26 Correct 11 ms 23704 KB n=12
27 Correct 11 ms 23756 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 14 ms 23756 KB n=100
4 Correct 12 ms 23756 KB n=100
5 Correct 14 ms 23836 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 11 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23756 KB n=100
10 Correct 14 ms 23756 KB n=100
11 Correct 11 ms 23760 KB n=100
12 Correct 12 ms 23768 KB n=100
13 Correct 12 ms 23784 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23720 KB n=100
16 Correct 11 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 11 ms 23832 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23756 KB n=100
21 Correct 12 ms 23756 KB n=100
22 Correct 12 ms 23836 KB n=100
23 Correct 12 ms 23832 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23808 KB n=100
26 Correct 11 ms 23704 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 11 ms 23884 KB n=500
29 Correct 13 ms 23868 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 12 ms 23948 KB n=500
32 Correct 11 ms 23884 KB n=500
33 Correct 12 ms 23940 KB n=500
34 Correct 13 ms 23884 KB n=500
35 Correct 12 ms 23884 KB n=500
36 Correct 12 ms 23940 KB n=500
37 Correct 12 ms 23852 KB n=500
38 Correct 12 ms 23884 KB n=500
39 Correct 12 ms 23884 KB n=500
40 Correct 12 ms 23884 KB n=500
41 Correct 12 ms 23884 KB n=500
42 Correct 12 ms 23932 KB n=500
43 Correct 12 ms 23884 KB n=500
44 Correct 12 ms 23884 KB n=500
45 Correct 12 ms 23884 KB n=500
46 Correct 11 ms 23944 KB n=500
47 Correct 11 ms 23948 KB n=500
48 Correct 11 ms 23884 KB n=500
49 Correct 11 ms 23884 KB n=500
50 Correct 11 ms 23944 KB n=500
51 Correct 11 ms 23904 KB n=500
52 Correct 11 ms 23884 KB n=500
53 Correct 11 ms 23884 KB n=500
54 Correct 11 ms 23840 KB n=500
55 Correct 11 ms 23884 KB n=278
56 Correct 11 ms 23884 KB n=500
57 Correct 11 ms 23912 KB n=500
58 Correct 12 ms 23932 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 14 ms 23756 KB n=100
4 Correct 12 ms 23756 KB n=100
5 Correct 14 ms 23836 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 11 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23756 KB n=100
10 Correct 14 ms 23756 KB n=100
11 Correct 11 ms 23760 KB n=100
12 Correct 12 ms 23768 KB n=100
13 Correct 12 ms 23784 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23720 KB n=100
16 Correct 11 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 11 ms 23832 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23756 KB n=100
21 Correct 12 ms 23756 KB n=100
22 Correct 12 ms 23836 KB n=100
23 Correct 12 ms 23832 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23808 KB n=100
26 Correct 11 ms 23704 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 11 ms 23884 KB n=500
29 Correct 13 ms 23868 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 12 ms 23948 KB n=500
32 Correct 11 ms 23884 KB n=500
33 Correct 12 ms 23940 KB n=500
34 Correct 13 ms 23884 KB n=500
35 Correct 12 ms 23884 KB n=500
36 Correct 12 ms 23940 KB n=500
37 Correct 12 ms 23852 KB n=500
38 Correct 12 ms 23884 KB n=500
39 Correct 12 ms 23884 KB n=500
40 Correct 12 ms 23884 KB n=500
41 Correct 12 ms 23884 KB n=500
42 Correct 12 ms 23932 KB n=500
43 Correct 12 ms 23884 KB n=500
44 Correct 12 ms 23884 KB n=500
45 Correct 12 ms 23884 KB n=500
46 Correct 11 ms 23944 KB n=500
47 Correct 11 ms 23948 KB n=500
48 Correct 11 ms 23884 KB n=500
49 Correct 11 ms 23884 KB n=500
50 Correct 11 ms 23944 KB n=500
51 Correct 11 ms 23904 KB n=500
52 Correct 11 ms 23884 KB n=500
53 Correct 11 ms 23884 KB n=500
54 Correct 11 ms 23840 KB n=500
55 Correct 11 ms 23884 KB n=278
56 Correct 11 ms 23884 KB n=500
57 Correct 11 ms 23912 KB n=500
58 Correct 12 ms 23932 KB n=500
59 Correct 13 ms 24268 KB n=2000
60 Correct 13 ms 24268 KB n=2000
61 Correct 14 ms 24332 KB n=2000
62 Correct 14 ms 24284 KB n=2000
63 Correct 14 ms 24192 KB n=2000
64 Correct 14 ms 24268 KB n=2000
65 Correct 13 ms 24252 KB n=2000
66 Correct 13 ms 24344 KB n=2000
67 Correct 14 ms 24268 KB n=2000
68 Correct 13 ms 24212 KB n=2000
69 Correct 13 ms 24396 KB n=2000
70 Correct 13 ms 24308 KB n=2000
71 Correct 14 ms 24292 KB n=2000
72 Correct 13 ms 24176 KB n=2000
73 Correct 13 ms 24268 KB n=2000
74 Correct 13 ms 24236 KB n=1844
75 Correct 15 ms 24252 KB n=2000
76 Correct 14 ms 24248 KB n=2000
77 Correct 14 ms 24268 KB n=2000
78 Correct 15 ms 24272 KB n=2000
79 Correct 14 ms 24292 KB n=2000
80 Correct 15 ms 24364 KB n=2000
81 Correct 14 ms 24324 KB n=2000
82 Correct 13 ms 24208 KB n=2000
83 Correct 13 ms 24344 KB n=2000
84 Correct 13 ms 24288 KB n=2000
85 Correct 14 ms 24316 KB n=2000
86 Correct 13 ms 24304 KB n=2000
87 Correct 14 ms 24268 KB n=2000
88 Correct 13 ms 24268 KB n=2000
89 Correct 14 ms 24268 KB n=2000
90 Correct 13 ms 24340 KB n=2000
91 Correct 15 ms 24268 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23756 KB n=5
2 Correct 13 ms 23756 KB n=100
3 Correct 14 ms 23756 KB n=100
4 Correct 12 ms 23756 KB n=100
5 Correct 14 ms 23836 KB n=100
6 Correct 11 ms 23756 KB n=100
7 Correct 11 ms 23756 KB n=100
8 Correct 11 ms 23756 KB n=100
9 Correct 11 ms 23756 KB n=100
10 Correct 14 ms 23756 KB n=100
11 Correct 11 ms 23760 KB n=100
12 Correct 12 ms 23768 KB n=100
13 Correct 12 ms 23784 KB n=100
14 Correct 11 ms 23756 KB n=100
15 Correct 11 ms 23720 KB n=100
16 Correct 11 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 11 ms 23832 KB n=100
19 Correct 11 ms 23756 KB n=100
20 Correct 11 ms 23756 KB n=100
21 Correct 12 ms 23756 KB n=100
22 Correct 12 ms 23836 KB n=100
23 Correct 12 ms 23832 KB n=100
24 Correct 12 ms 23756 KB n=100
25 Correct 12 ms 23808 KB n=100
26 Correct 11 ms 23704 KB n=12
27 Correct 11 ms 23756 KB n=100
28 Correct 11 ms 23884 KB n=500
29 Correct 13 ms 23868 KB n=500
30 Correct 13 ms 23884 KB n=500
31 Correct 12 ms 23948 KB n=500
32 Correct 11 ms 23884 KB n=500
33 Correct 12 ms 23940 KB n=500
34 Correct 13 ms 23884 KB n=500
35 Correct 12 ms 23884 KB n=500
36 Correct 12 ms 23940 KB n=500
37 Correct 12 ms 23852 KB n=500
38 Correct 12 ms 23884 KB n=500
39 Correct 12 ms 23884 KB n=500
40 Correct 12 ms 23884 KB n=500
41 Correct 12 ms 23884 KB n=500
42 Correct 12 ms 23932 KB n=500
43 Correct 12 ms 23884 KB n=500
44 Correct 12 ms 23884 KB n=500
45 Correct 12 ms 23884 KB n=500
46 Correct 11 ms 23944 KB n=500
47 Correct 11 ms 23948 KB n=500
48 Correct 11 ms 23884 KB n=500
49 Correct 11 ms 23884 KB n=500
50 Correct 11 ms 23944 KB n=500
51 Correct 11 ms 23904 KB n=500
52 Correct 11 ms 23884 KB n=500
53 Correct 11 ms 23884 KB n=500
54 Correct 11 ms 23840 KB n=500
55 Correct 11 ms 23884 KB n=278
56 Correct 11 ms 23884 KB n=500
57 Correct 11 ms 23912 KB n=500
58 Correct 12 ms 23932 KB n=500
59 Correct 13 ms 24268 KB n=2000
60 Correct 13 ms 24268 KB n=2000
61 Correct 14 ms 24332 KB n=2000
62 Correct 14 ms 24284 KB n=2000
63 Correct 14 ms 24192 KB n=2000
64 Correct 14 ms 24268 KB n=2000
65 Correct 13 ms 24252 KB n=2000
66 Correct 13 ms 24344 KB n=2000
67 Correct 14 ms 24268 KB n=2000
68 Correct 13 ms 24212 KB n=2000
69 Correct 13 ms 24396 KB n=2000
70 Correct 13 ms 24308 KB n=2000
71 Correct 14 ms 24292 KB n=2000
72 Correct 13 ms 24176 KB n=2000
73 Correct 13 ms 24268 KB n=2000
74 Correct 13 ms 24236 KB n=1844
75 Correct 15 ms 24252 KB n=2000
76 Correct 14 ms 24248 KB n=2000
77 Correct 14 ms 24268 KB n=2000
78 Correct 15 ms 24272 KB n=2000
79 Correct 14 ms 24292 KB n=2000
80 Correct 15 ms 24364 KB n=2000
81 Correct 14 ms 24324 KB n=2000
82 Correct 13 ms 24208 KB n=2000
83 Correct 13 ms 24344 KB n=2000
84 Correct 13 ms 24288 KB n=2000
85 Correct 14 ms 24316 KB n=2000
86 Correct 13 ms 24304 KB n=2000
87 Correct 14 ms 24268 KB n=2000
88 Correct 13 ms 24268 KB n=2000
89 Correct 14 ms 24268 KB n=2000
90 Correct 13 ms 24340 KB n=2000
91 Correct 15 ms 24268 KB n=2000
92 Correct 646 ms 71952 KB n=200000
93 Correct 929 ms 75624 KB n=200000
94 Correct 905 ms 79020 KB n=200000
95 Correct 622 ms 71776 KB n=200000
96 Correct 617 ms 71940 KB n=200000
97 Correct 952 ms 74672 KB n=200000
98 Correct 641 ms 72016 KB n=200000
99 Correct 782 ms 71364 KB n=200000
100 Correct 633 ms 71940 KB n=200000
101 Correct 886 ms 80364 KB n=200000
102 Correct 394 ms 72800 KB n=200000
103 Correct 391 ms 72772 KB n=200000
104 Correct 394 ms 72792 KB n=200000
105 Correct 401 ms 72628 KB n=200000
106 Correct 399 ms 72572 KB n=200000
107 Correct 395 ms 72524 KB n=200000
108 Correct 714 ms 71516 KB n=200000
109 Correct 704 ms 71364 KB n=200000
110 Correct 709 ms 71584 KB n=200000
111 Correct 615 ms 71772 KB n=200000
112 Correct 894 ms 79188 KB n=200000
113 Correct 938 ms 74656 KB n=200000
114 Correct 654 ms 71800 KB n=200000
115 Correct 962 ms 72716 KB n=200000
116 Correct 606 ms 71480 KB n=200000
117 Correct 911 ms 79500 KB n=200000
118 Correct 964 ms 73640 KB n=200000
119 Correct 603 ms 71484 KB n=200000
120 Correct 878 ms 79948 KB n=200000
121 Correct 830 ms 80092 KB n=200000
122 Correct 826 ms 80392 KB n=200000
123 Correct 434 ms 72112 KB n=200000
124 Correct 176 ms 37284 KB n=25264