Submission #139527

#TimeUsernameProblemLanguageResultExecution timeMemory
139527dcordbWall (IOI14_wall)C++11
8 / 100
3047 ms102492 KiB
#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(const 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(const vector <op> &v) { assert((int) v.size() <= 4); 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; r = v; swap(r[0], r[1]); return reduce(r); } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...