답안 #685548

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
685548 2023-01-24T14:03:16 Z speedyArda Birthday gift (IZhO18_treearray) C++14
56 / 100
98 ms 58380 KB
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 2e5+5;
int seglca[MAXN * 4], in[MAXN], out[MAXN], height[MAXN], seq[MAXN];
vector<int> euler;
vector< vector<int> > adj(MAXN);
set< pair<int, int> > pref[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]);
}


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];
        pref[seq[i]].insert({i, i});
        if(i > 0)
        {
            pref[lca(seq[i - 1], seq[i])].insert({i - 1, i});
        }
    }

    while(q--)
    {
        int type;
        cin >> type;
        if(type == 1)
        {
            int pos, val;
            cin >> pos >> val;
            pos--;
            pref[seq[pos]].erase({pos, pos});
            if(pos > 0)
                pref[lca(seq[pos - 1], seq[pos])].erase({pos - 1, pos});
            if(pos < m - 1)
                pref[lca(seq[pos + 1], seq[pos])].erase({pos, pos + 1});

            seq[pos] = val;
            pref[seq[pos]].insert({pos, pos});
            if(pos > 0)
                pref[lca(seq[pos - 1], seq[pos])].insert({pos - 1, pos});
            if(pos < m - 1)
                pref[lca(seq[pos + 1], seq[pos])].insert({pos, pos + 1});
            //updateans(1, 0, m - 1, pos, val);
        } else 
        {
            int l, r, v;
            cin >> l >> r >> v;
            l--;
            r--;

            auto it = pref[v].lower_bound({l, l});

            if(it != pref[v].end() && (*it).first >= l && (*it).second <= r)
                cout << (*it).first + 1 << " " << (*it).second + 1 << "\n";
            else
                cout << "-1 -1\n";
           
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14528 KB n=100
3 Correct 10 ms 14420 KB n=100
4 Correct 10 ms 14388 KB n=100
5 Correct 9 ms 14420 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14388 KB n=100
8 Correct 7 ms 14468 KB n=100
9 Correct 9 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 8 ms 14436 KB n=100
12 Correct 7 ms 14420 KB n=100
13 Correct 7 ms 14420 KB n=100
14 Correct 7 ms 14448 KB n=100
15 Correct 8 ms 14416 KB n=100
16 Correct 8 ms 14420 KB n=100
17 Correct 8 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14452 KB n=100
20 Correct 8 ms 14436 KB n=100
21 Correct 8 ms 14436 KB n=100
22 Correct 7 ms 14448 KB n=100
23 Correct 7 ms 14420 KB n=100
24 Correct 9 ms 14420 KB n=100
25 Correct 10 ms 14440 KB n=100
26 Correct 7 ms 14436 KB n=12
27 Correct 8 ms 14420 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14528 KB n=100
3 Correct 10 ms 14420 KB n=100
4 Correct 10 ms 14388 KB n=100
5 Correct 9 ms 14420 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14388 KB n=100
8 Correct 7 ms 14468 KB n=100
9 Correct 9 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 8 ms 14436 KB n=100
12 Correct 7 ms 14420 KB n=100
13 Correct 7 ms 14420 KB n=100
14 Correct 7 ms 14448 KB n=100
15 Correct 8 ms 14416 KB n=100
16 Correct 8 ms 14420 KB n=100
17 Correct 8 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14452 KB n=100
20 Correct 8 ms 14436 KB n=100
21 Correct 8 ms 14436 KB n=100
22 Correct 7 ms 14448 KB n=100
23 Correct 7 ms 14420 KB n=100
24 Correct 9 ms 14420 KB n=100
25 Correct 10 ms 14440 KB n=100
26 Correct 7 ms 14436 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 8 ms 14548 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 8 ms 14548 KB n=500
31 Correct 8 ms 14452 KB n=500
32 Correct 10 ms 14548 KB n=500
33 Correct 9 ms 14504 KB n=500
34 Correct 9 ms 14440 KB n=500
35 Correct 9 ms 14568 KB n=500
36 Correct 8 ms 14448 KB n=500
37 Correct 8 ms 14548 KB n=500
38 Correct 8 ms 14512 KB n=500
39 Correct 8 ms 14548 KB n=500
40 Correct 8 ms 14528 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 12 ms 14540 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 10 ms 14440 KB n=500
45 Correct 8 ms 14448 KB n=500
46 Correct 8 ms 14548 KB n=500
47 Correct 10 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14524 KB n=500
50 Correct 8 ms 14528 KB n=500
51 Correct 10 ms 14512 KB n=500
52 Correct 10 ms 14572 KB n=500
53 Correct 10 ms 14440 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 8 ms 14548 KB n=278
56 Correct 9 ms 14568 KB n=500
57 Correct 9 ms 14548 KB n=500
58 Correct 9 ms 14552 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14528 KB n=100
3 Correct 10 ms 14420 KB n=100
4 Correct 10 ms 14388 KB n=100
5 Correct 9 ms 14420 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14388 KB n=100
8 Correct 7 ms 14468 KB n=100
9 Correct 9 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 8 ms 14436 KB n=100
12 Correct 7 ms 14420 KB n=100
13 Correct 7 ms 14420 KB n=100
14 Correct 7 ms 14448 KB n=100
15 Correct 8 ms 14416 KB n=100
16 Correct 8 ms 14420 KB n=100
17 Correct 8 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14452 KB n=100
20 Correct 8 ms 14436 KB n=100
21 Correct 8 ms 14436 KB n=100
22 Correct 7 ms 14448 KB n=100
23 Correct 7 ms 14420 KB n=100
24 Correct 9 ms 14420 KB n=100
25 Correct 10 ms 14440 KB n=100
26 Correct 7 ms 14436 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 8 ms 14548 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 8 ms 14548 KB n=500
31 Correct 8 ms 14452 KB n=500
32 Correct 10 ms 14548 KB n=500
33 Correct 9 ms 14504 KB n=500
34 Correct 9 ms 14440 KB n=500
35 Correct 9 ms 14568 KB n=500
36 Correct 8 ms 14448 KB n=500
37 Correct 8 ms 14548 KB n=500
38 Correct 8 ms 14512 KB n=500
39 Correct 8 ms 14548 KB n=500
40 Correct 8 ms 14528 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 12 ms 14540 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 10 ms 14440 KB n=500
45 Correct 8 ms 14448 KB n=500
46 Correct 8 ms 14548 KB n=500
47 Correct 10 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14524 KB n=500
50 Correct 8 ms 14528 KB n=500
51 Correct 10 ms 14512 KB n=500
52 Correct 10 ms 14572 KB n=500
53 Correct 10 ms 14440 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 8 ms 14548 KB n=278
56 Correct 9 ms 14568 KB n=500
57 Correct 9 ms 14548 KB n=500
58 Correct 9 ms 14552 KB n=500
59 Correct 12 ms 14804 KB n=2000
60 Correct 12 ms 14960 KB n=2000
61 Correct 12 ms 14864 KB n=2000
62 Correct 12 ms 14812 KB n=2000
63 Correct 13 ms 14788 KB n=2000
64 Correct 12 ms 14932 KB n=2000
65 Correct 11 ms 14784 KB n=2000
66 Correct 12 ms 14828 KB n=2000
67 Correct 12 ms 14804 KB n=2000
68 Correct 13 ms 14804 KB n=2000
69 Correct 12 ms 14808 KB n=2000
70 Correct 10 ms 14764 KB n=2000
71 Correct 10 ms 14800 KB n=2000
72 Correct 10 ms 14804 KB n=2000
73 Correct 10 ms 14804 KB n=2000
74 Correct 12 ms 14832 KB n=1844
75 Correct 10 ms 14788 KB n=2000
76 Correct 11 ms 14840 KB n=2000
77 Correct 14 ms 14804 KB n=2000
78 Correct 15 ms 14804 KB n=2000
79 Correct 14 ms 14832 KB n=2000
80 Correct 12 ms 14932 KB n=2000
81 Correct 11 ms 14804 KB n=2000
82 Correct 14 ms 14808 KB n=2000
83 Correct 12 ms 14804 KB n=2000
84 Correct 14 ms 14804 KB n=2000
85 Correct 12 ms 14804 KB n=2000
86 Correct 14 ms 14836 KB n=2000
87 Correct 13 ms 14804 KB n=2000
88 Correct 11 ms 14932 KB n=2000
89 Correct 12 ms 14932 KB n=2000
90 Correct 11 ms 14932 KB n=2000
91 Correct 10 ms 14832 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB n=5
2 Correct 8 ms 14528 KB n=100
3 Correct 10 ms 14420 KB n=100
4 Correct 10 ms 14388 KB n=100
5 Correct 9 ms 14420 KB n=100
6 Correct 8 ms 14420 KB n=100
7 Correct 8 ms 14388 KB n=100
8 Correct 7 ms 14468 KB n=100
9 Correct 9 ms 14420 KB n=100
10 Correct 7 ms 14420 KB n=100
11 Correct 8 ms 14436 KB n=100
12 Correct 7 ms 14420 KB n=100
13 Correct 7 ms 14420 KB n=100
14 Correct 7 ms 14448 KB n=100
15 Correct 8 ms 14416 KB n=100
16 Correct 8 ms 14420 KB n=100
17 Correct 8 ms 14420 KB n=100
18 Correct 8 ms 14420 KB n=100
19 Correct 8 ms 14452 KB n=100
20 Correct 8 ms 14436 KB n=100
21 Correct 8 ms 14436 KB n=100
22 Correct 7 ms 14448 KB n=100
23 Correct 7 ms 14420 KB n=100
24 Correct 9 ms 14420 KB n=100
25 Correct 10 ms 14440 KB n=100
26 Correct 7 ms 14436 KB n=12
27 Correct 8 ms 14420 KB n=100
28 Correct 8 ms 14548 KB n=500
29 Correct 8 ms 14548 KB n=500
30 Correct 8 ms 14548 KB n=500
31 Correct 8 ms 14452 KB n=500
32 Correct 10 ms 14548 KB n=500
33 Correct 9 ms 14504 KB n=500
34 Correct 9 ms 14440 KB n=500
35 Correct 9 ms 14568 KB n=500
36 Correct 8 ms 14448 KB n=500
37 Correct 8 ms 14548 KB n=500
38 Correct 8 ms 14512 KB n=500
39 Correct 8 ms 14548 KB n=500
40 Correct 8 ms 14528 KB n=500
41 Correct 8 ms 14548 KB n=500
42 Correct 12 ms 14540 KB n=500
43 Correct 9 ms 14548 KB n=500
44 Correct 10 ms 14440 KB n=500
45 Correct 8 ms 14448 KB n=500
46 Correct 8 ms 14548 KB n=500
47 Correct 10 ms 14548 KB n=500
48 Correct 8 ms 14548 KB n=500
49 Correct 8 ms 14524 KB n=500
50 Correct 8 ms 14528 KB n=500
51 Correct 10 ms 14512 KB n=500
52 Correct 10 ms 14572 KB n=500
53 Correct 10 ms 14440 KB n=500
54 Correct 8 ms 14548 KB n=500
55 Correct 8 ms 14548 KB n=278
56 Correct 9 ms 14568 KB n=500
57 Correct 9 ms 14548 KB n=500
58 Correct 9 ms 14552 KB n=500
59 Correct 12 ms 14804 KB n=2000
60 Correct 12 ms 14960 KB n=2000
61 Correct 12 ms 14864 KB n=2000
62 Correct 12 ms 14812 KB n=2000
63 Correct 13 ms 14788 KB n=2000
64 Correct 12 ms 14932 KB n=2000
65 Correct 11 ms 14784 KB n=2000
66 Correct 12 ms 14828 KB n=2000
67 Correct 12 ms 14804 KB n=2000
68 Correct 13 ms 14804 KB n=2000
69 Correct 12 ms 14808 KB n=2000
70 Correct 10 ms 14764 KB n=2000
71 Correct 10 ms 14800 KB n=2000
72 Correct 10 ms 14804 KB n=2000
73 Correct 10 ms 14804 KB n=2000
74 Correct 12 ms 14832 KB n=1844
75 Correct 10 ms 14788 KB n=2000
76 Correct 11 ms 14840 KB n=2000
77 Correct 14 ms 14804 KB n=2000
78 Correct 15 ms 14804 KB n=2000
79 Correct 14 ms 14832 KB n=2000
80 Correct 12 ms 14932 KB n=2000
81 Correct 11 ms 14804 KB n=2000
82 Correct 14 ms 14808 KB n=2000
83 Correct 12 ms 14804 KB n=2000
84 Correct 14 ms 14804 KB n=2000
85 Correct 12 ms 14804 KB n=2000
86 Correct 14 ms 14836 KB n=2000
87 Correct 13 ms 14804 KB n=2000
88 Correct 11 ms 14932 KB n=2000
89 Correct 12 ms 14932 KB n=2000
90 Correct 11 ms 14932 KB n=2000
91 Correct 10 ms 14832 KB n=2000
92 Runtime error 98 ms 58380 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -