Submission #47104

# Submission time Handle Problem Language Result Execution time Memory
47104 2018-04-27T15:32:24 Z SpaimaCarpatilor Long Mansion (JOI17_long_mansion) C++17
100 / 100
742 ms 262144 KB
#include<bits/stdc++.h>

using namespace std;

const int NMAX = 500009;
int N, last[NMAX], lft[NMAX], rgt[NMAX], C[NMAX], minL[NMAX], maxR[NMAX];
vector < int > v[NMAX];

void read ()
{
    scanf ("%d", &N);
    for (int i=1; i<N; i++)
        scanf ("%d", &C[i]);
    for (int i=1; i<=N; i++)
    {
        int l, x;
        scanf ("%d", &l);
        while (l --)
            scanf ("%d", &x),
            v[i].push_back (x),
            last[x] = i;
        if (i < N)
            lft[i] = last[C[i]];
    }
    for (int i=1; i<=N; i++)
        last[i] = 0;
    for (int i=N; i>=2; i--)
    {
        for (auto it : v[i])
            last[it] = i;
        if (last[C[i - 1]] == 0) rgt[i - 1] = N + 1;
        else rgt[i - 1] = last[C[i - 1]];
    }
}

namespace segtree {
    int ma[4 * NMAX], aintVal[NMAX];

    void build (int nod, int st, int dr)
    {
        if (st == dr)
        {
            ma[nod] = aintVal[st];
            return ;
        }
        int mij = (st + dr) >> 1, f1 = nod << 1, f2 = f1 | 1;
        build (f1, st, mij);
        build (f2, mij + 1, dr);
        ma[nod] = max (ma[f1], ma[f2]);
    }

    int firstBigger (int nod, int st, int dr, int x, int y, int minVal)
    {
        if (x > y) return -1;
        int mij = (st + dr) >> 1, f1 = nod << 1, f2 = f1 | 1;
        if (x <= st && dr <= y)
        {
            if (ma[nod] <= minVal) return -1;
            if (st == dr) return st;
            if (ma[f1] > minVal) return firstBigger (f1, st, mij, x, y, minVal);
            return firstBigger (f2, mij + 1, dr, x, y, minVal);
        }
        if (x <= mij)
        {
            int curr = firstBigger (f1, st, mij, x, y, minVal);
            if (curr != -1) return curr;
        }
        if (mij < y)
            return firstBigger (f2, mij + 1, dr, x, y, minVal);
        return -1;
    }
};

void buildMinL ()
{
    for (int i=1; i<=N; i++)
        segtree::aintVal[i] = (i == 1 ? N + 1 : rgt[i - 1]);
    segtree::build (1, 1, N);
    for (int i=1; i<=N; i++)
    {
        minL[i] = segtree::firstBigger (1, 1, N, lft[i] + 1, i, i);
        if (minL[i] == -1)
            minL[i] = N + 1;
    }
}

void buildMaxR ()
{
    for (int i=1; i<=N; i++)
        segtree::aintVal[i] = -(i == N ? 0 : lft[i]);
    reverse (segtree::aintVal + 1, segtree::aintVal + N + 1);
    segtree::build (1, 1, N);
    for (int i=1; i<=N; i++)
    {
        int lEnd = i, rEnd = rgt[i - 1] - 1;
        maxR[i] = segtree::firstBigger (1, 1, N, N + 1 - rEnd, N + 1 - lEnd, -i);
        if (maxR[i] == -1) maxR[i] = 0;
        else maxR[i] = N + 1 - maxR[i];
    }
}

namespace RMQ {
    int mi[19][NMAX], ma[19][NMAX], lg[NMAX];
    void buildRmq ()
    {
        for (int i=1; i<=N; i++)
        {
            lg[i] = lg[i - 1];
            if ((2 << lg[i]) <= i) lg[i] ++;
        }
        for (int i=1; i<=N; i++)
            mi[0][i] = minL[i],
            ma[0][i] = maxR[i];
        for (int i=1; i<=lg[N]; i++)
            for (int j=1; j + (1 << i) - 1 <= N; j++)
                mi[i][j] = min (mi[i - 1][j], mi[i - 1][j + (1 << (i - 1))]),
                ma[i][j] = max (ma[i - 1][j], ma[i - 1][j + (1 << (i - 1))]);
    }

    int getMin (int l, int r)
    {
        int p = lg[r - l + 1];
        return min (mi[p][l], mi[p][r - (1 << p) + 1]);
    }

    int getMax (int l, int r)
    {
        int p = lg[r - l + 1];
        return max (ma[p][l], ma[p][r - (1 << p) + 1]);
    }
};

void processQueries ()
{
    int Q;
    scanf ("%d", &Q);
    while (Q --)
    {
        int x, y;
        scanf ("%d %d", &x, &y);
        bool ans = 1;
        if (x < y && RMQ::getMin (x, y - 1) <= x) ans = 0;
        else
        if (y < x && RMQ::getMax (y + 1, x) >= x) ans = 0;
        if (ans) printf ("YES\n");
        else printf ("NO\n");
    }
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

read ();
buildMinL ();
buildMaxR ();
RMQ::buildRmq ();
processQueries ();

return 0;
}

Compilation message

long_mansion.cpp: In function 'void read()':
long_mansion.cpp:11:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d", &N);
     ~~~~~~^~~~~~~~~~
long_mansion.cpp:13:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d", &C[i]);
         ~~~~~~^~~~~~~~~~~~~
long_mansion.cpp:17:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d", &l);
         ~~~~~~^~~~~~~~~~
