Submission #1271128

#TimeUsernameProblemLanguageResultExecution timeMemory
1271128hoangtien69Stone Arranging 2 (JOI23_ho_t1)C++20
60 / 100
2091 ms9284 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 200000 + 5;

int n;
int a[MAXN];
vector<int> vals;
int mp_val[MAXN];
int comp(int x)
{
    return int(lower_bound(vals.begin(), vals.end(), x) - vals.begin()) + 1;
}
struct Seg
{
    int n;
    vector<int> val;
    vector<int> lazy;
    Seg(int n = 0)
    {
        init(n);
    }
    void init(int _n)
    {
        n = _n;
        val.assign(4 * n + 5, 0);
        lazy.assign(4 * n + 5, -1);
    }
    void apply_set(int node, int color)
    {
        val[node] = color;
        lazy[node] = color;
    }
    void push(int node)
    {
        if (lazy[node] != -1)
        {
            apply_set(node << 1, lazy[node]);
            apply_set(node << 1 | 1, lazy[node]);
            lazy[node] = -1;
        }
    }
    void pull(int node)
    {
        int L = val[node << 1], R = val[node << 1 | 1];
        if (L != -1 && L == R)
            val[node] = L;
        else
            val[node] = -1;
    }
    void assign_range(int node, int l, int r, int ql, int qr, int color)
    {
        if (ql > r || qr < l)
            return;
        if (ql <= l && r <= qr)
        {
            apply_set(node, color);
            return;
        }
        push(node);
        int mid = (l + r) >> 1;
        assign_range(node << 1, l, mid, ql, qr, color);
        assign_range(node << 1 | 1, mid + 1, r, ql, qr, color);
        pull(node);
    }
    void assign_range(int l, int r, int color)
    {
        if (l > r)
            return;
        assign_range(1, 1, n, l, r, color);
    }
    int last_pos_with_color(int node, int l, int r, int ql, int qr, int C)
    {
        if (ql > r || qr < l)
            return -1;
        if (ql <= l && r <= qr)
        {
            if (val[node] != -1)
            {
                if (val[node] == C)
                    return r;
                else
                    return -1;
            }
        }
        if (l == r)
        {
            if (val[node] == C)
                return l;
            return -1;
        }
        push(node);
        int mid = (l + r) >> 1;
        int res = last_pos_with_color(node << 1 | 1, mid + 1, r, ql, qr, C);
        if (res != -1)
            return res;
        return last_pos_with_color(node << 1, l, mid, ql, qr, C);
    }
    int last_pos_with_color(int l, int r, int C)
    {
        if (l > r)
            return -1;
        return last_pos_with_color(1, 1, n, l, r, C);
    }

    int point_query(int node, int l, int r, int pos)
    {
        if (l == r)
            return val[node];
        push(node);
        int mid = (l + r) >> 1;
        if (pos <= mid)
            return point_query(node << 1, l, mid, pos);
        else
            return point_query(node << 1 | 1, mid + 1, r, pos);
    }
    int point_query(int pos)
    {
        return point_query(1, 1, n, pos);
    }
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    Seg st(n);
    for (int i = 1; i <= n; i++)
    {
        st.assign_range(i, i, a[i]);
        if (i > 1)
        {
            int j = st.last_pos_with_color(1, i - 1, a[i]);
            if (j != -1 && j + 1 <= i - 1)
            {
                st.assign_range(j + 1, i - 1, a[i]);
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        int c = st.point_query(i);
        cout << c << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...