답안 #1080026

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1080026 2024-08-29T06:24:52 Z Thanhs Long Mansion (JOI17_long_mansion) C++14
25 / 100
3000 ms 52308 KB
#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define double long double
 
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define aint(x) x.begin(), x.end()
 
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 5e5 + 5;

int n, b[NM], c[NM], a[NM], L[NM];

struct ST1
{
    int dt[4 * NM];
    void build(int x = 1, int l = 1, int r = n)
    {
        if (l == r)
        {
            dt[x] = b[l];
            return;
        }
        int m = l + r >> 1;
        build(x << 1, l, m);
        build(x << 1 | 1, m + 1, r);
        dt[x] = min(dt[x << 1], dt[x << 1 | 1]);
    }
    int get(int i, int l, int x = 1, int lx = 1, int rx = n)
    {
        // cout << i << ' ' << l << ' ' << x << ' ' << lx << ' ' << rx << endl;
        if (rx < l || dt[x] >= i)
            return rx + 1;
        if (lx == rx)
            return lx;
        int m = lx + rx >> 1;
        int res = m + 1;
        if (dt[x << 1] < i)
            res = get(i, l, x << 1, lx, m);
        if (res != m + 1)
            return res;
        return get(i, l, x << 1 | 1, m + 1, rx);
    }
}st1;

struct ST2
{
    int dt[4 * NM];
    void build(int x = 1, int l = 1, int r = n)
    {
        if (l == r)
        {
            dt[x] = c[l + 1];
            return;
        }
        int m = l + r >> 1;
        build(x << 1, l, m);
        build(x << 1 | 1, m + 1, r);
        dt[x] = max(dt[x << 1], dt[x << 1 | 1]);
    }
    int get(int i, int r, int x = 1, int lx = 1, int rx = n)
    {
        if (lx > r || dt[x] <= i)
            return lx - 1;
        if (lx == rx)
            return lx;
        int m = lx + rx >> 1;
        int res = m;
        if (dt[x << 1 | 1] > i)
            res = get(i, r, x << 1 | 1, m + 1, rx);
        if (res != m)
            return res;
        return get(i, r, x << 1, lx, m);
    }
}st2;

int l[NM], r[NM];
vector<int> v[NM];

void solve()
{
    cin >> n;
    for (int i = 2; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        b[i] = L[a[i]];
        int t; cin >> t;
        while (t--)
        {
            int x;
            cin >> x;
            setmax(L[x], i);
            v[i].push_back(x);
        }
    }
    for (int i = 1; i <= n; i++)
        L[i] = n + 1;
    for (int i = n; i >= 1; i--)
    {
        for (auto x : v[i])
            setmin(L[x], i);
        c[i] = L[a[i]];
    }
    b[1] = n + 1;
    st1.build();
    st2.build();
    for (int i = 1; i <= n; i++)
    {
        l[i] = r[i] = i;
        while (1)
        {
            int ll = l[i], rr = r[i];
            r[i] = st1.get(l[i], i + 1) - 1;
            l[i] = st2.get(r[i], i - 1) + 1;
            if (l[i] == ll && r[i] == rr)
                break;
        }
    }
    int q;
    cin >> q;
    while (q--)
    {
        int x, y;
        cin >> x >> y;
        cout << (l[x] <= y && r[x] >= y ? "YES\n" : "NO\n");
    }
}

int main()
{
    fastIO
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    int tc = 1; // cin >> tc;
    while (tc--)
        solve();

}

Compilation message

long_mansion.cpp: In member function 'void ST1::build(int, int, int)':
long_mansion.cpp:32:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |         int m = l + r >> 1;
      |                 ~~^~~
long_mansion.cpp: In member function 'int ST1::get(int, int, int, int, int)':
long_mansion.cpp:44:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |         int m = lx + rx >> 1;
      |                 ~~~^~~~
long_mansion.cpp: In member function 'void ST2::build(int, int, int)':
long_mansion.cpp:64:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |         int m = l + r >> 1;
      |                 ~~^~~
