답안 #122142

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122142 2019-06-27T16:50:03 Z rajarshi_basu Long Mansion (JOI17_long_mansion) C++14
25 / 100
3000 ms 52348 KB
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <complex>
#include <random>
#include <stack>
#include <chrono>
#include <set>

#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll,ll>
#define cd complex<double> 
#define ld long double
#define pld pair<ld,ld>
#define iii pair<ii,int>
#define vv vector

using namespace std;

const int MAXN = 5e5+10;
int C[MAXN];
int leftNeeded[MAXN];
int rightNeeded[MAXN];
vi keys[MAXN];
int L[MAXN];
int R[MAXN];
int last[MAXN];

#define endl '\n'
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    FOR(i,n-1)cin >> C[i];
    FOR(i,n){
        int x;cin >> x;
        FOR(j,x){
            int y;cin >> y;
            keys[i].pb(y);
        }
    }

    // input taking is done, now find left and right needed values for each edge.
    FOR(i,MAXN)last[i] = -1;
    FOR(i,n-1){
        for(auto e : keys[i]){
            last[e] = i;
        }
        leftNeeded[i] = last[C[i]];
    }
    FOR(i,MAXN)last[i] = n;
    for(int i = n-1;i>0;i--){
        for(auto e : keys[i])last[e] = i;
        rightNeeded[i-1] = last[C[i-1]];
    }
    FOR(i,n-1){
      //  cout << leftNeeded[i] << " " << rightNeeded[i] << endl;
    }
    //cout << endl;
    // we have found out the left and right needed. now we need to compute all the ranges.
    FOR(i,n){
        int rightl = (i == 0)?-1:R[i-1];
        if(rightl >= i){
            // we know that we cannot escape from this box anyway. 
            // now try to go left once.
            int minR = rightNeeded[i-1];
            int ind = -1;
            FORE(edge,i,min(rightl-1,minR-1)){
                if(leftNeeded[edge] < i){
                    ind = edge;
                    break;
                }
            }
            if(minR > rightl and ind == -1)ind = rightl;
            if(ind == -1){
                L[i] = L[i-1];
                R[i] = R[i-1];

            }else{
                L[i] = i;
                R[i] = ind;
            }
        }else{
            // try increasing right ptr one by one. and for each such move, increase left pointer as much as possible.
            int lptr = i;
            int rptr = i;
            while(rptr < n){
                while(lptr > 0 and rightNeeded[lptr-1] <= rptr)lptr--;
                if(leftNeeded[rptr] < lptr)break;
                rptr++;
            }
            L[i] = lptr;
            R[i] = rptr;
        }
      //  cout << L[i] << " " << R[i] << endl;
    }
   
    int q;
    cin >> q;
    //cout << q << endl;
    FOR(i,q){
        int x,y;
        cin >> x >> y;
        x--;y--;
        if(L[x] <= y and y <= R[x]){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 14208 KB Output is correct
2 Correct 18 ms 14336 KB Output is correct
3 Correct 27 ms 14464 KB Output is correct
4 Correct 17 ms 14208 KB Output is correct
5 Correct 17 ms 14208 KB Output is correct
6 Correct 17 ms 14208 KB Output is correct
7 Correct 17 ms 14240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 14208 KB Output is correct
2 Correct 18 ms 14336 KB Output is correct
3 Correct 27 ms 14464 KB Output is correct
4 Correct 17 ms 14208 KB Output is correct
5 Correct 17 ms 14208 KB Output is correct
6 Correct 17 ms 14208 KB Output is correct
7 Correct 17 ms 14240 KB Output is correct
8 Correct 142 ms 15852 KB Output is correct
9 Correct 123 ms 15736 KB Output is correct
10 Correct 122 ms 15992 KB Output is correct
11 Correct 125 ms 16248 KB Output is correct
12 Correct 110 ms 15924 KB Output is correct
13 Correct 118 ms 16044 KB Output is correct
14 Correct 117 ms 16136 KB Output is correct
15 Correct 122 ms 16072 KB Output is correct
16 Correct 116 ms 16284 KB Output is correct
17 Correct 117 ms 16116 KB Output is correct
18 Correct 117 ms 15992 KB Output is correct
19 Correct 117 ms 16064 KB Output is correct
20 Correct 116 ms 16248 KB Output is correct
21 Correct 118 ms 16224 KB Output is correct
22 Correct 116 ms 15992 KB Output is correct
23 Correct 116 ms 15864 KB Output is correct
24 Correct 119 ms 15932 KB Output is correct
25 Correct 115 ms 15864 KB Output is correct
26 Correct 119 ms 15768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 654 ms 21776 KB Output is correct
2 Correct 1105 ms 21548 KB Output is correct
3 Correct 1585 ms 27428 KB Output is correct
4 Correct 808 ms 28860 KB Output is correct
5 Correct 861 ms 28928 KB Output is correct
6 Correct 582 ms 27428 KB Output is correct
7 Correct 262 ms 27156 KB Output is correct
8 Correct 213 ms 27316 KB Output is correct
9 Correct 215 ms 27132 KB Output is correct
10 Correct 176 ms 27128 KB Output is correct
11 Correct 188 ms 27280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 14208 KB Output is correct
2 Correct 18 ms 14336 KB Output is correct
3 Correct 27 ms 14464 KB Output is correct
4 Correct 17 ms 14208 KB Output is correct
5 Correct 17 ms 14208 KB Output is correct
6 Correct 17 ms 14208 KB Output is correct
7 Correct 17 ms 14240 KB Output is correct
8 Correct 142 ms 15852 KB Output is correct
9 Correct 123 ms 15736 KB Output is correct
10 Correct 122 ms 15992 KB Output is correct
11 Correct 125 ms 16248 KB Output is correct
12 Correct 110 ms 15924 KB Output is correct
13 Correct 118 ms 16044 KB Output is correct
14 Correct 117 ms 16136 KB Output is correct
15 Correct 122 ms 16072 KB Output is correct
16 Correct 116 ms 16284 KB Output is correct
17 Correct 117 ms 16116 KB Output is correct
18 Correct 117 ms 15992 KB Output is correct
19 Correct 117 ms 16064 KB Output is correct
20 Correct 116 ms 16248 KB Output is correct
21 Correct 118 ms 16224 KB Output is correct
22 Correct 116 ms 15992 KB Output is correct
23 Correct 116 ms 15864 KB Output is correct
24 Correct 119 ms 15932 KB Output is correct
25 Correct 115 ms 15864 KB Output is correct
26 Correct 119 ms 15768 KB Output is correct
27 Correct 654 ms 21776 KB Output is correct
28 Correct 1105 ms 21548 KB Output is correct
29 Correct 1585 ms 27428 KB Output is correct
30 Correct 808 ms 28860 KB Output is correct
31 Correct 861 ms 28928 KB Output is correct
32 Correct 582 ms 27428 KB Output is correct
33 Correct 262 ms 27156 KB Output is correct
34 Correct 213 ms 27316 KB Output is correct
35 Correct 215 ms 27132 KB Output is correct
36 Correct 176 ms 27128 KB Output is correct
37 Correct 188 ms 27280 KB Output is correct
38 Correct 328 ms 47496 KB Output is correct
39 Correct 325 ms 52348 KB Output is correct
40 Correct 283 ms 41424 KB Output is correct
41 Execution timed out 3035 ms 39244 KB Time limit exceeded
42 Halted 0 ms 0 KB -