Submission #1356131


Source Code Expand

// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 1e5 + 5;

int N, Q;
Pii A[MAXN];
int C[MAXN];
Pii QRY[MAXN];

void input()
{
    read(N);
    for (int i = 0; i < N; ++i) {
        read(A[i].x); read(A[i].y);
    }
    for (int i = 0; i <= N; ++i) {
        read(C[i]);
    }
    read(Q);
    for (int i = 1; i <= Q; ++i) {
        read(QRY[i].x); read(QRY[i].y);
    }
}

void solve()
{
    static int hash[MAXN];

    std::sort(C, C + N + 1);
    std::copy(C, C + N + 1, hash);
    int hashsize = std::unique(hash, hash + N + 1) - hash;

    for (int i = 0; i <= N; ++i) {
        C[i] = std::lower_bound(hash, hash + hashsize, C[i]) - hash;
    }
    for (int i = 0; i < N; ++i) {
        A[i].x = std::lower_bound(hash, hash + hashsize, A[i].x) - hash;
        A[i].y = std::lower_bound(hash, hash + hashsize, A[i].y) - hash;
    }
    for (int i = 1; i <= Q; ++i) {
        QRY[i].x = std::lower_bound(hash, hash + hashsize, QRY[i].x) - hash;
        QRY[i].y = std::lower_bound(hash, hash + hashsize, QRY[i].y) - hash;
    }

    static int val[MAXN];

    for (int i = 0; i <= N; ++i) {
        --val[C[i]];
    }
    for (int i = 0; i < N; ++i) {
        ++val[A[i].x];
    }
    for (int i = 0; i < hashsize; ++i) {
        val[i + 1] += val[i];
    }

    static int dec[MAXN], inc[MAXN];
    __gnu_pbds::priority_queue<Pii, std::greater<Pii> > q0;
    __gnu_pbds::priority_queue<Pii> q1;
    std::multiset<Pii> unused;
    vector<Pii> interval;

    for (int i = 0; i < N; ++i) {
        if (A[i].x > A[i].y) {
            interval.push_back(A[i]);
            unused.insert(MP(A[i].y, A[i].x));
        }
    }
    std::sort(ALL(interval), std::greater<Pii>());

    int cur = 0;

    for (int i = hashsize - 1, j = 0, cnt = 0; i >= 0; --i) {
        for (; j < SZ(interval) && interval[j].x > i; ++j) {
            q0.push(MP(interval[j].y, interval[j].x));
        }
        val[i] += cnt;
        while (SZ(q0) && val[i] < -1) {
            Pii t = q0.top(); q0.pop();
            if (t.x > i) break;
            unused.erase(unused.find(t));
            ++val[i]; ++cnt; ++dec[t.x];
            ++inc[i + 1]; --inc[t.y];
            ++cur;
        }
        cnt -= dec[i];
        if (val[i] < -1) {
            for (int k = 1; k <= Q; ++k) {
                puts("-1");
            }
            return;
        }
    }
    for (int i = 0, cnt = 0; i < hashsize; ++i) {
        val[i] += cnt += inc[i];
    }

    static int ans[MAXN];

    memset(dec, 0, sizeof(dec));
    ans[0] = cur;
    for (int i = 1, cnt = 0; i <= hashsize; ++i) {
        while (SZ(unused) && unused.begin()->x < i) {
            q1.push(MP(unused.begin()->y, unused.begin()->x));
            unused.erase(unused.begin());
        }
        val[i - 1] += cnt;
        if (SZ(q1) && val[i - 1] < 0) {
            Pii t = q1.top(); q1.pop();
            if (t.x >= i) {
                ++val[i - 1]; ++cnt; ++dec[t.x];
                ++cur;
            }
        }
        cnt -= dec[i];
        if (val[i - 1] < 0) {
            for (; i <= hashsize; ++i) {
                ans[i] = +oo;
            }
            break;
        }
        ans[i] = cur;
    }

    for (int i = 1; i <= Q; ++i) {
        int t = std::min(ans[QRY[i].x], ans[QRY[i].y] + 1);
        if (t == +oo)
            puts("-1");
        else
            printf("%d\n", N + 1 - t);
    }
}

