Submission #1283771

#TimeUsernameProblemLanguageResultExecution timeMemory
1283771Faisal_SaqibBirthday gift (IZhO18_treearray)C++20
0 / 100
3 ms24120 KiB
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+100,lg=18;
vector<int> ma[N];
int a[N],h[N],par[N][lg];
void dfs(int x,int p=0)
{
    par[x][0]=p;
    for(int j=1;j<lg;j++)
    {
        par[x][j]=par[par[x][j-1]][j-1];
    }
    for(auto y:ma[x])
    {
        if(y!=p){h[y]=h[x]+1,dfs(y,x);}
    }
}
int lca(int x,int y)
{
    if(x==0)return y;
    if(h[x]<h[y])swap(x,y);
    for(int j=lg-1;j>=0;j--)
    {
        if(h[par[x][j]]>=h[y])
        {
            x=par[x][j];
        }
    }
    if(x==y)return x;
    for(int j=lg-1;j>=0;j--)
    {
        if(par[x][j]!=par[y][j])
        {
            x=par[x][j];
        }
    }
    return par[x][0];
}
int main()
{
    ios::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    int n,m,q;
    cin>>n>>m>>q;
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        ma[x].push_back(y);
        ma[y].push_back(x);
    }
    h[1]=1;
    dfs(1);
    for(int i=1;i<=m;i++)
    {
        cin>>a[i];
    }
    while(q--)
    {
        int type;
        cin>>type;
        if(type==1)
        {
            int p,v;
            cin>>p>>v;
            a[p]=v;
        }
        else{
            int l,r,v;
            cin>>l>>r>>v;
            bool tl=0;
            for(int p=l;p<=r and !tl;p++)
            {
                int lc=a[p];
                for(int np=p;np<=r;np++)
                {
                    lc=lca(lc,a[np]);
                    if(lc==v)
                    {
                        tl=1;
                        cout<<p<<' '<<np<<endl;
                        break;
                    }
                }
                // p=np;
            }
            if(!tl)
                cout<<-1<<' '<<-1<<endl;
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...