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;
typedef pair <char, int> op;
const int MAX = 1e6 + 5;
vector <op> lazy[4 * MAX];
int a[MAX];
vector <op> concat(const vector <op> &a, const vector <op> &b) {
vector <op> v;
for(auto o : a) v.push_back(o);
for(auto o : b) v.push_back(o);
return v;
}
vector <op> reduce(const op &a, const op &b) {
int x = a.second;
int y = b.second;
if(b.first == 's')
return {b};
if(a.first == 's') {
if(b.first == 'm') {
if(x <= y)
return {op('s', x)};
return {op('s', y)};
}
else {
assert(b.first == 'M');
if(x <= y)
return {op('s', y)};
return {op('s', x)};
}
}
if(a.first == 'm' && b.first == 'm') {
if(x <= y)
return {a};
return {b};
}
if(a.first == 'M' && b.first == 'M') {
if(x <= y)
return {b};
return {a};
}
if(a.first == 'm' && b.first == 'M') {
if(x <= y)
return {op('s', y)};
return {a, b};
}
assert(a.first == 'M' && b.first == 'm');
if(x <= y)
return {a, b};
return {op('s', y)};
}
vector <op> tryReduce(vector <op> v) {
for(int i = 0; i < (int) v.size() - 1; i++) {
auto r = reduce(v[i], v[i + 1]);
if((int) r.size() == 1) {
vector <op> aux;
for(int j = 0; j < i; j++)
aux.push_back(v[j]);
aux.push_back(r[0]);
for(int j = i + 2; j < (int) v.size(); j++)
aux.push_back(v[j]);
return aux;
}
}
return {};
}
vector <op> reduce(vector <op> v) {
if((int) v.size() == 1)
return v;
vector <op> r = tryReduce(v);
if(r.empty()) { //no pudo reducir
if((int) v.size() == 2)
return v;
swap(v[0], v[1]);
return reduce(v);
}
return reduce(r);
}
void build(int x, int st, int nd) {
lazy[x].push_back(op('M', 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) {
auto r = reduce(concat({op('s', a[st])}, lazy[x]));
assert((int) r.size() == 1);
a[st] = r[0].second;
}
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] = {op('M', 0)};
}
void upd(int x, int st, int nd, int a, int b, const op &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++) {
if(operation[i] == 1)
upd(1, 0, n - 1, l[i], r[i], op('M', h[i]));
else upd(1, 0, n - 1, l[i], r[i], op('m', h[i]));
}
for(int i = 0; i < n; i++) {
upd(1, 0, n - 1, i, i, op('M', 0));
ans[i] = a[i];
}
}
# | 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... |