#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define INF 100005
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#include "wall.h"
ii combine(ii upd_new, ii upd_old) { // apply upd_new to upd_old
ii res = {max(upd_new.fi, upd_old.fi), min(upd_new.se, upd_old.se)};
if (res.fi > res.se) { // The ranges are disjoint, so suffices to check either
return (upd_old.se < upd_new.se) ? make_pair(upd_new.fi, upd_new.fi) : make_pair(upd_new.se, upd_new.se);
}
return res;
}
struct Segtree { // Segtree of updates
int sz;
vector<ii> upd; // store (a, b)
vi left;
vi right;
vi mid;
Segtree(int N) {
sz = 1;
while (sz < N)
sz *= 2;
upd = vii(2 * sz, {0, INF});
left = vi(2 * sz, 0);
right = vi(2 * sz, 0);
mid = vi(2 * sz, 0);
for(int id = sz-1; id < 2*sz-1; id++) {
left[id] = id-sz+1;
right[id] = id-sz+2;
}
for(int id = sz-2; id >= 0; id--) {
left[id] = left[(id<<1)|1];
right[id] = right[2 * id + 2];
mid[id] = (left[id] + right[id])>>1;
}
}
void push(int id) { // O(1), for regular updates
if (mid[id]) {
upd[(id<<1)|1] = combine(upd[id], upd[(id<<1)|1]);
upd[2 * id + 2] = combine(upd[id], upd[2 * id + 2]);
upd[id] = {0, INF};
}
}
void push_recursive(int id) { // O(N), only for answer extraction at the end
if(mid[id]) {
push(id);
push_recursive((id<<1)|1);
push_recursive(2 * id + 2);
}
}
void range_upd(int qleft, int qright, ii val, int id) {
if (qleft <= left[id] && right[id] <= qright) {
upd[id] = combine(val, upd[id]);
return;
}
if (qright <= left[id] || right[id] <= qleft)
return;
push(id);
range_upd(qleft, qright, val, (id<<1)|1);
range_upd(qleft, qright, val, 2 * id + 2);
}
void range_upd(int qleft, int qright, ii val) {
range_upd(qleft, qright, val, 0);
}
};
void buildWall(int n, int k, int op[], int left[], int right[], int height[], int finalHeight[]) {
Segtree tree(n);
for(int z = 0; z < k; z++) {
ii upd_val = (op[z] == 1) ? make_pair(height[z], INF) : make_pair(0, height[z]);
tree.range_upd(left[z], right[z]+1, upd_val);
}
tree.push_recursive(0);
for(int z = 0; z < n; z++) {
finalHeight[z] = tree.upd[z+tree.sz-1].fi;
}
return;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
1116 KB |
Output is correct |
5 |
Correct |
3 ms |
1116 KB |
Output is correct |
6 |
Correct |
3 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
75 ms |
8132 KB |
Output is correct |
3 |
Correct |
102 ms |
4828 KB |
Output is correct |
4 |
Correct |
342 ms |
13664 KB |
Output is correct |
5 |
Correct |
149 ms |
13652 KB |
Output is correct |
6 |
Correct |
147 ms |
13716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
1116 KB |
Output is correct |
5 |
Correct |
3 ms |
1116 KB |
Output is correct |
6 |
Correct |
3 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
96 ms |
8236 KB |
Output is correct |
9 |
Correct |
102 ms |
4944 KB |
Output is correct |
10 |
Correct |
360 ms |
13640 KB |
Output is correct |
11 |
Correct |
164 ms |
13572 KB |
Output is correct |
12 |
Correct |
145 ms |
13784 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
14 |
Correct |
75 ms |
8020 KB |
Output is correct |
15 |
Correct |
24 ms |
2140 KB |
Output is correct |
16 |
Correct |
409 ms |
13816 KB |
Output is correct |
17 |
Correct |
160 ms |
13652 KB |
Output is correct |
18 |
Correct |
165 ms |
13700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
1112 KB |
Output is correct |
5 |
Correct |
3 ms |
1116 KB |
Output is correct |
6 |
Correct |
3 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
74 ms |
8036 KB |
Output is correct |
9 |
Correct |
110 ms |
4948 KB |
Output is correct |
10 |
Correct |
364 ms |
13672 KB |
Output is correct |
11 |
Correct |
148 ms |
13652 KB |
Output is correct |
12 |
Correct |
158 ms |
13716 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
82 ms |
8064 KB |
Output is correct |
15 |
Correct |
23 ms |
2140 KB |
Output is correct |
16 |
Correct |
381 ms |
13616 KB |
Output is correct |
17 |
Correct |
179 ms |
13704 KB |
Output is correct |
18 |
Correct |
167 ms |
13648 KB |
Output is correct |
19 |
Correct |
454 ms |
97972 KB |
Output is correct |
20 |
Correct |
411 ms |
98128 KB |
Output is correct |
21 |
Correct |
439 ms |
98116 KB |
Output is correct |
22 |
Correct |
444 ms |
98040 KB |
Output is correct |
23 |
Correct |
466 ms |
98008 KB |
Output is correct |
24 |
Correct |
443 ms |
98044 KB |
Output is correct |
25 |
Correct |
390 ms |
98128 KB |
Output is correct |
26 |
Correct |
389 ms |
98128 KB |
Output is correct |
27 |
Correct |
392 ms |
98128 KB |
Output is correct |
28 |
Correct |
371 ms |
98132 KB |
Output is correct |
29 |
Correct |
391 ms |
98048 KB |
Output is correct |
30 |
Correct |
380 ms |
98132 KB |
Output is correct |