long_mansion.cpp:20:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf ("%d", &x),
             ~~~~~~~~~~~~~~~~~~ 
             v[i].push_back (x),
             ~~~~~~~~~~~~~~~~~~^~
             last[x] = i;
             ~~~~~~~~~~~        
long_mansion.cpp: In function 'void processQueries()':
long_mansion.cpp:136:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d", &Q);
     ~~~~~~^~~~~~~~~~
long_mansion.cpp:140:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf ("%d %d", &x, &y);
         ~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12536 KB Output is correct
2 Correct 15 ms 12772 KB Output is correct
3 Correct 16 ms 13336 KB Output is correct
4 Correct 15 ms 13336 KB Output is correct
5 Correct 15 ms 13336 KB Output is correct
6 Correct 14 ms 13336 KB Output is correct
7 Correct 14 ms 13336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12536 KB Output is correct
2 Correct 15 ms 12772 KB Output is correct
3 Correct 16 ms 13336 KB Output is correct
4 Correct 15 ms 13336 KB Output is correct
5 Correct 15 ms 13336 KB Output is correct
6 Correct 14 ms 13336 KB Output is correct
7 Correct 14 ms 13336 KB Output is correct
8 Correct 167 ms 14292 KB Output is correct
9 Correct 167 ms 14300 KB Output is correct
10 Correct 185 ms 14708 KB Output is correct
11 Correct 176 ms 15072 KB Output is correct
12 Correct 169 ms 15072 KB Output is correct
13 Correct 166 ms 15072 KB Output is correct
14 Correct 178 ms 15072 KB Output is correct
15 Correct 264 ms 15072 KB Output is correct
16 Correct 150 ms 15072 KB Output is correct
17 Correct 164 ms 15072 KB Output is correct
18 Correct 167 ms 15072 KB Output is correct
19 Correct 177 ms 15072 KB Output is correct
20 Correct 157 ms 15072 KB Output is correct
21 Correct 150 ms 15072 KB Output is correct
22 Correct 167 ms 15072 KB Output is correct
23 Correct 157 ms 15072 KB Output is correct
24 Correct 157 ms 15072 KB Output is correct
25 Correct 160 ms 15072 KB Output is correct
26 Correct 160 ms 15072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 387 ms 41520 KB Output is correct
2 Correct 387 ms 48884 KB Output is correct
3 Correct 366 ms 55676 KB Output is correct
4 Correct 382 ms 63076 KB Output is correct
5 Correct 371 ms 70420 KB Output is correct
6 Correct 296 ms 76304 KB Output is correct
7 Correct 285 ms 82508 KB Output is correct
8 Correct 260 ms 88600 KB Output is correct
9 Correct 260 ms 94648 KB Output is correct
10 Correct 273 ms 100720 KB Output is correct
11 Correct 285 ms 106684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12536 KB Output is correct
2 Correct 15 ms 12772 KB Output is correct
3 Correct 16 ms 13336 KB Output is correct
4 Correct 15 ms 13336 KB Output is correct
5 Correct 15 ms 13336 KB Output is correct
6 Correct 14 ms 13336 KB Output is correct
7 Correct 14 ms 13336 KB Output is correct
8 Correct 167 ms 14292 KB Output is correct
9 Correct 167 ms 14300 KB Output is correct
10 Correct 185 ms 14708 KB Output is correct
11 Correct 176 ms 15072 KB Output is correct
12 Correct 169 ms 15072 KB Output is correct
13 Correct 166 ms 15072 KB Output is correct
14 Correct 178 ms 15072 KB Output is correct
15 Correct 264 ms 15072 KB Output is correct
16 Correct 150 ms 15072 KB Output is correct
17 Correct 164 ms 15072 KB Output is correct
18 Correct 167 ms 15072 KB Output is correct
19 Correct 177 ms 15072 KB Output is correct
20 Correct 157 ms 15072 KB Output is correct
21 Correct 150 ms 15072 KB Output is correct
22 Correct 167 ms 15072 KB Output is correct
23 Correct 157 ms 15072 KB Output is correct
24 Correct 157 ms 15072 KB Output is correct
25 Correct 160 ms 15072 KB Output is correct
26 Correct 160 ms 15072 KB Output is correct
27 Correct 387 ms 41520 KB Output is correct
28 Correct 387 ms 48884 KB Output is correct
29 Correct 366 ms 55676 KB Output is correct
30 Correct 382 ms 63076 KB Output is correct
31 Correct 371 ms 70420 KB Output is correct
32 Correct 296 ms 76304 KB Output is correct
33 Correct 285 ms 82508 KB Output is correct
34 Correct 260 ms 88600 KB Output is correct
35 Correct 260 ms 94648 KB Output is correct
36 Correct 273 ms 100720 KB Output is correct
37 Correct 285 ms 106684 KB Output is correct
38 Correct 705 ms 182796 KB Output is correct
39 Correct 742 ms 215424 KB Output is correct
40 Correct 614 ms 215424 KB Output is correct
41 Correct 648 ms 235732 KB Output is correct
42 Correct 325 ms 235732 KB Output is correct
43 Correct 297 ms 235732 KB Output is correct
44 Correct 505 ms 235732 KB Output is correct
45 Correct 502 ms 235732 KB Output is correct
46 Correct 410 ms 235732 KB Output is correct
47 Correct 286 ms 235732 KB Output is correct
48 Correct 290 ms 235732 KB Output is correct
49 Correct 398 ms 241228 KB Output is correct
50 Correct 395 ms 251444 KB Output is correct
51 Correct 403 ms 261840 KB Output is correct
52 Correct 381 ms 262144 KB Output is correct
53 Correct 509 ms 262144 KB Output is correct
54 Correct 579 ms 262144 KB Output is correct
55 Correct 476 ms 262144 KB Output is correct