Submission #331560

# Submission time Handle Problem Language Result Execution time Memory
331560 2020-11-28T23:15:03 Z limabeans Birthday gift (IZhO18_treearray) C++17
100 / 100
1369 ms 89708 KB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl







const int maxn = 2e5 + 10;
const int LOG = 19;



int n, m, q;
vector<int> g[maxn];
int dep[maxn];
int par[LOG+1][maxn];

int a[maxn];


void dfs(int at, int p) {
    for (int j=1; j<LOG; j++) {
	par[j][at] = par[j-1][par[j-1][at]];
    }
    for (int to: g[at]) {
	if (to == p) continue;
	dep[to]=1+dep[at];
	par[0][to] = at;
	dfs(to, at);
    }
}


int lca(int u, int v) {
    if (dep[u]<dep[v]) swap(u,v);

    int dx = dep[u]-dep[v];
    for (int j=LOG-1; j>=0; j--) {
	if (dx>>j&1) {
	    u = par[j][u];
	}
    }

    if (u==v) return u;

    for (int j=LOG-1; j>=0; j--) {
	if (par[j][u] != par[j][v]) {
	    u = par[j][u];
	    v = par[j][v];
	}
    }

    return par[0][v];
}

set<int> dp[2][maxn];


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

    cin>>n>>m>>q;

    for (int i=0; i<n-1; i++) {
	int u,v; cin>>u>>v;
	--u; --v;
	g[u].push_back(v);
	g[v].push_back(u);
    }


    dfs(0,-1);

    for (int i=0; i<m; i++) {
	cin>>a[i];
	--a[i];
    }

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

    while (q--) {
	int op;
	cin>>op;
	if (op==1) {
	    int i,v;
	    cin>>i>>v;
	    --i; --v;

	    //undo

	    dp[0][a[i]].erase(i);
	    if (i>0) {
		dp[1][lca(a[i-1],a[i])].erase(i-1);
	    }

	    if (i+1<m) {
		dp[1][lca(a[i],a[i+1])].erase(i);
	    }


	    //upd
	    a[i]=v;

	    dp[0][a[i]].insert(i);
	    if (i>0) {
		dp[1][lca(a[i-1],a[i])].insert(i-1);
	    }

	    if (i+1<m) {
		dp[1][lca(a[i],a[i+1])].insert(i);
	    }

	    
	} else {
	    int l,r,v;
	    cin>>l>>r>>v;
	    --l; --r; --v;
	    int x = -2, y = -2;
	    {
		auto iter = dp[0][v].lower_bound(l);
		if (iter!=dp[0][v].end() && *iter<=r) {
		    x = *iter;
		    y = *iter;
		}
	    }

	    {
		auto iter = dp[1][v].lower_bound(l);
		if (iter!=dp[1][v].end() && *iter<r) {
		    x = *iter;
		    y = *iter + 1;
		}
	    }

	    cout<<x+1<<" "<<y+1<<"\n";
	    
	}
    }
    
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB n=5
2 Correct 17 ms 23916 KB n=100
3 Correct 17 ms 23916 KB n=100
4 Correct 17 ms 23916 KB n=100
5 Correct 16 ms 23916 KB n=100
6 Correct 17 ms 23916 KB n=100
7 Correct 17 ms 23916 KB n=100
8 Correct 17 ms 23916 KB n=100
9 Correct 16 ms 23916 KB n=100
10 Correct 16 ms 23916 KB n=100
11 Correct 16 ms 23916 KB n=100
12 Correct 17 ms 23916 KB n=100
13 Correct 17 ms 23916 KB n=100
14 Correct 18 ms 23916 KB n=100
15 Correct 18 ms 23916 KB n=100
16 Correct 17 ms 23916 KB n=100
17 Correct 16 ms 23916 KB n=100
18 Correct 17 ms 23916 KB n=100
19 Correct 17 ms 23916 KB n=100
20 Correct 16 ms 23916 KB n=100
21 Correct 17 ms 23916 KB n=100
22 Correct 17 ms 23916 KB n=100
23 Correct 17 ms 23916 KB n=100
24 Correct 17 ms 23916 KB n=100
25 Correct 17 ms 23916 KB n=100
26 Correct 16 ms 23916 KB n=12
27 Correct 16 ms 23916 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB n=5
2 Correct 17 ms 23916 KB n=100
3 Correct 17 ms 23916 KB n=100
4 Correct 17 ms 23916 KB n=100
5 Correct 16 ms 23916 KB n=100
6 Correct 17 ms 23916 KB n=100
7 Correct 17 ms 23916 KB n=100
8 Correct 17 ms 23916 KB n=100
9 Correct 16 ms 23916 KB n=100
10 Correct 16 ms 23916 KB n=100
11 Correct 16 ms 23916 KB n=100
12 Correct 17 ms 23916 KB n=100
13 Correct 17 ms 23916 KB n=100
14 Correct 18 ms 23916 KB n=100
15 Correct 18 ms 23916 KB n=100
16 Correct 17 ms 23916 KB n=100
17 Correct 16 ms 23916 KB n=100
18 Correct 17 ms 23916 KB n=100
19 Correct 17 ms 23916 KB n=100
20 Correct 16 ms 23916 KB n=100
21 Correct 17 ms 23916 KB n=100
22 Correct 17 ms 23916 KB n=100
23 Correct 17 ms 23916 KB n=100
24 Correct 17 ms 23916 KB n=100
25 Correct 17 ms 23916 KB n=100
26 Correct 16 ms 23916 KB n=12
27 Correct 16 ms 23916 KB n=100
28 Correct 18 ms 24044 KB n=500
29 Correct 18 ms 24044 KB n=500
30 Correct 17 ms 24172 KB n=500
31 Correct 17 ms 24044 KB n=500
32 Correct 17 ms 24044 KB n=500
33 Correct 17 ms 24088 KB n=500
34 Correct 17 ms 24044 KB n=500
35 Correct 17 ms 24044 KB n=500
36 Correct 18 ms 24044 KB n=500
37 Correct 17 ms 24044 KB n=500
38 Correct 17 ms 24044 KB n=500
39 Correct 17 ms 24044 KB n=500
40 Correct 17 ms 24044 KB n=500
41 Correct 17 ms 24044 KB n=500
42 Correct 20 ms 24044 KB n=500
43 Correct 18 ms 24172 KB n=500
44 Correct 17 ms 24044 KB n=500
45 Correct 17 ms 24044 KB n=500
46 Correct 17 ms 24044 KB n=500
47 Correct 18 ms 24044 KB n=500
48 Correct 17 ms 24044 KB n=500
49 Correct 17 ms 24044 KB n=500
50 Correct 17 ms 24192 KB n=500
51 Correct 18 ms 24044 KB n=500
52 Correct 17 ms 24192 KB n=500
53 Correct 17 ms 24044 KB n=500
54 Correct 17 ms 24044 KB n=500
55 Correct 22 ms 24044 KB n=278
56 Correct 17 ms 24044 KB n=500
57 Correct 18 ms 24172 KB n=500
58 Correct 20 ms 24044 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB n=5
2 Correct 17 ms 23916 KB n=100
3 Correct 17 ms 23916 KB n=100
4 Correct 17 ms 23916 KB n=100
5 Correct 16 ms 23916 KB n=100
6 Correct 17 ms 23916 KB n=100
7 Correct 17 ms 23916 KB n=100
8 Correct 17 ms 23916 KB n=100
9 Correct 16 ms 23916 KB n=100
10 Correct 16 ms 23916 KB n=100
11 Correct 16 ms 23916 KB n=100
12 Correct 17 ms 23916 KB n=100
13 Correct 17 ms 23916 KB n=100
14 Correct 18 ms 23916 KB n=100
15 Correct 18 ms 23916 KB n=100
16 Correct 17 ms 23916 KB n=100
17 Correct 16 ms 23916 KB n=100
18 Correct 17 ms 23916 KB n=100
19 Correct 17 ms 23916 KB n=100
20 Correct 16 ms 23916 KB n=100
21 Correct 17 ms 23916 KB n=100
22 Correct 17 ms 23916 KB n=100
23 Correct 17 ms 23916 KB n=100
24 Correct 17 ms 23916 KB n=100
25 Correct 17 ms 23916 KB n=100
26 Correct 16 ms 23916 KB n=12
27 Correct 16 ms 23916 KB n=100
28 Correct 18 ms 24044 KB n=500
29 Correct 18 ms 24044 KB n=500
30 Correct 17 ms 24172 KB n=500
31 Correct 17 ms 24044 KB n=500
32 Correct 17 ms 24044 KB n=500
33 Correct 17 ms 24088 KB n=500
34 Correct 17 ms 24044 KB n=500
35 Correct 17 ms 24044 KB n=500
36 Correct 18 ms 24044 KB n=500
37 Correct 17 ms 24044 KB n=500
38 Correct 17 ms 24044 KB n=500
39 Correct 17 ms 24044 KB n=500
40 Correct 17 ms 24044 KB n=500
41 Correct 17 ms 24044 KB n=500
42 Correct 20 ms 24044 KB n=500
43 Correct 18 ms 24172 KB n=500
44 Correct 17 ms 24044 KB n=500
45 Correct 17 ms 24044 KB n=500
46 Correct 17 ms 24044 KB n=500
47 Correct 18 ms 24044 KB n=500
48 Correct 17 ms 24044 KB n=500
49 Correct 17 ms 24044 KB n=500
50 Correct 17 ms 24192 KB n=500
51 Correct 18 ms 24044 KB n=500
52 Correct 17 ms 24192 KB n=500
53 Correct 17 ms 24044 KB n=500
54 Correct 17 ms 24044 KB n=500
55 Correct 22 ms 24044 KB n=278
56 Correct 17 ms 24044 KB n=500
57 Correct 18 ms 24172 KB n=500
58 Correct 20 ms 24044 KB n=500
59 Correct 19 ms 24428 KB n=2000
60 Correct 22 ms 24556 KB n=2000
61 Correct 21 ms 24556 KB n=2000
62 Correct 20 ms 24428 KB n=2000
63 Correct 20 ms 24428 KB n=2000
64 Correct 20 ms 24428 KB n=2000
65 Correct 23 ms 24556 KB n=2000
66 Correct 20 ms 24556 KB n=2000
67 Correct 21 ms 24428 KB n=2000
68 Correct 20 ms 24556 KB n=2000
69 Correct 20 ms 24428 KB n=2000
70 Correct 20 ms 24428 KB n=2000
71 Correct 20 ms 24428 KB n=2000
72 Correct 22 ms 24428 KB n=2000
73 Correct 20 ms 24428 KB n=2000
74 Correct 20 ms 24556 KB n=1844
75 Correct 20 ms 24428 KB n=2000
76 Correct 21 ms 24428 KB n=2000
77 Correct 21 ms 24428 KB n=2000
78 Correct 21 ms 24428 KB n=2000
79 Correct 20 ms 24428 KB n=2000
80 Correct 22 ms 24556 KB n=2000
81 Correct 21 ms 24540 KB n=2000
82 Correct 20 ms 24428 KB n=2000
83 Correct 20 ms 24556 KB n=2000
84 Correct 20 ms 24428 KB n=2000
85 Correct 20 ms 24428 KB n=2000
86 Correct 21 ms 24428 KB n=2000
87 Correct 20 ms 24428 KB n=2000
88 Correct 20 ms 24556 KB n=2000
89 Correct 23 ms 24556 KB n=2000
90 Correct 20 ms 24556 KB n=2000
91 Correct 20 ms 24428 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23916 KB n=5
2 Correct 17 ms 23916 KB n=100
3 Correct 17 ms 23916 KB n=100
4 Correct 17 ms 23916 KB n=100
5 Correct 16 ms 23916 KB n=100
6 Correct 17 ms 23916 KB n=100
7 Correct 17 ms 23916 KB n=100
8 Correct 17 ms 23916 KB n=100
9 Correct 16 ms 23916 KB n=100
10 Correct 16 ms 23916 KB n=100
11 Correct 16 ms 23916 KB n=100
12 Correct 17 ms 23916 KB n=100
13 Correct 17 ms 23916 KB n=100
14 Correct 18 ms 23916 KB n=100
15 Correct 18 ms 23916 KB n=100
16 Correct 17 ms 23916 KB n=100
17 Correct 16 ms 23916 KB n=100
18 Correct 17 ms 23916 KB n=100
19 Correct 17 ms 23916 KB n=100
20 Correct 16 ms 23916 KB n=100
21 Correct 17 ms 23916 KB n=100
22 Correct 17 ms 23916 KB n=100
23 Correct 17 ms 23916 KB n=100
24 Correct 17 ms 23916 KB n=100
25 Correct 17 ms 23916 KB n=100
26 Correct 16 ms 23916 KB n=12
27 Correct 16 ms 23916 KB n=100
28 Correct 18 ms 24044 KB n=500
29 Correct 18 ms 24044 KB n=500
30 Correct 17 ms 24172 KB n=500
31 Correct 17 ms 24044 KB n=500
32 Correct 17 ms 24044 KB n=500
33 Correct 17 ms 24088 KB n=500
34 Correct 17 ms 24044 KB n=500
35 Correct 17 ms 24044 KB n=500
36 Correct 18 ms 24044 KB n=500
37 Correct 17 ms 24044 KB n=500
38 Correct 17 ms 24044 KB n=500
39 Correct 17 ms 24044 KB n=500
40 Correct 17 ms 24044 KB n=500
41 Correct 17 ms 24044 KB n=500
42 Correct 20 ms 24044 KB n=500
43 Correct 18 ms 24172 KB n=500
44 Correct 17 ms 24044 KB n=500
45 Correct 17 ms 24044 KB n=500
46 Correct 17 ms 24044 KB n=500
47 Correct 18 ms 24044 KB n=500
48 Correct 17 ms 24044 KB n=500
49 Correct 17 ms 24044 KB n=500
50 Correct 17 ms 24192 KB n=500
51 Correct 18 ms 24044 KB n=500
52 Correct 17 ms 24192 KB n=500
53 Correct 17 ms 24044 KB n=500
54 Correct 17 ms 24044 KB n=500
55 Correct 22 ms 24044 KB n=278
56 Correct 17 ms 24044 KB n=500
57 Correct 18 ms 24172 KB n=500
58 Correct 20 ms 24044 KB n=500
59 Correct 19 ms 24428 KB n=2000
60 Correct 22 ms 24556 KB n=2000
61 Correct 21 ms 24556 KB n=2000
62 Correct 20 ms 24428 KB n=2000
63 Correct 20 ms 24428 KB n=2000
64 Correct 20 ms 24428 KB n=2000
65 Correct 23 ms 24556 KB n=2000
66 Correct 20 ms 24556 KB n=2000
67 Correct 21 ms 24428 KB n=2000
68 Correct 20 ms 24556 KB n=2000
69 Correct 20 ms 24428 KB n=2000
70 Correct 20 ms 24428 KB n=2000
71 Correct 20 ms 24428 KB n=2000
72 Correct 22 ms 24428 KB n=2000
73 Correct 20 ms 24428 KB n=2000
74 Correct 20 ms 24556 KB n=1844
75 Correct 20 ms 24428 KB n=2000
76 Correct 21 ms 24428 KB n=2000
77 Correct 21 ms 24428 KB n=2000
78 Correct 21 ms 24428 KB n=2000
79 Correct 20 ms 24428 KB n=2000
80 Correct 22 ms 24556 KB n=2000
81 Correct 21 ms 24540 KB n=2000
82 Correct 20 ms 24428 KB n=2000
83 Correct 20 ms 24556 KB n=2000
84 Correct 20 ms 24428 KB n=2000
85 Correct 20 ms 24428 KB n=2000
86 Correct 21 ms 24428 KB n=2000
87 Correct 20 ms 24428 KB n=2000
88 Correct 20 ms 24556 KB n=2000
89 Correct 23 ms 24556 KB n=2000
90 Correct 20 ms 24556 KB n=2000
91 Correct 20 ms 24428 KB n=2000
92 Correct 1082 ms 73924 KB n=200000
93 Correct 1219 ms 82156 KB n=200000
94 Correct 1145 ms 87532 KB n=200000
95 Correct 1040 ms 73728 KB n=200000
96 Correct 1035 ms 73696 KB n=200000
97 Correct 1229 ms 80364 KB n=200000
98 Correct 1036 ms 73784 KB n=200000
99 Correct 1277 ms 74024 KB n=200000
100 Correct 1061 ms 73956 KB n=200000
101 Correct 1137 ms 89708 KB n=200000
102 Correct 605 ms 75072 KB n=200000
103 Correct 593 ms 74860 KB n=200000
104 Correct 596 ms 74860 KB n=200000
105 Correct 596 ms 75500 KB n=200000
106 Correct 601 ms 75244 KB n=200000
107 Correct 597 ms 75244 KB n=200000
108 Correct 1143 ms 73756 KB n=200000
109 Correct 1126 ms 73744 KB n=200000
110 Correct 1120 ms 74060 KB n=200000
111 Correct 1026 ms 73264 KB n=200000
112 Correct 1114 ms 87788 KB n=200000
113 Correct 1241 ms 80340 KB n=200000
114 Correct 1043 ms 73312 KB n=200000
115 Correct 1369 ms 76908 KB n=200000
116 Correct 961 ms 74080 KB n=200000
117 Correct 1110 ms 88596 KB n=200000
118 Correct 1244 ms 78700 KB n=200000
119 Correct 960 ms 73824 KB n=200000
120 Correct 1079 ms 88812 KB n=200000
121 Correct 1049 ms 88556 KB n=200000
122 Correct 1051 ms 89068 KB n=200000
123 Correct 613 ms 74988 KB n=200000
124 Correct 278 ms 39404 KB n=25264