답안 #755096

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
755096 2023-06-09T11:51:16 Z boris_mihov 송신탑 (IOI22_towers) C++17
17 / 100
1317 ms 57808 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXLOG = 20 + 5;
const int MAXN = 100000 + 10;
const int INF  = 1e9;

int n;
struct MST
{
    std::vector <int> tree[4*MAXN];
    void build(int l, int r, int node, int vals[])
    {
        if (l == r)
        {
            tree[node].push_back(vals[l]);
            return;
        }

        int mid = (l + r) / 2;
        build(l, mid, 2*node, vals);
        build(mid + 1, r, 2*node + 1, vals);
        tree[node].reserve(r - l + 1);

        int lPtr = 0, rPtr = 0;
        for (int i = l ; i <= r ; ++i)
        {
            if (lPtr == tree[2*node].size())
            {
                tree[node].push_back(tree[2*node + 1][rPtr++]);
                continue;
            }

            if (rPtr == tree[2*node + 1].size())
            {
                tree[node].push_back(tree[2*node][lPtr++]);
                continue;
            }

            if (tree[2*node][lPtr] < tree[2*node + 1][rPtr])
            {
                tree[node].push_back(tree[2*node][lPtr++]);
            } else
            {
                tree[node].push_back(tree[2*node + 1][rPtr++]);
            }
        }
    }

    int binaryCount(int node, int val)
    {
        int l = -1, r = tree[node].size(), mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (tree[node][mid] <= val) l = mid;
            else r = mid;
        }

        return r;
    }

    int binaryFirst(int node, int val)
    {
        int l = -1, r = tree[node].size(), mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (tree[node][mid] < val) l = mid;
            else r = mid;
        }

        return (r == tree[node].size() ? INF : tree[node][r]);
    }

    int binaryLast(int node, int val)
    {
        int l = -1, r = tree[node].size(), mid;
        while (l < r - 1)
        {
            mid = (l + r) / 2;
            if (tree[node][mid] <= val) l = mid;
            else r = mid;
        }

        return (l == -1 ? 0 : tree[node][l]);
    }

    int queryCount(int l, int r, int node, int queryL, int queryR, int queryValL, int queryValR)
    {
        if (queryL <= l && r <= queryR)
        {
            return binaryCount(node, queryValR) - binaryCount(node, queryValL - 1);
        }

        int res = 0;
        int mid = (l + r) / 2;
        if (queryL <= mid) res += queryCount(l, mid, 2*node, queryL, queryR, queryValL, queryValR);
        if (mid + 1 <= queryR) res += queryCount(mid + 1, r, 2*node + 1, queryL, queryR, queryValL, queryValR);
        return res;
    }

    int queryFirst(int l, int r, int node, int queryL, int queryR, int queryVal)
    {
        if (queryL <= l && r <= queryR)
        {
            return binaryFirst(node, queryVal);
        }

        int res = INF;
        int mid = (l + r) / 2;
        if (queryL <= mid) res = std::min(res, queryFirst(l, mid, 2*node, queryL, queryR, queryVal));
        if (mid + 1 <= queryR) res = std::min(res, queryFirst(mid + 1, r, 2*node + 1, queryL, queryR, queryVal));
        return res;
    }

    int queryLast(int l, int r, int node, int queryL, int queryR, int queryVal)
    {
        if (queryL <= l && r <= queryR)
        {
            return binaryLast(node, queryVal);
        }

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

    void build(int vals[])
    {
        build(1, n, 1, vals);
    }

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

    int queryFirst(int to, int l)
    {
        return queryFirst(1, n, 1, 1, to, l);
    }

    int queryLast(int to, int r)
    {
        return queryLast(1, n, 1, 1, to, r);
    }
};

MST left, right;
struct SparseMAX
{
    int sparseMAX[MAXLOG][MAXN];
    int vals[MAXN];
    int lg[MAXN];

    int cmp(int x, int y)
    {
        if (vals[x] > vals[y]) return x;
        return y;
    }

    void build(int _vals[])
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            sparseMAX[0][i] = i;
            vals[i] = _vals[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
            {
                sparseMAX[log][i] = cmp(sparseMAX[log - 1][i], sparseMAX[log - 1][i + (1 << log - 1)]);
            }
        }
    
        for (int i = 1 ; i <= n ; ++i)
        {
            lg[i] = lg[i - 1];
            if ((1 << lg[i] + 1) < i)
            {
                lg[i]++;
            }
        }
    }

    int findMAX(int l, int r)
    {
        int log = lg[r - l + 1];
        return vals[cmp(sparseMAX[log][l], sparseMAX[log][r - (1 << log) + 1])];
    }

    int findIDX(int l, int r)
    {
        int log = lg[r - l + 1];
        return cmp(sparseMAX[log][l], sparseMAX[log][r - (1 << log) + 1]);
    }
};

