Submission #245659

# Submission time Handle Problem Language Result Execution time Memory
245659 2020-07-07T05:16:33 Z Yera Birthday gift (IZhO18_treearray) C++17
56 / 100
223 ms 216836 KB
// In The Name Of God
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <bitset>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
 
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sagyndym_seni ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
 
const int N = 2e5+5, p1 = 911382323, p2 = 972663749, INF = 1e9+123;
 
inline int read(){
	char c = getchar_unlocked();
	bool minus = 0;
	while (c < '0' || '9' < c){
		if(c == '-'){ minus = 1;}
		c = getchar_unlocked();
		if(c == '-'){ minus = 1;}
	}
	int res = 0;
	while ('0' <= c && c <= '9') {
		res = (res << 3) + (res << 1) + c - '0';
		c = getchar_unlocked();
	}
	if(minus){ res = (~res) + 1;}
	return res;
}
 
int n, m, q;
int pos[N], LOG[N], vec[N];
pair<int, int> st[N][20];
vector<int> g[N];
vector<pair<int, int>> order;
 
void dfs(int v, int p, int depth){
	order.pb({depth, v});
	pos[v] = sz(order)-1;
	for(auto to : g[v]){
		if(to == p){ continue;}
		dfs(to, v, depth + 1);
		order.pb({depth, v});
	}
}
 
inline void precalc(){
	LOG[1] = 0;
	for(int i = 2; i < N; i++){
		LOG[i] = LOG[i >> 1] + 1;
	}
	for(int i = 0; i < sz(order); i++){
		st[i][0] = order[i];
	}
	for(int j = 1; j < 20; j++){
		for(int i = 0; i + (1 << j) < sz(order); i++){
			st[i][j] = min(st[i][j-1], st[i + (1 << (j-1))][j-1]);
		}
	}
}
 
inline int mn(int l, int r){
	if(r < l){ swap(l, r);}
	int j = LOG[r - l + 1];
	pair<int, int> res = min(st[l][j], st[r - (1 << j) + 1][j]);
	return res.s;
}

multiset<pair<int, int>> tUno[N << 2], tDos[N << 1];

void build_uno(int v, int vl, int vr){
	if(vl == vr){
		int val = vl == m - 1 ? -1 : mn(pos[vec[vl]], pos[vec[vl + 1]]);
		tUno[v].insert({val, vl});
		return;
	}
	int vm = (vl + vr) >> 1;
	build_uno(v*2+1, vl, vm);
	build_uno(v*2+2, vm+1, vr);
	merge(all(tUno[v*2+1]), all(tUno[v*2+2]), inserter(tUno[v], tUno[v].begin()));
}

void build_dos(int v, int vl, int vr){
	if(vl == vr){
		tDos[v].insert({vec[vl], vl});
		return;
	}
	int vm = (vl + vr) >> 1;
	build_dos(v*2+1, vl, vm);
	build_dos(v*2+2, vm+1, vr);
	merge(all(tDos[v*2+1]), all(tDos[v*2+2]), inserter(tDos[v], tDos[v].begin()));
}

void modify_dos(int v, int vl, int vr, int idx, int old_val, int new_val){
	tDos[v].erase(tDos[v].find({old_val, idx}));
	tDos[v].insert({new_val, idx});
	if(vl != vr){
		int vm = (vl + vr) >> 1;
		if(idx <= vm){
			modify_dos(v*2+1, vl, vm, idx, old_val, new_val);
		}else{
			modify_dos(v*2+2, vm+1, vr, idx, old_val, new_val);
		}
	}
}

void modify_uno(int v, int vl, int vr, int idx, int old_val, int new_val){
	tUno[v].erase(tUno[v].find({old_val, idx}));
	tUno[v].insert({new_val, idx});
	if(vl != vr){
		int vm = (vl + vr) >> 1;
		if(idx <= vm){
			modify_uno(v*2+1, vl, vm, idx, old_val, new_val);
		}else{
			modify_uno(v*2+2, vm+1, vr, idx, old_val, new_val);
		}
	}
}

