Submission #470023

# Submission time Handle Problem Language Result Execution time Memory
470023 2021-09-02T16:45:55 Z soba Birthday gift (IZhO18_treearray) C++14
100 / 100
1116 ms 93320 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int n, m , q ,l;
vector<int> adj[200005] ;
set<int>ansone[200005] , anstwo[200005];
int arr[200005];
int timer;
vector<int> tin, tout;
vector<vector<int>> up;

void dfs(int v, int p)
{
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i <= l; ++i)
        up[v][i] = up[up[v][i-1]][i-1];

    for (int u : adj[v]) {
        if (u != p)
            dfs(u, v);
    }

    tout[v] = ++timer;
    return;
}

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

int lca(int u, int v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = l; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}

void preprocess(int root) {
    tin.resize(n+1);
    tout.resize(n+1);
    timer = 0;
    l = ceil(log2(n+1));
    up.assign(n+1, vector<int>(l + 1));
    dfs(root, root);
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >>m >> q ;
    int x , y ,z, que , val , idx;
    for(int i  = 0 ;  i < n -1 ; i++)
    {
        cin >>x>>y ;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    preprocess(1);
    vector<int>vv;
    for(int i = 0 ; i < m ;i++)
    {
        cin >> arr[i];
        ansone[arr[i]].insert(i);
        if(i==0)continue;
        vv.push_back(lca(arr[i] , arr[i-1]));
    }
    for(int i = 0; i < m-1 ; i++)
    {
        anstwo[vv[i]].insert(i);
    }
    while(q--)
    {
        cin >> que;
        if(que==1)
        {
            cin >> x >> y ;
            x--;
            val=arr[x];
            ansone[val].erase(ansone[val].lower_bound(x));
            arr[x]=y;
            ansone[y].insert(x);
            if(x<m-1)
            {
                val=vv[x];
                anstwo[val].erase(anstwo[val].lower_bound(x));
                vv[x]=lca(arr[x] , arr[x+1]);
                anstwo[vv[x]].insert(x);
            }
            if(x>0)
            {
                val=vv[x-1];
                anstwo[val].erase(anstwo[val].lower_bound(x-1));
                vv[x-1]=lca(arr[x-1] , arr[x]);
                anstwo[vv[x-1]].insert(x-1);
            }
        }
        else
        {
            cin >>x >>y>>z;
            if(ansone[z].lower_bound(x-1)!=ansone[z].end())
            {
               // cout << "one\n";
                idx=*ansone[z].lower_bound(x-1);
                if(idx<y)
                {
                    cout << idx+1 << " " << idx+1 << "\n";
                    continue;
                }
            }
            if(anstwo[z].lower_bound(x-1)!=anstwo[z].end())
            {
                //cout << ""
                idx=*anstwo[z].lower_bound(x-1);
                if(idx<y-1)
                {
                    cout << idx+1<< " " <<idx+2<< "\n";
                    continue;
                }
            }
            cout << "-1 -1\n";
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Correct 13 ms 23780 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23756 KB n=100
5 Correct 14 ms 23756 KB n=100
6 Correct 14 ms 23816 KB n=100
7 Correct 14 ms 23716 KB n=100
8 Correct 16 ms 23776 KB n=100
9 Correct 16 ms 23820 KB n=100
10 Correct 13 ms 23820 KB n=100
11 Correct 13 ms 23792 KB n=100
12 Correct 13 ms 23768 KB n=100
13 Correct 14 ms 23812 KB n=100
14 Correct 13 ms 23768 KB n=100
15 Correct 15 ms 23784 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 16 ms 23756 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 16 ms 23796 KB n=100
21 Correct 13 ms 23756 KB n=100
22 Correct 13 ms 23756 KB n=100
23 Correct 13 ms 23716 KB n=100
24 Correct 13 ms 23760 KB n=100
25 Correct 14 ms 23732 KB n=100
26 Correct 13 ms 23744 KB n=12
27 Correct 13 ms 23784 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Correct 13 ms 23780 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23756 KB n=100
5 Correct 14 ms 23756 KB n=100
6 Correct 14 ms 23816 KB n=100
7 Correct 14 ms 23716 KB n=100
8 Correct 16 ms 23776 KB n=100
9 Correct 16 ms 23820 KB n=100
10 Correct 13 ms 23820 KB n=100
11 Correct 13 ms 23792 KB n=100
12 Correct 13 ms 23768 KB n=100
13 Correct 14 ms 23812 KB n=100
14 Correct 13 ms 23768 KB n=100
15 Correct 15 ms 23784 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 16 ms 23756 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 16 ms 23796 KB n=100
21 Correct 13 ms 23756 KB n=100
22 Correct 13 ms 23756 KB n=100
23 Correct 13 ms 23716 KB n=100
24 Correct 13 ms 23760 KB n=100
25 Correct 14 ms 23732 KB n=100
26 Correct 13 ms 23744 KB n=12
27 Correct 13 ms 23784 KB n=100
28 Correct 14 ms 23884 KB n=500
29 Correct 16 ms 23896 KB n=500
30 Correct 14 ms 23896 KB n=500
31 Correct 16 ms 23920 KB n=500
32 Correct 13 ms 23884 KB n=500
33 Correct 14 ms 23884 KB n=500
34 Correct 14 ms 23884 KB n=500
35 Correct 14 ms 23924 KB n=500
36 Correct 14 ms 23884 KB n=500
37 Correct 14 ms 23884 KB n=500
38 Correct 14 ms 23868 KB n=500
39 Correct 13 ms 23916 KB n=500
40 Correct 13 ms 23872 KB n=500
41 Correct 14 ms 23884 KB n=500
42 Correct 14 ms 23912 KB n=500
43 Correct 14 ms 23884 KB n=500
44 Correct 16 ms 23912 KB n=500
45 Correct 14 ms 23912 KB n=500
46 Correct 15 ms 23928 KB n=500
47 Correct 13 ms 23884 KB n=500
48 Correct 14 ms 23884 KB n=500
49 Correct 14 ms 23852 KB n=500
50 Correct 14 ms 23788 KB n=500
51 Correct 14 ms 23884 KB n=500
52 Correct 14 ms 23876 KB n=500
53 Correct 14 ms 23908 KB n=500
54 Correct 14 ms 23884 KB n=500
55 Correct 14 ms 23884 KB n=278
56 Correct 14 ms 23884 KB n=500
57 Correct 13 ms 23852 KB n=500
58 Correct 13 ms 23824 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Correct 13 ms 23780 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23756 KB n=100
5 Correct 14 ms 23756 KB n=100
6 Correct 14 ms 23816 KB n=100
7 Correct 14 ms 23716 KB n=100
8 Correct 16 ms 23776 KB n=100
9 Correct 16 ms 23820 KB n=100
10 Correct 13 ms 23820 KB n=100
11 Correct 13 ms 23792 KB n=100
12 Correct 13 ms 23768 KB n=100
13 Correct 14 ms 23812 KB n=100
14 Correct 13 ms 23768 KB n=100
15 Correct 15 ms 23784 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 16 ms 23756 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 16 ms 23796 KB n=100
21 Correct 13 ms 23756 KB n=100
22 Correct 13 ms 23756 KB n=100
23 Correct 13 ms 23716 KB n=100
24 Correct 13 ms 23760 KB n=100
25 Correct 14 ms 23732 KB n=100
26 Correct 13 ms 23744 KB n=12
27 Correct 13 ms 23784 KB n=100
28 Correct 14 ms 23884 KB n=500
29 Correct 16 ms 23896 KB n=500
30 Correct 14 ms 23896 KB n=500
31 Correct 16 ms 23920 KB n=500
32 Correct 13 ms 23884 KB n=500
33 Correct 14 ms 23884 KB n=500
34 Correct 14 ms 23884 KB n=500
35 Correct 14 ms 23924 KB n=500
36 Correct 14 ms 23884 KB n=500
37 Correct 14 ms 23884 KB n=500
38 Correct 14 ms 23868 KB n=500
39 Correct 13 ms 23916 KB n=500
40 Correct 13 ms 23872 KB n=500
41 Correct 14 ms 23884 KB n=500
42 Correct 14 ms 23912 KB n=500
43 Correct 14 ms 23884 KB n=500
44 Correct 16 ms 23912 KB n=500
45 Correct 14 ms 23912 KB n=500
46 Correct 15 ms 23928 KB n=500
47 Correct 13 ms 23884 KB n=500
48 Correct 14 ms 23884 KB n=500
49 Correct 14 ms 23852 KB n=500
50 Correct 14 ms 23788 KB n=500
51 Correct 14 ms 23884 KB n=500
52 Correct 14 ms 23876 KB n=500
53 Correct 14 ms 23908 KB n=500
54 Correct 14 ms 23884 KB n=500
55 Correct 14 ms 23884 KB n=278
56 Correct 14 ms 23884 KB n=500
57 Correct 13 ms 23852 KB n=500
58 Correct 13 ms 23824 KB n=500
59 Correct 18 ms 24244 KB n=2000
60 Correct 16 ms 24244 KB n=2000
61 Correct 16 ms 24268 KB n=2000
62 Correct 16 ms 24272 KB n=2000
63 Correct 18 ms 24228 KB n=2000
64 Correct 16 ms 24268 KB n=2000
65 Correct 17 ms 24272 KB n=2000
66 Correct 16 ms 24268 KB n=2000
67 Correct 16 ms 24268 KB n=2000
68 Correct 17 ms 24276 KB n=2000
69 Correct 16 ms 24268 KB n=2000
70 Correct 16 ms 24392 KB n=2000
71 Correct 17 ms 24276 KB n=2000
72 Correct 16 ms 24192 KB n=2000
73 Correct 16 ms 24268 KB n=2000
74 Correct 17 ms 24240 KB n=1844
75 Correct 16 ms 24268 KB n=2000
76 Correct 17 ms 24256 KB n=2000
77 Correct 17 ms 24268 KB n=2000
78 Correct 19 ms 24224 KB n=2000
79 Correct 20 ms 24268 KB n=2000
80 Correct 19 ms 24268 KB n=2000
81 Correct 16 ms 24296 KB n=2000
82 Correct 16 ms 24200 KB n=2000
83 Correct 17 ms 24260 KB n=2000
84 Correct 16 ms 24200 KB n=2000
85 Correct 16 ms 24268 KB n=2000
86 Correct 19 ms 24268 KB n=2000
87 Correct 19 ms 24204 KB n=2000
88 Correct 18 ms 24236 KB n=2000
89 Correct 19 ms 24264 KB n=2000
90 Correct 16 ms 24356 KB n=2000
91 Correct 16 ms 24268 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23756 KB n=5
2 Correct 13 ms 23780 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23756 KB n=100
5 Correct 14 ms 23756 KB n=100
6 Correct 14 ms 23816 KB n=100
7 Correct 14 ms 23716 KB n=100
8 Correct 16 ms 23776 KB n=100
9 Correct 16 ms 23820 KB n=100
10 Correct 13 ms 23820 KB n=100
11 Correct 13 ms 23792 KB n=100
12 Correct 13 ms 23768 KB n=100
13 Correct 14 ms 23812 KB n=100
14 Correct 13 ms 23768 KB n=100
15 Correct 15 ms 23784 KB n=100
16 Correct 14 ms 23756 KB n=100
17 Correct 13 ms 23756 KB n=100
18 Correct 16 ms 23756 KB n=100
19 Correct 13 ms 23820 KB n=100
20 Correct 16 ms 23796 KB n=100
21 Correct 13 ms 23756 KB n=100
22 Correct 13 ms 23756 KB n=100
23 Correct 13 ms 23716 KB n=100
24 Correct 13 ms 23760 KB n=100
25 Correct 14 ms 23732 KB n=100
26 Correct 13 ms 23744 KB n=12
27 Correct 13 ms 23784 KB n=100
28 Correct 14 ms 23884 KB n=500
29 Correct 16 ms 23896 KB n=500
30 Correct 14 ms 23896 KB n=500
31 Correct 16 ms 23920 KB n=500
32 Correct 13 ms 23884 KB n=500
33 Correct 14 ms 23884 KB n=500
34 Correct 14 ms 23884 KB n=500
35 Correct 14 ms 23924 KB n=500
36 Correct 14 ms 23884 KB n=500
37 Correct 14 ms 23884 KB n=500
38 Correct 14 ms 23868 KB n=500
39 Correct 13 ms 23916 KB n=500
40 Correct 13 ms 23872 KB n=500
41 Correct 14 ms 23884 KB n=500
42 Correct 14 ms 23912 KB n=500
43 Correct 14 ms 23884 KB n=500
44 Correct 16 ms 23912 KB n=500
45 Correct 14 ms 23912 KB n=500
46 Correct 15 ms 23928 KB n=500
47 Correct 13 ms 23884 KB n=500
48 Correct 14 ms 23884 KB n=500
49 Correct 14 ms 23852 KB n=500
50 Correct 14 ms 23788 KB n=500
51 Correct 14 ms 23884 KB n=500
52 Correct 14 ms 23876 KB n=500
53 Correct 14 ms 23908 KB n=500
54 Correct 14 ms 23884 KB n=500
55 Correct 14 ms 23884 KB n=278
56 Correct 14 ms 23884 KB n=500
57 Correct 13 ms 23852 KB n=500
58 Correct 13 ms 23824 KB n=500
59 Correct 18 ms 24244 KB n=2000
60 Correct 16 ms 24244 KB n=2000
61 Correct 16 ms 24268 KB n=2000
62 Correct 16 ms 24272 KB n=2000
63 Correct 18 ms 24228 KB n=2000
64 Correct 16 ms 24268 KB n=2000
65 Correct 17 ms 24272 KB n=2000
66 Correct 16 ms 24268 KB n=2000
67 Correct 16 ms 24268 KB n=2000
68 Correct 17 ms 24276 KB n=2000
69 Correct 16 ms 24268 KB n=2000
70 Correct 16 ms 24392 KB n=2000
71 Correct 17 ms 24276 KB n=2000
72 Correct 16 ms 24192 KB n=2000
73 Correct 16 ms 24268 KB n=2000
74 Correct 17 ms 24240 KB n=1844
75 Correct 16 ms 24268 KB n=2000
76 Correct 17 ms 24256 KB n=2000
77 Correct 17 ms 24268 KB n=2000
78 Correct 19 ms 24224 KB n=2000
79 Correct 20 ms 24268 KB n=2000
80 Correct 19 ms 24268 KB n=2000
81 Correct 16 ms 24296 KB n=2000
82 Correct 16 ms 24200 KB n=2000
83 Correct 17 ms 24260 KB n=2000
84 Correct 16 ms 24200 KB n=2000
85 Correct 16 ms 24268 KB n=2000
86 Correct 19 ms 24268 KB n=2000
87 Correct 19 ms 24204 KB n=2000
88 Correct 18 ms 24236 KB n=2000
89 Correct 19 ms 24264 KB n=2000
90 Correct 16 ms 24356 KB n=2000
91 Correct 16 ms 24268 KB n=2000
92 Correct 826 ms 83860 KB n=200000
93 Correct 886 ms 88832 KB n=200000
94 Correct 689 ms 92172 KB n=200000
95 Correct 820 ms 83624 KB n=200000
96 Correct 767 ms 83768 KB n=200000
97 Correct 972 ms 87876 KB n=200000
98 Correct 803 ms 83824 KB n=200000
99 Correct 1024 ms 83984 KB n=200000
100 Correct 811 ms 83796 KB n=200000
101 Correct 681 ms 93320 KB n=200000
102 Correct 541 ms 84956 KB n=200000
103 Correct 545 ms 84944 KB n=200000
104 Correct 511 ms 84880 KB n=200000
105 Correct 559 ms 85308 KB n=200000
106 Correct 543 ms 85324 KB n=200000
107 Correct 544 ms 85436 KB n=200000
108 Correct 910 ms 83776 KB n=200000
109 Correct 921 ms 83868 KB n=200000
110 Correct 859 ms 83744 KB n=200000
111 Correct 846 ms 83132 KB n=200000
112 Correct 644 ms 92424 KB n=200000
113 Correct 992 ms 87900 KB n=200000
114 Correct 862 ms 83308 KB n=200000
115 Correct 1083 ms 85852 KB n=200000
116 Correct 793 ms 83908 KB n=200000
117 Correct 640 ms 92732 KB n=200000
118 Correct 1116 ms 86664 KB n=200000
119 Correct 782 ms 83788 KB n=200000
120 Correct 577 ms 92344 KB n=200000
121 Correct 613 ms 92420 KB n=200000
122 Correct 600 ms 92700 KB n=200000
123 Correct 545 ms 85180 KB n=200000
124 Correct 196 ms 39824 KB n=25264