struct SparseMIN
{
    int sparseMIN[MAXLOG][MAXN];
    int vals[MAXN];
    int lg[MAXN];

    int cmp(int x, int y)
    {
        if (vals[x] < vals[y]) return x;
        return y;
    }

    void build(int _vals[])
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            sparseMIN[0][i] = i;
            vals[i] = _vals[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i + (1 << log) - 1 <= n ; ++i)
            {
                sparseMIN[log][i] = cmp(sparseMIN[log - 1][i], sparseMIN[log - 1][i + (1 << log - 1)]);
            }
        }
    
        for (int i = 1 ; i <= n ; ++i)
        {
            lg[i] = lg[i - 1];
            if ((1 << lg[i] + 1) < i)
            {
                lg[i]++;
            }
        }
    }

    int findMIN(int l, int r)
    {
        int log = lg[r - l + 1];
        return vals[cmp(sparseMIN[log][l], sparseMIN[log][r - (1 << log) + 1])];
    }

    int findIDX(int l, int r)
    {
        int log = lg[r - l + 1];
        return cmp(sparseMIN[log][l], sparseMIN[log][r - (1 << log) + 1]);
    }
};

int a[MAXN];
int b[MAXN];
int c[MAXN];
int d[MAXN];
int h[MAXN];
int perm[MAXN];
int cost[MAXN];
SparseMAX sparseMAX;
SparseMIN sparseMIN;
std::stack <int> st;
std::vector <int> v;
MST tree;

void init(int N, std::vector <int> H) 
{
    n = N;
    for (int i = 1 ; i <= n ; ++i)
    {
        h[i] = H[i - 1];
    }

    sparseMIN.build(h);
    sparseMAX.build(h);
    st.push(0);

    for (int i = 1 ; i <= n ; ++i)
    {
        while (h[st.top()] > h[i])
        {
            st.pop();
        }

        a[i] = st.top();
        st.push(i);
    }


    while (!st.empty())
    {
        st.pop();
    }

    st.push(n + 1);
    for (int i = n ; i >= 1 ; --i)
    {
        while (h[st.top()] > h[i])
        {
            st.pop();
        }

        c[i] = st.top();
        st.push(i);
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        if (a[i] == i - 1)
        {
            b[i] = 0;
        } else
        {
            b[i] = sparseMAX.findMAX(a[i] + 1, i - 1) - h[i];
        }

        if (c[i] == i + 1)
        {
            d[i] = 0;
        } else
        {
            d[i] = sparseMAX.findMAX(i + 1, c[i] - 1) - h[i];
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        cost[i] = INF; 
        if (a[i] > 0) cost[i] = std::min(cost[i], b[i]);
        if (c[i] < n + 1) cost[i] = std::min(cost[i], d[i]);
    }

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

    tree.build(perm);
}

int max_towers(int L, int R, int D) 
{
    L++; R++;
    int l = 0, r = n + 1, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (cost[perm[mid]] >= D) l = mid;
        else r = mid;
    }

    if (l == 0)
    {
        return 1;
    }

    int cnt = tree.queryCount(l, L, R);
    if (cnt == 0)
    {
        return 1;
    }

    int first = tree.queryFirst(l, L);
    int last = tree.queryLast(l, R);

    if (L <= a[first] && a[first] < first - 1)
    {
        int res = sparseMIN.findMIN(L, a[first]);
        if (res + D <= sparseMAX.findMAX(a[first] + 1, first - 1))
        {
            cnt++;
        }
    }

    if (c[last] <= R && last + 1 < c[last])
    {
        int res = sparseMIN.findMIN(c[last], R);
        if (res + D <= sparseMAX.findMAX(last + 1, c[last] - 1))
        {
            cnt++;
        }
    }

    return cnt;
}

Compilation message

towers.cpp: In member function 'void MST::build(int, int, int, int*)':
towers.cpp:33:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |             if (lPtr == tree[2*node].size())
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~
towers.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |             if (rPtr == tree[2*node + 1].size())
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
towers.cpp: In member function 'int MST::binaryFirst(int, int)':
towers.cpp:78:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |         return (r == tree[node].size() ? INF : tree[node][r]);
      |                 ~~^~~~~~~~~~~~~~~~~~~~
