제출 #302940

#제출 시각아이디문제언어결과실행 시간메모리
302940wiwiho벽 (IOI14_wall)C++14
100 / 100
1047 ms99576 KiB
//#define NDEBUG

#include "wall.h"

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b) - 1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

//#define TEST

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

struct Node{
    int l = -1, r = -1, mx = 0, mn = 0;
};

vector<Node> st;
int ts = 0;

int build(int l, int r){
    int id = ts++;
    if(l == r) return id;
    int m = (l + r) / 2;
    st[id].l = build(l, m);
    st[id].r = build(m + 1, r);
    return id;
}

void push(int id){
    if(st[id].l == -1) return;
    int l = st[id].l, r = st[id].r;

    if(max(st[l].mn, st[id].mn) <= min(st[l].mx, st[id].mx)){
        st[l].mn = max(st[l].mn, st[id].mn);
        st[l].mx = min(st[l].mx, st[id].mx);
    }
    else if(st[l].mx < st[id].mn) st[l].mn = st[l].mx = st[id].mn;
    else st[l].mn = st[l].mx = st[id].mx;

    if(max(st[r].mn, st[id].mn) <= min(st[r].mx, st[id].mx)){
        st[r].mn = max(st[r].mn, st[id].mn);
        st[r].mx = min(st[r].mx, st[id].mx);
    }
    else if(st[r].mx < st[id].mn) st[r].mn = st[r].mx = st[id].mn;
    else st[r].mn = st[r].mx = st[id].mx;
}

void modifyadd(int l, int r, int v, int L, int R, int id){
    push(id);
    if(L == l && R == r){
        st[id].mn = max(st[id].mn, v);
        st[id].mx = max(st[id].mx, st[id].mn);
        return;
    }
    int M = (L + R) / 2;
    if(r <= M) modifyadd(l, r, v, L, M, st[id].l);
    else if(l > M) modifyadd(l, r, v, M + 1, R, st[id].r);
    else{
        modifyadd(l, M, v, L, M, st[id].l);
        modifyadd(M + 1, r, v, M + 1, R, st[id].r);
    }
    st[id].mn = min(st[st[id].l].mn, st[st[id].r].mn);
    st[id].mx = max(st[st[id].l].mx, st[st[id].r].mx);
}

void modifyremove(int l, int r, int v, int L, int R, int id){
    if(L == l && R == r){
        st[id].mx = min(st[id].mx, v);
        st[id].mn = min(st[id].mx, st[id].mn);
        return;
    }
    push(id);
    int M = (L + R) / 2;
    if(r <= M) modifyremove(l, r, v, L, M, st[id].l);
    else if(l > M) modifyremove(l, r, v, M + 1, R, st[id].r);
    else{
        modifyremove(l, M, v, L, M, st[id].l);
        modifyremove(M + 1, r, v, M + 1, R, st[id].r);
    }
    st[id].mn = min(st[st[id].l].mn, st[st[id].r].mn);
    st[id].mx = max(st[st[id].l].mx, st[st[id].r].mx);
}

void dfs(int L, int R, int id, int finalHeight[]){
    if(L == R){
        finalHeight[L] = st[id].mn;
        return;
    }
    push(id);
    int M = (L + R) / 2;
    dfs(L, M, st[id].l, finalHeight);
    dfs(M + 1, R, st[id].r, finalHeight);
}

void buildWall(int n, int k, int op[], int left[], int right[], int height[], int finalHeight[]){

    st.resize(2 * n);
    build(0, n - 1);

    for(int i = 0; i < k; i++){
        if(op[i] == 1){
            modifyadd(left[i], right[i], height[i], 0, n - 1, 0);
        }
        else{
            modifyremove(left[i], right[i], height[i], 0, n - 1, 0);
        }
    }

    dfs(0, n - 1, 0, finalHeight);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...