답안 #1081802

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1081802 2024-08-30T11:14:30 Z Zero_OP Long Mansion (JOI17_long_mansion) C++14
100 / 100
172 ms 54356 KB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;

template<typename T> 
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }  return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){    
        if(a < b){
            return a = b, true;
        } return false;
    }

template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
 
template<typename T>
struct tensor<1, T> : public vector<T> {
    tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};

const int MAX = 5e5 + 5;

int n, q, k[MAX], c[MAX], lastOcc[MAX], requireLeft[MAX], requireRight[MAX], l[MAX], r[MAX];
vector<int> a[MAX];

bool can_extend(int l, int r, int pos){
    return (requireLeft[pos] != 0 && l <= requireLeft[pos] && requireLeft[pos] <= r) || (requireRight[pos] != n + 1 && l <= requireRight[pos] && requireRight[pos] <= r);
}

void testcase(){
    cin >> n;
    rep(i, 1, n) cin >> c[i];

    rep(i, 1, n + 1){
        cin >> k[i];
        a[i].resize(k[i]);
        rep(j, 0, k[i]){
            cin >> a[i][j];
        }
    }

    rep(i, 1, n){
        rep(j, 0, k[i]){
            lastOcc[a[i][j]] = i;
        }

        requireLeft[i] = lastOcc[c[i]]; //from i + 1 to i
    }

    rep(i, 1, n + 1) lastOcc[i] = n + 1;
    requireRight[n] = n + 1;

    for(int i = n; i > 1; --i){
        rep(j, 0, k[i]){
            lastOcc[a[i][j]] = i;
        }

        requireRight[i - 1] = lastOcc[c[i - 1]]; //from i to i + 1
    }

    rep(i, 1, n + 1){
        int l = i, r = i;
        while(true){
            //can extend
            if(l > 1 && can_extend(l, r, l - 1)){
                --l;
                maximize(r, ::r[l]);
                minimize(l, ::l[l]);
            } else if(r < n && can_extend(l, r, r)){
                ++r;
            } else break;
        }
        ::l[i] = l;
        ::r[i] = r;
    }



    cin >> q;
    rep(_, 0, q){
        int x, y;
        cin >> x >> y;
        if(l[x] <= y && y <= r[x]){
            cout << "YES\n";
        } else cout << "NO\n";
    }
}


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    file("task");

    int T = 1;
    // cin >> T;
    while(T--){
        testcase();
    }

    return 0;
}

Compilation message

long_mansion.cpp: In function 'int main()':
long_mansion.cpp:9:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:116:5: note: in expansion of macro 'file'
  116 |     file("task");
      |     ^~~~