int query_uno(int v, int vl, int vr, int l, int r, int val){
	if(l > vr || r < vl || r < l){ return -1;}
	if(l <= vl && vr <= r){
		auto it = tUno[v].lower_bound({val, -1});
		if(it == tUno[v].end()){ return -1;}
		pair<int, int> temp = *it;
		return temp.f == val ? temp.s : -1;
	}
	int vm = (vl + vr) >> 1;
	int q = query_uno(v*2+1, vl, vm, l, r, val);
	if(q != -1){ return q;}
	return query_uno(v*2+2, vm+1, vr, l, r, val);
}

int query_dos(int v, int vl, int vr, int l, int r, int val){
	if(l > vr || r < vl || r < l){ return -1;}
	if(l <= vl && vr <= r){
		auto it = tDos[v].lower_bound({val, -1});
		if(it == tDos[v].end()){ return -1;}
		pair<int, int> temp = *it;
		return temp.f == val ? temp.s : -1;
	}
	int vm = (vl + vr) >> 1;
	int q = query_dos(v*2+1, vl, vm, l, r, val);
	if(q != -1){ return q;}
	return query_dos(v*2+2, vm+1, vr, l, r, val);
}

int main(){
	sagyndym_seni;
	n = read(); m = read(); q = read();
	for(int i = 1; i < n; i++){
		int v = read()-1, u = read()-1;
		g[v].pb(u);
		g[u].pb(v);
	}
	for(int i = 0; i < m; i++){
		vec[i] = read()-1;
	}
	dfs(0, 0, 0);
	precalc();
	build_uno(0, 0, m - 1);
	build_dos(0, 0, m - 1);
	while(q--){
		int tp = read()-1;
		if(!tp){
			int p = read()-1, v = read()-1;
			modify_dos(0, 0, m - 1, p, vec[p], v);
			if(p != m - 1){
				modify_uno(0, 0, m - 1, p, mn(pos[vec[p]], pos[vec[p + 1]]), mn(pos[v], pos[vec[p + 1]]));
			}
			if(p != 0){
				modify_uno(0, 0, m - 1, p - 1, mn(pos[vec[p-1]], pos[vec[p]]), mn(pos[v], pos[vec[p-1]]));
			}
			vec[p] = v;
		}else{
			int l = read()-1, r = read()-1, p = read()-1;
			int res1 = query_dos(0, 0, m - 1, l, r, p);
			if(res1 != -1){ cout<<'\n'<<res1 + 1<<' '<<res1 + 1; continue;}
			int res2 = query_uno(0, 0, m - 1, l, r - 1, p);
			res2 == -1 ? cout<<"\n-1 -1" : cout<<'\n'<<res2 + 1<<' '<<res2 + 2;
		}
	}
	return 0;
}
/* TIMUS: 292220YC*/
# Verdict Execution time Memory Grader output
1 Correct 40 ms 62208 KB n=5
2 Correct 40 ms 62336 KB n=100
3 Correct 41 ms 62336 KB n=100
4 Correct 47 ms 62336 KB n=100
5 Correct 49 ms 62328 KB n=100
6 Correct 50 ms 62328 KB n=100
7 Correct 40 ms 62336 KB n=100
8 Correct 48 ms 62332 KB n=100
9 Correct 41 ms 62328 KB n=100
10 Correct 41 ms 62336 KB n=100
11 Correct 42 ms 62336 KB n=100
12 Correct 48 ms 62328 KB n=100
13 Correct 50 ms 62328 KB n=100
14 Correct 41 ms 62328 KB n=100
15 Correct 41 ms 62336 KB n=100
16 Correct 45 ms 62328 KB n=100
17 Correct 41 ms 62336 KB n=100
18 Correct 43 ms 62328 KB n=100
19 Correct 42 ms 62328 KB n=100
20 Correct 43 ms 62328 KB n=100
21 Correct 40 ms 62336 KB n=100
22 Correct 40 ms 62336 KB n=100
23 Correct 41 ms 62336 KB n=100
24 Correct 49 ms 62328 KB n=100
25 Correct 41 ms 62328 KB n=100
26 Correct 43 ms 62208 KB n=12
27 Correct 52 ms 62336 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 40 ms 62208 KB n=5
2 Correct 40 ms 62336 KB n=100
3 Correct 41 ms 62336 KB n=100
4 Correct 47 ms 62336 KB n=100
5 Correct 49 ms 62328 KB n=100
6 Correct 50 ms 62328 KB n=100
7 Correct 40 ms 62336 KB n=100
8 Correct 48 ms 62332 KB n=100
9 Correct 41 ms 62328 KB n=100
10 Correct 41 ms 62336 KB n=100
11 Correct 42 ms 62336 KB n=100
12 Correct 48 ms 62328 KB n=100
13 Correct 50 ms 62328 KB n=100
14 Correct 41 ms 62328 KB n=100
15 Correct 41 ms 62336 KB n=100
16 Correct 45 ms 62328 KB n=100
17 Correct 41 ms 62336 KB n=100
18 Correct 43 ms 62328 KB n=100
19 Correct 42 ms 62328 KB n=100
20 Correct 43 ms 62328 KB n=100
21 Correct 40 ms 62336 KB n=100
22 Correct 40 ms 62336 KB n=100
23 Correct 41 ms 62336 KB n=100
24 Correct 49 ms 62328 KB n=100
25 Correct 41 ms 62328 KB n=100
26 Correct 43 ms 62208 KB n=12
27 Correct 52 ms 62336 KB n=100
28 Correct 58 ms 62840 KB n=500
29 Correct 55 ms 62872 KB n=500
30 Correct 51 ms 62840 KB n=500
31 Correct 53 ms 62840 KB n=500
32 Correct 52 ms 62840 KB n=500
33 Correct 50 ms 62840 KB n=500
34 Correct 43 ms 62840 KB n=500
35 Correct 44 ms 62840 KB n=500
36 Correct 42 ms 62840 KB n=500
37 Correct 43 ms 62848 KB n=500
38 Correct 42 ms 62848 KB n=500
39 Correct 43 ms 62848 KB n=500
40 Correct 42 ms 62848 KB n=500
41 Correct 43 ms 62848 KB n=500
42 Correct 44 ms 62848 KB n=500
43 Correct 50 ms 62840 KB n=500
44 Correct 43 ms 62848 KB n=500
45 Correct 49 ms 62840 KB n=500
46 Correct 43 ms 62840 KB n=500
47 Correct 44 ms 62840 KB n=500
48 Correct 49 ms 62840 KB n=500
49 Correct 47 ms 62848 KB n=500
50 Correct 50 ms 62848 KB n=500
51 Correct 53 ms 62840 KB n=500
52 Correct 50 ms 62840 KB n=500
53 Correct 43 ms 62908 KB n=500
54 Correct 51 ms 62840 KB n=500
55 Correct 48 ms 62720 KB n=278
56 Correct 43 ms 62840 KB n=500
57 Correct 44 ms 62848 KB n=500
58 Correct 47 ms 62844 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 40 ms 62208 KB n=5
2 Correct 40 ms 62336 KB n=100
3 Correct 41 ms 62336 KB n=100
4 Correct 47 ms 62336 KB n=100
5 Correct 49 ms 62328 KB n=100
6 Correct 50 ms 62328 KB n=100
7 Correct 40 ms 62336 KB n=100
8 Correct 48 ms 62332 KB n=100
9 Correct 41 ms 62328 KB n=100
10 Correct 41 ms 62336 KB n=100
11 Correct 42 ms 62336 KB n=100
12 Correct 48 ms 62328 KB n=100
13 Correct 50 ms 62328 KB n=100
14 Correct 41 ms 62328 KB n=100
15 Correct 41 ms 62336 KB n=100
16 Correct 45 ms 62328 KB n=100
17 Correct 41 ms 62336 KB n=100
18 Correct 43 ms 62328 KB n=100
19 Correct 42 ms 62328 KB n=100
20 Correct 43 ms 62328 KB n=100
21 Correct 40 ms 62336 KB n=100
22 Correct 40 ms 62336 KB n=100
23 Correct 41 ms 62336 KB n=100
24 Correct 49 ms 62328 KB n=100
25 Correct 41 ms 62328 KB n=100
26 Correct 43 ms 62208 KB n=12
27 Correct 52 ms 62336 KB n=100
28 Correct 58 ms 62840 KB n=500
29 Correct 55 ms 62872 KB n=500
30 Correct 51 ms 62840 KB n=500
31 Correct 53 ms 62840 KB n=500
32 Correct 52 ms 62840 KB n=500
33 Correct 50 ms 62840 KB n=500
34 Correct 43 ms 62840 KB n=500
35 Correct 44 ms 62840 KB n=500
36 Correct 42 ms 62840 KB n=500
37 Correct 43 ms 62848 KB n=500
38 Correct 42 ms 62848 KB n=500
39 Correct 43 ms 62848 KB n=500
40 Correct 42 ms 62848 KB n=500
41 Correct 43 ms 62848 KB n=500
42 Correct 44 ms 62848 KB n=500
43 Correct 50 ms 62840 KB n=500
44 Correct 43 ms 62848 KB n=500
45 Correct 49 ms 62840 KB n=500
46 Correct 43 ms 62840 KB n=500
47 Correct 44 ms 62840 KB n=500
48 Correct 49 ms 62840 KB n=500
49 Correct 47 ms 62848 KB n=500
50 Correct 50 ms 62848 KB n=500
51 Correct 53 ms 62840 KB n=500
52 Correct 50 ms 62840 KB n=500
53 Correct 43 ms 62908 KB n=500
54 Correct 51 ms 62840 KB n=500
55 Correct 48 ms 62720 KB n=278
56 Correct 43 ms 62840 KB n=500
57 Correct 44 ms 62848 KB n=500
58 Correct 47 ms 62844 KB n=500
59 Correct 70 ms 65272 KB n=2000
60 Correct 60 ms 65272 KB n=2000
61 Correct 60 ms 65272 KB n=2000
62 Correct 67 ms 65272 KB n=2000
63 Correct 57 ms 65144 KB n=2000
64 Correct 60 ms 65272 KB n=2000
65 Correct 58 ms 65144 KB n=2000
66 Correct 59 ms 65340 KB n=2000
67 Correct 58 ms 65144 KB n=2000
68 Correct 62 ms 65272 KB n=2000
69 Correct 49 ms 65280 KB n=2000
70 Correct 56 ms 65272 KB n=2000
71 Correct 48 ms 65272 KB n=2000
72 Correct 51 ms 65144 KB n=2000
73 Correct 50 ms 65144 KB n=2000
74 Correct 57 ms 65152 KB n=1844
75 Correct 50 ms 65280 KB n=2000
76 Correct 67 ms 65144 KB n=2000
77 Correct 57 ms 65144 KB n=2000
78 Correct 71 ms 65280 KB n=2000
79 Correct 56 ms 65152 KB n=2000
80 Correct 66 ms 65272 KB n=2000
81 Correct 70 ms 65272 KB n=2000
82 Correct 66 ms 65144 KB n=2000
83 Correct 61 ms 65272 KB n=2000
84 Correct 65 ms 65152 KB n=2000
85 Correct 59 ms 65288 KB n=2000
86 Correct 73 ms 65272 KB n=2000
87 Correct 58 ms 65272 KB n=2000
88 Correct 63 ms 65400 KB n=2000
89 Correct 60 ms 65388 KB n=2000
90 Correct 86 ms 65316 KB n=2000
91 Correct 50 ms 65144 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 40 ms 62208 KB n=5
2 Correct 40 ms 62336 KB n=100
3 Correct 41 ms 62336 KB n=100
4 Correct 47 ms 62336 KB n=100
5 Correct 49 ms 62328 KB n=100
6 Correct 50 ms 62328 KB n=100
7 Correct 40 ms 62336 KB n=100
8 Correct 48 ms 62332 KB n=100
9 Correct 41 ms 62328 KB n=100
10 Correct 41 ms 62336 KB n=100
11 Correct 42 ms 62336 KB n=100
12 Correct 48 ms 62328 KB n=100
13 Correct 50 ms 62328 KB n=100
14 Correct 41 ms 62328 KB n=100
15 Correct 41 ms 62336 KB n=100
16 Correct 45 ms 62328 KB n=100
17 Correct 41 ms 62336 KB n=100
18 Correct 43 ms 62328 KB n=100
19 Correct 42 ms 62328 KB n=100
20 Correct 43 ms 62328 KB n=100
21 Correct 40 ms 62336 KB n=100
22 Correct 40 ms 62336 KB n=100
23 Correct 41 ms 62336 KB n=100
24 Correct 49 ms 62328 KB n=100
25 Correct 41 ms 62328 KB n=100
26 Correct 43 ms 62208 KB n=12
27 Correct 52 ms 62336 KB n=100
28 Correct 58 ms 62840 KB n=500
29 Correct 55 ms 62872 KB n=500
30 Correct 51 ms 62840 KB n=500
31 Correct 53 ms 62840 KB n=500
32 Correct 52 ms 62840 KB n=500
33 Correct 50 ms 62840 KB n=500
34 Correct 43 ms 62840 KB n=500
35 Correct 44 ms 62840 KB n=500
36 Correct 42 ms 62840 KB n=500
37 Correct 43 ms 62848 KB n=500
38 Correct 42 ms 62848 KB n=500
39 Correct 43 ms 62848 KB n=500
40 Correct 42 ms 62848 KB n=500
41 Correct 43 ms 62848 KB n=500
42 Correct 44 ms 62848 KB n=500
43 Correct 50 ms 62840 KB n=500
44 Correct 43 ms 62848 KB n=500
45 Correct 49 ms 62840 KB n=500
46 Correct 43 ms 62840 KB n=500
47 Correct 44 ms 62840 KB n=500
48 Correct 49 ms 62840 KB n=500
49 Correct 47 ms 62848 KB n=500
50 Correct 50 ms 62848 KB n=500
51 Correct 53 ms 62840 KB n=500
52 Correct 50 ms 62840 KB n=500
53 Correct 43 ms 62908 KB n=500
54 Correct 51 ms 62840 KB n=500
55 Correct 48 ms 62720 KB n=278
56 Correct 43 ms 62840 KB n=500
57 Correct 44 ms 62848 KB n=500
58 Correct 47 ms 62844 KB n=500
59 Correct 70 ms 65272 KB n=2000
60 Correct 60 ms 65272 KB n=2000
61 Correct 60 ms 65272 KB n=2000
62 Correct 67 ms 65272 KB n=2000
63 Correct 57 ms 65144 KB n=2000
64 Correct 60 ms 65272 KB n=2000
65 Correct 58 ms 65144 KB n=2000
66 Correct 59 ms 65340 KB n=2000
67 Correct 58 ms 65144 KB n=2000
68 Correct 62 ms 65272 KB n=2000
69 Correct 49 ms 65280 KB n=2000
70 Correct 56 ms 65272 KB n=2000
71 Correct 48 ms 65272 KB n=2000
72 Correct 51 ms 65144 KB n=2000
73 Correct 50 ms 65144 KB n=2000
74 Correct 57 ms 65152 KB n=1844
75 Correct 50 ms 65280 KB n=2000
76 Correct 67 ms 65144 KB n=2000
77 Correct 57 ms 65144 KB n=2000
78 Correct 71 ms 65280 KB n=2000
79 Correct 56 ms 65152 KB n=2000
80 Correct 66 ms 65272 KB n=2000
81 Correct 70 ms 65272 KB n=2000
82 Correct 66 ms 65144 KB n=2000
83 Correct 61 ms 65272 KB n=2000
84 Correct 65 ms 65152 KB n=2000
85 Correct 59 ms 65288 KB n=2000
86 Correct 73 ms 65272 KB n=2000
87 Correct 58 ms 65272 KB n=2000
88 Correct 63 ms 65400 KB n=2000
89 Correct 60 ms 65388 KB n=2000
90 Correct 86 ms 65316 KB n=2000
91 Correct 50 ms 65144 KB n=2000
92 Runtime error 223 ms 216836 KB Execution killed with signal 11 (could be triggered by violating memory limits)
93 Halted 0 ms 0 KB -