| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 91890 | emil_physmath | Birthday gift (IZhO18_treearray) | C++17 | 2 ms | 564 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
const int MAXN=2005;
int n, l, timer, a[MAXN], tin[MAXN], tout[MAXN];
vector<int> nei[MAXN], up[MAXN];
int lca(int u, int v);
void dfs (int u, int p=1);
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=0; i<m; i++)
cin>>a[i];
int type, pos, L, R, v, ans;
while (q--)
{
scanf("%d", &type);
if (type==1)
{
scanf("%d%d", &pos, &v);
a[pos-1]=v;
}
else
{
scanf("%d%d%d", &L, &R, &v); L--; R--;
ans=-1;
for (int i=L; i<R; i++)
if (lca(a[i], a[i+1])==v)
{
ans=i+1;
break;
}
if (ans==-1)
{
for (int i=L; i<=R; i++)
if (upper(a[i], v))
{
ans=i+1;
break;
}
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;
}
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];
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
