답안 #883369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883369 2023-12-05T08:27:28 Z boris_mihov Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
1939 ms 159832 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <cassert>
#include <vector>
#include <bitset>
#include <queue>
#include <set>

typedef long long llong;
const int MAXN = 1000000 + 10;
const int MAXLOG = 20;

const llong INF = 4e18;
const int INTINF = 2e9;

int n, q;
struct Sparse
{
    int sparse[MAXLOG][MAXN];
    int getLog[MAXN];

    void build(int a[])
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            sparse[0][i] = a[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
            {
                sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
            }
        }

        for (int i = 1 ; i <= n ; ++i)
        {
            getLog[i] = getLog[i - 1];
            if ((1 << getLog[i] + 1) < i) getLog[i]++;
        }
    }

    int findMAX(int l, int r)
    {
        assert(l - 1 <= r);
        if (l > r) return -INTINF;
        int log = getLog[r - l + 1];
        return std::max(sparse[log][l], sparse[log][r - (1 << log) + 1]);
    }
};

struct Fenwick
{
    int tree[MAXN];
    void update(int pos, int value)
    {
        for (int idx = pos ; idx <= n ; idx += idx & (-idx))
        {
            tree[idx] += value;
        }
    }

    int query(int pos)
    {
        int res = 0;
        for (int idx = pos ; idx > 0 ; idx -= idx & (-idx))
        {
            res += tree[idx];
        }

        return res;
    }

    void reset(int pos)
    {
        update(pos, -query(pos) + query(pos - 1));
    }

    int findKth(int k)
    {
        int pos = 0;
        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (pos + (1 << log) <= n && tree[pos + (1 << log)] < k)
            {
                pos += (1 << log);
                k -= tree[pos];
            }
        }

        return pos + 1;
    }
};

struct SegmentTree
{
    int tree[4*MAXN];
    void update(int l, int r, int node, int queryPos, int queryVal)
    {
        if (l == r)
        {
            tree[node] = queryVal;
            return;
        }

        int mid = (l + r) / 2;
        if (queryPos <= mid) update(l, mid, 2*node, queryPos, queryVal);
        else update(mid + 1, r, 2*node + 1, queryPos, queryVal);
        tree[node] = std::max(tree[2*node], tree[2*node + 1]);
    }

    int query(int l, int r, int node, int queryL, int queryR)
    {
        if (queryL <= l && r <= queryR)
        {
            return tree[node];
        }

        int res = 0;
        int mid = (l + r) / 2;
        if (queryL <= mid) res = std::max(res, query(l, mid, 2*node, queryL, queryR));
        if (mid + 1 <= queryR) res = std::max(res, query(mid + 1, r, 2*node + 1, queryL, queryR));
        return res;
    }

    void update(int pos, int val)
    {
        update(1, n, 1, pos, val);
    }

    int query(int l, int r)
    {
        return query(1, n, 1, l, r);
    }
};

struct Query
{
    int l, r, k, idx;
    friend bool operator > (const Query &a, const Query &b)
    {
        return a.k > b.k;
    }
};

int a[MAXN];
int perm[MAXN];
bool answer[MAXN];
Query query[MAXN];
SegmentTree tree;
Fenwick active;
Fenwick sorted;
Sparse sparse;

void addNum(int idx)
{
    int cntTo = active.query(idx - 1);
    if (cntTo > 0)
    {
        int prev = active.findKth(cntTo);
        sorted.reset(prev);
        if (a[prev] > a[idx]) sorted.update(prev, 1);
        tree.update(prev, sparse.findMAX(prev + 1, idx - 1) + a[prev]);
    }

    if (active.query(n) == cntTo)
    {
        tree.update(idx, sparse.findMAX(idx + 1, n) + a[idx]);
    } else
    {
        int next = active.findKth(cntTo + 1);
        tree.update(idx, sparse.findMAX(idx + 1, next - 1) + a[idx]);
        if (a[idx] > a[next])
        {
            sorted.update(idx, 1);
        }
    }

    active.update(idx, 1);
}

void solve()
{
    std::iota(perm + 1, perm + 1 + n, 1);
    std::sort(perm + 1, perm + 1 + n, [&](int x, int y)
    {
        return a[x] > a[y];
    });

    std::sort(query + 1, query + 1 + q, std::greater <Query> ());
    int numsPtr = 0;

    sparse.build(a);
    for (int i = 1 ; i <= q ; ++i)
    {
        auto [l, r, k, idx] = query[i];
        while (numsPtr + 1 <= n && 2 * a[perm[numsPtr + 1]] > k)
        {
            numsPtr++;
            addNum(perm[numsPtr]);
        }

        int toL = active.query(l - 1);
        int toR = active.query(r);

        if (toL == toR)
        {
            answer[idx] = true;
            continue;
        } 

        int posL = active.findKth(toL + 1);
        int posR = active.findKth(toR);

        assert(l <= posL && posL <= posR && posR <= r);
        if (sorted.query(posR - 1) - sorted.query(posL - 1) > 0)
        {
            answer[idx] = false;
            continue;
        }

        int res = 0;
        if (posR < r)
        {
            res = std::max(res, a[posR] + sparse.findMAX(posR + 1, r));
        }

        if (posL < posR) res = std::max(res, tree.query(posL, posR - 1));
        answer[idx] = (res <= k);
    }
}

