답안 #705665

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705665 2023-03-04T23:31:29 Z danikoynov 송신탑 (IOI22_towers) C++17
23 / 100
4000 ms 21080 KB
#include "towers.h"

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;

int n, h[maxn], pref[maxn], peak_pos;
int cnt = 0;


int par[maxn], rnk[maxn];

int find_leader(int v)
{
    return (v == par[v]) ? v : par[v] = find_leader(par[v]);
}

void unite(int v, int u)
{
    v = find_leader(v);
    u = find_leader(u);
    if (v == u)
        return;

    if (rnk[v] < rnk[u])
        swap(v, u);
    rnk[v] += rnk[u];
    par[u] = v;
}

int dp[maxn], bef[maxn], aft[maxn];


struct node
{
    int max_number, min_number;

    node(int _max_number = 0, int _min_number = 2e9)
    {
        max_number = _max_number;
        min_number = _min_number;
    }
};

node tree[4 * maxn];
node merge_node(node left, node right)
{
    node comb;
    comb.max_number = max(left.max_number, right.max_number);
    comb.min_number = min(left.min_number, right.min_number);
    return comb;
}
void build_tree(int root, int left, int right)
{
    if (left == right)
    {
        tree[root] = node(h[left], h[left]);
        return;
    }

    int mid = (left + right) / 2;
    build_tree(root * 2, left, mid);
    build_tree(root * 2 + 1, mid + 1, right);

    tree[root] = merge_node(tree[root * 2], tree[root * 2 + 1]);
}
void update(int root, int left, int right, int pos, int val)
{
    if (left == right)
    {
        tree[root] = val;
        return;
    }

    int mid = (left + right) / 2;
    if (pos <= mid)
        update(root * 2, left, mid, pos, val);
    else
        update(root * 2 + 1, mid + 1, right, pos, val);

    tree[root] = merge_node(tree[root * 2], tree[root * 2 + 1]);
}

node query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
        return 0;

    if (left >= qleft && right <= qright)
        return tree[root];

    int mid = (left + right) / 2;
    return merge_node(query(root * 2, left, mid, qleft, qright),
                      query(root * 2 + 1, mid + 1, right, qleft, qright));
}

int find_rightmost(int root, int left, int right, int qleft, int qright, int val)
{
    ///cout << root << " " << left << " " << right << " " << qleft << " " << qright << " " << val << endl;
    if (tree[root].max_number < val ||
            left > qright || right < qleft)
        return 0;

    if (left == right)
        return left;

    int mid = (left + right) / 2;
    if (left >= qleft && right <= qright)
    {
        if (tree[root * 2 + 1].max_number >= val)
            return find_rightmost(root * 2 + 1, mid + 1, right, qleft, qright, val);
        return find_rightmost(root * 2, left, mid, qleft, qright, val);
    }

    return max(find_rightmost(root * 2, left, mid, qleft, qright, val),
               find_rightmost(root * 2 + 1, mid + 1, right, qleft, qright, val));
}

int find_leftmost(int root, int left, int right, int qleft, int qright, int val)
{
    if (tree[root].max_number < val ||
            left > qright || right < qleft)
        return n + 1;

    if (left == right)
        return left;
    int mid = (left + right) / 2;
    if (left >= qleft && right <= qright)
    {
        if (tree[root * 2].max_number >= val)
            return find_leftmost(root * 2, left, mid, qleft, qright, val);
        return find_leftmost(root * 2 + 1, mid + 1, right, qleft, qright, val);

    }

    return min(find_leftmost(root * 2, left, mid, qleft, qright, val),
               find_leftmost(root * 2 + 1, mid + 1, right, qleft, qright, val));
}



struct diff_node
{
    int max_number, min_number;
    int diff_to_right;
    int diff_to_left;

    diff_node()
    {
        max_number = 0;
        min_number = 2e9;
        diff_to_right = 0;
        diff_to_left = 0;
    }
};

