# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1181608 | steveonalex | Floppy (RMI20_floppy) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.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 gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull 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);}
double rngesus_d(double l, double r){
double wow = (double) ((ull) rng()) / ((ull)(0-1));
return wow * (r - l) + l;
}
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{
int n;
vector<pair<int,int>> a;
SegmentTree(int _n){
n = _n;
a.resize(n * 2 + 2, {-INF, 0});
}
void update(int i, pair<int, int> v){
i += n; a[i] = v;
while(i > 1){
i >>= 1;
a[i] = max(a[i * 2], a[i * 2 + 1]);
}
}
pair<int,int> get(int l, int r){
l += n; r += n + 1;
pair<int, int> ans = {-INF, 0};
while(l < r){
if (l & 1) maximize(ans, a[l++]);
if (r & 1) maximize(ans, a[--r]);
l >>= 1; r >>= 1;
}
return ans;
}
};
void dfs(int l, int r, SegmentTree &st, string &ans){
if (l > r) {
ans.push_back('0');
return;
}
ans.push_back('1');
int mid = st.get(l, r).second;
dfs(l, mid-1, st, ans);
dfs(mid+1, r, st, ans);
}
void read_array(int subtask, const vector<int> &v){
int n = v.size();
SegmentTree st(n);
for(int i = 1; i <= n; ++i) st.update(i, make_pair(v[i-1], i));
string ans;
dfs(1, n, st, ans);
ans = ans.substr(1);
save_to_floppy(ans);
}
const int N = 4e4 + 69;
array<int, 2> graph[N];
int parent[N], child_cnt[N];
void dfs(int u, int h, vector<int> &height_list){
if (graph[u][0] != -1) dfs(graph[u][0], h - 1, height_list);
height_list.push_back(h);
if (graph[u][1] != -1) dfs(graph[u][1], h - 1, height_list);
}
vector<int> solve_queries(int subtask, int n, const string &data, const vector<int> &a, const vector<int> &b){
for(int i = 1; i <= n; ++i){
graph[i] = {{-1, -1}};
parent[i] = child_cnt[i] = 0;
}
int cnt = 1;
int u = 1;
for(char c: data){
while(child_cnt[u] == 2) u = parent[u];
if (c == '1'){
graph[u][child_cnt[u]++] = ++cnt;
parent[cnt] = u;
u = cnt;
}
else{
child_cnt[u]++;
}
}
vector<int> h;
dfs(1, -1, h);
SegmentTree st(n);
for(int i = 1; i <= n; ++i) st.update(i, make_pair(h[i-1], i));
vector<int> ans;
for(int it = 0; it < a.size(); ++it){
int l = a[it] + 1, r = b[it] + 1;
ans.push_back(st.get(l, r).second - 1);
}
return ans;
}