Submission #46511

#TimeUsernameProblemLanguageResultExecution timeMemory
46511RayaBurong25_1Wall (IOI14_wall)C++17
100 / 100
1115 ms262144 KiB
#include "wall.h"
#include <vector>
#define INF 1000000007
std::vector<int> V;
typedef struct node node;
struct node
{
    int h;
    int lop, lhmin = 0, lhmax = INF;
};
node Seg[8000005];
int min(int a, int b)
{
    return (a < b)?a:b;
}
int max(int a, int b)
{
    return (a > b)?a:b;
}
void flush(int s, int e, int cell)
{
    int i;
    if (Seg[cell].lop == 1)
    {
        Seg[cell].h = min(Seg[cell].h, Seg[cell].lhmax);
        Seg[cell].h = max(Seg[cell].h, Seg[cell].lhmin);
        if (s != e)
        {
            for (i = 1; i <= 2; i++)
            {
                if (Seg[cell].lhmax < Seg[2*cell + i].lhmin)
                {
                    Seg[2*cell + i].lhmin = Seg[cell].lhmax;
                    Seg[2*cell + i].lhmax = Seg[cell].lhmax;
                }
                else
                    Seg[2*cell + i].lhmax = min(Seg[2*cell + i].lhmax, Seg[cell].lhmax);

                if (Seg[cell].lhmin > Seg[2*cell + i].lhmax)
                {
                    Seg[2*cell + i].lhmin = Seg[cell].lhmin;
                    Seg[2*cell + i].lhmax = Seg[cell].lhmin;
                }
                else
                    Seg[2*cell + i].lhmin = max(Seg[2*cell + i].lhmin, Seg[cell].lhmin);
                Seg[2*cell + i].lop = Seg[cell].lop;
            }
        }
        Seg[cell].lop = 0;
        Seg[cell].lhmin = 0;
        Seg[cell].lhmax = INF;
    }
    else if (Seg[cell].lop == 2)
    {
        Seg[cell].h = max(Seg[cell].h, Seg[cell].lhmin);
        Seg[cell].h = min(Seg[cell].h, Seg[cell].lhmax);
        if (s != e)
        {
            for (i = 1; i <= 2; i++)
            {
                if (Seg[cell].lhmin > Seg[2*cell + i].lhmax)
                {
                    Seg[2*cell + i].lhmin = Seg[cell].lhmin;
                    Seg[2*cell + i].lhmax = Seg[cell].lhmin;
                }
                else
                    Seg[2*cell + i].lhmin = max(Seg[2*cell + i].lhmin, Seg[cell].lhmin);
                Seg[2*cell + i].lop = Seg[cell].lop;

                if (Seg[cell].lhmax < Seg[2*cell + i].lhmin)
                {
                    Seg[2*cell + i].lhmin = Seg[cell].lhmax;
                    Seg[2*cell + i].lhmax = Seg[cell].lhmax;
                }
                else
                    Seg[2*cell + i].lhmax = min(Seg[2*cell + i].lhmax, Seg[cell].lhmax);
            }
        }
        Seg[cell].lop = 0;
        Seg[cell].lhmin = 0;
        Seg[cell].lhmax = INF;
    }
}
void update(int op, int qs, int qe, int h, int s, int e, int cell)
{
    flush(s, e, cell);
    if (qs == s && qe == e)
    {
        if (op == 1)
        {
            if (h > Seg[cell].lhmax)
            {
                Seg[cell].lhmin = h;
                Seg[cell].lhmax = h;
            }
            else
                Seg[cell].lhmin = max(Seg[cell].lhmin, h);
            Seg[cell].lop = op;
        }
        else
        {
            if (h < Seg[cell].lhmin)
            {
                Seg[cell].lhmin = h;
                Seg[cell].lhmax = h;
            }
            else
                Seg[cell].lhmax = min(Seg[cell].lhmax, h);
            Seg[cell].lop = op;
        }
        return;
    }
    int m = (s + e)/2;
    if (qs <= m)
        update(op, qs, min(m, qe), h, s, m, 2*cell + 1);
    if (qe >= m + 1)
        update(op, max(qs, m + 1), qe, h, m + 1, e, 2*cell + 2);
}
void answerAll(int s, int e, int cell)
{
    flush(s, e, cell);
    if (s == e)
    {
        V.push_back(Seg[cell].h);
        return;
    }
    int m = (s + e)/2;
    answerAll(s, m, 2*cell + 1);
    answerAll(m + 1, e, 2*cell + 2);
}
void buildWall(int n, int k, int op[], int left[], int right[], int height[], int finalHeight[])
{
    int i;
    for (i = 0; i < k; i++)
        update(op[i], left[i], right[i], height[i], 0, n - 1, 0);
    answerAll(0, n - 1, 0);
    for (i = 0; i < n; i++)
        finalHeight[i] = V[i];
    return;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...