diff_node merge_diff(diff_node dn1, diff_node dn2)
{
    diff_node res;
    res.max_number = max(dn1.max_number, dn2.max_number);
    res.min_number = min(dn1.min_number, dn2.min_number);
    res.diff_to_right = max(dn2.max_number - dn1.min_number, max(dn1.diff_to_right, dn2.diff_to_right));
    res.diff_to_left = max(dn1.max_number - dn2.min_number, max(dn1.diff_to_left, dn2.diff_to_left));
    return res;
}

diff_node diff_tree[4 * maxn];
void build_diff_tree(int root, int left, int right)
{
    if (left == right)
    {
        diff_tree[root].max_number = diff_tree[root].min_number = h[left];
        return;
    }

    int mid = (left + right) / 2;
    build_diff_tree(root * 2, left, mid);
    build_diff_tree(root * 2 + 1, mid + 1, right);

    diff_tree[root] = merge_diff(diff_tree[root * 2], diff_tree[root * 2 + 1]);
}

diff_node diff_query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
        return diff_node();

    if (left >= qleft && right <= qright)
        return diff_tree[root];

    int mid = (left + right) / 2;
    return merge_diff(diff_query(root * 2, left, mid, qleft, qright),
                      diff_query(root * 2 + 1, mid + 1, right, qleft, qright));
}

vector < pair < int, int > > val;
unordered_map < int, int > rev;
void init(int N, std::vector<int> H)
{
    n = N;
    for (int i = 0; i < n; i ++)
        h[i + 1] = H[i], rev[H[i]] = i + 1;

    for (int i = 1; i <= n; i ++)
        if (h[i] > h[i - 1] && h[i] > h[i + 1])
            cnt ++;
    for (int i = 2; i < n; i ++)
    {
        pref[i] = pref[i - 1];
        if (h[i] > h[i - 1] && h[i] > h[i + 1])
        {
            pref[i] ++, peak_pos = i;

        }
    }

    build_tree(1, 1, n);
    build_diff_tree(1, 1, n);
    vector < int > st;
    st.push_back(0);
    for (int i = 1; i <= n; i ++)
    {


        while(!st.empty() && h[st.back()] >= h[i])
            st.pop_back();
        bef[i] = st.back();
        st.push_back(i);
    }

    st.clear();
    st.push_back(n + 1);
    for (int i = n; i > 0; i --)
    {


        while(!st.empty() && h[st.back()] >= h[i])
            st.pop_back();
        aft[i] = st.back();
        st.push_back(i);
    }

    for (int i = 1; i <= n; i ++)
    {

        ///cout << bef[i] << " " << aft[i] << endl;
        int h1 = 0, h2 = 0;
        if (bef[i] != i - 1)
            h1 = query(1, 1, n, bef[i] + 1, i - 1).max_number;
        if (bef[i] == 0)
            h1 = 2e9 + 10;
        if (aft[i] != i + 1)
            h2 = query(1, 1, n, i + 1, aft[i] - 1).max_number;
        if (aft[i] == n + 1)
            h2 = 2e9 + 10;
        val.push_back({min(h1, h2) - h[i], i});
        ///cout << query(1, 1, n, bef[i] + 1)
    }

    sort(val.begin(), val.end());
    //for (int v : val)
    //    cout << v << " ";
    // cout << endl;
}


