#include "wall.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 100000
#define MAX_N 2000000
struct S{
int l, r;
int nmin, nmax;
int from;
};
vector<S> seg;
int ans[MAX_N+1];
void init(int x, int y){
int idx=(int)seg.size();
seg.push_back({-1, -1, 0, 0, -1});
if(x==y){
return;
}
int m=(x+y)/2;
if(x<=m){
seg[idx].l=(int)seg.size();
init(x, m);
seg[seg[idx].l].from=idx;
}
if(m+1<=y){
seg[idx].r=(int)seg.size();
init(m+1, y);
seg[seg[idx].r].from=idx;
}
return;
}
void updaten(int x){
if(seg[x].from!=-1){
int t=seg[x].from;
if(seg[t].nmax<seg[x].nmax){
seg[x].nmax=seg[t].nmax;
}
if(seg[t].nmax<seg[x].nmin){
seg[x].nmin=seg[t].nmax;
}
if(seg[t].nmin>seg[x].nmax){
seg[x].nmax=seg[t].nmin;
}
if(seg[t].nmin>seg[x].nmin){
seg[x].nmin=seg[t].nmin;
}
}
return;
}
void update(int idx, int li, int ri, int data, int nx, int ny){
//int nx=seg[idx].s, ny=seg[idx].e;
if(seg[idx].l!=-1){
updaten(seg[idx].l);
}
if(seg[idx].r!=-1){
updaten(seg[idx].r);
}
if(li>ny || ri<nx) return;
if(data>0){
if(seg[idx].nmax<data){
seg[idx].nmax=data;
}
}else{
data=-data;
if(seg[idx].nmin>data){
seg[idx].nmin=data;
}
data=-data;
}
if(li<=nx && ri>=ny) {
if(data>0){
if(seg[idx].nmin<data){
seg[idx].nmin=data;
}
}else{
data=-data;
if(seg[idx].nmax>data){
seg[idx].nmax=data;
}
data=-data;
}
return;
}
if(seg[idx].r!=-1){
update(seg[idx].r, li, ri, data, nx, (nx+ny)/2);
}
if(seg[idx].l!=-1){
update(seg[idx].l, li, ri, data, (nx+ny)/2+1, ny);
}
seg[idx].nmax=max((seg[idx].r==-1?-1:seg[seg[idx].r].nmax), (seg[idx].l==-1?-1:seg[seg[idx].l].nmax));
seg[idx].nmin=min((seg[idx].r==-1?INF+1:seg[seg[idx].r].nmin), (seg[idx].l==-1?INF+1:seg[seg[idx].l].nmin));
}
void find_ans(int idx, int nx, int ny){
if(seg[idx].l!=-1){
updaten(seg[idx].l);
}
if(seg[idx].r!=-1){
updaten(seg[idx].r);
}
if(nx==ny){
ans[nx]=seg[idx].nmax;
return;
}
if(seg[idx].l!=-1){
find_ans(seg[idx].l, nx, nx+ny);
}
if(seg[idx].r!=-1){
find_ans(seg[idx].r, (nx+ny)+1, ny);
}
}
void buildWall(int n, int k, int op[], int left[], int right[], int height[], int finalHeight[]){
init(0, n-1);
for(int i=0; i<k; i++){
if(op[i]==1){
update(0, left[i], right[i], height[i], 0, n-1);
}else{
update(0, left[i], right[i], -height[i], 0, n-1);
}
}
find_ans(0, 0, n-1);
for(int i=0; i<n; i++){
finalHeight[i]=ans[i];
}
return;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
3 |
Incorrect |
4 ms |
640 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
142 ms |
14124 KB |
Output is correct |
3 |
Incorrect |
268 ms |
14932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14932 KB |
Output is correct |
2 |
Correct |
3 ms |
14932 KB |
Output is correct |
3 |
Incorrect |
4 ms |
14932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14932 KB |
Output is correct |
2 |
Correct |
3 ms |
14932 KB |
Output is correct |
3 |
Incorrect |
4 ms |
14932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |