# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
378978 | daniel920712 | Birthday gift (IZhO18_treearray) | C++14 | 611 ms | 262148 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int deg[200005];
vector < int > Next[200005];
int Father[30][200005]={0};
int all[200005];
int now=1;
struct A
{
int l,r;
int nxl,nxr;
int ans;
}Node[200005];
void F(int here,int fa,int deg)
{
int j;
::deg[here]=deg;
for(auto i:Next[here])
{
if(i==fa) continue;
Father[0][i]=here;
for(j=1;j<25;j++) Father[j][i]=Father[j-1][Father[j-1][i]];
F(i,here,deg+1);
}
}
int LCA(int a,int b)
{
int i;
if(deg[a]<deg[b]) swap(a,b);
for(i=20;i>=0;i--) if(deg[a]>deg[b]&°[a]-(1<<i)>=deg[b]) a=Father[i][a];
for(i=20;i>=0;i--)
{
if(deg[a]==deg[b]&&Father[i][a]!=Father[i][b])
{
a=Father[i][a];
b=Father[i][b];
}
}
if(a==b) return a;
if(Father[0][a]==Father[0][b]) return Father[0][a];
}
void build(int l,int r,int here)
{
Node[here].l=l;
Node[here].r=r;
if(l==r)
{
Node[here].ans=all[l];
return;
}
Node[here].nxl=now++;
Node[here].nxr=now++;
build(l,(l+r)/2,Node[here].nxl);
build((l+r)/2+1,r,Node[here].nxr);
Node[here].ans=LCA(Node[Node[here].nxl].ans,Node[Node[here].nxr].ans);
}
void cha(int where,int con,int here)
{
if(where==Node[here].l&&where==Node[here].r)
{
Node[here].ans=con;
return;
}
if(where<=(Node[here].l+Node[here].r)/2) cha(where,con,Node[here].nxl);
else cha(where,con,Node[here].nxr);
Node[here].ans=LCA(Node[Node[here].nxl].ans,Node[Node[here].nxr].ans);
}
int Find(int l,int r,int here)
{
if(Node[here].l==l&&Node[here].r==r) return Node[here].ans;
if(r<=(Node[here].l+Node[here].r)/2) return Find(l,r,Node[here].nxl);
else if(l>(Node[here].l+Node[here].r)/2) return Find(l,r,Node[here].nxr);
else return LCA(Find(l,(Node[here].l+Node[here].r)/2,Node[here].nxl),Find((Node[here].l+Node[here].r)/2+1,r,Node[here].nxr));
}
int main()
{
int N,M,Q,i,j,a,b,l,r,t,ok=0,tt,ll,rr;
scanf("%d %d %d",&N,&M,&Q);
for(i=1;i<N;i++)
{
scanf("%d %d",&a,&b);
Next[a].push_back(b);
Next[b].push_back(a);
}
F(1,-1,0);
for(i=1;i<=M;i++) scanf("%d",&all[i]);
build(1,M,0);
while(Q--)
{
scanf("%d",&t);
if(t==1)
{
scanf("%d %d",&l,&r);
all[l]=r;
cha(l,r,0);
}
else
{
scanf("%d %d %d",&l,&r,&t);
ok=0;
for(i=l;i<=r;i++)
{
if(all[i]==t)
{
printf("%d %d\n",i,i);
ok=1;
break;
}
}
if(!ok)
{
ll=l;
rr=l;
now=all[l];
while(rr<r)
{
rr++;
now=LCA(now,all[rr]);
if(now==t)
{
printf("%d %d\n",ll,rr);
ok=1;
break;
}
while(ll<rr&°[now]<=deg[t])
{
ll++;
now=Find(ll,rr,0);
}
}
}
if(!ok) printf("-1 -1\n");
}
}
return 0;
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/
컴파일 시 표준 에러 (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... |