vector < int > act[maxn];
int cnt_query = 0;
int max_towers(int L, int R, int D)
{

    L ++;
    R ++;
    cnt_query ++;


    int lf = 0, rf = (int)(val.size()) - 1;
    while(lf <= rf)
    {
        int mf = (lf + rf) / 2;
        if (val[mf].first < D)
            lf = mf + 1;
        else
            rf = mf - 1;
    }
    ///vector < int > towers;
    int leftmost = n + 1, rightmost = 0, ans = 0;
    for (int i = lf; i < val.size(); i ++)
    {
        if (val[i].second >= L && val[i].second <= R)
        {
            ans ++;
            ///towers.push_back(val[i].second);
            leftmost = min(leftmost, val[i].second);
            rightmost = max(rightmost, val[i].second);
        }
    }

    if (leftmost > rightmost)
    {
        int lowest_tower = rev[query(1, 1, n, L, R).min_number];
        leftmost = lowest_tower;
        rightmost = lowest_tower;
        ans = 1;
    }

    int highest_tower = 0;
    int La = find_rightmost(1, 1, n, 1, leftmost, h[leftmost] + D);
    diff_node dn = diff_query(1, 1, n, L, La);
    if (dn.diff_to_right >= D)
        ans ++;
    ///cout << La << endl;
    ///cout << leftmost << " " << rightmost << endl;
    /**for (int tower = leftmost - 1; tower >= L; tower --)
    {
        if (tower <= La && h[tower] + D <= highest_tower &&
                highest_tower >= h[leftmost] + D)
        {
            ans ++;
            break;
        }
        highest_tower = max(highest_tower, h[tower]);
    }*/

    highest_tower = 0;
    int Ra = find_leftmost(1, 1, n, rightmost, n, h[rightmost] + D);
    for (int tower = rightmost + 1; tower <= R; tower ++)
    {

        if (tower >= Ra && h[tower] + D <= highest_tower &&
                highest_tower >= h[rightmost] + D)
        {
            ans ++;
            break;
        }
        highest_tower = max(highest_tower, h[tower]);
    }

    return ans;



}

Compilation message

towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:288:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  288 |     for (int i = lf; i < val.size(); i ++)
      |                      ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1158 ms 17112 KB Output is correct