int main()
{
#ifdef __DEBUG
    freopen("F.in", "r", stdin);
    freopen("F.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 易挑锦妇机中字。难得玉人心下事。
//     -- 刘克庄《玉楼春·戏林推》

Submission Info

Submission Time
Task F - Two Faced Cards
User Ivlleiooq
Language C++14 (GCC 5.4.1)
Score 2000
Code Size 5804 Byte
Status AC
Exec Time 142 ms
Memory 16868 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 2000 / 2000
Status
AC × 3
AC × 61
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_40.txt, subtask_1_41.txt, subtask_1_42.txt, subtask_1_43.txt, subtask_1_44.txt, subtask_1_45.txt, subtask_1_46.txt, subtask_1_47.txt, subtask_1_48.txt, subtask_1_49.txt, subtask_1_50.txt, subtask_1_51.txt, subtask_1_52.txt, subtask_1_53.txt, subtask_1_54.txt, subtask_1_55.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 640 KB
sample_02.txt AC 1 ms 640 KB
sample_03.txt AC 1 ms 640 KB
subtask_1_01.txt AC 44 ms 5500 KB
subtask_1_02.txt AC 62 ms 9176 KB
subtask_1_03.txt AC 24 ms 3200 KB
subtask_1_04.txt AC 64 ms 10852 KB
subtask_1_05.txt AC 34 ms 3964 KB
subtask_1_06.txt AC 86 ms 10584 KB
subtask_1_07.txt AC 84 ms 7796 KB
subtask_1_08.txt AC 105 ms 9480 KB
subtask_1_09.txt AC 83 ms 10612 KB
subtask_1_10.txt AC 53 ms 8252 KB
subtask_1_11.txt AC 42 ms 4216 KB
subtask_1_12.txt AC 50 ms 5012 KB
subtask_1_13.txt AC 60 ms 7288 KB
subtask_1_14.txt AC 38 ms 6320 KB
subtask_1_15.txt AC 59 ms 6004 KB
subtask_1_16.txt AC 25 ms 2676 KB
subtask_1_17.txt AC 123 ms 14196 KB
subtask_1_18.txt AC 33 ms 5824 KB
subtask_1_19.txt AC 69 ms 5752 KB
subtask_1_20.txt AC 112 ms 9520 KB
subtask_1_21.txt AC 104 ms 9592 KB
subtask_1_22.txt AC 129 ms 16868 KB
subtask_1_23.txt AC 105 ms 9464 KB
subtask_1_24.txt AC 128 ms 16868 KB
subtask_1_25.txt AC 129 ms 14452 KB
subtask_1_26.txt AC 128 ms 15972 KB
subtask_1_27.txt AC 107 ms 8692 KB
subtask_1_28.txt AC 139 ms 10980 KB
subtask_1_29.txt AC 129 ms 14452 KB
subtask_1_30.txt AC 127 ms 15972 KB
subtask_1_31.txt AC 107 ms 8692 KB
subtask_1_32.txt AC 140 ms 10980 KB
subtask_1_33.txt AC 134 ms 14580 KB
subtask_1_34.txt AC 126 ms 15844 KB
subtask_1_35.txt AC 108 ms 10100 KB
subtask_1_36.txt AC 141 ms 10980 KB
subtask_1_37.txt AC 129 ms 14452 KB
subtask_1_38.txt AC 125 ms 15844 KB
subtask_1_39.txt AC 108 ms 8692 KB
subtask_1_40.txt AC 142 ms 10980 KB
subtask_1_41.txt AC 79 ms 8696 KB
subtask_1_42.txt AC 59 ms 4912 KB
subtask_1_43.txt AC 107 ms 13684 KB
subtask_1_44.txt AC 63 ms 4924 KB
subtask_1_45.txt AC 56 ms 4220 KB
subtask_1_46.txt AC 69 ms 8696 KB
subtask_1_47.txt AC 46 ms 3712 KB
subtask_1_48.txt AC 92 ms 12660 KB
subtask_1_49.txt AC 50 ms 4352 KB
subtask_1_50.txt AC 42 ms 3584 KB
subtask_1_51.txt AC 1 ms 640 KB
subtask_1_52.txt AC 1 ms 640 KB
subtask_1_53.txt AC 1 ms 640 KB
subtask_1_54.txt AC 1 ms 640 KB
subtask_1_55.txt AC 1 ms 640 KB