답안 #883845

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883845 2023-12-06T07:54:12 Z vjudge1 Passport (JOI23_passport) C++17
6 / 100
1687 ms 123740 KB
/* Author : Mychecksdead  */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 8e5+100, M = 1e5+10, K = 21;
 
struct Data{
  int L, R;
};
 
int n, q, L[N][K], R[N][K];
Data up[N][K];
 
void build(int l, int r, int k, int bit){
  if(l == r){
    L[k][bit] = up[l][bit].L;
    R[k][bit] = up[l][bit].R;
    return;
  }
  int mid = l+r>>1;
  build(l, mid, k<<1, bit);
  build(mid+1, r, k<<1|1, bit);
  L[k][bit] = min(L[k<<1][bit], L[k<<1|1][bit]);
  R[k][bit] = max(R[k<<1][bit], R[k<<1|1][bit]);
}


 
int getL(int l, int r, int ql, int qr, int k, int bit){
  if(ql > r || l > qr) return MOD;
  if(ql <= l && r <= qr) return L[k][bit];
  int mid = l+r>>1;
  return min(getL(l, mid, ql, qr, k<<1, bit), getL(mid+1, r, ql, qr, k<<1|1, bit));
}
int getR(int l, int r, int ql, int qr, int k, int bit){
  if(ql > r || l > qr) return 0;
  if(ql <= l && r <= qr) return R[k][bit];
  int mid = l+r>>1;
  return max(getR(l, mid, ql, qr, k<<1, bit), getR(mid+1, r, ql, qr, k<<1|1, bit));
}
 
void solve(){
  cin >> n;
  for(int i = 1; i <= n; ++i){
    int l, r; cin >> l >> r;
    up[i][0] = {l, r};
  }
  build(1, n, 1, 0);
  for(int j = 1; j < K; ++j){
    for(int i = 1; i <= n; ++i){
      up[i][j] = {getL(1, n, up[i][j - 1].L, up[i][j - 1].R, 1, j - 1), getR(1, n, up[i][j - 1].L, up[i][j - 1].R, 1, j - 1)};
    }
    build(1, n, 1, j);
  }
  cin >> q;
  for(int i = 1; i <= q; ++i){
    int l; cin >> l;
    Data x = {l, l};
    bool ok = 0;
    int ans = 0;
    for(int j = K - 1; j >= 0; --j){
      Data u = {getL(1, n, x.L, x.R, 1, j), getR(1, n, x.L, x.R, 1, j)};
      if(u.L == 1 && u.R == n){
        ok = 1;
      }else{
        x = u;
        ans += (1<<j);
      }
      // cout << x.L << ' ' << x.R << '\n';
    }
    if(!ok) cout << "-1\n";
    else{
      Data u = {getL(1, n, x.L, x.R, 1, 0), getR(1, n, x.L, x.R, 1, 0)};
      assert(u.L == 1 && u.R == n);
      cout << ans + 1 << '\n';
    }
  }
}
 
 
int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tt = 1, aa;
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  while(tt--){
    solve();
  }
  cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
  return 0;
} 

Compilation message

passport.cpp: In function 'void build(int, int, int, int)':
passport.cpp:25:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |   int mid = l+r>>1;
      |             ~^~
passport.cpp: In function 'int getL(int, int, int, int, int, int)':
passport.cpp:37:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |   int mid = l+r>>1;
      |             ~^~
passport.cpp: In function 'int getR(int, int, int, int, int, int)':
passport.cpp:43:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |   int mid = l+r>>1;
      |             ~^~
passport.cpp: In function 'int main()':
passport.cpp:88:15: warning: unused variable 'aa' [-Wunused-variable]
   88 |   int tt = 1, aa;
      |               ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 485 ms 123736 KB Output is correct
5 Correct 1189 ms 123740 KB Output is correct
6 Correct 1687 ms 123736 KB Output is correct
7 Correct 263 ms 123732 KB Output is correct
8 Correct 771 ms 117580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Incorrect 1 ms 4444 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Incorrect 1 ms 4444 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Incorrect 1 ms 4444 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 485 ms 123736 KB Output is correct
5 Correct 1189 ms 123740 KB Output is correct
6 Correct 1687 ms 123736 KB Output is correct
7 Correct 263 ms 123732 KB Output is correct
8 Correct 771 ms 117580 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Incorrect 1 ms 4444 KB Output isn't correct
16 Halted 0 ms 0 KB -