Submission #422237

#TimeUsernameProblemLanguageResultExecution timeMemory
422237Runtime_error_Birthday gift (IZhO18_treearray)C++14
100 / 100
1822 ms83372 KiB

#include <bits/stdc++.h>
#define pb push_back
#define pi pair<int,int>
using namespace std;
const int inf = 2e5+9,lg = 19;
int n,m,q,a[inf],par[lg][inf],level[inf];
vector<int> adj[inf];
set<int> ans[inf],idx[inf];

void dfs(int u,int p){
    level[u] = level[p]+1;
    par[0][u] = p;
    for(int j=1;j<lg;j++)
        par[j][u] = par[j-1][ par[j-1][u] ];
    for(auto v:adj[u])
        if(v != p)
            dfs(v,u);
}

int LCA(int u,int v){
    if(level[v] < level[u])
        swap(u,v);
    int d = level[v] - level[u];
    for(int j=lg-1;j>=0;j--)
        if(d & (1<<j))
            v = par[j][v];
    if(u == v)return u;
    for(int j=lg-1;j>=0;j--)
        if(par[j][v] != par[j][u])
            v = par[j][v],u = par[j][u];
    return (u == v?v:par[0][v]);
}

int main(){

    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);
    }
    dfs(1,0);

    for(int i=1;i<=m;i++){
        scanf("%d",a+i);
        if(i>1)
            ans[ LCA(a[i-1],a[i]) ].insert(i-1);
        idx[a[i]].insert(i);
    }

     while(q--){
        int type,i,v,l,r;
        scanf("%d",&type);
        if(type == 1){
            scanf("%d%d",&i,&v);
            if(i<m){
                ans[ LCA(a[i],a[i+1]) ].erase(i);
                ans[ LCA(v,a[i+1]) ].insert(i);
            }
            if(i>1){
                ans[ LCA(a[i-1],a[i]) ].erase(i-1);
                ans[ LCA(a[i-1],v) ].insert(i-1);
            }
            idx[a[i]].erase(i);
            a[i] = v;
            idx[a[i]].insert(i);
        }
        else {
            scanf("%d%d%d",&l,&r,&v);
            set<int> ::iterator it;
            if(idx[v].size() > 0 && *idx[v].rbegin() >= l){
                it = idx[v].lower_bound(l);
                if(*it <= r){
                    printf("%d %d\n",*it,*it);
                    continue;
                }
            }
            if(ans[v].size() >0 && *ans[v].rbegin() >=l){
                it = ans[v].lower_bound(l);
                if(*it < r){
                    printf("%d %d\n",*it,*it+1);
                    continue;
                }
            }
            puts("-1 -1");
        }
    }
}
/*
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
*/

Compilation message (stderr)

treearray.cpp: In function 'int main()':
treearray.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     scanf("%d%d%d",&n,&m,&q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
treearray.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         scanf("%d%d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~
treearray.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         scanf("%d",a+i);
      |         ~~~~~^~~~~~~~~~
treearray.cpp:55:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |         scanf("%d",&type);
      |         ~~~~~^~~~~~~~~~~~
treearray.cpp:57:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |             scanf("%d%d",&i,&v);
      |             ~~~~~^~~~~~~~~~~~~~
treearray.cpp:71:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |             scanf("%d%d%d",&l,&r,&v);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...