Submission #346906

# Submission time Handle Problem Language Result Execution time Memory
346906 2021-01-11T10:14:48 Z knightron0 Birthday gift (IZhO18_treearray) C++14
100 / 100
1344 ms 228204 KB
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define clr(a) memset(a, 0, sizeof(a))
#define sz(x) x.size()
#define printvector(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout<<*it<<" "; cout<<endl;
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b)/gcd(a, b)
#define int long long int
#define ull unsigned long long
#define printarray(arr, n) for(int i= 0;i<n;i++) cout<<arr[i]<<' '; cout<<endl;
#define printvecpairs(vec) for(auto it: vec) cout<<it.fr<<' '<<it.sc<<endl;
#define initdp(a) memset(a, -1, sizeof(a));
#define endl '\n'
#define float long double

using namespace std;
const int MOD = 1e9 + 7;
const int INF = 2e15;
const int MAXN = 2e5 + 5;

int fastexpo(int b, int exp){
    if(exp == 0) return 1;
    if(exp == 1) return b;
    int ans = (fastexpo(b,exp/2) % MOD);
    ans *= ans;
    ans %= MOD;
    if(exp % 2 == 1){
        ans *= b;
    }
    ans %= MOD;
    return ans;
}

set<int> prlca[MAXN];
set<int> sngle[MAXN];
vector<int> adj[MAXN];
int up[MAXN][100];
int timer = 0;
int tout[MAXN], tin[MAXN];
int n;
int l;
int a[MAXN];

void dfs(int v, int p){
	timer++;
	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);
		}
	}
	timer++;
	tout[v] = timer;
}

bool isanc(int a, int b){
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void preprocess(){
	timer = 0;
	clr(tout);
	clr(tin);
	l = ceil(log2(n));	
	clr(up);
}

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

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   	int m, q;
   	cin>>n>>m>>q; 
   	for(int i= 0;i<n-1;i++){
   		int t1, t2;
   		cin>>t1>>t2;
   		adj[t1].pb(t2);
   		adj[t2].pb(t1);
   	}
   	for(int i= 1;i<=m;i++){
   		cin>>a[i];
   	}
   	preprocess();
   	dfs(1, 1);
   	int mem[m+1];
   	for(int i=1;i<m;i++){
   		int lc = lca(a[i],a[i+1]);
   		mem[i] = lc;
   		prlca[lc].insert(i);
   		sngle[a[i]].insert(i);
   	}
   	sngle[a[m]].insert(m);
   	while(q--){
   		int t1;
   		cin>>t1;
   		if(t1==2){
   			int x, y, req;
   			cin>>x>>y>>req;
   			auto it = prlca[req].lower_bound(x);
   			int mn = -1;
   			if(it != prlca[req].end() && !prlca[req].empty()){
   				mn = *it;
   			}
   			auto it2 = sngle[req].lower_bound(x);
   			int mn2 = -1;
   			if(it2 != sngle[req].end() && !sngle[req].empty()){
   				mn2 = *it2;
   			}
   			if((mn >= x && mn <= y && mn+1 <= m && mn >= 1) || (mn2 >= x && mn2 <= y && mn2 <= m && mn2 >= 1)){
   				if(mn2 >= x && mn2 <= y && mn2 <= m && mn2 >= 1){
   					cout<<mn2<<' '<<mn2<<endl;
   					continue;
   				} else {
   					if(mn >= x && mn+1 <= y && mn+1 <= m && mn >= 1){
   						cout<<mn<<' '<<mn+1<<endl;
   						continue;
   					}
   				}
   			}
			cout<<"-1"<<' '<<"-1"<<endl;
   		}else {
   			int x, y;
   			cin>>x>>y;
   			sngle[a[x]].erase(sngle[a[x]].find(x));
   			sngle[y].insert(x);
   			// x-1, x
   			if(x > 1) {
   				int prev = mem[x-1];
   				int newlca = lca(a[x-1], y);
   				if(prev != newlca){
   					prlca[prev].erase(prlca[prev].erase(x-1));
   					prlca[newlca].insert(x-1);
   				}
   				mem[x-1] = newlca;

   			}
   			// x, x+1
   			if(x < m){
				int prev = mem[x];
   				int newlca = lca(a[x+1], y);
   				if(prev != newlca){
   					prlca[prev].erase(prlca[prev].erase(x));
   					prlca[newlca].insert(x);
   				}
   				mem[x] = newlca;
   			}
   			a[x] = y;
   		}
   	}
   	return 0;
}



