Submission #685521

# Submission time Handle Problem Language Result Execution time Memory
685521 2023-01-24T13:11:34 Z speedyArda Birthday gift (IZhO18_treearray) C++14
56 / 100
369 ms 39396 KB
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 2e5+5;
int seglca[MAXN * 4], segans[MAXN * 4], in[MAXN], out[MAXN], height[MAXN], seq[MAXN];
vector<int> euler;
vector< vector<int> > adj(MAXN);
void dfs(int v, int p, int h)
{
    in[v] = euler.size();
    euler.push_back(v);
    height[v] = h;
    for(int e : adj[v])
    {
        if(e == p)
            continue;
        dfs(e, v, h + 1);
        euler.push_back(v);
    }
    out[v] = euler.size() - 1;

}

void buildlca(int v, int tl, int tr)
{
    if(tl == tr)
    {
        seglca[v] = euler[tl];
        //cout << v << " " << tl << " " << tr << " " << seglca[v] << "\n";
        return;
    }

    int tm = (tl + tr) / 2;
    buildlca(2 * v, tl, tm);
    buildlca(2 * v + 1, tm + 1, tr);
    if(height[seglca[v * 2]] < height[seglca[v * 2 + 1]])
        seglca[v] = seglca[v * 2];
    else   
        seglca[v] = seglca[v * 2 + 1];
    //cout << v << " " << tl << " " << tr << " " << seglca[v * 2] << " " << seglca[2 * v + 1] << " " <<  in[seglca[v * 2]] << " " << in[seglca[v * 2 + 1]] << " " << seglca[v] << "\n";
}

