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 <bits/stdc++.h>
#include "wall.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int INF = 1e9 + 69;
struct SegmentTree{
struct Node{
int t, op, h;
Node(int _t = -1, int _op = -1, int _h = -1){
t = _t, op = _op, h = _h;
}
};
int time = 0;
int n;
vector<pair<Node, Node>> a;
SegmentTree(int _n){
n = _n;
a.resize(n * 4 + 4, {Node(0, 1, 0), Node(0, 2, 0)});
}
pair<Node, Node> combine(pair<Node, Node> a, pair<Node, Node> b){
Node sigma_duck[] = {a.first, a.second, b.first, b.second};
sort(sigma_duck, sigma_duck + 4, [] (Node a, Node b){return a.t < b.t;});
pair<Node, Node> ans;
for(Node i: sigma_duck){
if (i.t == -1) continue;
if (i.op == 1){
if (maximize(ans.first.h, i.h)) {
ans.first = i;
if (ans.second.t != -1) maximize(ans.second.h, i.h);
}
}
else{
if (ans.second.h == -1 || minimize(ans.second.h, i.h)) {
ans.second = i;
if (ans.first.t != -1) minimize(ans.first.h, i.h);
}
}
}
return ans;
}
void down(int id){
a[id * 2] = combine(a[id * 2], a[id]);
a[id * 2 + 1] = combine(a[id * 2 + 1], a[id]);
a[id] = {Node(), Node()};
}
void update(int u, int v, int op, int h, int l, int r, int id){
if (u <= l && r <= v) {
if (op == 1) a[id] = combine(a[id], {Node(++time, op, h), Node()});
else a[id] = combine(a[id], {Node(), Node(++time, op, h)});
return;
}
down(id);
int mid = (l + r) >> 1;
if (u <= mid) update(u, v, op, h, l, mid, id * 2);
if (v > mid) update(u, v, op, h, mid + 1, r, id * 2 + 1);
}
void update(int u, int v, int op, int h){update(u, v, op, h, 0, n-1, 1);}
int get(int i, int l, int r, int id){
if (l == r){
return a[id].first.h;
}
down(id);
int mid = (l + r) >> 1;
if (i <= mid) return get(i, l, mid, id * 2);
return get(i, mid + 1, r, id * 2 + 1);
}
int get(int i){return get(i, 0, n-1, 1);}
};
void buildWall(int n, int k, int op[], int left[], int right[],int height[], int finalHeight[]){
SegmentTree st(n);
for(int i = 0; i<k; ++i) st.update(left[i], right[i], op[i], height[i]);
for(int i= 0; i<n; ++i) finalHeight[i] = st.get(i);
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// int n, q; cin >> n >> q;
// SegmentTree st(n);
// for(int i = 0; i<q; ++i){
// int op, l, r, h; cin >> op >> l >> r >> h;
// st.update(l, r, op, h);
// for(int i = 0; i<n; ++i) cout << st.get(i) << " "; cout << "\n";
// }
// return 0;
// }
# | 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... |