답안 #1098378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1098378 2024-10-09T10:54:50 Z thangdz2k7 Long Mansion (JOI17_long_mansion) C++17
100 / 100
748 ms 109892 KB
// author : thembululquaUwU
// 3.9.2024

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'

using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;

const int N = 5e5 + 5;
const int mod = 1e9 + 7;

void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}

int n, q, c[N], pos[N], le[N], ri[N];
vector <int> keys[N], event[N];

struct Segtree{

    int Min[N << 2], inf;

    void rz() {
        for (int i = 1; i <= 4 * n; ++ i) Min[i] = inf;
    }

    void fix(int u, int val, int s = 1, int l = 1, int r = n){
        if (l == r){
            Min[s] = val; return;
        }

        int mid = l + r >> 1;
        if (mid >= u) fix(u, val, s << 1, l, mid);
        else fix(u, val, s << 1 | 1, mid + 1, r);
        if (inf == mod) Min[s] = min(Min[s << 1], Min[s << 1 | 1]);
        else Min[s] = max(Min[s << 1], Min[s << 1 | 1]);
    }

    int get(int u, int v, int s = 1, int l = 1, int r = n){
        if (u > r || v < l) return inf;
        if (u <= l && r <= v) return Min[s];

        int mid = l + r >> 1;
        if (inf == mod) return min(get(u, v, s << 1, l, mid), get(u, v, s << 1 | 1, mid + 1, r));
        return max(get(u, v, s << 1, l, mid), get(u, v, s << 1 | 1, mid + 1, r));
    }
} T1, T2, T3, T4;

int used[N], g[N], h[N];

void solve_left(){
    for (int i = 1; i <= n; ++ i){
        event[ri[i]].pb(i); if (i > 1) T1.fix(i, i);
        for (auto k : event[i]) T1.fix(k, mod);
        if (le[i] < i) T2.fix(i, T1.get(le[i] + 1, i));
        event[i].clear(); used[i] = 0;
    }

    for (int i = 1; i <= n; ++ i){
        for (auto k : keys[i]) used[k] = 1;
        if (i < n && !used[c[i]]) g[i] = i;
    }
    g[n + 1] = n + 1;
    for (int i = n; i >= 1; -- i) if (!g[i]) g[i] = g[i + 1];
}

void solve_right(){
    for (int i = n; i >= 1; -- i){
        event[le[i]].pb(i); if (i < n) T3.fix(i, i);
        for (auto k : event[i]) T3.fix(k, -mod);
        if (i < ri[i]) T4.fix(i, T3.get(i, ri[i] - 1));
        event[i].clear(); used[i] = 0;
    }

    for (int i = n; i >= 1; -- i){
        for (auto k : keys[i]) used[k] = 1;
        if (i > 1 && !used[c[i - 1]]) h[i] = i;
    }
    h[0] = 0;
    for (int i = 1; i <= n; ++ i) if (!h[i]) h[i] = h[i - 1];
}

void solve(){
    cin >> n; T1.inf = T2.inf = mod; T3.inf = T4.inf = -mod;
    T1.rz(); T2.rz(); T3.rz(); T4.rz();
    for (int i = 1; i < n; ++ i) cin >> c[i];
    for (int i = 1; i <= n; ++ i){
        int b; cin >> b;
        while (b --){
            int a; cin >> a; keys[i].pb(a);
        }
    }

    for (int i = 1; i <= n; ++ i) pos[i] = n + 1;
    for (int i = n; i >= 1; -- i){
        for (auto k : keys[i]) pos[k] = i;
        if (i > 1) ri[i] = pos[c[i - 1]]; else ri[i] = n + 1;
    }

    for (int i = 1; i <= n; ++ i) pos[i] = 0;
    for (int i = 1; i <= n; ++ i){
        for (auto k : keys[i]) pos[k] = i;
        if (i < n) le[i] = pos[c[i]]; else le[i] = 0;
    }

    solve_left(); solve_right();

    cin >> q;
    while (q --){
        int l, r; cin >> l >> r;
        if (l < r){
            if (T2.get(l, r - 1) <= l || g[l] < r) cout << "NO" << endl;
            else cout << "YES" << endl;
        }
        else {
            if (T4.get(r + 1, l) >= l || h[l] > r) cout << "NO" << endl;
            else cout << "YES" << endl;
        }
    }
}

