This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "wall.h"
#include <bits/stdc++.h>
using namespace std;
const int
MAX = 1e6 + 5,
INF = 1e6;
vector <int> lazy[4 * MAX];
int a[MAX];
vector <int> concat(const vector <int> &a, const vector <int> &b) {
vector <int> v;
for(auto o : a) v.push_back(o);
for(auto o : b) v.push_back(o);
return v;
}
int reduce(const int &a, const int &b, bool &ok) {
int x = abs(a);
int y = abs(b);
if(a < 0 && b < 0)
return (x <= y) ? a : b;
if(a >= 0 && b >= 0)
return (x <= y) ? b : a;
ok = false;
return 0;
}
vector <int> tryReduce(vector <int> v) {
for(int i = 0; i < (int) v.size() - 1; i++) {
bool ok = true;
int r = reduce(v[i], v[i + 1], ok);
if(ok) {
vector <int> aux;
for(int j = 0; j < i; j++)
aux.push_back(v[j]);
aux.push_back(r);
for(int j = i + 2; j < (int) v.size(); j++)
aux.push_back(v[j]);
return aux;
}
}
return {};
}
int sgn(int x) { return (x >= 0) ? +1 : -1; }
void transform(int &a, int &b) {
assert(sgn(a) != sgn(b));
int x = abs(a);
int y = abs(b);
if(a < 0 && b >= 0) {
if(x <= y) {
a = INF;
b *= -1;
}
else swap(a, b);
}
else {
assert(b < 0);
if(x <= y)
swap(a, b);
else {
a = -1;
b *= -1;
}
}
}
vector <int> reduce(vector <int> v) {
assert((int) v.size() <= 4);
while((int) v.size() > 1) {
auto r = tryReduce(v);
if(r.empty()) {
if((int) v.size() == 2)
break;
transform(v[0], v[1]);
}
else v = r;
}
assert((int) v.size() <= 2);
return v;
}
void build(int x, int st, int nd) {
lazy[x] = {0};
if(st == nd) return;
int mid = (st + nd) >> 1;
build(2 * x, st, mid);
build(2 * x + 1, mid + 1, nd);
}
void prop(int x, int st, int nd) {
if(st == nd) {
for(auto o : lazy[x]) {
if(o >= 0)
a[st] = max(a[st], o);
else a[st] = min(a[st], -o);
}
}
else {
lazy[2 * x] = reduce(concat(lazy[2 * x], lazy[x]));
lazy[2 * x + 1] = reduce(concat(lazy[2 * x + 1], lazy[x]));
}
lazy[x] = {0};
}
void upd(int x, int st, int nd, int a, int b, const int &o) {
prop(x, st, nd);
if(st > b || nd < a)
return;
if(st >= a && nd <= b) {
lazy[x] = {o};
prop(x, st, nd);
return;
}
int mid = (st + nd) >> 1;
upd(2 * x, st, mid, a, b, o);
upd(2 * x + 1, mid + 1, nd, a, b, o);
}
void buildWall(int n, int m, int operation[], int l[], int r[], int h[], int ans[]) {
build(1, 0, n - 1);
for(int i = 0; i < m; i++) {
int height = h[i] + 1;
if(operation[i] == 1)
upd(1, 0, n - 1, l[i], r[i], height);
else upd(1, 0, n - 1, l[i], r[i], -height);
}
for(int i = 0; i < n; i++) {
upd(1, 0, n - 1, i, i, 0);
ans[i] = max(0, a[i] - 1);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |