Submission #98020

# Submission time Handle Problem Language Result Execution time Memory
98020 2019-02-19T19:38:19 Z qkxwsm Birthday gift (IZhO18_treearray) C++14
100 / 100
2624 ms 84344 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

random_device(rd);
mt19937 rng(rd());
const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();

struct custom_hash
{
	template<class T>
	unsigned long long operator()(T v) const
	{
		unsigned long long x = v;
		x += FIXED_RANDOM;
		// x += 11400714819323198485ull;
		// x = (x ^ (x >> 30)) * 13787848793156543929ull;
		x = (x ^ (x >> 27)) * 10723151780598845931ull;
		return x ^ (x >> 31);
	}
};

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;

template<class T>
T randomize(T mod)
{
	return (uniform_int_distribution<T>(0, mod - 1))(rng);
}
template<class T>
void readi(T &x)
{
	x = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
	{
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		x = x * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		x = -x;
	}
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int buf[20], n = 0;
	while(output)
	{
		buf[n] = ((output % 10));
		output /= 10;
		n++;
	}
	for (n--; n >= 0; n--)
	{
		putchar(buf[n] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long expo(long long a, long long e, long long mod)
{
	return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod));
}
template<class T, class U>
void nmod(T &x, U mod)
{
	if (x >= mod) x -= mod;
}
template<class T>
T gcd(T a, T b)
{
	return (b ? gcd(b, a % b) : a);
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define DBG(x) cerr << #x << " = " << (x) << endl
#define SZ(x) ((int) ((x).size()))
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-9;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 200013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdd> vpd;

int N, M, Q, T;
vi edge[MAXN];
int arr[MAXN];
int parent[MAXN], st[MAXN], ft[MAXN];
set<int> pos[MAXN], pos1[MAXN];
int ancestor[23][MAXN];

void dfs(int u)
{
	st[u] = T;
	ft[u] = T;
	T++;
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		parent[v] = u;
		dfs(v);
		ft[u] = ft[v];
	}
}
bool insubt(int u, int v)
{
	//is v in subtree of u?
	if (u == N) return true;
	return (st[u] <= st[v] && st[v] <= ft[u]);
}
int lca(int u, int v)
{
	if (insubt(v, u))
	{
		return v;
	}
	FORD(i, 21, 0)
	{
		if (!insubt(ancestor[i][v], u))
		{
			v = ancestor[i][v];
		}
	}
	return parent[v];
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	// cout << fixed << setprecision(10);
	// cerr << fixed << setprecision(10);
	// if (fopen("file.in", "r"))
	// {
	// 	freopen ("file.in", "r", stdin);
	// 	freopen ("file.out", "w", stdout);
	// }
	cin >> N >> M >> Q;
	FOR(i, 0, N - 1)
	{
		int u, v;
		cin >> u >> v;
		u--; v--;
		edge[u].PB(v);
		edge[v].PB(u);
	}
	dfs(0);
	parent[0] = N;
	FOR(i, 0, 22)
	{
		ancestor[i][N] = N;
	}
	FOR(i, 0, N)
	{
		ancestor[0][i] = parent[i];
	}
	FOR(i, 1, 22)
	{
		FOR(j, 0, N)
		{
			ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j]];
		}
	}
	FOR(i, 0, M)
	{
		cin >> arr[i]; arr[i]--;
	}
	FOR(i, 0, M)
	{
		pos[arr[i]].insert(i);
		if (i)
		{
			pos1[lca(arr[i - 1], arr[i])].insert(i - 1);
		}
	}
	while(Q--)
	{
		int qid;
		cin >> qid;
		if (qid == 1)
		{
			int idx, v;
			cin >> idx >> v;
			idx--; v--;
			pos[arr[idx]].erase(idx);
			if (idx)
			{
				pos1[lca(arr[idx - 1], arr[idx])].erase(idx - 1);
			}
			if (idx != M - 1)
			{
				pos1[lca(arr[idx], arr[idx + 1])].erase(idx);
			}
			arr[idx] = v;
			pos[arr[idx]].insert(idx);
			if (idx)
			{
				pos1[lca(arr[idx - 1], arr[idx])].insert(idx - 1);
			}
			if (idx != M - 1)
			{
				pos1[lca(arr[idx], arr[idx + 1])].insert(idx);
			}
		}
		if (qid == 2)
		{
			int lt, rt, v;
			cin >> lt >> rt >> v;
			lt--; rt--; v--;
			auto one = pos[v].LB(lt), two = pos1[v].LB(lt);
			if (one != pos[v].end() && *one <= rt)
			{
				cout << *one + 1 << ' ' << *one + 1 << '\n';
			}
			else if (two != pos1[v].end() && *two < rt)
			{
				cout << *two + 1 << ' ' << *two + 2 << '\n';
			}
			else
			{
				cout << "-1 -1\n";
			}
		}
	}
	// cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
	return 0;
}
/* READ READ READ
* int overflow, maxn too small, special cases (n=1?, two distinct?), cin.tie() interactive
* reread the problem, try small cases
* note down possible sources of error as you go
* do smth instead of nothing
*/
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24064 KB n=5
2 Correct 26 ms 24064 KB n=100
3 Correct 25 ms 24064 KB n=100
4 Correct 26 ms 24064 KB n=100
5 Correct 23 ms 24064 KB n=100
6 Correct 24 ms 24064 KB n=100
7 Correct 25 ms 24056 KB n=100
8 Correct 25 ms 23980 KB n=100
9 Correct 25 ms 24056 KB n=100
10 Correct 24 ms 24036 KB n=100
11 Correct 24 ms 23936 KB n=100
12 Correct 27 ms 24020 KB n=100
13 Correct 27 ms 24100 KB n=100
14 Correct 27 ms 24028 KB n=100
15 Correct 28 ms 24064 KB n=100
16 Correct 26 ms 23980 KB n=100
17 Correct 28 ms 23936 KB n=100
18 Correct 24 ms 24056 KB n=100
19 Correct 24 ms 24064 KB n=100
20 Correct 24 ms 24064 KB n=100
21 Correct 25 ms 24056 KB n=100
22 Correct 24 ms 24064 KB n=100
23 Correct 24 ms 24056 KB n=100
24 Correct 25 ms 24064 KB n=100
25 Correct 29 ms 23936 KB n=100
26 Correct 25 ms 24064 KB n=12
27 Correct 25 ms 24056 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24064 KB n=5
2 Correct 26 ms 24064 KB n=100
3 Correct 25 ms 24064 KB n=100
4 Correct 26 ms 24064 KB n=100
5 Correct 23 ms 24064 KB n=100
6 Correct 24 ms 24064 KB n=100
7 Correct 25 ms 24056 KB n=100
8 Correct 25 ms 23980 KB n=100
9 Correct 25 ms 24056 KB n=100
10 Correct 24 ms 24036 KB n=100
11 Correct 24 ms 23936 KB n=100
12 Correct 27 ms 24020 KB n=100
13 Correct 27 ms 24100 KB n=100
14 Correct 27 ms 24028 KB n=100
15 Correct 28 ms 24064 KB n=100
16 Correct 26 ms 23980 KB n=100
17 Correct 28 ms 23936 KB n=100
18 Correct 24 ms 24056 KB n=100
19 Correct 24 ms 24064 KB n=100
20 Correct 24 ms 24064 KB n=100
21 Correct 25 ms 24056 KB n=100
22 Correct 24 ms 24064 KB n=100
23 Correct 24 ms 24056 KB n=100
24 Correct 25 ms 24064 KB n=100
25 Correct 29 ms 23936 KB n=100
26 Correct 25 ms 24064 KB n=12
27 Correct 25 ms 24056 KB n=100
28 Correct 26 ms 24056 KB n=500
29 Correct 28 ms 24056 KB n=500
30 Correct 24 ms 24064 KB n=500
31 Correct 24 ms 24056 KB n=500
32 Correct 29 ms 24024 KB n=500
33 Correct 26 ms 24108 KB n=500
34 Correct 25 ms 24064 KB n=500
35 Correct 31 ms 24064 KB n=500
36 Correct 26 ms 24064 KB n=500
37 Correct 26 ms 24108 KB n=500
38 Correct 26 ms 24064 KB n=500
39 Correct 30 ms 24184 KB n=500
40 Correct 26 ms 24064 KB n=500
41 Correct 24 ms 24064 KB n=500
42 Correct 25 ms 24044 KB n=500
43 Correct 24 ms 24064 KB n=500
44 Correct 25 ms 24064 KB n=500
45 Correct 27 ms 24064 KB n=500
46 Correct 26 ms 24064 KB n=500
47 Correct 25 ms 24184 KB n=500
48 Correct 26 ms 24056 KB n=500
49 Correct 27 ms 24184 KB n=500
50 Correct 27 ms 24044 KB n=500
51 Correct 25 ms 24064 KB n=500
52 Correct 27 ms 24064 KB n=500
53 Correct 26 ms 24056 KB n=500
54 Correct 26 ms 24192 KB n=500
55 Correct 31 ms 24080 KB n=278
56 Correct 31 ms 24056 KB n=500
57 Correct 26 ms 24064 KB n=500
58 Correct 28 ms 24056 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24064 KB n=5
2 Correct 26 ms 24064 KB n=100
3 Correct 25 ms 24064 KB n=100
4 Correct 26 ms 24064 KB n=100
5 Correct 23 ms 24064 KB n=100
6 Correct 24 ms 24064 KB n=100
7 Correct 25 ms 24056 KB n=100
8 Correct 25 ms 23980 KB n=100
9 Correct 25 ms 24056 KB n=100
10 Correct 24 ms 24036 KB n=100
11 Correct 24 ms 23936 KB n=100
12 Correct 27 ms 24020 KB n=100
13 Correct 27 ms 24100 KB n=100
14 Correct 27 ms 24028 KB n=100
15 Correct 28 ms 24064 KB n=100
16 Correct 26 ms 23980 KB n=100
17 Correct 28 ms 23936 KB n=100
18 Correct 24 ms 24056 KB n=100
19 Correct 24 ms 24064 KB n=100
20 Correct 24 ms 24064 KB n=100
21 Correct 25 ms 24056 KB n=100
22 Correct 24 ms 24064 KB n=100
23 Correct 24 ms 24056 KB n=100
24 Correct 25 ms 24064 KB n=100
25 Correct 29 ms 23936 KB n=100
26 Correct 25 ms 24064 KB n=12
27 Correct 25 ms 24056 KB n=100
28 Correct 26 ms 24056 KB n=500
29 Correct 28 ms 24056 KB n=500
30 Correct 24 ms 24064 KB n=500
31 Correct 24 ms 24056 KB n=500
32 Correct 29 ms 24024 KB n=500
33 Correct 26 ms 24108 KB n=500
34 Correct 25 ms 24064 KB n=500
35 Correct 31 ms 24064 KB n=500
36 Correct 26 ms 24064 KB n=500
37 Correct 26 ms 24108 KB n=500
38 Correct 26 ms 24064 KB n=500
39 Correct 30 ms 24184 KB n=500
40 Correct 26 ms 24064 KB n=500
41 Correct 24 ms 24064 KB n=500
42 Correct 25 ms 24044 KB n=500
43 Correct 24 ms 24064 KB n=500
44 Correct 25 ms 24064 KB n=500
45 Correct 27 ms 24064 KB n=500
46 Correct 26 ms 24064 KB n=500
47 Correct 25 ms 24184 KB n=500
48 Correct 26 ms 24056 KB n=500
49 Correct 27 ms 24184 KB n=500
50 Correct 27 ms 24044 KB n=500
51 Correct 25 ms 24064 KB n=500
52 Correct 27 ms 24064 KB n=500
53 Correct 26 ms 24056 KB n=500
54 Correct 26 ms 24192 KB n=500
55 Correct 31 ms 24080 KB n=278
56 Correct 31 ms 24056 KB n=500
57 Correct 26 ms 24064 KB n=500
58 Correct 28 ms 24056 KB n=500
59 Correct 28 ms 24576 KB n=2000
60 Correct 30 ms 24568 KB n=2000
61 Correct 32 ms 24568 KB n=2000
62 Correct 37 ms 24532 KB n=2000
63 Correct 30 ms 24440 KB n=2000
64 Correct 29 ms 24576 KB n=2000
65 Correct 27 ms 24572 KB n=2000
66 Correct 27 ms 24568 KB n=2000
67 Correct 27 ms 24568 KB n=2000
68 Correct 28 ms 24568 KB n=2000
69 Correct 27 ms 24704 KB n=2000
70 Correct 26 ms 24568 KB n=2000
71 Correct 26 ms 24568 KB n=2000
72 Correct 26 ms 24568 KB n=2000
73 Correct 33 ms 24440 KB n=2000
74 Correct 28 ms 24448 KB n=1844
75 Correct 27 ms 24440 KB n=2000
76 Correct 28 ms 24576 KB n=2000
77 Correct 24 ms 24448 KB n=2000
78 Correct 29 ms 24488 KB n=2000
79 Correct 28 ms 24440 KB n=2000
80 Correct 32 ms 24576 KB n=2000
81 Correct 30 ms 24568 KB n=2000
82 Correct 35 ms 24448 KB n=2000
83 Correct 33 ms 24568 KB n=2000
84 Correct 28 ms 24448 KB n=2000
85 Correct 30 ms 24576 KB n=2000
86 Correct 32 ms 24632 KB n=2000
87 Correct 27 ms 24440 KB n=2000
88 Correct 28 ms 24568 KB n=2000
89 Correct 27 ms 24576 KB n=2000
90 Correct 27 ms 24576 KB n=2000
91 Correct 28 ms 24568 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24064 KB n=5
2 Correct 26 ms 24064 KB n=100
3 Correct 25 ms 24064 KB n=100
4 Correct 26 ms 24064 KB n=100
5 Correct 23 ms 24064 KB n=100
6 Correct 24 ms 24064 KB n=100
7 Correct 25 ms 24056 KB n=100
8 Correct 25 ms 23980 KB n=100
9 Correct 25 ms 24056 KB n=100
10 Correct 24 ms 24036 KB n=100
11 Correct 24 ms 23936 KB n=100
12 Correct 27 ms 24020 KB n=100
13 Correct 27 ms 24100 KB n=100
14 Correct 27 ms 24028 KB n=100
15 Correct 28 ms 24064 KB n=100
16 Correct 26 ms 23980 KB n=100
17 Correct 28 ms 23936 KB n=100
18 Correct 24 ms 24056 KB n=100
19 Correct 24 ms 24064 KB n=100
20 Correct 24 ms 24064 KB n=100
21 Correct 25 ms 24056 KB n=100
22 Correct 24 ms 24064 KB n=100
23 Correct 24 ms 24056 KB n=100
24 Correct 25 ms 24064 KB n=100
25 Correct 29 ms 23936 KB n=100
26 Correct 25 ms 24064 KB n=12
27 Correct 25 ms 24056 KB n=100
28 Correct 26 ms 24056 KB n=500
29 Correct 28 ms 24056 KB n=500
30 Correct 24 ms 24064 KB n=500
31 Correct 24 ms 24056 KB n=500
32 Correct 29 ms 24024 KB n=500
33 Correct 26 ms 24108 KB n=500
34 Correct 25 ms 24064 KB n=500
35 Correct 31 ms 24064 KB n=500
36 Correct 26 ms 24064 KB n=500
37 Correct 26 ms 24108 KB n=500
38 Correct 26 ms 24064 KB n=500
39 Correct 30 ms 24184 KB n=500
40 Correct 26 ms 24064 KB n=500
41 Correct 24 ms 24064 KB n=500
42 Correct 25 ms 24044 KB n=500
43 Correct 24 ms 24064 KB n=500
44 Correct 25 ms 24064 KB n=500
45 Correct 27 ms 24064 KB n=500
46 Correct 26 ms 24064 KB n=500
47 Correct 25 ms 24184 KB n=500
48 Correct 26 ms 24056 KB n=500
49 Correct 27 ms 24184 KB n=500
50 Correct 27 ms 24044 KB n=500
51 Correct 25 ms 24064 KB n=500
52 Correct 27 ms 24064 KB n=500
53 Correct 26 ms 24056 KB n=500
54 Correct 26 ms 24192 KB n=500
55 Correct 31 ms 24080 KB n=278
56 Correct 31 ms 24056 KB n=500
57 Correct 26 ms 24064 KB n=500
58 Correct 28 ms 24056 KB n=500
59 Correct 28 ms 24576 KB n=2000
60 Correct 30 ms 24568 KB n=2000
61 Correct 32 ms 24568 KB n=2000
62 Correct 37 ms 24532 KB n=2000
63 Correct 30 ms 24440 KB n=2000
64 Correct 29 ms 24576 KB n=2000
65 Correct 27 ms 24572 KB n=2000
66 Correct 27 ms 24568 KB n=2000
67 Correct 27 ms 24568 KB n=2000
68 Correct 28 ms 24568 KB n=2000
69 Correct 27 ms 24704 KB n=2000
70 Correct 26 ms 24568 KB n=2000
71 Correct 26 ms 24568 KB n=2000
72 Correct 26 ms 24568 KB n=2000
73 Correct 33 ms 24440 KB n=2000
74 Correct 28 ms 24448 KB n=1844
75 Correct 27 ms 24440 KB n=2000
76 Correct 28 ms 24576 KB n=2000
77 Correct 24 ms 24448 KB n=2000
78 Correct 29 ms 24488 KB n=2000
79 Correct 28 ms 24440 KB n=2000
80 Correct 32 ms 24576 KB n=2000
81 Correct 30 ms 24568 KB n=2000
82 Correct 35 ms 24448 KB n=2000
83 Correct 33 ms 24568 KB n=2000
84 Correct 28 ms 24448 KB n=2000
85 Correct 30 ms 24576 KB n=2000
86 Correct 32 ms 24632 KB n=2000
87 Correct 27 ms 24440 KB n=2000
88 Correct 28 ms 24568 KB n=2000
89 Correct 27 ms 24576 KB n=2000
90 Correct 27 ms 24576 KB n=2000
91 Correct 28 ms 24568 KB n=2000
92 Correct 1203 ms 77940 KB n=200000
93 Correct 2353 ms 81252 KB n=200000
94 Correct 1854 ms 83464 KB n=200000
95 Correct 1180 ms 77752 KB n=200000
96 Correct 1255 ms 77676 KB n=200000
97 Correct 2371 ms 80604 KB n=200000
98 Correct 1228 ms 77660 KB n=200000
99 Correct 1349 ms 77948 KB n=200000
100 Correct 1266 ms 77804 KB n=200000
101 Correct 2167 ms 84344 KB n=200000
102 Correct 621 ms 78920 KB n=200000
103 Correct 591 ms 78712 KB n=200000
104 Correct 617 ms 78808 KB n=200000
105 Correct 669 ms 79068 KB n=200000
106 Correct 606 ms 79096 KB n=200000
107 Correct 597 ms 79152 KB n=200000
108 Correct 1139 ms 77716 KB n=200000
109 Correct 1121 ms 77736 KB n=200000
110 Correct 1198 ms 77756 KB n=200000
111 Correct 1249 ms 77348 KB n=200000
112 Correct 1795 ms 83536 KB n=200000
113 Correct 2280 ms 80668 KB n=200000
114 Correct 1186 ms 77120 KB n=200000
115 Correct 2624 ms 79272 KB n=200000
116 Correct 1166 ms 77928 KB n=200000
117 Correct 1749 ms 83832 KB n=200000
118 Correct 2356 ms 79916 KB n=200000
119 Correct 1128 ms 78008 KB n=200000
120 Correct 1632 ms 83396 KB n=200000
121 Correct 1773 ms 83188 KB n=200000
122 Correct 1879 ms 83576 KB n=200000
123 Correct 619 ms 79024 KB n=200000
124 Correct 450 ms 39008 KB n=25264