towers.cpp: In member function 'void SparseMAX::build(int*)':
towers.cpp:182:97: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  182 |                 sparseMAX[log][i] = cmp(sparseMAX[log - 1][i], sparseMAX[log - 1][i + (1 << log - 1)]);
      |                                                                                             ~~~~^~~
towers.cpp:189:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  189 |             if ((1 << lg[i] + 1) < i)
      |                       ~~~~~~^~~
towers.cpp: In member function 'void SparseMIN::build(int*)':
towers.cpp:233:97: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  233 |                 sparseMIN[log][i] = cmp(sparseMIN[log - 1][i], sparseMIN[log - 1][i + (1 << log - 1)]);
      |                                                                                             ~~~~^~~
towers.cpp:240:29: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  240 |             if ((1 << lg[i] + 1) < i)
      |                       ~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 547 ms 45368 KB 12th lines differ - on the 1st token, expected: '2', found: '1'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 28624 KB Output is correct
2 Correct 18 ms 28984 KB Output is correct
3 Correct 17 ms 29008 KB Output is correct
4 Correct 17 ms 29012 KB Output is correct
5 Correct 21 ms 28964 KB Output is correct
6 Correct 21 ms 29008 KB Output is correct
7 Incorrect 18 ms 28952 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 28624 KB Output is correct
2 Correct 18 ms 28984 KB Output is correct
3 Correct 17 ms 29008 KB Output is correct
4 Correct 17 ms 29012 KB Output is correct
5 Correct 21 ms 28964 KB Output is correct
6 Correct 21 ms 29008 KB Output is correct
7 Incorrect 18 ms 28952 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 933 ms 57200 KB 5th lines differ - on the 1st token, expected: '571', found: '570'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 365 ms 34964 KB Output is correct
2 Correct 1274 ms 57288 KB Output is correct
3 Correct 1117 ms 57304 KB Output is correct
4 Correct 1149 ms 57296 KB Output is correct
5 Correct 1271 ms 57284 KB Output is correct
6 Correct 1317 ms 57392 KB Output is correct
7 Correct 1181 ms 57372 KB Output is correct
8 Correct 966 ms 57404 KB Output is correct
9 Correct 972 ms 57808 KB Output is correct
10 Correct 1026 ms 57408 KB Output is correct
11 Correct 806 ms 57404 KB Output is correct
12 Correct 75 ms 57288 KB Output is correct
13 Correct 72 ms 57392 KB Output is correct
14 Correct 95 ms 57340 KB Output is correct
15 Correct 79 ms 57808 KB Output is correct
16 Correct 67 ms 57344 KB Output is correct
17 Correct 72 ms 56388 KB Output is correct
18 Correct 72 ms 57272 KB Output is correct
19 Correct 85 ms 57284 KB Output is correct
20 Correct 73 ms 57296 KB Output is correct
21 Correct 77 ms 57396 KB Output is correct
22 Correct 81 ms 57300 KB Output is correct
23 Correct 84 ms 57284 KB Output is correct
24 Correct 74 ms 57364 KB Output is correct
25 Correct 66 ms 57804 KB Output is correct
26 Correct 84 ms 57404 KB Output is correct
27 Correct 75 ms 57664 KB Output is correct
28 Correct 17 ms 29004 KB Output is correct
29 Correct 17 ms 28948 KB Output is correct
30 Correct 17 ms 28944 KB Output is correct
31 Correct 17 ms 29008 KB Output is correct
32 Correct 17 ms 29008 KB Output is correct
33 Correct 16 ms 28752 KB Output is correct
34 Correct 17 ms 29032 KB Output is correct
35 Correct 19 ms 29016 KB Output is correct
36 Correct 16 ms 29008 KB Output is correct
37 Correct 20 ms 29000 KB Output is correct
38 Correct 17 ms 29008 KB Output is correct
39 Correct 16 ms 29008 KB Output is correct
40 Correct 18 ms 28968 KB Output is correct
41 Correct 22 ms 28996 KB Output is correct
42 Correct 18 ms 29008 KB Output is correct
43 Correct 17 ms 29024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 28624 KB Output is correct
2 Correct 18 ms 28984 KB Output is correct
3 Correct 17 ms 29008 KB Output is correct
4 Correct 17 ms 29012 KB Output is correct
5 Correct 21 ms 28964 KB Output is correct
6 Correct 21 ms 29008 KB Output is correct
7 Incorrect 18 ms 28952 KB 1st lines differ - on the 1st token, expected: '34', found: '33'
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 547 ms 45368 KB 12th lines differ - on the 1st token, expected: '2', found: '1'
2 Halted 0 ms 0 KB -