void print()
{
    for (int i = 1 ; i <= q ; ++i)
    {
        std::cout << answer[i] << '\n';
    }
}

void input()
{
    std::cin >> n >> q;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i];
    }

    for (int i = 1 ; i <= q ; ++i)
    {
        std::cin >> query[i].l >> query[i].r >> query[i].k;
        query[i].idx = i;
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();
    print();

    return 0;
}

Compilation message

sortbooks.cpp: In member function 'void Sparse::build(int*)':
sortbooks.cpp:35:93: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   35 |                 sparse[log][i] = std::max(sparse[log - 1][i], sparse[log - 1][i + (1 << log - 1)]);
      |                                                                                         ~~~~^~~
sortbooks.cpp:42:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   42 |             if ((1 << getLog[i] + 1) < i) getLog[i]++;
      |                       ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 12632 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 4 ms 22876 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 22876 KB Output is correct
6 Correct 3 ms 24920 KB Output is correct
7 Correct 3 ms 24924 KB Output is correct
8 Correct 3 ms 24924 KB Output is correct
9 Correct 3 ms 24920 KB Output is correct
10 Correct 3 ms 24924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 12632 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 4 ms 22876 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 22876 KB Output is correct
6 Correct 3 ms 24920 KB Output is correct
7 Correct 3 ms 24924 KB Output is correct
8 Correct 3 ms 24924 KB Output is correct
9 Correct 3 ms 24920 KB Output is correct
10 Correct 3 ms 24924 KB Output is correct
11 Correct 7 ms 29020 KB Output is correct
12 Correct 9 ms 33372 KB Output is correct
13 Correct 8 ms 33392 KB Output is correct
14 Correct 9 ms 33368 KB Output is correct
15 Correct 9 ms 33368 KB Output is correct
16 Correct 7 ms 33372 KB Output is correct
17 Correct 7 ms 33332 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1890 ms 126080 KB Output is correct
2 Correct 1900 ms 126088 KB Output is correct
3 Correct 1799 ms 126076 KB Output is correct
4 Correct 1841 ms 126076 KB Output is correct
5 Correct 1336 ms 124756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 55428 KB Output is correct
2 Correct 116 ms 56916 KB Output is correct
3 Correct 96 ms 57140 KB Output is correct
4 Correct 96 ms 57252 KB Output is correct
5 Correct 97 ms 57160 KB Output is correct
6 Correct 60 ms 56660 KB Output is correct
7 Correct 61 ms 56656 KB Output is correct
8 Correct 82 ms 56904 KB Output is correct
9 Correct 34 ms 30544 KB Output is correct
10 Correct 81 ms 56912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 12632 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 4 ms 22876 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 22876 KB Output is correct
6 Correct 3 ms 24920 KB Output is correct
7 Correct 3 ms 24924 KB Output is correct
8 Correct 3 ms 24924 KB Output is correct
9 Correct 3 ms 24920 KB Output is correct
10 Correct 3 ms 24924 KB Output is correct
11 Correct 7 ms 29020 KB Output is correct
12 Correct 9 ms 33372 KB Output is correct
13 Correct 8 ms 33392 KB Output is correct
14 Correct 9 ms 33368 KB Output is correct
15 Correct 9 ms 33368 KB Output is correct
16 Correct 7 ms 33372 KB Output is correct
17 Correct 7 ms 33332 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 269 ms 71672 KB Output is correct
20 Correct 292 ms 73576 KB Output is correct
21 Correct 266 ms 73376 KB Output is correct
22 Correct 276 ms 73540 KB Output is correct
23 Correct 259 ms 73308 KB Output is correct
24 Correct 181 ms 72528 KB Output is correct
25 Correct 188 ms 72336 KB Output is correct
26 Correct 187 ms 72680 KB Output is correct
27 Correct 187 ms 72664 KB Output is correct
28 Correct 193 ms 72756 KB Output is correct
29 Correct 186 ms 72788 KB Output is correct
30 Correct 191 ms 72784 KB Output is correct
31 Correct 185 ms 72784 KB Output is correct
32 Correct 186 ms 72680 KB Output is correct
33 Correct 188 ms 72600 KB Output is correct
34 Correct 145 ms 72016 KB Output is correct
35 Correct 151 ms 72124 KB Output is correct
36 Correct 145 ms 71764 KB Output is correct
37 Correct 146 ms 71708 KB Output is correct
38 Correct 145 ms 72016 KB Output is correct
39 Correct 168 ms 71504 KB Output is correct
40 Correct 118 ms 64008 KB Output is correct
41 Correct 120 ms 70216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 12632 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 4 ms 22876 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 22876 KB Output is correct
6 Correct 3 ms 24920 KB Output is correct
7 Correct 3 ms 24924 KB Output is correct
8 Correct 3 ms 24924 KB Output is correct
9 Correct 3 ms 24920 KB Output is correct
10 Correct 3 ms 24924 KB Output is correct
11 Correct 7 ms 29020 KB Output is correct
12 Correct 9 ms 33372 KB Output is correct
13 Correct 8 ms 33392 KB Output is correct
14 Correct 9 ms 33368 KB Output is correct
15 Correct 9 ms 33368 KB Output is correct
16 Correct 7 ms 33372 KB Output is correct
17 Correct 7 ms 33332 KB Output is correct
18 Correct 6 ms 33372 KB Output is correct
19 Correct 1890 ms 126080 KB Output is correct
20 Correct 1900 ms 126088 KB Output is correct
21 Correct 1799 ms 126076 KB Output is correct
22 Correct 1841 ms 126076 KB Output is correct
23 Correct 1336 ms 124756 KB Output is correct
24 Correct 122 ms 55428 KB Output is correct
25 Correct 116 ms 56916 KB Output is correct
26 Correct 96 ms 57140 KB Output is correct
27 Correct 96 ms 57252 KB Output is correct
28 Correct 97 ms 57160 KB Output is correct
29 Correct 60 ms 56660 KB Output is correct
30 Correct 61 ms 56656 KB Output is correct
31 Correct 82 ms 56904 KB Output is correct
32 Correct 34 ms 30544 KB Output is correct
33 Correct 81 ms 56912 KB Output is correct
34 Correct 269 ms 71672 KB Output is correct
35 Correct 292 ms 73576 KB Output is correct
36 Correct 266 ms 73376 KB Output is correct
37 Correct 276 ms 73540 KB Output is correct
38 Correct 259 ms 73308 KB Output is correct
39 Correct 181 ms 72528 KB Output is correct
40 Correct 188 ms 72336 KB Output is correct
41 Correct 187 ms 72680 KB Output is correct
42 Correct 187 ms 72664 KB Output is correct
43 Correct 193 ms 72756 KB Output is correct
44 Correct 186 ms 72788 KB Output is correct
45 Correct 191 ms 72784 KB Output is correct
46 Correct 185 ms 72784 KB Output is correct
47 Correct 186 ms 72680 KB Output is correct
48 Correct 188 ms 72600 KB Output is correct
49 Correct 145 ms 72016 KB Output is correct
50 Correct 151 ms 72124 KB Output is correct
51 Correct 145 ms 71764 KB Output is correct
52 Correct 146 ms 71708 KB Output is correct
53 Correct 145 ms 72016 KB Output is correct
54 Correct 168 ms 71504 KB Output is correct
55 Correct 118 ms 64008 KB Output is correct
56 Correct 120 ms 70216 KB Output is correct
57 Correct 1939 ms 159644 KB Output is correct
58 Correct 1881 ms 159676 KB Output is correct
59 Correct 1849 ms 159552 KB Output is correct
60 Correct 1856 ms 159744 KB Output is correct
61 Correct 1901 ms 159832 KB Output is correct
62 Correct 1858 ms 159676 KB Output is correct
63 Correct 929 ms 154448 KB Output is correct
64 Correct 926 ms 153940 KB Output is correct
65 Correct 1035 ms 156220 KB Output is correct
66 Correct 1026 ms 156500 KB Output is correct
67 Correct 1049 ms 156148 KB Output is correct
68 Correct 1012 ms 156372 KB Output is correct
69 Correct 1108 ms 156284 KB Output is correct
70 Correct 1029 ms 156584 KB Output is correct
71 Correct 1034 ms 156388 KB Output is correct
72 Correct 1020 ms 156388 KB Output is correct
73 Correct 754 ms 150340 KB Output is correct
74 Correct 766 ms 151412 KB Output is correct
75 Correct 757 ms 150548 KB Output is correct
76 Correct 751 ms 150320 KB Output is correct
77 Correct 807 ms 150556 KB Output is correct
78 Correct 912 ms 151036 KB Output is correct
79 Correct 624 ms 135620 KB Output is correct
80 Correct 622 ms 141128 KB Output is correct