답안 #296300

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
296300 2020-09-10T13:20:51 Z BThero Long Mansion (JOI17_long_mansion) C++17
25 / 100
3000 ms 88624 KB
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = (int)5e5 + 5;

vector<int> group[MAXN];
vector<int> keys[MAXN];
int betw[MAXN];
int A[MAXN], B[MAXN];
int L[MAXN], R[MAXN];
int n, q;

void calcB() {
  B[0] = n + 1;
  
  for (int i = 1; i <= n; ++i) {
    group[i].clear();
    B[i] = n + 1;
  }
  
  for (int i = 2; i <= n; ++i) {
    group[betw[i - 1]].pb(i - 1);
    
    for (int x : keys[i]) {
      for (int y : group[x]) {
        B[y] = i;
      }
      
      group[x].clear();
    }
  }
}

void calcA() {
  A[n + 1] = 0;
  
  for (int i = 1; i <= n; ++i) {
    group[i].clear();
    A[i] = 0;
  }
  
  for (int i = n - 1; i > 0; --i) {
    group[betw[i]].pb(i + 1);
    
    for (int x : keys[i]) {
      for (int y : group[x]) {
        A[y] = i;
      }
      
      group[x].clear();
    }
  }
}

void calcR() {
  for (int i = 1; i <= n; ++i) {
    R[i] = n + 1;
  }
  
  for (int i = 0; i <= n + 1; ++i) {
    group[i].clear();
  }
  
  for (int i = 1; i <= n; ++i) {
    group[B[i - 1]].pb(i);
  }
  
  set<int> S, T;
  
  for (int i = 1; i <= n + 1; ++i) {
    S.insert(i);
    T.insert(i);
  }
  
  for (int r = 1; r <= n; ++r) {
    for (int x : group[r]) {
      S.erase(x);
    }
  
    // A[r + 1] < l
    int l = A[r + 1] + 1;
    l = *S.lower_bound(l);
    // B[l - 1] > r
    
    auto it = T.lower_bound(l);
    
    while (*it <= r) {
      R[*it] = r;
      it = T.erase(it);
    }
  }
}

void calcL() {
  for (int i = 1; i <= n; ++i) {
    L[i] = 0;
  }
  
  for (int l = n; l > 0; --l) {
    // B[l - 1] > r
    int r = B[l - 1] - 1;
    // A[r + 1] < l
    
    while (l <= r && A[r + 1] >= l) {
      --r;
    }
    
    for (int i = l; i <= r; ++i) {
      L[i] = max(L[i], l);
    }
  }
}

void solve() {
  scanf("%d", &n);
  
  for (int i = 1; i < n; ++i) {
    scanf("%d", &betw[i]);
  }
  
  for (int i = 1; i <= n; ++i) {
    int sz;
    scanf("%d", &sz);
    keys[i].resize(sz);
    
    for (int j = 0; j < sz; ++j) {
      scanf("%d", &keys[i][j]);
    }
  }
  
  calcB();
  calcA();
  calcR();
  calcL();
  
  scanf("%d", &q);
  
  for (int i = 1; i <= q; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    // a -> b
    bool ans = 1;
    
    if (a < b) {
      if (R[a] < b) {
        ans = 0;
      }
    }
    else {
      if (L[a] > b) {
        ans = 0;
      }
    }
    
    if (ans) {
      printf("YES\n");
    }
    else {
      printf("NO\n");
    }
  }
}

int main() {
  int tt = 1;
  
  while (tt--) {
    solve();
  }

  return 0;
}

Compilation message

long_mansion.cpp: In function 'void solve()':
long_mansion.cpp:128:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  128 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
long_mansion.cpp:131:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  131 |     scanf("%d", &betw[i]);
      |     ~~~~~^~~~~~~~~~~~~~~~
long_mansion.cpp:136:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  136 |     scanf("%d", &sz);
      |     ~~~~~^~~~~~~~~~~
long_mansion.cpp:140:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  140 |       scanf("%d", &keys[i][j]);
      |       ~~~~~^~~~~~~~~~~~~~~~~~~
long_mansion.cpp:149:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  149 |   scanf("%d", &q);
      |   ~~~~~^~~~~~~~~~
long_mansion.cpp:153:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  153 |     scanf("%d %d", &a, &b);
      |     ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24192 KB Output is correct