long_mansion.cpp: In member function 'int ST2::get(int, int, int, int, int)':
long_mansion.cpp:75:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |         int m = lx + rx >> 1;
      |                 ~~~^~~~
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 4 ms 25436 KB Output is correct
4 Correct 4 ms 25292 KB Output is correct
5 Correct 4 ms 25180 KB Output is correct
6 Correct 4 ms 25180 KB Output is correct
7 Correct 22 ms 25308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 4 ms 25436 KB Output is correct
4 Correct 4 ms 25292 KB Output is correct
5 Correct 4 ms 25180 KB Output is correct
6 Correct 4 ms 25180 KB Output is correct
7 Correct 22 ms 25308 KB Output is correct
8 Correct 55 ms 26660 KB Output is correct
9 Correct 70 ms 26572 KB Output is correct
10 Correct 91 ms 26848 KB Output is correct
11 Correct 57 ms 27140 KB Output is correct
12 Correct 56 ms 26852 KB Output is correct
13 Correct 56 ms 26840 KB Output is correct
14 Correct 97 ms 26968 KB Output is correct
15 Correct 90 ms 26872 KB Output is correct
16 Correct 57 ms 27220 KB Output is correct
17 Correct 56 ms 26964 KB Output is correct
18 Correct 54 ms 26900 KB Output is correct
19 Correct 53 ms 26924 KB Output is correct
20 Correct 55 ms 26964 KB Output is correct
21 Correct 51 ms 27304 KB Output is correct
22 Correct 77 ms 26964 KB Output is correct
23 Correct 100 ms 26836 KB Output is correct
24 Correct 103 ms 26708 KB Output is correct
25 Correct 81 ms 26716 KB Output is correct
26 Correct 71 ms 26640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 32892 KB Output is correct
2 Correct 152 ms 32852 KB Output is correct
3 Correct 121 ms 32848 KB Output is correct
4 Correct 164 ms 33056 KB Output is correct
5 Correct 164 ms 32848 KB Output is correct
6 Correct 117 ms 32592 KB Output is correct
7 Correct 90 ms 32648 KB Output is correct
8 Correct 83 ms 32592 KB Output is correct
9 Correct 90 ms 32536 KB Output is correct
10 Correct 81 ms 32592 KB Output is correct
11 Correct 99 ms 32796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 25180 KB Output is correct
2 Correct 5 ms 25180 KB Output is correct
3 Correct 4 ms 25436 KB Output is correct
4 Correct 4 ms 25292 KB Output is correct
5 Correct 4 ms 25180 KB Output is correct
6 Correct 4 ms 25180 KB Output is correct
7 Correct 22 ms 25308 KB Output is correct
8 Correct 55 ms 26660 KB Output is correct
9 Correct 70 ms 26572 KB Output is correct
10 Correct 91 ms 26848 KB Output is correct
11 Correct 57 ms 27140 KB Output is correct
12 Correct 56 ms 26852 KB Output is correct
13 Correct 56 ms 26840 KB Output is correct
14 Correct 97 ms 26968 KB Output is correct
15 Correct 90 ms 26872 KB Output is correct
16 Correct 57 ms 27220 KB Output is correct
17 Correct 56 ms 26964 KB Output is correct
18 Correct 54 ms 26900 KB Output is correct
19 Correct 53 ms 26924 KB Output is correct
20 Correct 55 ms 26964 KB Output is correct
21 Correct 51 ms 27304 KB Output is correct
22 Correct 77 ms 26964 KB Output is correct
23 Correct 100 ms 26836 KB Output is correct
24 Correct 103 ms 26708 KB Output is correct
25 Correct 81 ms 26716 KB Output is correct
26 Correct 71 ms 26640 KB Output is correct
27 Correct 120 ms 32892 KB Output is correct
28 Correct 152 ms 32852 KB Output is correct
29 Correct 121 ms 32848 KB Output is correct
30 Correct 164 ms 33056 KB Output is correct
31 Correct 164 ms 32848 KB Output is correct
32 Correct 117 ms 32592 KB Output is correct
33 Correct 90 ms 32648 KB Output is correct
34 Correct 83 ms 32592 KB Output is correct
35 Correct 90 ms 32536 KB Output is correct
36 Correct 81 ms 32592 KB Output is correct
37 Correct 99 ms 32796 KB Output is correct
38 Correct 193 ms 49016 KB Output is correct
39 Correct 205 ms 52052 KB Output is correct
40 Correct 168 ms 45436 KB Output is correct
41 Correct 219 ms 52308 KB Output is correct
42 Correct 106 ms 32340 KB Output is correct
43 Correct 128 ms 32376 KB Output is correct
44 Correct 165 ms 37916 KB Output is correct
45 Correct 189 ms 37972 KB Output is correct
46 Correct 150 ms 37972 KB Output is correct
47 Correct 105 ms 32592 KB Output is correct
48 Correct 101 ms 32552 KB Output is correct
49 Correct 152 ms 37968 KB Output is correct
50 Correct 155 ms 37968 KB Output is correct
51 Correct 186 ms 38056 KB Output is correct
52 Execution timed out 3071 ms 36184 KB Time limit exceeded
53 Halted 0 ms 0 KB -