답안 #69924

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
69924 2018-08-22T03:13:04 Z MatheusLealV Long Mansion (JOI17_long_mansion) C++17
100 / 100
651 ms 253348 KB
#include <bits/stdc++.h>
#define N 500050
using namespace std;

int n, q, c[N], L[N], R[N];

int e[N];

vector<int> pos[N], T[N];

void solve(int l, int r, int i)
{
    while(true)
    {
        L[i] = l;

        R[i] = r;

        bool ok = false;

        if(r < n)
        {
            int q = upper_bound(pos[c[r]].begin(), pos[c[r]].end(), r) - lower_bound(pos[c[r]].begin(), pos[c[r]].end(), l);
            
            if(q) r = max(r + 1, e[r]), ok = 1;//solve(l, max(r + 1, R[r]), i), ok = 1;
        }

        if(l > 1 and !ok)
        {
            int q = upper_bound(pos[c[l - 1]].begin(), pos[c[l - 1]].end(), r) - lower_bound(pos[c[l - 1]].begin(), pos[c[l - 1]].end(), l);

            if(q) l = min(l - 1, L[l]), ok = 1;;//solve(min(l - 1, L[l]), r, i);        
        }

        if(!ok) break;
    }
}

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

    cin>>n;

    for(int i = 1; i < n; i++) cin>>c[i];

    for(int i = 1; i <= n; i++)
    {
        L[i] = R[i] = i;

        int a, b;

        cin>>a;

        for(int x = 1; x <= a; x++)
        {
            cin>>b;

            pos[b].push_back(i);

            T[i].push_back(b);
        }
    }

    for(int i = n; i >= 1; i--)
    {
    	e[i] = i;

    	for(auto x: T[i])
    	{
    		if(x == c[i])
    		{
    			e[i] = e[i + 1];

    			while(e[i] <= n)
    			{
           			 int q = upper_bound(pos[c[e[i]]].begin(), pos[c[e[i]]].end(), e[i]) - lower_bound(pos[c[e[i]]].begin(), pos[c[e[i]]].end(), i);

           			 if(q) e[i] = e[e[i] + 1];

           			 else break;
    			}

    			break;
    		}
    	}
 
    }

    for(int i = 1; i <= n; i++)
    {
        if(L[i - 1] <= i and i <= R[i - 1])
        {
            int q = upper_bound(pos[c[i - 1]].begin(), pos[c[i - 1]].end(), e[i]) - lower_bound(pos[c[i - 1]].begin(), pos[c[i - 1]].end(), i);

            if(q) L[i] = L[i - 1], R[i] = max(R[i - 1], e[i]);

            else L[i] = i, R[i] = e[i];
        }

       else solve(i, i, i);
    }

    cin>>q;

    for(int i = 1; i <= q; i++)
    {
        int x, y;

        cin>>x>>y;

        if(L[x] <= y and y <= R[x]) cout<<"YES\n";

        else cout<<"NO\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 24056 KB Output is correct
2 Correct 28 ms 24156 KB Output is correct
3 Correct 29 ms 24340 KB Output is correct
4 Correct 26 ms 24340 KB Output is correct
5 Correct 27 ms 24340 KB Output is correct
6 Correct 28 ms 24340 KB Output is correct
7 Correct 28 ms 24436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 24056 KB Output is correct
2 Correct 28 ms 24156 KB Output is correct
3 Correct 29 ms 24340 KB Output is correct
4 Correct 26 ms 24340 KB Output is correct
5 Correct 27 ms 24340 KB Output is correct
6 Correct 28 ms 24340 KB Output is correct
7 Correct 28 ms 24436 KB Output is correct
8 Correct 154 ms 25812 KB Output is correct
9 Correct 166 ms 25812 KB Output is correct
10 Correct 159 ms 25964 KB Output is correct
11 Correct 195 ms 26284 KB Output is correct
12 Correct 147 ms 26284 KB Output is correct
13 Correct 197 ms 26284 KB Output is correct
14 Correct 156 ms 26284 KB Output is correct
15 Correct 136 ms 26284 KB Output is correct
16 Correct 140 ms 26316 KB Output is correct
17 Correct 148 ms 26316 KB Output is correct
18 Correct 133 ms 26316 KB Output is correct
19 Correct 153 ms 26316 KB Output is correct
20 Correct 147 ms 26316 KB Output is correct
21 Correct 162 ms 26316 KB Output is correct
22 Correct 133 ms 26316 KB Output is correct
23 Correct 132 ms 26316 KB Output is correct
24 Correct 129 ms 26316 KB Output is correct
25 Correct 128 ms 26316 KB Output is correct
26 Correct 155 ms 26316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 361 ms 38932 KB Output is correct
2 Correct 373 ms 46024 KB Output is correct
3 Correct 341 ms 52632 KB Output is correct
4 Correct 412 ms 60336 KB Output is correct
5 Correct 339 ms 67468 KB Output is correct
6 Correct 269 ms 72548 KB Output is correct
7 Correct 254 ms 78844 KB Output is correct
8 Correct 269 ms 84916 KB Output is correct
9 Correct 219 ms 90912 KB Output is correct
10 Correct 204 ms 90928 KB Output is correct
11 Correct 219 ms 90928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 24056 KB Output is correct
2 Correct 28 ms 24156 KB Output is correct
3 Correct 29 ms 24340 KB Output is correct
4 Correct 26 ms 24340 KB Output is correct
5 Correct 27 ms 24340 KB Output is correct
6 Correct 28 ms 24340 KB Output is correct
7 Correct 28 ms 24436 KB Output is correct
8 Correct 154 ms 25812 KB Output is correct
9 Correct 166 ms 25812 KB Output is correct
10 Correct 159 ms 25964 KB Output is correct
11 Correct 195 ms 26284 KB Output is correct
12 Correct 147 ms 26284 KB Output is correct
13 Correct 197 ms 26284 KB Output is correct
14 Correct 156 ms 26284 KB Output is correct
15 Correct 136 ms 26284 KB Output is correct
16 Correct 140 ms 26316 KB Output is correct
17 Correct 148 ms 26316 KB Output is correct
18 Correct 133 ms 26316 KB Output is correct
19 Correct 153 ms 26316 KB Output is correct
20 Correct 147 ms 26316 KB Output is correct
21 Correct 162 ms 26316 KB Output is correct
22 Correct 133 ms 26316 KB Output is correct
23 Correct 132 ms 26316 KB Output is correct
24 Correct 129 ms 26316 KB Output is correct
25 Correct 128 ms 26316 KB Output is correct
26 Correct 155 ms 26316 KB Output is correct
27 Correct 361 ms 38932 KB Output is correct
28 Correct 373 ms 46024 KB Output is correct
29 Correct 341 ms 52632 KB Output is correct
30 Correct 412 ms 60336 KB Output is correct
31 Correct 339 ms 67468 KB Output is correct
32 Correct 269 ms 72548 KB Output is correct
33 Correct 254 ms 78844 KB Output is correct
34 Correct 269 ms 84916 KB Output is correct
35 Correct 219 ms 90912 KB Output is correct
36 Correct 204 ms 90928 KB Output is correct
37 Correct 219 ms 90928 KB Output is correct
38 Correct 545 ms 114304 KB Output is correct
39 Correct 573 ms 129876 KB Output is correct
40 Correct 462 ms 131408 KB Output is correct
41 Correct 651 ms 149512 KB Output is correct
42 Correct 242 ms 149512 KB Output is correct
43 Correct 251 ms 149512 KB Output is correct
44 Correct 429 ms 161996 KB Output is correct
45 Correct 463 ms 172404 KB Output is correct
46 Correct 368 ms 182828 KB Output is correct
47 Correct 338 ms 183560 KB Output is correct
48 Correct 286 ms 190360 KB Output is correct
49 Correct 367 ms 205888 KB Output is correct
50 Correct 417 ms 215616 KB Output is correct
51 Correct 473 ms 226284 KB Output is correct
52 Correct 295 ms 235528 KB Output is correct
53 Correct 374 ms 246020 KB Output is correct
54 Correct 477 ms 253348 KB Output is correct
55 Correct 408 ms 253348 KB Output is correct