답안 #92146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
92146 2019-01-01T16:44:28 Z emil_physmath Birthday gift (IZhO18_treearray) C++17
0 / 100
28 ms 28588 KB
#include <iostream>
#include <stdio.h>
#include <vector>
#include <set>
using namespace std;
const int MAXN=200005;
 
int n, l, timer, a[MAXN], tin[MAXN], tout[MAXN], lca[MAXN];
vector<int> nei[MAXN], up[MAXN];
set<int> lcas[MAXN], atind[MAXN];
 
int LCA(int u, int v);
void dfs (int u, int p=1);
inline bool upper (int u, int v);
int main()
{
	int m, q;
	cin>>n>>m>>q;
	for (int i=1; i<n; i++)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		nei[u].push_back(v);
		nei[v].push_back(u);
	}
	l=1;
	while((1<<l)<=n)
		l++;
	for(int i=1; i<=n; i++)
		up[i].resize(l+1);
	dfs(1);
	for (int i=1; i<=m; i++)
	{
		scanf("%d", a+i);
		atind[a[i]].insert(i);
	}
	for (int i=1; i<m; i++)
	{
		lca[i]=LCA(a[i], a[i+1]);
		lcas[lca[i]].insert(i);
	}
	int type, pos, L, R, v, ans;
	while (q--)
	{
		scanf("%d", &type);
		if (type==1)
		{
			scanf("%d%d", &pos, &v);
			atind[a[pos]].erase(pos);
			a[pos]=v;
			atind[v].insert(pos);
			if (pos-1>=1)
			{
				lcas[lca[pos-1]].erase(pos-1);
				lca[pos-1]=LCA(a[pos-1], a[pos]);
				lcas[lca[pos-1]].insert(pos-1);
			}
			if (pos+1<=m)
			{
				lcas[lca[pos]].erase(pos);
				lca[pos]=LCA(a[pos], a[pos+1]);
				lcas[lca[pos]].insert(pos);
			}
		}
		else
		{
			scanf("%d%d%d", &L, &R, &v);
			ans=-1;
			/*
			for (int i=L; i<R; i++)
				if (lca[i]==v)
				{
					ans=i;
					break;
				}
			*/
			auto it=lcas[v].lower_bound(L);
			if (it!=lcas[v].end() && *it<=R)
				ans=*it;
			if (ans==-1)
			{
				/*
				for (int i=L; i<=R; i++)
					if (a[i]==v)
					{
						ans=i;
						break;
					}
				*/
				auto it=atind[v].lower_bound(L);
				if (it!=atind[v].end() && *it<=R)
					ans=*it;
				if (ans==-1)
					printf("-1 -1\n");
				else
					printf("%d %d\n", ans, ans);
			}
			else
				printf("%d %d\n", ans, ans+1);
		}
	}
 
	char I;
	cin >> I;
	return 0;
}

void dfs(int u, int p) {
	tin[u]=++timer;
	up[u][0]=p;
	for (int i=1; i<=l; i++)
		up[u][i]=up[up[u][i-1]][i-1];
	for (int i=0; i<nei[u].size(); i++)
		if (nei[u][i]!=p)
			dfs(nei[u][i], u);
	tout[u]=++timer;
}
 
inline bool upper(int u, int v) {
	return tin[u]<=tin[v] && tout[u]>=tout[v];
}
 
int LCA(int u, int v) {
	if (upper(u, v))  return u;
	if (upper(v, u))  return v;
	for (int i=l; i>=0; i--)
		if (!upper (up[u][i], v))
			u=up[u][i];
	return up[u][0];
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:113:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<nei[u].size(); i++)
                ~^~~~~~~~~~~~~~
treearray.cpp: In function 'int main()':
treearray.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~
treearray.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a+i);
   ~~~~~^~~~~~~~~~~
treearray.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &type);
   ~~~~~^~~~~~~~~~~~~
treearray.cpp:48: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:67:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d%d", &L, &R, &v);
    ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 28 ms 28580 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 22 ms 28508 KB n=100
6 Correct 23 ms 28536 KB n=100
7 Correct 22 ms 28536 KB n=100
8 Correct 22 ms 28588 KB n=100
9 Correct 22 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 23 ms 28536 KB n=100
12 Incorrect 22 ms 28536 KB Wrong output format.
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 28 ms 28580 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 22 ms 28508 KB n=100
6 Correct 23 ms 28536 KB n=100
7 Correct 22 ms 28536 KB n=100
8 Correct 22 ms 28588 KB n=100
9 Correct 22 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 23 ms 28536 KB n=100
12 Incorrect 22 ms 28536 KB Wrong output format.
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 28 ms 28580 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 22 ms 28508 KB n=100
6 Correct 23 ms 28536 KB n=100
7 Correct 22 ms 28536 KB n=100
8 Correct 22 ms 28588 KB n=100
9 Correct 22 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 23 ms 28536 KB n=100
12 Incorrect 22 ms 28536 KB Wrong output format.
13 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 28536 KB n=5
2 Correct 27 ms 28536 KB n=100
3 Correct 28 ms 28580 KB n=100
4 Correct 26 ms 28536 KB n=100
5 Correct 22 ms 28508 KB n=100
6 Correct 23 ms 28536 KB n=100
7 Correct 22 ms 28536 KB n=100
8 Correct 22 ms 28588 KB n=100
9 Correct 22 ms 28536 KB n=100
10 Correct 23 ms 28536 KB n=100
11 Correct 23 ms 28536 KB n=100
12 Incorrect 22 ms 28536 KB Wrong output format.
13 Halted 0 ms 0 KB -