# AGC001F - Wide Swap

## Code

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int Maxn = 500010;
int n, K;
int a[Maxn], ans[Maxn];
vector <int> vec[Maxn]; int no[Maxn]; bool v[Maxn];
int mx[Maxn<<2];
bool cmp(int x, int y) { return a[x] > a[y]; }
int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
priority_queue <int> q;
void build_tree(int now, int L, int R) {
if(L < R){
int mid = L+R>>1, lc = now<<1, rc = now<<1|1;
build_tree(lc, L, mid);
build_tree(rc, mid+1, R);
mx[now] = _max(mx[lc], mx[rc]);
} else mx[now] = a[L];
}
void chg(int now, int L, int R, int x) {
if(L == R){ mx[now] = 0; return; }
int mid = L+R>>1, lc = now<<1, rc = now<<1|1;
if(x <= mid) chg(lc, L, mid, x);
else chg(rc, mid+1, R, x);
mx[now] = _max(mx[lc], mx[rc]);
}
int qry(int now, int L, int R, int l, int r) {
if(L == l && R == r) return mx[now];
int mid = L+R>>1, lc = now<<1, rc = now<<1|1;
if(r <= mid) return qry(lc, L, mid, l, r);
else if(l > mid) return qry(rc, mid+1, R, l, r);
else return _max(qry(lc, L, mid, l, mid), qry(rc, mid+1, R, mid+1, r));
}
void ins(int x) {
if(x < 0 || x > n/K || no[x] == vec[x].size() || v[x] == true) return;
int p = vec[x][no[x]];
if(qry(1, 1, n, _max(1,p-K+1), _min(n,p+K-1)) == a[p]){
no[x]++; v[x] = true;
q.push(p);
}
}
int main() {
int i, j, k;
scanf("%d%d", &n, &K);
for(i = 1; i <= n; i++) scanf("%d", &a[i]);
build_tree(1, 1, n);
for(i = 1; i <= n; i++) vec[i/K].push_back(i);
for(i = 0; i <= n/K; i++) sort(vec[i].begin(), vec[i].end(), cmp);
for(i = 0; i <= n/K; i++) ins(i);
for(i = n; i >= 1; i--){
int x = q.top(); q.pop();
ans[x] = i; chg(1, 1, n, x);
v[x/K] = false;
ins(x/K-1); ins(x/K); ins(x/K+1);
}
for(i = 1; i <= n; i++) printf("%d\n", ans[i]);
return 0;
}


## Something More

posted @ 2018-10-15 20:46  Ra1nbow  阅读(365)  评论(0编辑  收藏