# Verdict Execution time Memory Grader output
1 Correct 94 ms 183532 KB n=5
2 Correct 94 ms 183532 KB n=100
3 Correct 94 ms 183552 KB n=100
4 Correct 107 ms 183508 KB n=100
5 Correct 95 ms 183532 KB n=100
6 Correct 98 ms 183532 KB n=100
7 Correct 95 ms 183532 KB n=100
8 Correct 96 ms 183532 KB n=100
9 Correct 94 ms 183532 KB n=100
10 Correct 95 ms 183532 KB n=100
11 Correct 96 ms 183532 KB n=100
12 Correct 94 ms 183580 KB n=100
13 Correct 93 ms 183532 KB n=100
14 Correct 94 ms 183532 KB n=100
15 Correct 96 ms 183532 KB n=100
16 Correct 94 ms 183532 KB n=100
17 Correct 94 ms 183532 KB n=100
18 Correct 94 ms 183532 KB n=100
19 Correct 94 ms 183532 KB n=100
20 Correct 95 ms 183532 KB n=100
21 Correct 95 ms 183660 KB n=100
22 Correct 99 ms 183572 KB n=100
23 Correct 94 ms 183532 KB n=100
24 Correct 95 ms 183532 KB n=100
25 Correct 94 ms 183660 KB n=100
26 Correct 99 ms 183532 KB n=12
27 Correct 94 ms 183532 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 94 ms 183532 KB n=5
2 Correct 94 ms 183532 KB n=100
3 Correct 94 ms 183552 KB n=100
4 Correct 107 ms 183508 KB n=100
5 Correct 95 ms 183532 KB n=100
6 Correct 98 ms 183532 KB n=100
7 Correct 95 ms 183532 KB n=100
8 Correct 96 ms 183532 KB n=100
9 Correct 94 ms 183532 KB n=100
10 Correct 95 ms 183532 KB n=100
11 Correct 96 ms 183532 KB n=100
12 Correct 94 ms 183580 KB n=100
13 Correct 93 ms 183532 KB n=100
14 Correct 94 ms 183532 KB n=100
15 Correct 96 ms 183532 KB n=100
16 Correct 94 ms 183532 KB n=100
17 Correct 94 ms 183532 KB n=100
18 Correct 94 ms 183532 KB n=100
19 Correct 94 ms 183532 KB n=100
20 Correct 95 ms 183532 KB n=100
21 Correct 95 ms 183660 KB n=100
22 Correct 99 ms 183572 KB n=100
23 Correct 94 ms 183532 KB n=100
24 Correct 95 ms 183532 KB n=100
25 Correct 94 ms 183660 KB n=100
26 Correct 99 ms 183532 KB n=12
27 Correct 94 ms 183532 KB n=100
28 Correct 95 ms 183568 KB n=500
29 Correct 93 ms 183660 KB n=500
30 Correct 95 ms 183532 KB n=500
31 Correct 94 ms 183532 KB n=500
32 Correct 93 ms 183532 KB n=500
33 Correct 97 ms 183532 KB n=500
34 Correct 101 ms 183532 KB n=500
35 Correct 91 ms 183660 KB n=500
36 Correct 96 ms 183660 KB n=500
37 Correct 98 ms 183532 KB n=500
38 Correct 93 ms 183660 KB n=500
39 Correct 94 ms 183532 KB n=500
40 Correct 92 ms 183532 KB n=500
41 Correct 99 ms 183584 KB n=500
42 Correct 95 ms 183732 KB n=500
43 Correct 93 ms 183532 KB n=500
44 Correct 91 ms 183532 KB n=500
45 Correct 97 ms 183532 KB n=500
46 Correct 96 ms 183660 KB n=500
47 Correct 95 ms 183532 KB n=500
48 Correct 96 ms 183916 KB n=500
49 Correct 94 ms 183660 KB n=500
50 Correct 97 ms 183532 KB n=500
51 Correct 95 ms 183804 KB n=500
52 Correct 96 ms 183660 KB n=500
53 Correct 96 ms 183616 KB n=500
54 Correct 96 ms 183660 KB n=500
55 Correct 95 ms 183532 KB n=278
56 Correct 94 ms 183660 KB n=500
57 Correct 96 ms 183660 KB n=500
58 Correct 96 ms 183684 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 94 ms 183532 KB n=5
2 Correct 94 ms 183532 KB n=100
3 Correct 94 ms 183552 KB n=100
4 Correct 107 ms 183508 KB n=100
5 Correct 95 ms 183532 KB n=100
6 Correct 98 ms 183532 KB n=100
7 Correct 95 ms 183532 KB n=100
8 Correct 96 ms 183532 KB n=100
9 Correct 94 ms 183532 KB n=100
10 Correct 95 ms 183532 KB n=100
11 Correct 96 ms 183532 KB n=100
12 Correct 94 ms 183580 KB n=100
13 Correct 93 ms 183532 KB n=100
14 Correct 94 ms 183532 KB n=100
15 Correct 96 ms 183532 KB n=100
16 Correct 94 ms 183532 KB n=100
17 Correct 94 ms 183532 KB n=100
18 Correct 94 ms 183532 KB n=100
19 Correct 94 ms 183532 KB n=100
20 Correct 95 ms 183532 KB n=100
21 Correct 95 ms 183660 KB n=100
22 Correct 99 ms 183572 KB n=100
23 Correct 94 ms 183532 KB n=100
24 Correct 95 ms 183532 KB n=100
25 Correct 94 ms 183660 KB n=100
26 Correct 99 ms 183532 KB n=12
27 Correct 94 ms 183532 KB n=100
28 Correct 95 ms 183568 KB n=500
29 Correct 93 ms 183660 KB n=500
30 Correct 95 ms 183532 KB n=500
31 Correct 94 ms 183532 KB n=500
32 Correct 93 ms 183532 KB n=500
33 Correct 97 ms 183532 KB n=500
34 Correct 101 ms 183532 KB n=500
35 Correct 91 ms 183660 KB n=500
36 Correct 96 ms 183660 KB n=500
37 Correct 98 ms 183532 KB n=500
38 Correct 93 ms 183660 KB n=500
39 Correct 94 ms 183532 KB n=500
40 Correct 92 ms 183532 KB n=500
41 Correct 99 ms 183584 KB n=500
42 Correct 95 ms 183732 KB n=500
43 Correct 93 ms 183532 KB n=500
44 Correct 91 ms 183532 KB n=500
45 Correct 97 ms 183532 KB n=500
46 Correct 96 ms 183660 KB n=500
47 Correct 95 ms 183532 KB n=500
48 Correct 96 ms 183916 KB n=500
49 Correct 94 ms 183660 KB n=500
50 Correct 97 ms 183532 KB n=500
51 Correct 95 ms 183804 KB n=500
52 Correct 96 ms 183660 KB n=500
53 Correct 96 ms 183616 KB n=500
54 Correct 96 ms 183660 KB n=500
55 Correct 95 ms 183532 KB n=278
56 Correct 94 ms 183660 KB n=500
57 Correct 96 ms 183660 KB n=500
58 Correct 96 ms 183684 KB n=500
59 Correct 93 ms 183788 KB n=2000
60 Correct 97 ms 183916 KB n=2000
61 Correct 99 ms 183916 KB n=2000
62 Correct 98 ms 183788 KB n=2000
63 Correct 107 ms 184044 KB n=2000
64 Correct 98 ms 183788 KB n=2000
65 Correct 99 ms 183788 KB n=2000
66 Correct 99 ms 183916 KB n=2000
67 Correct 100 ms 183788 KB n=2000
68 Correct 97 ms 183788 KB n=2000
69 Correct 98 ms 184044 KB n=2000
70 Correct 100 ms 183788 KB n=2000
71 Correct 99 ms 183788 KB n=2000
72 Correct 98 ms 183788 KB n=2000
73 Correct 97 ms 183916 KB n=2000
74 Correct 96 ms 183788 KB n=1844
75 Correct 108 ms 183916 KB n=2000
76 Correct 99 ms 183788 KB n=2000
77 Correct 97 ms 183788 KB n=2000
78 Correct 99 ms 183788 KB n=2000
79 Correct 97 ms 183788 KB n=2000
80 Correct 98 ms 183912 KB n=2000
81 Correct 98 ms 183892 KB n=2000
82 Correct 97 ms 183788 KB n=2000
83 Correct 97 ms 183916 KB n=2000
84 Correct 98 ms 184048 KB n=2000
85 Correct 97 ms 183916 KB n=2000
86 Correct 99 ms 183788 KB n=2000
87 Correct 98 ms 183788 KB n=2000
88 Correct 97 ms 184044 KB n=2000
89 Correct 97 ms 183916 KB n=2000
90 Correct 98 ms 184040 KB n=2000
91 Correct 97 ms 183788 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 94 ms 183532 KB n=5
2 Correct 94 ms 183532 KB n=100
3 Correct 94 ms 183552 KB n=100
4 Correct 107 ms 183508 KB n=100
5 Correct 95 ms 183532 KB n=100
6 Correct 98 ms 183532 KB n=100
7 Correct 95 ms 183532 KB n=100
8 Correct 96 ms 183532 KB n=100
9 Correct 94 ms 183532 KB n=100
10 Correct 95 ms 183532 KB n=100
11 Correct 96 ms 183532 KB n=100
12 Correct 94 ms 183580 KB n=100
13 Correct 93 ms 183532 KB n=100
14 Correct 94 ms 183532 KB n=100
15 Correct 96 ms 183532 KB n=100
16 Correct 94 ms 183532 KB n=100
17 Correct 94 ms 183532 KB n=100
18 Correct 94 ms 183532 KB n=100
19 Correct 94 ms 183532 KB n=100
20 Correct 95 ms 183532 KB n=100
21 Correct 95 ms 183660 KB n=100
22 Correct 99 ms 183572 KB n=100
23 Correct 94 ms 183532 KB n=100
24 Correct 95 ms 183532 KB n=100
25 Correct 94 ms 183660 KB n=100
26 Correct 99 ms 183532 KB n=12
27 Correct 94 ms 183532 KB n=100
28 Correct 95 ms 183568 KB n=500
29 Correct 93 ms 183660 KB n=500
30 Correct 95 ms 183532 KB n=500
31 Correct 94 ms 183532 KB n=500
32 Correct 93 ms 183532 KB n=500
33 Correct 97 ms 183532 KB n=500
34 Correct 101 ms 183532 KB n=500
35 Correct 91 ms 183660 KB n=500
36 Correct 96 ms 183660 KB n=500
37 Correct 98 ms 183532 KB n=500
38 Correct 93 ms 183660 KB n=500
39 Correct 94 ms 183532 KB n=500
40 Correct 92 ms 183532 KB n=500
41 Correct 99 ms 183584 KB n=500
42 Correct 95 ms 183732 KB n=500
43 Correct 93 ms 183532 KB n=500
44 Correct 91 ms 183532 KB n=500
45 Correct 97 ms 183532 KB n=500
46 Correct 96 ms 183660 KB n=500
47 Correct 95 ms 183532 KB n=500
48 Correct 96 ms 183916 KB n=500
49 Correct 94 ms 183660 KB n=500
50 Correct 97 ms 183532 KB n=500
51 Correct 95 ms 183804 KB n=500
52 Correct 96 ms 183660 KB n=500
53 Correct 96 ms 183616 KB n=500
54 Correct 96 ms 183660 KB n=500
55 Correct 95 ms 183532 KB n=278
56 Correct 94 ms 183660 KB n=500
57 Correct 96 ms 183660 KB n=500
58 Correct 96 ms 183684 KB n=500
59 Correct 93 ms 183788 KB n=2000
60 Correct 97 ms 183916 KB n=2000
61 Correct 99 ms 183916 KB n=2000
62 Correct 98 ms 183788 KB n=2000
63 Correct 107 ms 184044 KB n=2000
64 Correct 98 ms 183788 KB n=2000
65 Correct 99 ms 183788 KB n=2000
66 Correct 99 ms 183916 KB n=2000
67 Correct 100 ms 183788 KB n=2000
68 Correct 97 ms 183788 KB n=2000
69 Correct 98 ms 184044 KB n=2000
70 Correct 100 ms 183788 KB n=2000
71 Correct 99 ms 183788 KB n=2000
72 Correct 98 ms 183788 KB n=2000
73 Correct 97 ms 183916 KB n=2000
74 Correct 96 ms 183788 KB n=1844
75 Correct 108 ms 183916 KB n=2000
76 Correct 99 ms 183788 KB n=2000
77 Correct 97 ms 183788 KB n=2000
78 Correct 99 ms 183788 KB n=2000
79 Correct 97 ms 183788 KB n=2000
80 Correct 98 ms 183912 KB n=2000
81 Correct 98 ms 183892 KB n=2000
82 Correct 97 ms 183788 KB n=2000
83 Correct 97 ms 183916 KB n=2000
84 Correct 98 ms 184048 KB n=2000
85 Correct 97 ms 183916 KB n=2000
86 Correct 99 ms 183788 KB n=2000
87 Correct 98 ms 183788 KB n=2000
88 Correct 97 ms 184044 KB n=2000
89 Correct 97 ms 183916 KB n=2000
90 Correct 98 ms 184040 KB n=2000
91 Correct 97 ms 183788 KB n=2000
92 Correct 803 ms 221032 KB n=200000
93 Correct 1042 ms 224620 KB n=200000
94 Correct 832 ms 227260 KB n=200000
95 Correct 756 ms 220892 KB n=200000
96 Correct 756 ms 221020 KB n=200000
97 Correct 1150 ms 223720 KB n=200000
98 Correct 769 ms 220768 KB n=200000
99 Correct 1031 ms 221420 KB n=200000
100 Correct 793 ms 221024 KB n=200000
101 Correct 743 ms 228204 KB n=200000
102 Correct 603 ms 222376 KB n=200000
103 Correct 628 ms 222188 KB n=200000
104 Correct 617 ms 222316 KB n=200000
105 Correct 620 ms 222828 KB n=200000
106 Correct 624 ms 222700 KB n=200000
107 Correct 619 ms 222700 KB n=200000
108 Correct 923 ms 221164 KB n=200000
109 Correct 947 ms 221164 KB n=200000
110 Correct 939 ms 221236 KB n=200000
111 Correct 804 ms 220380 KB n=200000
112 Correct 800 ms 227724 KB n=200000
113 Correct 1084 ms 223772 KB n=200000
114 Correct 799 ms 220508 KB n=200000
115 Correct 1344 ms 221804 KB n=200000
116 Correct 726 ms 220380 KB n=200000
117 Correct 750 ms 227692 KB n=200000
118 Correct 1233 ms 222348 KB n=200000
119 Correct 701 ms 221180 KB n=200000
120 Correct 703 ms 227068 KB n=200000
121 Correct 706 ms 227052 KB n=200000
122 Correct 703 ms 227436 KB n=200000
123 Correct 643 ms 222572 KB n=200000
124 Correct 287 ms 196844 KB n=25264