Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
2
Indexable
Never
#define maxN 100005
using namespace std;
   
struct node {
    int idx, val;
};
 
node Node[4 * maxN];
int a[maxN], N;
 
void init(int N)
{
    ::N = N;
    register int i;
    for (i = 1; i <= N; i++){
        a[i] = 0;
    }
    for (i = 0; i < 4 * N + 1; i++){
        Node[i].idx = 0;
        Node[i].val = 0;
    }
    return;
}
   
void update(int index, int low, int high, int pos)
{
    if (pos < low || pos>high)
        return;
    if (low == high){
        Node[index].val = a[pos];
        Node[index].idx = pos;
        return;
    }
    register int mid = (low + high) / 2;
    update(2 * index + 1, low, mid, pos);
    update(2 * index + 2, mid + 1, high, pos);
  
    if (Node[2 * index + 1].val > Node[2 * index + 2].val){
        Node[index].val = Node[2 * index + 1].val;
        Node[index].idx = Node[2 * index + 1].idx;
    }
    else{
        Node[index].val = Node[2 * index + 2].val;
        Node[index].idx = Node[2 * index + 2].idx;
    }
}
   
node query(int idx, int low, int high, int l, int r){
    if (r < low || l > high || low > high){
        node a;
        a.idx = 1;
        a.val = -1;
        return a;
    }
    if (low >= l && r >= high)
        return Node[idx]; 
    register int mid = (low + high) / 2;
    node left = query(2 * idx + 1, low, mid, l, r);
    node right = query(2 * idx + 2, mid + 1, high, l, r);
    if (left.val > right.val)
        return left;
    return right;
}
     
int getArea(){
    node maxAns = Node[0];
    register int maxArea = maxAns.val, maxIdx = maxAns.idx, temp = maxIdx;
    while (temp < N){
        node right = query(0, 1, N, temp + 1, N);
        if (right.val == 0)
            break;
        maxArea += (right.idx - temp) * right.val;
        temp = right.idx;
    }
    temp = maxIdx;
    while (temp > 1){
        node left = query(0, 1, N, 1, temp - 1);
        if (left.val == 0)
            break;
        maxArea += (temp - left.idx) * left.val;
        temp = left.idx;
    }
    return maxArea;
}
   
int stock(int mLoc, int mBox){
    a[mLoc] += mBox;
    update(0, 1, N, mLoc);
    return getArea();
}
   
int ship(int mLoc, int mBox){
    a[mLoc] -= mBox;
    update(0, 1, N, mLoc);
    return getArea();
}
   
int getHeight(int mLoc){
    return a[mLoc];
}