2 Correct 22 ms 24320 KB Output is correct
3 Correct 23 ms 24704 KB Output is correct
4 Correct 20 ms 24192 KB Output is correct
5 Correct 22 ms 24192 KB Output is correct
6 Correct 20 ms 24192 KB Output is correct
7 Correct 21 ms 24192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24192 KB Output is correct
2 Correct 22 ms 24320 KB Output is correct
3 Correct 23 ms 24704 KB Output is correct
4 Correct 20 ms 24192 KB Output is correct
5 Correct 22 ms 24192 KB Output is correct
6 Correct 20 ms 24192 KB Output is correct
7 Correct 21 ms 24192 KB Output is correct
8 Correct 182 ms 25720 KB Output is correct
9 Correct 178 ms 25720 KB Output is correct
10 Correct 188 ms 26104 KB Output is correct
11 Correct 181 ms 26488 KB Output is correct
12 Correct 176 ms 25720 KB Output is correct
13 Correct 173 ms 25980 KB Output is correct
14 Correct 178 ms 25976 KB Output is correct
15 Correct 170 ms 25976 KB Output is correct
16 Correct 158 ms 26232 KB Output is correct
17 Correct 172 ms 25848 KB Output is correct
18 Correct 171 ms 25976 KB Output is correct
19 Correct 172 ms 25976 KB Output is correct
20 Correct 163 ms 26232 KB Output is correct
21 Correct 159 ms 26232 KB Output is correct
22 Correct 172 ms 25976 KB Output is correct
23 Correct 170 ms 25848 KB Output is correct
24 Correct 173 ms 25876 KB Output is correct
25 Correct 169 ms 25848 KB Output is correct
26 Correct 180 ms 25720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 370 ms 41888 KB Output is correct
2 Correct 373 ms 42104 KB Output is correct
3 Correct 372 ms 42080 KB Output is correct
4 Correct 379 ms 41984 KB Output is correct
5 Correct 359 ms 41976 KB Output is correct
6 Correct 353 ms 42744 KB Output is correct
7 Correct 322 ms 43260 KB Output is correct
8 Correct 302 ms 43384 KB Output is correct
9 Correct 315 ms 43384 KB Output is correct
10 Correct 308 ms 43384 KB Output is correct
11 Correct 323 ms 43436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24192 KB Output is correct
2 Correct 22 ms 24320 KB Output is correct
3 Correct 23 ms 24704 KB Output is correct
4 Correct 20 ms 24192 KB Output is correct
5 Correct 22 ms 24192 KB Output is correct
6 Correct 20 ms 24192 KB Output is correct
7 Correct 21 ms 24192 KB Output is correct
8 Correct 182 ms 25720 KB Output is correct
9 Correct 178 ms 25720 KB Output is correct
10 Correct 188 ms 26104 KB Output is correct
11 Correct 181 ms 26488 KB Output is correct
12 Correct 176 ms 25720 KB Output is correct
13 Correct 173 ms 25980 KB Output is correct
14 Correct 178 ms 25976 KB Output is correct
15 Correct 170 ms 25976 KB Output is correct
16 Correct 158 ms 26232 KB Output is correct
17 Correct 172 ms 25848 KB Output is correct
18 Correct 171 ms 25976 KB Output is correct
19 Correct 172 ms 25976 KB Output is correct
20 Correct 163 ms 26232 KB Output is correct
21 Correct 159 ms 26232 KB Output is correct
22 Correct 172 ms 25976 KB Output is correct
23 Correct 170 ms 25848 KB Output is correct
24 Correct 173 ms 25876 KB Output is correct
25 Correct 169 ms 25848 KB Output is correct
26 Correct 180 ms 25720 KB Output is correct
27 Correct 370 ms 41888 KB Output is correct
28 Correct 373 ms 42104 KB Output is correct
29 Correct 372 ms 42080 KB Output is correct
30 Correct 379 ms 41984 KB Output is correct
31 Correct 359 ms 41976 KB Output is correct
32 Correct 353 ms 42744 KB Output is correct
33 Correct 322 ms 43260 KB Output is correct
34 Correct 302 ms 43384 KB Output is correct
35 Correct 315 ms 43384 KB Output is correct
36 Correct 308 ms 43384 KB Output is correct
37 Correct 323 ms 43436 KB Output is correct
38 Execution timed out 3066 ms 88624 KB Time limit exceeded
39 Halted 0 ms 0 KB -