Submission #97302

# Submission time Handle Problem Language Result Execution time Memory
97302 2019-02-15T01:57:05 Z qkxwsm Birthday gift (IZhO18_treearray) C++14
56 / 100
4000 ms 40284 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];
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]--;
	}
	while(Q--)
	{
		int qid;
		cin >> qid;
		if (qid == 1)
		{
			int idx, v;
			cin >> idx >> v;
			idx--; v--;
			arr[idx] = v;
		}
		if (qid == 2)
		{
			int lt, rt, v;
			cin >> lt >> rt >> v;
			lt--; rt--; v--;
			int loc = -1; pii res = {lt, -1};
			FOR(i, lt, rt + 1)
			{
				if (insubt(v, arr[i]))
				{
					loc = (loc == -1 ? arr[i] : lca(loc, arr[i]));
				}
				else
				{
					loc = -1;
					res.fi = i + 1;
				}
				if (loc == v)
				{
					res.se = i;
					break;
				}
			}
			if (res.se == -1)
			{
				cout << "-1 -1\n";
			}
			else
			{
				cout << res.fi + 1 << ' ' << res.se + 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 7 ms 5376 KB n=5
2 Correct 8 ms 5248 KB n=100
3 Correct 7 ms 5248 KB n=100
4 Correct 8 ms 5120 KB n=100
5 Correct 8 ms 5248 KB n=100
6 Correct 7 ms 5248 KB n=100
7 Correct 9 ms 5248 KB n=100
8 Correct 6 ms 5248 KB n=100
9 Correct 8 ms 5248 KB n=100
10 Correct 8 ms 5248 KB n=100
11 Correct 9 ms 5248 KB n=100
12 Correct 8 ms 5248 KB n=100
13 Correct 7 ms 5248 KB n=100
14 Correct 8 ms 5240 KB n=100
15 Correct 7 ms 5120 KB n=100
16 Correct 7 ms 5248 KB n=100
17 Correct 9 ms 5124 KB n=100
18 Correct 8 ms 5248 KB n=100
19 Correct 7 ms 5248 KB n=100
20 Correct 8 ms 5248 KB n=100
21 Correct 9 ms 5248 KB n=100
22 Correct 8 ms 5248 KB n=100
23 Correct 8 ms 5120 KB n=100
24 Correct 8 ms 5248 KB n=100
25 Correct 8 ms 5248 KB n=100
26 Correct 7 ms 5120 KB n=12
27 Correct 8 ms 5248 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB n=5
2 Correct 8 ms 5248 KB n=100
3 Correct 7 ms 5248 KB n=100
4 Correct 8 ms 5120 KB n=100
5 Correct 8 ms 5248 KB n=100
6 Correct 7 ms 5248 KB n=100
7 Correct 9 ms 5248 KB n=100
8 Correct 6 ms 5248 KB n=100
9 Correct 8 ms 5248 KB n=100
10 Correct 8 ms 5248 KB n=100
11 Correct 9 ms 5248 KB n=100
12 Correct 8 ms 5248 KB n=100
13 Correct 7 ms 5248 KB n=100
14 Correct 8 ms 5240 KB n=100
15 Correct 7 ms 5120 KB n=100
16 Correct 7 ms 5248 KB n=100
17 Correct 9 ms 5124 KB n=100
18 Correct 8 ms 5248 KB n=100
19 Correct 7 ms 5248 KB n=100
20 Correct 8 ms 5248 KB n=100
21 Correct 9 ms 5248 KB n=100
22 Correct 8 ms 5248 KB n=100
23 Correct 8 ms 5120 KB n=100
24 Correct 8 ms 5248 KB n=100
25 Correct 8 ms 5248 KB n=100
26 Correct 7 ms 5120 KB n=12
27 Correct 8 ms 5248 KB n=100
28 Correct 7 ms 5248 KB n=500
29 Correct 10 ms 5248 KB n=500
30 Correct 8 ms 5248 KB n=500
31 Correct 10 ms 5248 KB n=500
32 Correct 8 ms 5248 KB n=500
33 Correct 10 ms 5248 KB n=500
34 Correct 7 ms 5248 KB n=500
35 Correct 8 ms 5248 KB n=500
36 Correct 8 ms 5248 KB n=500
37 Correct 9 ms 5248 KB n=500
38 Correct 8 ms 5248 KB n=500
39 Correct 8 ms 5248 KB n=500
40 Correct 8 ms 5248 KB n=500
41 Correct 8 ms 5248 KB n=500
42 Correct 9 ms 5248 KB n=500
43 Correct 8 ms 5248 KB n=500
44 Correct 8 ms 5296 KB n=500
45 Correct 8 ms 5248 KB n=500
46 Correct 9 ms 5248 KB n=500
47 Correct 9 ms 5248 KB n=500
48 Correct 7 ms 5248 KB n=500
49 Correct 9 ms 5376 KB n=500
50 Correct 8 ms 5248 KB n=500
51 Correct 10 ms 5248 KB n=500
52 Correct 9 ms 5248 KB n=500
53 Correct 9 ms 5248 KB n=500
54 Correct 12 ms 5248 KB n=500
55 Correct 7 ms 5248 KB n=278
56 Correct 13 ms 5248 KB n=500
57 Correct 17 ms 5248 KB n=500
58 Correct 7 ms 5248 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB n=5
2 Correct 8 ms 5248 KB n=100
3 Correct 7 ms 5248 KB n=100
4 Correct 8 ms 5120 KB n=100
5 Correct 8 ms 5248 KB n=100
6 Correct 7 ms 5248 KB n=100
7 Correct 9 ms 5248 KB n=100
8 Correct 6 ms 5248 KB n=100
9 Correct 8 ms 5248 KB n=100
10 Correct 8 ms 5248 KB n=100
11 Correct 9 ms 5248 KB n=100
12 Correct 8 ms 5248 KB n=100
13 Correct 7 ms 5248 KB n=100
14 Correct 8 ms 5240 KB n=100
15 Correct 7 ms 5120 KB n=100
16 Correct 7 ms 5248 KB n=100
17 Correct 9 ms 5124 KB n=100
18 Correct 8 ms 5248 KB n=100
19 Correct 7 ms 5248 KB n=100
20 Correct 8 ms 5248 KB n=100
21 Correct 9 ms 5248 KB n=100
22 Correct 8 ms 5248 KB n=100
23 Correct 8 ms 5120 KB n=100
24 Correct 8 ms 5248 KB n=100
25 Correct 8 ms 5248 KB n=100
26 Correct 7 ms 5120 KB n=12
27 Correct 8 ms 5248 KB n=100
28 Correct 7 ms 5248 KB n=500
29 Correct 10 ms 5248 KB n=500
30 Correct 8 ms 5248 KB n=500
31 Correct 10 ms 5248 KB n=500
32 Correct 8 ms 5248 KB n=500
33 Correct 10 ms 5248 KB n=500
34 Correct 7 ms 5248 KB n=500
35 Correct 8 ms 5248 KB n=500
36 Correct 8 ms 5248 KB n=500
37 Correct 9 ms 5248 KB n=500
38 Correct 8 ms 5248 KB n=500
39 Correct 8 ms 5248 KB n=500
40 Correct 8 ms 5248 KB n=500
41 Correct 8 ms 5248 KB n=500
42 Correct 9 ms 5248 KB n=500
43 Correct 8 ms 5248 KB n=500
44 Correct 8 ms 5296 KB n=500
45 Correct 8 ms 5248 KB n=500
46 Correct 9 ms 5248 KB n=500
47 Correct 9 ms 5248 KB n=500
48 Correct 7 ms 5248 KB n=500
49 Correct 9 ms 5376 KB n=500
50 Correct 8 ms 5248 KB n=500
51 Correct 10 ms 5248 KB n=500
52 Correct 9 ms 5248 KB n=500
53 Correct 9 ms 5248 KB n=500
54 Correct 12 ms 5248 KB n=500
55 Correct 7 ms 5248 KB n=278
56 Correct 13 ms 5248 KB n=500
57 Correct 17 ms 5248 KB n=500
58 Correct 7 ms 5248 KB n=500
59 Correct 10 ms 5504 KB n=2000
60 Correct 29 ms 5624 KB n=2000
61 Correct 28 ms 5504 KB n=2000
62 Correct 23 ms 5504 KB n=2000
63 Correct 8 ms 5504 KB n=2000
64 Correct 30 ms 5624 KB n=2000
65 Correct 8 ms 5504 KB n=2000
66 Correct 40 ms 5632 KB n=2000
67 Correct 10 ms 5504 KB n=2000
68 Correct 26 ms 5504 KB n=2000
69 Correct 14 ms 5504 KB n=2000
70 Correct 14 ms 5632 KB n=2000
71 Correct 14 ms 5504 KB n=2000
72 Correct 13 ms 5504 KB n=2000
73 Correct 10 ms 5504 KB n=2000
74 Correct 8 ms 5504 KB n=1844
75 Correct 12 ms 5504 KB n=2000
76 Correct 15 ms 5504 KB n=2000
77 Correct 14 ms 5504 KB n=2000
78 Correct 21 ms 5504 KB n=2000
79 Correct 9 ms 5504 KB n=2000
80 Correct 28 ms 5632 KB n=2000
81 Correct 24 ms 5504 KB n=2000
82 Correct 12 ms 5504 KB n=2000
83 Correct 33 ms 5624 KB n=2000
84 Correct 13 ms 5632 KB n=2000
85 Correct 29 ms 5504 KB n=2000
86 Correct 25 ms 5632 KB n=2000
87 Correct 12 ms 5504 KB n=2000
88 Correct 249 ms 5624 KB n=2000
89 Correct 233 ms 5624 KB n=2000
90 Correct 100 ms 5632 KB n=2000
91 Correct 13 ms 5504 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB n=5
2 Correct 8 ms 5248 KB n=100
3 Correct 7 ms 5248 KB n=100
4 Correct 8 ms 5120 KB n=100
5 Correct 8 ms 5248 KB n=100
6 Correct 7 ms 5248 KB n=100
7 Correct 9 ms 5248 KB n=100
8 Correct 6 ms 5248 KB n=100
9 Correct 8 ms 5248 KB n=100
10 Correct 8 ms 5248 KB n=100
11 Correct 9 ms 5248 KB n=100
12 Correct 8 ms 5248 KB n=100
13 Correct 7 ms 5248 KB n=100
14 Correct 8 ms 5240 KB n=100
15 Correct 7 ms 5120 KB n=100
16 Correct 7 ms 5248 KB n=100
17 Correct 9 ms 5124 KB n=100
18 Correct 8 ms 5248 KB n=100
19 Correct 7 ms 5248 KB n=100
20 Correct 8 ms 5248 KB n=100
21 Correct 9 ms 5248 KB n=100
22 Correct 8 ms 5248 KB n=100
23 Correct 8 ms 5120 KB n=100
24 Correct 8 ms 5248 KB n=100
25 Correct 8 ms 5248 KB n=100
26 Correct 7 ms 5120 KB n=12
27 Correct 8 ms 5248 KB n=100
28 Correct 7 ms 5248 KB n=500
29 Correct 10 ms 5248 KB n=500
30 Correct 8 ms 5248 KB n=500
31 Correct 10 ms 5248 KB n=500
32 Correct 8 ms 5248 KB n=500
33 Correct 10 ms 5248 KB n=500
34 Correct 7 ms 5248 KB n=500
35 Correct 8 ms 5248 KB n=500
36 Correct 8 ms 5248 KB n=500
37 Correct 9 ms 5248 KB n=500
38 Correct 8 ms 5248 KB n=500
39 Correct 8 ms 5248 KB n=500
40 Correct 8 ms 5248 KB n=500
41 Correct 8 ms 5248 KB n=500
42 Correct 9 ms 5248 KB n=500
43 Correct 8 ms 5248 KB n=500
44 Correct 8 ms 5296 KB n=500
45 Correct 8 ms 5248 KB n=500
46 Correct 9 ms 5248 KB n=500
47 Correct 9 ms 5248 KB n=500
48 Correct 7 ms 5248 KB n=500
49 Correct 9 ms 5376 KB n=500
50 Correct 8 ms 5248 KB n=500
51 Correct 10 ms 5248 KB n=500
52 Correct 9 ms 5248 KB n=500
53 Correct 9 ms 5248 KB n=500
54 Correct 12 ms 5248 KB n=500
55 Correct 7 ms 5248 KB n=278
56 Correct 13 ms 5248 KB n=500
57 Correct 17 ms 5248 KB n=500
58 Correct 7 ms 5248 KB n=500
59 Correct 10 ms 5504 KB n=2000
60 Correct 29 ms 5624 KB n=2000
61 Correct 28 ms 5504 KB n=2000
62 Correct 23 ms 5504 KB n=2000
63 Correct 8 ms 5504 KB n=2000
64 Correct 30 ms 5624 KB n=2000
65 Correct 8 ms 5504 KB n=2000
66 Correct 40 ms 5632 KB n=2000
67 Correct 10 ms 5504 KB n=2000
68 Correct 26 ms 5504 KB n=2000
69 Correct 14 ms 5504 KB n=2000
70 Correct 14 ms 5632 KB n=2000
71 Correct 14 ms 5504 KB n=2000
72 Correct 13 ms 5504 KB n=2000
73 Correct 10 ms 5504 KB n=2000
74 Correct 8 ms 5504 KB n=1844
75 Correct 12 ms 5504 KB n=2000
76 Correct 15 ms 5504 KB n=2000
77 Correct 14 ms 5504 KB n=2000
78 Correct 21 ms 5504 KB n=2000
79 Correct 9 ms 5504 KB n=2000
80 Correct 28 ms 5632 KB n=2000
81 Correct 24 ms 5504 KB n=2000
82 Correct 12 ms 5504 KB n=2000
83 Correct 33 ms 5624 KB n=2000
84 Correct 13 ms 5632 KB n=2000
85 Correct 29 ms 5504 KB n=2000
86 Correct 25 ms 5632 KB n=2000
87 Correct 12 ms 5504 KB n=2000
88 Correct 249 ms 5624 KB n=2000
89 Correct 233 ms 5624 KB n=2000
90 Correct 100 ms 5632 KB n=2000
91 Correct 13 ms 5504 KB n=2000
92 Correct 401 ms 40284 KB n=200000
93 Execution timed out 4045 ms 39004 KB Time limit exceeded
94 Halted 0 ms 0 KB -