int main(){
    if (fopen("pqh.inp", "r")){
        freopen("pqh.inp", "r", stdin);
        freopen("pqh.out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t = 1; // cin >> t;
    while (t --) solve();
    return 0;
}

Compilation message

long_mansion.cpp:18:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   18 | void maxl(auto &a, auto b) {a = max(a, b);}
      |           ^~~~
long_mansion.cpp:18:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   18 | void maxl(auto &a, auto b) {a = max(a, b);}
      |                    ^~~~
long_mansion.cpp:19:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   19 | void minl(auto &a, auto b) {a = min(a, b);}
      |           ^~~~
long_mansion.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   19 | void minl(auto &a, auto b) {a = min(a, b);}
      |                    ^~~~
long_mansion.cpp: In member function 'void Segtree::fix(int, int, int, int, int)':
long_mansion.cpp:37:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |         int mid = l + r >> 1;
      |                   ~~^~~
long_mansion.cpp: In member function 'int Segtree::get(int, int, int, int, int)':
long_mansion.cpp:48:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   48 |         int mid = l + r >> 1;
      |                   ~~^~~
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         freopen("pqh.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         freopen("pqh.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24156 KB Output is correct
2 Correct 12 ms 25456 KB Output is correct
3 Correct 17 ms 24664 KB Output is correct
4 Correct 8 ms 24408 KB Output is correct
5 Correct 8 ms 25432 KB Output is correct
6 Correct 8 ms 25432 KB Output is correct
7 Correct 8 ms 25432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24156 KB Output is correct
2 Correct 12 ms 25456 KB Output is correct
3 Correct 17 ms 24664 KB Output is correct
4 Correct 8 ms 24408 KB Output is correct
5 Correct 8 ms 25432 KB Output is correct
6 Correct 8 ms 25432 KB Output is correct
7 Correct 8 ms 25432 KB Output is correct
8 Correct 137 ms 31312 KB Output is correct
9 Correct 139 ms 31252 KB Output is correct
10 Correct 152 ms 43604 KB Output is correct
11 Correct 151 ms 46180 KB Output is correct
12 Correct 132 ms 30804 KB Output is correct
13 Correct 111 ms 43504 KB Output is correct
14 Correct 115 ms 31512 KB Output is correct
15 Correct 119 ms 31424 KB Output is correct
16 Correct 118 ms 43372 KB Output is correct
17 Correct 111 ms 43600 KB Output is correct
18 Correct 151 ms 43604 KB Output is correct
19 Correct 120 ms 31316 KB Output is correct
20 Correct 134 ms 30204 KB Output is correct
21 Correct 129 ms 30032 KB Output is correct
22 Correct 122 ms 30288 KB Output is correct
23 Correct 162 ms 31060 KB Output is correct
24 Correct 111 ms 31240 KB Output is correct
25 Correct 124 ms 31316 KB Output is correct
26 Correct 119 ms 33120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 318 ms 45652 KB Output is correct
2 Correct 327 ms 59384 KB Output is correct
3 Correct 300 ms 52876 KB Output is correct
4 Correct 339 ms 54656 KB Output is correct
5 Correct 313 ms 48220 KB Output is correct
6 Correct 213 ms 47440 KB Output is correct
7 Correct 240 ms 47152 KB Output is correct
8 Correct 240 ms 47276 KB Output is correct
9 Correct 247 ms 47908 KB Output is correct
10 Correct 268 ms 47808 KB Output is correct
11 Correct 281 ms 47700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24156 KB Output is correct
2 Correct 12 ms 25456 KB Output is correct
3 Correct 17 ms 24664 KB Output is correct
4 Correct 8 ms 24408 KB Output is correct
5 Correct 8 ms 25432 KB Output is correct
6 Correct 8 ms 25432 KB Output is correct
7 Correct 8 ms 25432 KB Output is correct
8 Correct 137 ms 31312 KB Output is correct
9 Correct 139 ms 31252 KB Output is correct
10 Correct 152 ms 43604 KB Output is correct
11 Correct 151 ms 46180 KB Output is correct
12 Correct 132 ms 30804 KB Output is correct
13 Correct 111 ms 43504 KB Output is correct
14 Correct 115 ms 31512 KB Output is correct
15 Correct 119 ms 31424 KB Output is correct
16 Correct 118 ms 43372 KB Output is correct
17 Correct 111 ms 43600 KB Output is correct
18 Correct 151 ms 43604 KB Output is correct
19 Correct 120 ms 31316 KB Output is correct
20 Correct 134 ms 30204 KB Output is correct
21 Correct 129 ms 30032 KB Output is correct
22 Correct 122 ms 30288 KB Output is correct
23 Correct 162 ms 31060 KB Output is correct
24 Correct 111 ms 31240 KB Output is correct
25 Correct 124 ms 31316 KB Output is correct
26 Correct 119 ms 33120 KB Output is correct
27 Correct 318 ms 45652 KB Output is correct
28 Correct 327 ms 59384 KB Output is correct
29 Correct 300 ms 52876 KB Output is correct
30 Correct 339 ms 54656 KB Output is correct
31 Correct 313 ms 48220 KB Output is correct
32 Correct 213 ms 47440 KB Output is correct
33 Correct 240 ms 47152 KB Output is correct
34 Correct 240 ms 47276 KB Output is correct
35 Correct 247 ms 47908 KB Output is correct
36 Correct 268 ms 47808 KB Output is correct
37 Correct 281 ms 47700 KB Output is correct
38 Correct 730 ms 95308 KB Output is correct
39 Correct 748 ms 109684 KB Output is correct
40 Correct 587 ms 82188 KB Output is correct
41 Correct 640 ms 109892 KB Output is correct
42 Correct 219 ms 51664 KB Output is correct
43 Correct 238 ms 49096 KB Output is correct
44 Correct 300 ms 66644 KB Output is correct
45 Correct 319 ms 66512 KB Output is correct
46 Correct 299 ms 66388 KB Output is correct
47 Correct 229 ms 49208 KB Output is correct
48 Correct 237 ms 48976 KB Output is correct
49 Correct 273 ms 67668 KB Output is correct
50 Correct 326 ms 66644 KB Output is correct
51 Correct 303 ms 66896 KB Output is correct
52 Correct 348 ms 66216 KB Output is correct
53 Correct 432 ms 83380 KB Output is correct
54 Correct 558 ms 101708 KB Output is correct
55 Correct 401 ms 84832 KB Output is correct