2 Execution timed out 4016 ms 20984 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11984 KB Output is correct
2 Correct 6 ms 12112 KB Output is correct
3 Correct 6 ms 12112 KB Output is correct
4 Correct 6 ms 12112 KB Output is correct
5 Correct 7 ms 12176 KB Output is correct
6 Correct 6 ms 12100 KB Output is correct
7 Correct 7 ms 12168 KB Output is correct
8 Correct 7 ms 12112 KB Output is correct
9 Correct 8 ms 12112 KB Output is correct
10 Correct 7 ms 12240 KB Output is correct
11 Correct 7 ms 12112 KB Output is correct
12 Correct 7 ms 11984 KB Output is correct
13 Correct 7 ms 12112 KB Output is correct
14 Correct 7 ms 12112 KB Output is correct
15 Correct 7 ms 12112 KB Output is correct
16 Correct 6 ms 12112 KB Output is correct
17 Correct 6 ms 12112 KB Output is correct
18 Correct 6 ms 12112 KB Output is correct
19 Correct 7 ms 12112 KB Output is correct
20 Correct 7 ms 12112 KB Output is correct
21 Correct 7 ms 12112 KB Output is correct
22 Correct 7 ms 12112 KB Output is correct
23 Correct 7 ms 12240 KB Output is correct
24 Correct 8 ms 12216 KB Output is correct
25 Correct 7 ms 12112 KB Output is correct
26 Correct 10 ms 12112 KB Output is correct
27 Correct 10 ms 12184 KB Output is correct
28 Correct 7 ms 12112 KB Output is correct
29 Correct 8 ms 12112 KB Output is correct
30 Correct 8 ms 12112 KB Output is correct
31 Correct 7 ms 12112 KB Output is correct
32 Correct 7 ms 12112 KB Output is correct
33 Correct 7 ms 12112 KB Output is correct
34 Correct 6 ms 12112 KB Output is correct
35 Correct 6 ms 12112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11984 KB Output is correct
2 Correct 6 ms 12112 KB Output is correct
3 Correct 6 ms 12112 KB Output is correct
4 Correct 6 ms 12112 KB Output is correct
5 Correct 7 ms 12176 KB Output is correct
6 Correct 6 ms 12100 KB Output is correct
7 Correct 7 ms 12168 KB Output is correct
8 Correct 7 ms 12112 KB Output is correct
9 Correct 8 ms 12112 KB Output is correct
10 Correct 7 ms 12240 KB Output is correct
11 Correct 7 ms 12112 KB Output is correct
12 Correct 7 ms 11984 KB Output is correct
13 Correct 7 ms 12112 KB Output is correct
14 Correct 7 ms 12112 KB Output is correct
15 Correct 7 ms 12112 KB Output is correct
16 Correct 6 ms 12112 KB Output is correct
17 Correct 6 ms 12112 KB Output is correct
18 Correct 6 ms 12112 KB Output is correct
19 Correct 7 ms 12112 KB Output is correct
20 Correct 7 ms 12112 KB Output is correct
21 Correct 7 ms 12112 KB Output is correct
22 Correct 7 ms 12112 KB Output is correct
23 Correct 7 ms 12240 KB Output is correct
24 Correct 8 ms 12216 KB Output is correct
25 Correct 7 ms 12112 KB Output is correct
26 Correct 10 ms 12112 KB Output is correct
27 Correct 10 ms 12184 KB Output is correct
28 Correct 7 ms 12112 KB Output is correct
29 Correct 8 ms 12112 KB Output is correct
30 Correct 8 ms 12112 KB Output is correct
31 Correct 7 ms 12112 KB Output is correct
32 Correct 7 ms 12112 KB Output is correct
33 Correct 7 ms 12112 KB Output is correct
34 Correct 6 ms 12112 KB Output is correct
35 Correct 6 ms 12112 KB Output is correct
36 Correct 47 ms 17276 KB Output is correct
37 Correct 62 ms 20668 KB Output is correct
38 Correct 64 ms 20600 KB Output is correct
39 Correct 63 ms 20608 KB Output is correct
40 Correct 61 ms 20752 KB Output is correct
41 Correct 60 ms 20672 KB Output is correct
42 Correct 58 ms 20636 KB Output is correct
43 Correct 50 ms 20980 KB Output is correct
44 Correct 49 ms 21068 KB Output is correct
45 Correct 59 ms 20856 KB Output is correct
46 Correct 65 ms 20852 KB Output is correct
47 Correct 63 ms 20752 KB Output is correct
48 Correct 59 ms 20600 KB Output is correct
49 Correct 61 ms 20600 KB Output is correct
50 Correct 50 ms 21044 KB Output is correct
51 Correct 62 ms 20960 KB Output is correct
52 Correct 66 ms 20592 KB Output is correct
53 Correct 59 ms 20604 KB Output is correct
54 Correct 63 ms 20564 KB Output is correct
55 Correct 49 ms 20992 KB Output is correct
56 Correct 59 ms 20860 KB Output is correct
57 Correct 63 ms 20472 KB Output is correct
58 Correct 64 ms 20668 KB Output is correct
59 Correct 63 ms 20604 KB Output is correct
60 Correct 59 ms 20664 KB Output is correct
61 Correct 57 ms 20608 KB Output is correct
62 Correct 71 ms 20684 KB Output is correct
63 Correct 62 ms 20640 KB Output is correct
64 Correct 50 ms 21052 KB Output is correct
65 Correct 51 ms 21080 KB Output is correct
66 Correct 56 ms 20860 KB Output is correct
67 Correct 56 ms 21008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4002 ms 20624 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 435 ms 14136 KB Output is correct
2 Correct 2742 ms 20852 KB Output is correct
3 Correct 2767 ms 20608 KB Output is correct
4 Correct 3657 ms 20596 KB Output is correct
5 Correct 3934 ms 20580 KB Output is correct
6 Correct 3523 ms 20688 KB Output is correct
7 Execution timed out 4030 ms 20688 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11984 KB Output is correct
2 Correct 6 ms 12112 KB Output is correct
3 Correct 6 ms 12112 KB Output is correct
4 Correct 6 ms 12112 KB Output is correct
5 Correct 7 ms 12176 KB Output is correct
6 Correct 6 ms 12100 KB Output is correct
7 Correct 7 ms 12168 KB Output is correct
8 Correct 7 ms 12112 KB Output is correct
9 Correct 8 ms 12112 KB Output is correct
10 Correct 7 ms 12240 KB Output is correct
11 Correct 7 ms 12112 KB Output is correct
12 Correct 7 ms 11984 KB Output is correct
13 Correct 7 ms 12112 KB Output is correct
14 Correct 7 ms 12112 KB Output is correct
15 Correct 7 ms 12112 KB Output is correct
16 Correct 6 ms 12112 KB Output is correct
17 Correct 6 ms 12112 KB Output is correct
18 Correct 6 ms 12112 KB Output is correct
19 Correct 7 ms 12112 KB Output is correct
20 Correct 7 ms 12112 KB Output is correct
21 Correct 7 ms 12112 KB Output is correct
22 Correct 7 ms 12112 KB Output is correct
23 Correct 7 ms 12240 KB Output is correct
24 Correct 8 ms 12216 KB Output is correct
25 Correct 7 ms 12112 KB Output is correct
26 Correct 10 ms 12112 KB Output is correct
27 Correct 10 ms 12184 KB Output is correct
28 Correct 7 ms 12112 KB Output is correct
29 Correct 8 ms 12112 KB Output is correct
30 Correct 8 ms 12112 KB Output is correct
31 Correct 7 ms 12112 KB Output is correct
32 Correct 7 ms 12112 KB Output is correct
33 Correct 7 ms 12112 KB Output is correct
34 Correct 6 ms 12112 KB Output is correct
35 Correct 6 ms 12112 KB Output is correct
36 Correct 47 ms 17276 KB Output is correct
37 Correct 62 ms 20668 KB Output is correct
38 Correct 64 ms 20600 KB Output is correct
39 Correct 63 ms 20608 KB Output is correct
40 Correct 61 ms 20752 KB Output is correct
41 Correct 60 ms 20672 KB Output is correct
42 Correct 58 ms 20636 KB Output is correct
43 Correct 50 ms 20980 KB Output is correct
44 Correct 49 ms 21068 KB Output is correct
45 Correct 59 ms 20856 KB Output is correct
46 Correct 65 ms 20852 KB Output is correct
47 Correct 63 ms 20752 KB Output is correct
48 Correct 59 ms 20600 KB Output is correct
49 Correct 61 ms 20600 KB Output is correct
50 Correct 50 ms 21044 KB Output is correct
51 Correct 62 ms 20960 KB Output is correct
52 Correct 66 ms 20592 KB Output is correct
53 Correct 59 ms 20604 KB Output is correct
54 Correct 63 ms 20564 KB Output is correct
55 Correct 49 ms 20992 KB Output is correct
56 Correct 59 ms 20860 KB Output is correct
57 Correct 63 ms 20472 KB Output is correct
58 Correct 64 ms 20668 KB Output is correct
59 Correct 63 ms 20604 KB Output is correct
60 Correct 59 ms 20664 KB Output is correct
61 Correct 57 ms 20608 KB Output is correct
62 Correct 71 ms 20684 KB Output is correct
63 Correct 62 ms 20640 KB Output is correct
64 Correct 50 ms 21052 KB Output is correct
65 Correct 51 ms 21080 KB Output is correct
66 Correct 56 ms 20860 KB Output is correct
67 Correct 56 ms 21008 KB Output is correct
68 Execution timed out 4002 ms 20624 KB Time limit exceeded
69 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1158 ms 17112 KB Output is correct
2 Execution timed out 4016 ms 20984 KB Time limit exceeded
3 Halted 0 ms 0 KB -