Submission #390079

# Submission time Handle Problem Language Result Execution time Memory
390079 2021-04-15T10:04:42 Z KoD Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
488 ms 34408 KB
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

template <class T>
void fix(Vec<T>& vec) {
    std::sort(vec.begin(), vec.end());
    vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}

template <class T>
int lowb(const Vec<T>& vec, const T& x) {
    return std::lower_bound(vec.cbegin(), vec.cend(), x) - vec.cbegin();
}

template <class T>
int upb(const Vec<T>& vec, const T& x) {
    return std::upper_bound(vec.cbegin(), vec.cend(), x) - vec.cbegin();
}

template <class T>
void setmax(T& lhs, const T& rhs) {
    if (lhs < rhs) {
        lhs = rhs;
    }
}

struct Segtree {
    int size;
    Vec<int> data;
    Segtree(const int n): size(n), data(2 * n, -1) {}
    
    void chmax(int l, int r, const int x) {
        l += size;
        r += size;
        while (l < r) {
            if (l & 1) setmax(data[l++], x);
            if (r & 1) setmax(data[--r], x);
            l >>= 1;
            r >>= 1;
        }
    }

    int get(int i) const {
        int ret = -1;
        i += size;
        while (i > 0) {
            setmax(ret, data[i]);
            i >>= 1;
        }
        return ret;
    }
};

struct Fenwick {
    Vec<int> data;
    Fenwick(const int n): data(n + 1) { }

    void add(int i) {
        for (i += 1; i < (int) data.size(); i += i & -i) {
            data[i] ^= 1;
        }
    }

    int get(int i) const {
        int ret = 0;
        for (; i > 0; i -= i & -i) {
            ret ^= data[i];
        }
        return ret;
    }

    int fold(const int l, const int r) const {
        return get(l) ^ get(r);
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int N, Q;
    std::cin >> N >> Q;
    Vec<int> A(N), B(N), T(Q);
    for (int i = 0; i < N; ++i) {
        std::cin >> A[i] >> B[i];
    }
    for (int i = 0; i < Q; ++i) {
        std::cin >> T[i];
    }

    Vec<int> xs, ys, ts;
    xs.reserve(N);
    ys.reserve(N);
    for (int i = 0; i < N; ++i) {
        xs.push_back(std::min(A[i], B[i]));
        ys.push_back(std::max(A[i], B[i]));
    }
    fix(xs);
    fix(ys);
    ts = T;
    fix(ts);

    Vec<int> xid(N), yid(N), tid(Q);
    for (int i = 0; i < N; ++i) {
        xid[i] = lowb(xs, std::min(A[i], B[i]));
        yid[i] = lowb(ys, std::max(A[i], B[i]));
    }
    for (int i = 0; i < Q; ++i) {
        tid[i] = lowb(ts, T[i]);
    }

    Vec<int> time(N);
    {
        Vec<Vec<int>> qs(xs.size());
        for (int i = 0; i < N; ++i) {
            qs[xid[i]].push_back(i);
        }
        Vec<Vec<int>> add(xs.size());
        for (int i = 0; i < Q; ++i) {
            const auto x = upb(xs, T[i]);
            if (x > 0) {
                add[x - 1].push_back(i);
            }
        }
        Segtree seg(ys.size());
        for (int x = (int) xs.size() - 1; x >= 0; --x) {
            for (const auto i: add[x]) {
                seg.chmax(upb(ys, T[i]), (int) ys.size(), i);
            }
            for (const auto i: qs[x]) {
                time[i] = seg.get(yid[i]);
            }
        }
    }

    Vec<int> cnt(N);
    {
        Vec<Vec<int>> qs(Q);
        for (int i = 0; i < N; ++i) {
            if (time[i] != Q - 1) {
                qs[time[i] + 1].push_back(i);
            }
        }
        Fenwick fen(ts.size());
        for (int q = Q - 1; q >= 0; --q) {
            fen.add(tid[q]);
            for (const auto i: qs[q]) {
                cnt[i] = fen.fold(lowb(ts, std::max(A[i], B[i])), (int) ts.size());
            }
        }
    }

    long long sum = 0;
    for (int i = 0; i < N; ++i) {
        sum += ((time[i] >= 0 or A[i] >= B[i]) ^ cnt[i]) ? std::max(A[i], B[i]) : std::min(A[i], B[i]);
    }
    std::cout << sum << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 456 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 356 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 456 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 356 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 16 ms 2028 KB Output is correct
15 Correct 33 ms 3632 KB Output is correct
16 Correct 51 ms 5352 KB Output is correct
17 Correct 70 ms 7136 KB Output is correct
18 Correct 68 ms 7016 KB Output is correct
19 Correct 66 ms 7084 KB Output is correct
20 Correct 70 ms 7108 KB Output is correct
21 Correct 57 ms 6812 KB Output is correct
22 Correct 45 ms 6532 KB Output is correct
23 Correct 48 ms 6672 KB Output is correct
24 Correct 51 ms 6804 KB Output is correct
25 Correct 42 ms 6460 KB Output is correct
26 Correct 42 ms 4084 KB Output is correct
27 Correct 50 ms 4532 KB Output is correct
28 Correct 45 ms 4504 KB Output is correct
29 Correct 68 ms 5480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 2 ms 460 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 456 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 2 ms 460 KB Output is correct
11 Correct 2 ms 356 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 16 ms 2028 KB Output is correct
15 Correct 33 ms 3632 KB Output is correct
16 Correct 51 ms 5352 KB Output is correct
17 Correct 70 ms 7136 KB Output is correct
18 Correct 68 ms 7016 KB Output is correct
19 Correct 66 ms 7084 KB Output is correct
20 Correct 70 ms 7108 KB Output is correct
21 Correct 57 ms 6812 KB Output is correct
22 Correct 45 ms 6532 KB Output is correct
23 Correct 48 ms 6672 KB Output is correct
24 Correct 51 ms 6804 KB Output is correct
25 Correct 42 ms 6460 KB Output is correct
26 Correct 42 ms 4084 KB Output is correct
27 Correct 50 ms 4532 KB Output is correct
28 Correct 45 ms 4504 KB Output is correct
29 Correct 68 ms 5480 KB Output is correct
30 Correct 133 ms 12268 KB Output is correct
31 Correct 196 ms 15016 KB Output is correct
32 Correct 280 ms 20200 KB Output is correct
33 Correct 460 ms 34224 KB Output is correct
34 Correct 86 ms 10048 KB Output is correct
35 Correct 488 ms 34264 KB Output is correct
36 Correct 442 ms 34164 KB Output is correct
37 Correct 462 ms 34132 KB Output is correct
38 Correct 443 ms 34036 KB Output is correct
39 Correct 455 ms 34408 KB Output is correct
40 Correct 349 ms 32164 KB Output is correct
41 Correct 441 ms 34400 KB Output is correct
42 Correct 450 ms 34408 KB Output is correct
43 Correct 202 ms 31328 KB Output is correct
44 Correct 234 ms 31552 KB Output is correct
45 Correct 204 ms 31288 KB Output is correct
46 Correct 274 ms 32612 KB Output is correct
47 Correct 304 ms 33104 KB Output is correct
48 Correct 269 ms 21324 KB Output is correct
49 Correct 272 ms 21584 KB Output is correct