long_mansion.cpp:9:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:116:5: note: in expansion of macro 'file'
  116 |     file("task");
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12380 KB Output is correct
2 Correct 6 ms 12380 KB Output is correct
3 Correct 7 ms 12636 KB Output is correct
4 Correct 6 ms 12376 KB Output is correct
5 Correct 6 ms 12380 KB Output is correct
6 Correct 6 ms 12376 KB Output is correct
7 Correct 6 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12380 KB Output is correct
2 Correct 6 ms 12380 KB Output is correct
3 Correct 7 ms 12636 KB Output is correct
4 Correct 6 ms 12376 KB Output is correct
5 Correct 6 ms 12380 KB Output is correct
6 Correct 6 ms 12376 KB Output is correct
7 Correct 6 ms 12380 KB Output is correct
8 Correct 64 ms 18260 KB Output is correct
9 Correct 65 ms 18004 KB Output is correct
10 Correct 62 ms 18428 KB Output is correct
11 Correct 61 ms 18832 KB Output is correct
12 Correct 58 ms 17744 KB Output is correct
13 Correct 59 ms 18336 KB Output is correct
14 Correct 61 ms 18256 KB Output is correct
15 Correct 60 ms 18256 KB Output is correct
16 Correct 60 ms 18256 KB Output is correct
17 Correct 56 ms 18448 KB Output is correct
18 Correct 58 ms 18488 KB Output is correct
19 Correct 57 ms 18260 KB Output is correct
20 Correct 58 ms 18256 KB Output is correct
21 Correct 56 ms 18256 KB Output is correct
22 Correct 55 ms 18256 KB Output is correct
23 Correct 61 ms 18164 KB Output is correct
24 Correct 61 ms 18148 KB Output is correct
25 Correct 61 ms 18000 KB Output is correct
26 Correct 70 ms 18176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 26960 KB Output is correct
2 Correct 104 ms 26964 KB Output is correct
3 Correct 103 ms 26704 KB Output is correct
4 Correct 104 ms 26980 KB Output is correct
5 Correct 107 ms 27000 KB Output is correct
6 Correct 91 ms 26196 KB Output is correct
7 Correct 81 ms 25936 KB Output is correct
8 Correct 89 ms 25940 KB Output is correct
9 Correct 88 ms 25940 KB Output is correct
10 Correct 85 ms 25936 KB Output is correct
11 Correct 86 ms 25888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12380 KB Output is correct
2 Correct 6 ms 12380 KB Output is correct
3 Correct 7 ms 12636 KB Output is correct
4 Correct 6 ms 12376 KB Output is correct
5 Correct 6 ms 12380 KB Output is correct
6 Correct 6 ms 12376 KB Output is correct
7 Correct 6 ms 12380 KB Output is correct
8 Correct 64 ms 18260 KB Output is correct
9 Correct 65 ms 18004 KB Output is correct
10 Correct 62 ms 18428 KB Output is correct
11 Correct 61 ms 18832 KB Output is correct
12 Correct 58 ms 17744 KB Output is correct
13 Correct 59 ms 18336 KB Output is correct
14 Correct 61 ms 18256 KB Output is correct
15 Correct 60 ms 18256 KB Output is correct
16 Correct 60 ms 18256 KB Output is correct
17 Correct 56 ms 18448 KB Output is correct
18 Correct 58 ms 18488 KB Output is correct
19 Correct 57 ms 18260 KB Output is correct
20 Correct 58 ms 18256 KB Output is correct
21 Correct 56 ms 18256 KB Output is correct
22 Correct 55 ms 18256 KB Output is correct
23 Correct 61 ms 18164 KB Output is correct
24 Correct 61 ms 18148 KB Output is correct
25 Correct 61 ms 18000 KB Output is correct
26 Correct 70 ms 18176 KB Output is correct
27 Correct 103 ms 26960 KB Output is correct
28 Correct 104 ms 26964 KB Output is correct
29 Correct 103 ms 26704 KB Output is correct
30 Correct 104 ms 26980 KB Output is correct
31 Correct 107 ms 27000 KB Output is correct
32 Correct 91 ms 26196 KB Output is correct
33 Correct 81 ms 25936 KB Output is correct
34 Correct 89 ms 25940 KB Output is correct
35 Correct 88 ms 25940 KB Output is correct
36 Correct 85 ms 25936 KB Output is correct
37 Correct 86 ms 25888 KB Output is correct
38 Correct 164 ms 48724 KB Output is correct
39 Correct 172 ms 54356 KB Output is correct
40 Correct 155 ms 41808 KB Output is correct
41 Correct 155 ms 52660 KB Output is correct
42 Correct 93 ms 27216 KB Output is correct
43 Correct 102 ms 27216 KB Output is correct
44 Correct 117 ms 35688 KB Output is correct
45 Correct 137 ms 35812 KB Output is correct
46 Correct 124 ms 35920 KB Output is correct
47 Correct 92 ms 27220 KB Output is correct
48 Correct 95 ms 26960 KB Output is correct
49 Correct 120 ms 35768 KB Output is correct
50 Correct 122 ms 35668 KB Output is correct
51 Correct 122 ms 35920 KB Output is correct
52 Correct 115 ms 34640 KB Output is correct
53 Correct 140 ms 42064 KB Output is correct
54 Correct 163 ms 49984 KB Output is correct
55 Correct 136 ms 42324 KB Output is correct