답안 #65817

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
65817 2018-08-09T00:37:43 Z kingpig9 Birthday gift (IZhO18_treearray) C++11
0 / 100
30 ms 31212 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1 << 18, MAXLG = 18;

#define debug(...) fprintf(stderr, __VA_ARGS__)
//#define debug(...)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))

int N, M, Q;
vector<int> adj[MAXN];
int par[MAXN][MAXLG], depth[MAXN];
int A[MAXN];

int getpar (int x, int d) {
	for (int i = 0; d; d >>= 1) {
		if (d & 1) {
			x = par[x][i];
		}
	}
	return x;
}

int lca (int x, int y) {
	if (x == 0 || y == 0) {
		return 0;
	}
	if (x == -1) {
		return y;
	}
	if (y == -1) {
		return x;
	}

	if (depth[x] < depth[y]) {
		swap(x, y);
	}
	x = getpar(x, depth[x] - depth[y]);
	if (x == y) {
		return x;
	}

	for (int i = MAXLG - 1; i >= 0; i--) {
		if (par[x][i] != par[y][i]) {
			x = par[x][i];
			y = par[y][i];
		}
	}
	return par[x][0];
}

void dfs (int x) {
	for (int y : adj[x]) {
		adj[y].erase(find(all(adj[y]), x));
		par[y][0] = x;
		for (int i = 1; i < MAXLG; i++) {
			par[y][i] = par[par[y][i - 1]][i - 1];
		}
		depth[y] = depth[x] + 1;

		dfs(y);
	}
}

set<int> st[MAXN][2];

pii solve (int lt, int rt, int v) {
	set<int> *s = st[v];
	auto it = s[0].lower_bound(lt);
	if (it != s[0].end() && *it <= rt) {
		return pii(*it, *it);
	}
	it = s[1].lower_bound(lt);
	if (it != s[1].end() && *it < rt) {
		return pii(*it, *it + 1);
	}
	return pii(-1, -1);
}

int main() {
	scanf("%d %d %d", &N, &M, &Q);
	for (int i = 1; i < N; i++) {
		int x, y;
		scanf("%d %d", &x, &y);
		adj[x].push_back(y);
		adj[y].push_back(x);
	}

	dfs(1);

	for (int i = 1; i <= M; i++) {
		scanf("%d", &A[i]);
	}
	for (int i = 1; i <= M; i++) {
		st[A[i]][0].insert(i);
		if (i + 1 <= M) {
			st[lca(A[i], A[i + 1])][1].insert(i);
		}
	}

	//and now...queries
	for (int qi = 1; qi <= Q; qi++) {
		int qt, v;
		scanf("%d", &qt);
		if (qt == 1) {
			int pos;
			scanf("%d %d", &pos, &v);

			st[A[pos]][0].erase(pos);
			if (pos + 1 <= M) {
				st[lca(A[pos], A[pos + 1])][1].erase(pos);
			}
			if (pos - 1 >= 1) {
				st[lca(A[pos - 1], A[pos])][1].erase(pos - 1);
			}

			A[pos] = v;

			st[A[pos]][0].insert(pos);
			if (pos + 1 <= M) {
				st[lca(A[pos], A[pos + 1])][1].insert(pos);
			}
			if (pos - 1 >= 1) {
				st[lca(A[pos - 1], A[pos])][1].insert(pos - 1);
			}
		} else {
			int lt, rt;
			scanf("%d %d %d", &lt, &rt, &v);
			pii ans = solve(lt, rt, v);
			printf("%d %d\n", ans.fi, ans.se);
		}
	}
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &N, &M, &Q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A[i]);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &qt);
   ~~~~~^~~~~~~~~~~
treearray.cpp:113:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &pos, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:134:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d %d", &lt, &rt, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 31100 KB n=5
2 Incorrect 30 ms 31212 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 31100 KB n=5
2 Incorrect 30 ms 31212 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 31100 KB n=5
2 Incorrect 30 ms 31212 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 31100 KB n=5
2 Incorrect 30 ms 31212 KB Jury has the answer but participant has not
3 Halted 0 ms 0 KB -