답안 #89726

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
89726 2018-12-18T08:31:25 Z mirbek01 Birthday gift (IZhO18_treearray) C++17
0 / 100
22 ms 24064 KB
# include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 2;

int n, m, q, a[N];
int p[20][N], tin[N], tout[N], timer;
vector <int> g[N];
set <int> s[N], ps[N];
set <int> :: iterator it;

void dfs(int v, int pr = 0){
      tin[v] = ++ timer;
      p[0][v] = pr;
      for(int i = 1; i <= 19; i ++)
            p[i][v] = p[i - 1][ p[i - 1][v] ];
      for(int to : g[v]){
            if(to == pr)
                  continue;
            dfs(to, v);
      }
      tout[v] = timer;
}

bool upper(int a, int b){
      return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int lca(int a, int b){
      if(upper(a, b))
            return a;
      if(upper(b, a))
            return b;
      for(int i = 19; i >= 0; i --){
            if(p[i][a] && !upper(p[i][a], b))
                  a = p[i][a];
      }
      return p[0][a];
}

int main(){
      scanf("%d %d %d", &n, &m, &q);

      for(int i = 1; i < n; i ++){
            int u, v;
            scanf("%d %d", &u, &v);
            g[u].push_back(v);
            g[v].push_back(u);
      }

      dfs(1);

      for(int i = 1; i <= m; i ++){
            scanf("%d", a + i);
            if(i > 1){
                  int lc = lca(a[i - 1], a[i]);
                  s[lc].insert(i - 1);
            }
            ps[a[i]].insert(i);
      }

      while(q --){
            int l, r, v, type;
            scanf("%d", &type);
            scanf("%d %d", &l, &r);
            if(type == 1){
                  ps[ a[l] ].erase(l);
                  ps[ r ].insert(l);
                  if(l + 1 <= m){
                        int lc = lca(a[l], a[l + 1]);
                        s[lc].erase(l);
                        lc = lca(r, a[l + 1]);
                        s[lc].insert(l);
                  }
                  if(l - 1 >= 1){
                        int lc = lca(a[l], a[l - 1]);
                        s[lc].erase(l - 1);
                        lc = lca(r, a[l - 1]);
                        s[lc].insert(l - 1);
                  }
                  a[l] = r;
            } else {
                  scanf("%d", &v);
                  if(l == r){
                        if(a[l] == v){
                              cout << l << " " << l << endl;
                        } else {
                              puts("-1 -1");
                        }
                  } else {
                        if(!ps[v].empty()){
                              it = ps[v].lower_bound(l);
                              if((*it) <= r){
                                    cout << (*it) << " " << (*it) << endl;
                                    continue;
                              }
                        }
                        if(s[v].empty()){
                              puts("-1 -1");
                              continue;
                        }
                        it = s[v].lower_bound(l);
                        if(*it >= r){
                              puts("-1 -1");
                              continue;
                        }
                        cout << *it << " " << (*it) + 1 << endl;
                  }
            }
      }
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:43:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d %d", &n, &m, &q);
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:47:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:55:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", a + i);
             ~~~~~^~~~~~~~~~~~~
treearray.cpp:65:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &type);
             ~~~~~^~~~~~~~~~~~~
treearray.cpp:66:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &l, &r);
             ~~~~~^~~~~~~~~~~~~~~~~
treearray.cpp:84:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
                   scanf("%d", &v);
                   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23928 KB n=5
2 Incorrect 22 ms 24064 KB Wrong output format.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23928 KB n=5
2 Incorrect 22 ms 24064 KB Wrong output format.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23928 KB n=5
2 Incorrect 22 ms 24064 KB Wrong output format.
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 23928 KB n=5
2 Incorrect 22 ms 24064 KB Wrong output format.
3 Halted 0 ms 0 KB -