Submission #1866973
Source Code Expand
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
#define INF 1e18
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef vector < pair<int, int> > vii;
typedef long double ld;
typedef tree<pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
ll sparse_odd[222222][20], sparse_even[222222][20], idx[222222], a[222222];
bool cmp(ii a, ii b){
ll x = INF;
ll y = INF;
ll l = a.fi;
ll r = a.se;
if(l%2==0){
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
x=min(x,sparse_even[l][i]);
l+=(1<<i);
}
}
}
else{
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
x=min(x,sparse_odd[l][i]);
l+=(1<<i);
}
}
}
l = b.fi;
r = b.se;
if(l%2==0){
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
y=min(y,sparse_even[l][i]);
l+=(1<<i);
}
}
}
else{
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
y=min(y,sparse_odd[l][i]);
l+=(1<<i);
}
}
}
return x>y;
}
priority_queue <ii, vii, function<bool(ii,ii)> > solve(cmp);
vector<ii> ans;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
idx[a[i]]=i;
if(i%2){
sparse_odd[i][0]=a[i];
sparse_even[i][0]=INF;
}
else{
sparse_odd[i][0]=INF;
sparse_even[i][0]=a[i];
}
}
for(int j = 1; j < 20; j++){
for(int i = 0; i < n; i++){
if(i+(1<<j)-1<n){
sparse_even[i][j]=min(sparse_even[i][j-1],sparse_even[i+(1<<(j-1))][j-1]);
sparse_odd[i][j]=min(sparse_odd[i][j-1],sparse_odd[i+(1<<(j-1))][j-1]);
}
}
}
solve.push(mp(0,n));
while(!solve.empty()){
ll l, r, l1;
ll x=INF;
l = solve.top().fi;
r = solve.top().se;
solve.pop();
if(l<r){
if(l%2==0){
l1=l;
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
x=min(x,sparse_even[l][i]);
l+=(1<<i);
}
}
ll y=INF;
l=idx[x]+1;
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
y=min(y,sparse_odd[l][i]);
l+=(1<<i);
}
}
ans.pb(mp(x,y));
solve.push(mp(l1,idx[x]));
solve.push(mp(idx[x]+1,idx[y]));
solve.push(mp(idx[y]+1,r));
}
else{
l1=l;
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
x=min(x,sparse_odd[l][i]);
l+=(1<<i);
}
}
ll y=INF;
l=idx[x]+1;
for(int i = 19; i >= 0; i--){
if(l+(1<<i)-1<r){
y=min(y,sparse_even[l][i]);
l+=(1<<i);
}
}
ans.pb(mp(x,y));
solve.push(mp(l1,idx[x]));
solve.push(mp(idx[x]+1,idx[y]));
solve.push(mp(idx[y]+1,r));
}
}
}
for(int i = 0; i < ans.size(); i++){
cout << ans[i].fi << ' ' << ans[i].se << ' ';
}
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Young Maids |
User |
vjudge1 |
Language |
C++14 (GCC 5.4.1) |
Score |
800 |
Code Size |
3001 Byte |
Status |
AC |
Exec Time |
721 ms |
Memory |
72804 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 |
2 ms |
4352 KB |
0_01.txt |
AC |
2 ms |
4352 KB |
0_02.txt |
AC |
2 ms |
4352 KB |
1_00.txt |
AC |
2 ms |
4352 KB |
1_01.txt |
AC |
2 ms |
4352 KB |
1_02.txt |
AC |
685 ms |
72292 KB |
1_03.txt |
AC |
664 ms |
72292 KB |
1_04.txt |
AC |
695 ms |
72292 KB |
1_05.txt |
AC |
657 ms |
72292 KB |
1_06.txt |
AC |
668 ms |
72804 KB |
1_07.txt |
AC |
638 ms |
72292 KB |
1_08.txt |
AC |
668 ms |
72292 KB |
1_09.txt |
AC |
670 ms |
72292 KB |
1_10.txt |
AC |
707 ms |
72292 KB |
1_11.txt |
AC |
721 ms |
72292 KB |
1_12.txt |
AC |
656 ms |
72036 KB |
1_13.txt |
AC |
671 ms |
72292 KB |
1_14.txt |
AC |
665 ms |
72164 KB |
1_15.txt |
AC |
674 ms |
72164 KB |
1_16.txt |
AC |
647 ms |
72164 KB |
1_17.txt |
AC |
658 ms |
72292 KB |
1_18.txt |
AC |
659 ms |
72164 KB |
1_19.txt |
AC |
647 ms |
72164 KB |