int getlca(int v, int tl, int tr, int l, int r)
{
    if(l > r)
        return -1;
    if(tl == l && tr == r)
        return seglca[v];
    int tm = (tl + tr) / 2;

    int left = getlca(2 * v, tl, tm, l, min(r, tm)), right = getlca(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
    if(right == -1)
        return left;
    if(left == -1)
        return right;
    if(height[left] < height[right])
        return left;
    else
        return right;

}

int lca(int a, int b)
{
    if(in[a] > in[b])
        swap(a, b);
    return getlca(1, 0, euler.size() - 1, in[a], in[b]);
}


void buildans(int v, int tl, int tr)
{
    if(tl == tr)
    {
        segans[v] = seq[tl];
        return;
    }

    int tm = (tl + tr) / 2;

    buildans(2 * v, tl, tm);
    buildans(2 * v + 1, tm + 1, tr);
    segans[v] = lca(segans[v * 2], segans[v * 2 + 1]);
}

void updateans(int v, int tl, int tr, int pos, int val)
{
    if(tl == tr)
    {
        segans[v] = val;
        return;
    }

    int tm = (tl + tr) / 2;
    if(tm >= pos)
        updateans(2 * v, tl, tm, pos, val);
    else
        updateans(2 * v + 1, tm + 1, tr, pos, val);

    segans[v] = lca(segans[2 * v], segans[2 * v + 1]);
}


int getans(int v, int tl, int tr, int l, int r)
{
    if(l > r)
        return -1;
    if(l == tl && r == tr)
    {
        return segans[v];
    }
    int tm = (tl + tr) / 2;
    int left = getans(2 * v, tl, tm, l, min(r, tm)), right = getans(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
    if(left == -1)
        return right;
    if(right == -1)
        return left;
    return lca(left, right);
}


int main() 
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    for(int i = 1; i <= n - 1; i++)
    {
        int f, s;
        cin >> f >> s;
        adj[f].push_back(s);
        adj[s].push_back(f);
    }

    dfs(1, -1, 0);
    buildlca(1, 0, euler.size() - 1);
    for(int i = 0; i < m; i++)
        cin >> seq[i];
    buildans(1, 0, m - 1);
    //cout << in[2] << " " << in[3] << " " << height[2] << " " << height[3] << " " << lca(2, 2) << "\n";
    while(q--)
    {
        int type;
        cin >> type;
        if(type == 1)
        {
            int pos, val;
            cin >> pos >> val;
            pos--;
            seq[pos] = val;
            //updateans(1, 0, m - 1, pos, val);
        } else 
        {
            int l, r, v;
            cin >> l >> r >> v;
            l--;
            r--;
            int curr = 0;
            int last = l;
            bool valid = false;
            for(int i = l; i <= r; i++)
            {
                if(!(in[v] <= in[seq[i]] && in[seq[i]] <= out[v])) {
                    curr = 0;
                    last = i + 1;
                    continue;
                }
                if(curr == 0)
                    curr = seq[i];
                else
                    curr = lca(curr, seq[i]);
                if(curr == v)
                {
                    cout << last + 1 << " " << i + 1 << "\n";
                    valid = true;
                    break;
                }
            }

            if(!valid)
                cout << "-1 -1\n";
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5040 KB n=100
8 Correct 3 ms 5036 KB n=100
9 Correct 4 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4964 KB n=100
15 Correct 3 ms 4964 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 4 ms 4948 KB n=100
19 Correct 2 ms 5036 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 2 ms 5040 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 3 ms 5040 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5040 KB n=100
8 Correct 3 ms 5036 KB n=100
9 Correct 4 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4964 KB n=100
15 Correct 3 ms 4964 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 4 ms 4948 KB n=100
19 Correct 2 ms 5036 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 2 ms 5040 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 3 ms 5040 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
28 Correct 3 ms 5076 KB n=500
29 Correct 5 ms 5076 KB n=500
30 Correct 5 ms 5076 KB n=500
31 Correct 5 ms 5076 KB n=500
32 Correct 3 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 3 ms 5076 KB n=500
35 Correct 4 ms 5076 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5040 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 4 ms 5056 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 4 ms 5044 KB n=500
43 Correct 4 ms 5076 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 7 ms 5048 KB n=500
47 Correct 4 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 4 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 5 ms 5100 KB n=500
52 Correct 5 ms 5044 KB n=500
53 Correct 4 ms 5044 KB n=500
54 Correct 12 ms 5076 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 21 ms 5076 KB n=500
57 Correct 17 ms 5076 KB n=500
58 Correct 3 ms 5076 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5040 KB n=100
8 Correct 3 ms 5036 KB n=100
9 Correct 4 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4964 KB n=100
15 Correct 3 ms 4964 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 4 ms 4948 KB n=100
19 Correct 2 ms 5036 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 2 ms 5040 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 3 ms 5040 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
28 Correct 3 ms 5076 KB n=500
29 Correct 5 ms 5076 KB n=500
30 Correct 5 ms 5076 KB n=500
31 Correct 5 ms 5076 KB n=500
32 Correct 3 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 3 ms 5076 KB n=500
35 Correct 4 ms 5076 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5040 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 4 ms 5056 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 4 ms 5044 KB n=500
43 Correct 4 ms 5076 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 7 ms 5048 KB n=500
47 Correct 4 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 4 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 5 ms 5100 KB n=500
52 Correct 5 ms 5044 KB n=500
53 Correct 4 ms 5044 KB n=500
54 Correct 12 ms 5076 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 21 ms 5076 KB n=500
57 Correct 17 ms 5076 KB n=500
58 Correct 3 ms 5076 KB n=500
59 Correct 5 ms 5204 KB n=2000
60 Correct 42 ms 5308 KB n=2000
61 Correct 39 ms 5332 KB n=2000
62 Correct 31 ms 5324 KB n=2000
63 Correct 5 ms 5308 KB n=2000
64 Correct 38 ms 5344 KB n=2000
65 Correct 4 ms 5172 KB n=2000
66 Correct 47 ms 5384 KB n=2000
67 Correct 5 ms 5204 KB n=2000
68 Correct 39 ms 5308 KB n=2000
69 Correct 7 ms 5304 KB n=2000
70 Correct 6 ms 5204 KB n=2000
71 Correct 7 ms 5204 KB n=2000
72 Correct 5 ms 5204 KB n=2000
73 Correct 5 ms 5264 KB n=2000
74 Correct 5 ms 5236 KB n=1844
75 Correct 8 ms 5204 KB n=2000
76 Correct 29 ms 5288 KB n=2000
77 Correct 22 ms 5300 KB n=2000
78 Correct 32 ms 5272 KB n=2000
79 Correct 5 ms 5204 KB n=2000
80 Correct 59 ms 5392 KB n=2000
81 Correct 56 ms 5340 KB n=2000
82 Correct 7 ms 5172 KB n=2000
83 Correct 51 ms 5332 KB n=2000
84 Correct 8 ms 5204 KB n=2000
85 Correct 32 ms 5332 KB n=2000
86 Correct 22 ms 5304 KB n=2000
87 Correct 7 ms 5260 KB n=2000
88 Correct 369 ms 5400 KB n=2000
89 Correct 366 ms 5332 KB n=2000
90 Correct 194 ms 5412 KB n=2000
91 Correct 9 ms 5204 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB n=5
2 Correct 3 ms 4948 KB n=100
3 Correct 3 ms 4948 KB n=100
4 Correct 3 ms 4948 KB n=100
5 Correct 3 ms 5028 KB n=100
6 Correct 3 ms 4948 KB n=100
7 Correct 3 ms 5040 KB n=100
8 Correct 3 ms 5036 KB n=100
9 Correct 4 ms 4948 KB n=100
10 Correct 3 ms 4948 KB n=100
11 Correct 3 ms 4948 KB n=100
12 Correct 3 ms 4948 KB n=100
13 Correct 3 ms 4948 KB n=100
14 Correct 3 ms 4964 KB n=100
15 Correct 3 ms 4964 KB n=100
16 Correct 3 ms 5076 KB n=100
17 Correct 3 ms 4948 KB n=100
18 Correct 4 ms 4948 KB n=100
19 Correct 2 ms 5036 KB n=100
20 Correct 3 ms 4948 KB n=100
21 Correct 3 ms 4948 KB n=100
22 Correct 2 ms 5040 KB n=100
23 Correct 3 ms 4948 KB n=100
24 Correct 3 ms 5076 KB n=100
25 Correct 3 ms 5040 KB n=100
26 Correct 3 ms 4948 KB n=12
27 Correct 4 ms 4948 KB n=100
28 Correct 3 ms 5076 KB n=500
29 Correct 5 ms 5076 KB n=500
30 Correct 5 ms 5076 KB n=500
31 Correct 5 ms 5076 KB n=500
32 Correct 3 ms 5076 KB n=500
33 Correct 4 ms 5076 KB n=500
34 Correct 3 ms 5076 KB n=500
35 Correct 4 ms 5076 KB n=500
36 Correct 3 ms 5076 KB n=500
37 Correct 3 ms 5076 KB n=500
38 Correct 3 ms 5040 KB n=500
39 Correct 3 ms 5076 KB n=500
40 Correct 4 ms 5056 KB n=500
41 Correct 3 ms 5076 KB n=500
42 Correct 4 ms 5044 KB n=500
43 Correct 4 ms 5076 KB n=500
44 Correct 4 ms 5076 KB n=500
45 Correct 3 ms 5076 KB n=500
46 Correct 7 ms 5048 KB n=500
47 Correct 4 ms 5076 KB n=500
48 Correct 3 ms 5076 KB n=500
49 Correct 4 ms 5076 KB n=500
50 Correct 3 ms 5076 KB n=500
51 Correct 5 ms 5100 KB n=500
52 Correct 5 ms 5044 KB n=500
53 Correct 4 ms 5044 KB n=500
54 Correct 12 ms 5076 KB n=500
55 Correct 3 ms 5076 KB n=278
56 Correct 21 ms 5076 KB n=500
57 Correct 17 ms 5076 KB n=500
58 Correct 3 ms 5076 KB n=500
59 Correct 5 ms 5204 KB n=2000
60 Correct 42 ms 5308 KB n=2000
61 Correct 39 ms 5332 KB n=2000
62 Correct 31 ms 5324 KB n=2000
63 Correct 5 ms 5308 KB n=2000
64 Correct 38 ms 5344 KB n=2000
65 Correct 4 ms 5172 KB n=2000
66 Correct 47 ms 5384 KB n=2000
67 Correct 5 ms 5204 KB n=2000
68 Correct 39 ms 5308 KB n=2000
69 Correct 7 ms 5304 KB n=2000
70 Correct 6 ms 5204 KB n=2000
71 Correct 7 ms 5204 KB n=2000
72 Correct 5 ms 5204 KB n=2000
73 Correct 5 ms 5264 KB n=2000
74 Correct 5 ms 5236 KB n=1844
75 Correct 8 ms 5204 KB n=2000
76 Correct 29 ms 5288 KB n=2000
77 Correct 22 ms 5300 KB n=2000
78 Correct 32 ms 5272 KB n=2000
79 Correct 5 ms 5204 KB n=2000
80 Correct 59 ms 5392 KB n=2000
81 Correct 56 ms 5340 KB n=2000
82 Correct 7 ms 5172 KB n=2000
83 Correct 51 ms 5332 KB n=2000
84 Correct 8 ms 5204 KB n=2000
85 Correct 32 ms 5332 KB n=2000
86 Correct 22 ms 5304 KB n=2000
87 Correct 7 ms 5260 KB n=2000
88 Correct 369 ms 5400 KB n=2000
89 Correct 366 ms 5332 KB n=2000
90 Correct 194 ms 5412 KB n=2000
91 Correct 9 ms 5204 KB n=2000
92 Runtime error 80 ms 39396 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -