답안 #572145

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
572145 2022-06-03T18:55:21 Z vovamr Birthday gift (IZhO18_treearray) C++17
0 / 100
22 ms 42708 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

const int N = 2e5 + 100;
ve<int> gr[N];
const int lg = 19;
int in[N], out[N], up[N][lg];
int tim = 0;

inline void dfs(int v, int p) {
	up[v][0] = p;
	in[v] = ++tim;
	for (auto &to : gr[v]) {
		if (to == p) continue;
		dfs(to, v);
	} out[v] = ++tim;
}
inline bool anc(int a, int b) { return in[a] <= in[b] && out[b] <= out[a]; }
inline int lca(int a, int b) {
	if (anc(a, b)) return a;
	if (anc(b, a)) return b;
	for (int i = lg - 1; ~i; --i) {
		if (!anc(up[a][i], b)) {
			a = up[a][i];
		}
	} return up[a][0];
}

set<pii> t[4 * N];
inline void ins(int v, int vl, int vr, int pos, pii x) {
	t[v].insert(x);
	if (vl == vr) return;
	int m = vl + vr >> 1;
	if (pos <= m) ins(2 * v + 1, vl, m, pos, x);
	else ins(2 * v + 2, m + 1, vr, pos, x);
}
inline void del(int v, int vl, int vr, int pos, pii x) {
	t[v].erase(x);
	if (vl == vr) return;
	int m = vl + vr >> 1;
	if (pos <= m) del(2 * v + 1, vl, m, pos, x);
	else del(2 * v + 2, m + 1, vr, pos, x);
}
inline pii ok(int v, int vl, int vr, int l, int r, int x) {
	if (l > r) return {-1, -1};
	else if (vl == l && vr == r) {
		auto it = t[v].lower_bound(mpp(x, -1));
		if (it == t[v].end() || it->fi != x) return mpp(-1, -1);
		return *it;
	}
	int m = vl + vr >> 1;
	pii a = ok(2 * v + 1, vl, m, l, min(r, m), x);
	pii b = ok(2 * v + 2, m + 1, vr, max(l, m + 1), r, x);
	if (b.fi != -1) swap(a, b);
	return a;
}

inline void solve() {
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 1; i < n; ++i) {
		int v, u;
		cin >> v >> u, --v, --u;
		gr[v].pb(u), gr[u].pb(v);
	} dfs(0, 0);
	for (int i = 1; i < lg; ++i) {
		for (int v = 0; v < n; ++v) {
			up[v][i] = up[up[v][i - 1]][i - 1];
		}
	}

	ve<int> a(m);
	for (auto &i : a) cin >> i, --i;
	for (int i = 0; i < m; ++i) ins(0, 0, m - 1, i, mpp(a[i], i));
	for (int i = 1; i < m; ++i) ins(0, 0, m - 1, i, mpp(lca(a[i], a[i - 1]), i));

	while (q--) {
		int e;
		cin >> e;
		if (e == 1) {
			int pos, x;
			cin >> pos >> x, --pos, --x;

			if (pos) del(0, 0, m - 1, pos, mpp(lca(a[pos], a[pos - 1]), pos));
			if (pos + 1 < m) del(0, 0, m - 1, pos + 1, mpp(lca(a[pos], a[pos + 1]), pos + 1));
			del(0, 0, m - 1, pos, mpp(a[pos], pos));

			a[pos] = x;

			if (pos) ins(0, 0, m - 1, pos, mpp(lca(a[pos], a[pos - 1]), pos));
			if (pos + 1 < m) ins(0, 0, m - 1, pos + 1, mpp(lca(a[pos], a[pos + 1]), pos + 1));
			ins(0, 0, m - 1, pos, mpp(a[pos], pos));
		}
		else {
			int l, r, v;
			cin >> l >> r >> v, --l, --r, --v;
			if (a[l] == v) cout << l + 1 << " " << l + 1 << '\n';
			else {
				pii ans = ok(0, 0, m - 1, l + 1, r, v);
				if (ans.fi == -1) cout << "-1 -1" << '\n';
				else {
					if (a[ans.se] == v) cout << ans.se + 1 << " " << ans.se + 1 << '\n';
					else cout << ans.se << " " << ans.se + 1 << '\n';
				}
			}
		}
	}
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}

Compilation message

treearray.cpp: In function 'void ins(int, int, int, int, pii)':
treearray.cpp:52:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |  int m = vl + vr >> 1;
      |          ~~~^~~~
treearray.cpp: In function 'void del(int, int, int, int, pii)':
treearray.cpp:59:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |  int m = vl + vr >> 1;
      |          ~~~^~~~
treearray.cpp: In function 'pii ok(int, int, int, int, int, int)':
treearray.cpp:70:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |  int m = vl + vr >> 1;
      |          ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 42580 KB n=5
2 Incorrect 20 ms 42708 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 42580 KB n=5
2 Incorrect 20 ms 42708 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 42580 KB n=5
2 Incorrect 20 ms 42708 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 42580 KB n=5
2 Incorrect 20 ms 42708 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -