Submission #1533457
Source Code Expand
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define rt return
using dbl = double;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
/*
セグメント木を利用したRMQ
初期化 O(N)
クエリ O(log(N))
*/
template<class Val, class Cmp>
class DynamicRMQ {
int n;
Val init;
vector<Val> dat;
Cmp cmp;
inline Val query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) return init;
if (a <= l&&r <= b) return dat[k];
else {
Val vl, vr;
vl = query(a, b, k << 1, l, (l + r) >> 1);
vr = query(a, b, (k << 1) | 1, (l + r) >> 1, r);
return cmp(vl, vr) ? vl : vr;
}
}
public:
DynamicRMQ() {}
DynamicRMQ(int n_, Val init_) :n(1), init(init_) {
for (; n<n_; n <<= 1);
dat = vector<Val>(n << 1, init);
}
void update(int k, Val a) {
k += n;
dat[k] = a;
while (k > 1) {
k >>= 1;
dat[k] = cmp(dat[k << 1], dat[(k << 1) | 1]) ? dat[k << 1] : dat[(k << 1) | 1];
}
}
Val query(int a, int b) {
return query(a, b, 1, 0, n);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N, INF = 1 << 30;
cin >> N;
vi P(N), A(N + 1);
rep(i, N) {
cin >> P[i];
A[P[i]] = i;
}
using RMQ = DynamicRMQ<pii, less<pii> >;
using T = tuple<pii, int, int>;
RMQ rmq[2];
rep(i, 2)rmq[i] = RMQ(N, mp(INF,i));
rep(i, N)rmq[i&1].update(i, mp(P[i],i));
priority_queue<T, vector<T>, greater<T> > Q;
Q.push(T(rmq[0].query(0, N), 0, N));
vi ans;
auto f = [&](int l, int r) {
if (l >= r)return;
pii p = rmq[l&1].query(l, r);
Q.push(T(p, l, r));
return;
};
while (sz(Q)) {
pii p;
int x, m, l, r;
tie(p, l, r) = Q.top();
Q.pop();
tie(x, m) = p;
// l=<m<n<r
int y, n;
tie(y, n) = rmq[!(l&1)].query(m + 1, r);
ans.push_back(x);
ans.push_back(y);
rmq[0].update(m, mp(INF, m));
rmq[1].update(n, mp(INF, n));
f(l, m);
f(m+1, n);
f(n + 1, r);
}
rep(i, N)cout << ans[i] << (i != N - 1 ? ' ' : '\n');
}
Submission Info
Submission Time |
|
Task |
E - Young Maids |
User |
paruki |
Language |
C++14 (GCC 5.4.1) |
Score |
800 |
Code Size |
2785 Byte |
Status |
AC |
Exec Time |
149 ms |
Memory |
12788 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
800 / 800 |
Status |
|
|
Set Name |
Test Cases |
Sample |
0_00.txt, 0_01.txt, 0_02.txt |
All |
0_00.txt, 0_01.txt, 0_02.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt |
Case Name |
Status |
Exec Time |
Memory |
0_00.txt |
AC |
1 ms |
256 KB |
0_01.txt |
AC |
1 ms |
256 KB |
0_02.txt |
AC |
1 ms |
256 KB |
1_00.txt |
AC |
1 ms |
256 KB |
1_01.txt |
AC |
1 ms |
256 KB |
1_02.txt |
AC |
102 ms |
12148 KB |
1_03.txt |
AC |
101 ms |
12148 KB |
1_04.txt |
AC |
119 ms |
12148 KB |
1_05.txt |
AC |
136 ms |
12788 KB |
1_06.txt |
AC |
139 ms |
12784 KB |
1_07.txt |
AC |
136 ms |
12788 KB |
1_08.txt |
AC |
134 ms |
12616 KB |
1_09.txt |
AC |
98 ms |
12148 KB |
1_10.txt |
AC |
126 ms |
12148 KB |
1_11.txt |
AC |
104 ms |
12148 KB |
1_12.txt |
AC |
135 ms |
12532 KB |
1_13.txt |
AC |
137 ms |
12660 KB |
1_14.txt |
AC |
149 ms |
12660 KB |
1_15.txt |
AC |
139 ms |
12660 KB |
1_16.txt |
AC |
136 ms |
12660 KB |
1_17.txt |
AC |
139 ms |
12660 KB |
1_18.txt |
AC |
136 ms |
12660 KB |
1_19.txt |
AC |
136 ms |
12660 KB |