답안 #529261

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529261 2022-02-22T14:48:16 Z Radman_ Long Mansion (JOI17_long_mansion) C++17
100 / 100
1174 ms 75928 KB
//                  //
//     Radmanシ     //
//                 //
#include <bits/stdc++.h>

using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#define int long long
//typedef long long ll;

typedef pair<int, int> pii;
typedef pair<int, pii> pip;
typedef pair<pii, int> ppi;
typedef pair<pii, pii> ppp;
#define F first
#define S second

#define _ios_ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0);

#define debug(x) cerr << #x << " = " << x << endl
#define debug2(x,y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H) ;
  debug_out(T...);
}
#define debugn(...)  cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)

#define all(x) x.begin(), x.end()

const int MAXN = 5e5+5, MAXSQ = 550, INF = 1e9;
int c[MAXN], mark[MAXN], key[MAXN];
pii ans[MAXN], suf[MAXN], pre[MAXN];
vector<int> a[MAXN], v;
int n;

inline void f(int l, int r, int mid)
{
    for (int i = l; i <= mid; i++)
    {
        suf[i] = {i, mid};
        mark[i] = 0;
    }
    for (int i: v)
        key[i] = 0;
    v.clear();
    int p = mid;
    for (int i = mid; i >= l; i--)
    {
        for (int j: a[i])
        {
            key[j] = 1;
            v.push_back(j);
        }
        while (p < r && key[c[p]])
        {
            p++;
            for (int j: a[p])
            {
                key[j] = 1;
                v.push_back(j);
            }
        }
        suf[i].S = p;
        if (i > l && key[c[i-1]])
            mark[i] = 1;
    }
    for (int i = l+1; i <= mid; i++)
    {
        if (mark[i])
            suf[i] = suf[i-1];
    }
    for (int i = l; i <= mid; i++)
    {
        if (ans[i].S == mid)
            ans[i] = suf[ans[i].F];
    }


    for (int i = mid+1; i <= r; i++)
    {
        pre[i] = {mid+1, i};
        mark[i] = 0;
    }
    for (int i: v)
        key[i] = 0;
    v.clear();
    p = mid+1;
    for (int i = mid+1; i <= r; i++)
    {
        for (int j: a[i])
        {
            key[j] = 1;
            v.push_back(j);
        }
        while (p > l && key[c[p-1]])
        {
            p--;
            for (int j: a[p])
            {
                key[j] = 1;
                v.push_back(j);
            }
        }
        pre[i].F = p;
        if (i < r && key[c[i]])
            mark[i] = 1;
    }
    for (int i = r-1; i > mid; i--)
    {
        if (mark[i])
            pre[i] = pre[i+1];
    }
    for (int i = mid+1; i <= r; i++)
    {
        if (ans[i].F == mid+1)
            ans[i] = pre[ans[i].S];
    }
    return;
}

void solve(int l, int r)
{
    //debug2(l, r);
    if (l == r)
    {
        ans[l] = {l, r};
        return;
    }
    int mid = (r + l) >> 1;
    solve(l, mid);
    solve(mid+1, r);
    f(l, r, mid);
    return;
}

