1. 程式人生 > >loj2472 「九省聯考 2018」IIIDX

loj2472 「九省聯考 2018」IIIDX

query OS scan code AC class pan source double

ref

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
int n, a[500005], fa[500005], siz[500005], cnt[500005], ans[500005];
int hmn[500005];
double k;
bool vis[500005];
struct SGT{
    int val[2000005], tag[2000005];
    void build(int o, int l, int r){
        if(l==r)    val[o] = l;
        else
{ int mid=(l+r)>>1; int lson=o<<1; int rson=lson|1; if(l<=mid) build(lson, l, mid); if(mid<r) build(rson, mid+1, r); val[o] = min(val[lson], val[rson]); } } void pushDown(int o, int lson, int
rson){ tag[lson] += tag[o]; tag[rson] += tag[o]; val[lson] += tag[o]; val[rson] += tag[o]; tag[o] = 0; } void update(int o, int l, int r, int x, int y, int k){ if(l>=x && r<=y){ val[o] += k; tag[o] += k; } else
{ int mid=(l+r)>>1; int lson=o<<1; int rson=lson|1; if(tag[o]) pushDown(o, lson, rson); if(x<=mid) update(lson, l, mid, x, y, k); if(mid<y) update(rson, mid+1, r, x, y, k); val[o] = min(val[lson], val[rson]); } } int query(int o, int l, int r, int x){ if(l==r) return val[o]>=x?l:l+1; else{ int mid=(l+r)>>1; int lson=o<<1; int rson=lson|1; if(tag[o]) pushDown(o, lson, rson); if(val[rson]>=x) return query(lson, l, mid, x); else return query(rson, mid+1, r, x); } } }sgt; bool cmp(int a, int b){ return a>b; } int main(){ cin>>n>>k; for(int i=1; i<=n; i++){ siz[i] = 1; fa[i] = i / k; scanf("%d", &a[i]); } sort(a+1, a+1+n, cmp); for(int i=n; i; i--){ siz[fa[i]] += siz[i]; if(a[i]!=a[i+1]) cnt[i] = 0; else cnt[i] = cnt[i+1] + 1; } sgt.build(1, 1, n); for(int i=1; i<=n; i++){ if(fa[i] && !vis[fa[i]]){ sgt.update(1, 1, n, ans[fa[i]], n, siz[fa[i]]-1); vis[fa[i]] = true; } int x=sgt.query(1, 1, n, siz[i]); x += cnt[x]; hmn[x]++; x -= hmn[x] - 1; ans[i] = x; sgt.update(1, 1, n, x, n, -siz[i]); } for(int i=1; i<=n; i++) printf("%d ", a[ans[i]]); printf("\n"); return 0; }

loj2472 「九省聯考 2018」IIIDX