답안 #83109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
83109 2018-11-05T12:02:44 Z Good Birthday gift (IZhO18_treearray) C++11
0 / 100
25 ms 23928 KB
/*
ID: blackha5
TASK: test
LANG: C++
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ff first
#define ss second
#define Maxn 200003
#define ll long long
#define pb push_back
#define Inf 1000000009
#define ppb() pop_back()
#define pii pair <int , int>
#define mid(x, y) (x + y) / 2
#define all(x) x.begin(),x.end()
#define llInf 1000000000000000009
#define tr(i, c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;
using namespace __gnu_pbds;
typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> order;

int n, m, q;
int A[Maxn];
int lvl[Maxn];
int mat[Maxn][21];

vector <int> adj[Maxn];
set <int> s[Maxn], t[Maxn];

void dfs (int nd, int p) {
	mat[nd][0] = p;
	lvl[nd] = lvl[p] + 1;
	
	for (auto i: adj[nd])
		if (i != p)
			dfs (i, nd);
}

void build () {
	for (int i = 1; i <= 19; i++)
		for (int j = 1; j <= n; j++)
			mat[j][i] = mat[mat[j][i - 1]][i - 1];
}

int lca (int x, int y) {
	if (lvl[x] < lvl[y])
		swap (x, y);
	
	for (int i = 19; i >= 0; i--)
		if (lvl[mat[x][i]] >= lvl[y])
			x = mat[x][i];
	
	if (x == y)
		return x;
	
	for (int i = 19; i >= 0; i--) 
		if (mat[x][i] != mat[y][i])
			x = mat[x][i], y = mat[y][i];
	
	return mat[x][0];					
}

int main () {
	//freopen ("file.in", "r", stdin);
	//freopen ("file.out", "w", stdout);
	
 	//srand ((unsigned) time ( NULL ));
	//int randomNumber = rand() % 10 + 1;

	scanf ("%d%d%d", &n, &m, &q);
	
	for (int i = 1; i < n; i++) {
		int u, v;
		
		scanf ("%d%d", &u, &v);
		adj[u].pb (v);
		adj[v].pb (u);
		s[i].insert (Inf);
		t[i].insert (Inf);
	}
	s[n].insert (Inf);
	t[n].insert (Inf);

	dfs (1, 0);
	build ();
	
	for (int i = 1; i <= m; i++) {
		scanf ("%d", A + i);
		t[A[i]].insert (i);
	}	
	
	for (int i = 1; i < m; i++) {
		cout << lca (A[i], A[i + 1]) << ' ' << i << endl;
		s[lca (A[i], A[i + 1])].insert (i);
	}	
	
	while (q--) {
		int type, l, v;
		scanf ("%d%d", &type, &l);
		
		if (type == 2) {
			int r;
			scanf ("%d%d", &r, &v);
			int x = *s[v].lower_bound (l);
			int y = *t[v].lower_bound (l);
			
			if (y <= r)
				printf ("%d %d\n", y, y);
			else if (x <= r - 1)
				printf ("%d %d\n", x, x + 1);
			else
				puts ("-1 -1");		
		}	
		
		else {
			scanf ("%d", &v);
			t[A[l]].erase (l);
			t[v].insert (l);
			
			if (l > 1) {
				int x = lca (A[l - 1], A[l]);
				s[x].erase (l - 1);
				x = lca (A[l - 1], v);
				s[x].insert(l - 1);		
			}
			if (l < n) {
				int x = lca (A[l], A[l + 1]);
				s[x].erase (l);
				x = lca (v, A[l + 1]);
				s[x].insert (l);
			}
			A[l] = v;
		}
	}		
	return 0;
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:73:8: 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:78:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d%d", &u, &v);
   ~~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:91:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d", A + i);
   ~~~~~~^~~~~~~~~~~~~
treearray.cpp:102:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d%d", &type, &l);
   ~~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf ("%d%d", &r, &v);
    ~~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf ("%d", &v);
    ~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 23928 KB Wrong output format.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 23928 KB Wrong output format.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 23928 KB Wrong output format.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 23928 KB Wrong output format.
2 Halted 0 ms 0 KB -