int32_t main()
{
    _ios_
    int n;
    cin >> n;
    for (int i = 1; i < n; i++)
        cin >> c[i];
    for (int i = 1; i <= n; i++)
    {
        int k;
        cin >> k;
        for (int j = 1; j <= k; j++)
        {
            int x;
            cin >> x;
            a[i].push_back(x);
        }
    }
    solve(1, n);
    int q;
    cin >> q;
    for (int i = 1; i <= q; i++)
    {
        int x, y;
        cin >> x >> y;
        if (ans[x].F <= y && y <= ans[x].S)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

/*
5
1 2 3 4
2 2 3
1 1
1 1
1 3
1 4
4
2 4
4 2
1 5
5 3

*/

Compilation message

long_mansion.cpp:9: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    9 | #pragma GCC optimization("O3")
      | 
long_mansion.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   10 | #pragma GCC optimization("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12468 KB Output is correct
2 Correct 15 ms 12556 KB Output is correct
3 Correct 16 ms 12716 KB Output is correct
4 Correct 18 ms 12460 KB Output is correct
5 Correct 17 ms 12364 KB Output is correct
6 Correct 15 ms 12364 KB Output is correct
7 Correct 20 ms 12452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12468 KB Output is correct
2 Correct 15 ms 12556 KB Output is correct
3 Correct 16 ms 12716 KB Output is correct
4 Correct 18 ms 12460 KB Output is correct
5 Correct 17 ms 12364 KB Output is correct
6 Correct 15 ms 12364 KB Output is correct
7 Correct 20 ms 12452 KB Output is correct
8 Correct 731 ms 18296 KB Output is correct
9 Correct 650 ms 18276 KB Output is correct
10 Correct 638 ms 18684 KB Output is correct
11 Correct 666 ms 19280 KB Output is correct
12 Correct 729 ms 17824 KB Output is correct
13 Correct 675 ms 18392 KB Output is correct
14 Correct 668 ms 18352 KB Output is correct
15 Correct 697 ms 18404 KB Output is correct
16 Correct 632 ms 18228 KB Output is correct
17 Correct 651 ms 18328 KB Output is correct
18 Correct 709 ms 18432 KB Output is correct
19 Correct 688 ms 18496 KB Output is correct
20 Correct 675 ms 18432 KB Output is correct
21 Correct 672 ms 18300 KB Output is correct
22 Correct 644 ms 18280 KB Output is correct
23 Correct 667 ms 18244 KB Output is correct
24 Correct 655 ms 18116 KB Output is correct
25 Correct 708 ms 18164 KB Output is correct
26 Correct 676 ms 18192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 809 ms 36876 KB Output is correct
2 Correct 870 ms 36536 KB Output is correct
3 Correct 892 ms 35756 KB Output is correct
4 Correct 848 ms 36736 KB Output is correct
5 Correct 850 ms 36656 KB Output is correct
6 Correct 836 ms 31484 KB Output is correct
7 Correct 810 ms 31272 KB Output is correct
8 Correct 815 ms 31236 KB Output is correct
9 Correct 796 ms 31196 KB Output is correct
10 Correct 776 ms 31080 KB Output is correct
11 Correct 757 ms 31124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 12468 KB Output is correct
2 Correct 15 ms 12556 KB Output is correct
3 Correct 16 ms 12716 KB Output is correct
4 Correct 18 ms 12460 KB Output is correct
5 Correct 17 ms 12364 KB Output is correct
6 Correct 15 ms 12364 KB Output is correct
7 Correct 20 ms 12452 KB Output is correct
8 Correct 731 ms 18296 KB Output is correct
9 Correct 650 ms 18276 KB Output is correct
10 Correct 638 ms 18684 KB Output is correct
11 Correct 666 ms 19280 KB Output is correct
12 Correct 729 ms 17824 KB Output is correct
13 Correct 675 ms 18392 KB Output is correct
14 Correct 668 ms 18352 KB Output is correct
15 Correct 697 ms 18404 KB Output is correct
16 Correct 632 ms 18228 KB Output is correct
17 Correct 651 ms 18328 KB Output is correct
18 Correct 709 ms 18432 KB Output is correct
19 Correct 688 ms 18496 KB Output is correct
20 Correct 675 ms 18432 KB Output is correct
21 Correct 672 ms 18300 KB Output is correct
22 Correct 644 ms 18280 KB Output is correct
23 Correct 667 ms 18244 KB Output is correct
24 Correct 655 ms 18116 KB Output is correct
25 Correct 708 ms 18164 KB Output is correct
26 Correct 676 ms 18192 KB Output is correct
27 Correct 809 ms 36876 KB Output is correct
28 Correct 870 ms 36536 KB Output is correct
29 Correct 892 ms 35756 KB Output is correct
30 Correct 848 ms 36736 KB Output is correct
31 Correct 850 ms 36656 KB Output is correct
32 Correct 836 ms 31484 KB Output is correct
33 Correct 810 ms 31272 KB Output is correct
34 Correct 815 ms 31236 KB Output is correct
35 Correct 796 ms 31196 KB Output is correct
36 Correct 776 ms 31080 KB Output is correct
37 Correct 757 ms 31124 KB Output is correct
38 Correct 1004 ms 67108 KB Output is correct
39 Correct 998 ms 75928 KB Output is correct
40 Correct 1027 ms 57064 KB Output is correct
41 Correct 1174 ms 74528 KB Output is correct
42 Correct 826 ms 31864 KB Output is correct
43 Correct 730 ms 31720 KB Output is correct
44 Correct 837 ms 45044 KB Output is correct
45 Correct 837 ms 45476 KB Output is correct
46 Correct 850 ms 45928 KB Output is correct
47 Correct 733 ms 32816 KB Output is correct
48 Correct 768 ms 32456 KB Output is correct
49 Correct 851 ms 46292 KB Output is correct
50 Correct 842 ms 45208 KB Output is correct
51 Correct 872 ms 47044 KB Output is correct
52 Correct 809 ms 45380 KB Output is correct
53 Correct 848 ms 58404 KB Output is correct
54 Correct 944 ms 70608 KB Output is correct
55 Correct 857 ms 58172 KB Output is correct