submission_id
stringlengths
5
9
problem_id
stringlengths
3
6
date
int64
-1
-1
language
stringclasses
11 values
verdict
stringclasses
1 value
cpu_time
int64
-1
15k
memory
int64
-1
1.07B
code
stringlengths
16
11k
source
stringclasses
1 value
testcount
int64
0
604
lenght
int64
16
11k
s059487525
p04052
1,574,907,194
cpp
Accepted
762
26,880
/* #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") #pragma GCC optimize("Os") #pragma GCC target("avx2,fma") #pragma GCC optimize("Ofast") #pragma GCC optimize("Os") */ #include <bits/stdc++.h> //#include <windows.h> #define ll long long #define X first #define Y second #define MP make_pair using namespace std; const int N = 5e5 + 123; const ll mod = 1e9 + 7; int n, k, f[N], b[N], Aybar_gde_F[N]; pair<int, int> a[N]; void fupd(int v, int val){ for(;v <= n;v = (v | (v + 1))) f[v] += val; } int fget(int v){ int val = 0; for(;v >= 0;v = (v & (v + 1)) - 1) val += f[v]; return val; } int t[N * 4], tt[N * 4]; void build(int v, int tl, int tr){ if(tl == tr){ t[v] = b[tl]; return; } int tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); t[v] = min(t[v * 2], t[v * 2 + 1]); } void push(int v){ if(tt[v] == 0) return; t[v * 2] = max(0, t[v * 2] + tt[v]); t[v * 2 + 1] = max(0, t[v * 2 + 1] + tt[v]); tt[v * 2] += tt[v], tt[v * 2 + 1] += tt[v]; tt[v] = 0; } void upd(int v, int tl, int tr, int l, int r, int val){ if(tl > r || l > tr) return; if(tl >= l && tr <= r){ t[v] = max(0, t[v] + val); tt[v] += val; return; } push(v); int tm = (tl + tr) / 2; upd(v * 2, tl, tm, l, r, val); upd(v * 2 + 1, tm + 1, tr, l, r, val); t[v] = min(t[v * 2], t[v * 2 + 1]); } int get_solve(int v, int tl, int tr){ if(tl == tr){ return tl; } push(v); int tm = (tl + tr) / 2; if(t[v * 2] < t[v * 2 + 1]){ return get_solve(v * 2, tl, tm); } else{ return get_solve(v * 2 + 1, tm + 1, tr); } } int main () { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for(int i = 1;i <= n;i++){ cin >> a[i].X; a[i].X *= -1; a[i].Y = i; } /* for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ if(abs(i - j) < k && -a[i].X < -a[j].X) cout << i << " " << j << "\n"; } } */ sort(a + 1, a + n + 1); for(int i = 1;i <= n;i++){ int pos = a[i].Y; b[pos] = fget(min(n, pos + k - 1)) - fget(max(0, pos - k)); fupd(pos, 1); } build(1, 1, n); for(int i = n;i >= 1;i--){ int pos = get_solve(1, 1, n); //cerr << b[pos] << " " << pos << "\n"; //cerr << pos << "\n"; Aybar_gde_F[pos] = i; upd(1, 1, n, max(1, pos - k + 1), min(n, pos + k - 1), -1); upd(1, 1, n, pos, pos, N); //debug(1, 1, n); } for(int i = 1;i <= n;i++) cout << Aybar_gde_F[i] << "\n"; return 0; }
codenet
-1
2,555
s594668966
p04052
1,532,964,889
cpp
Accepted
642
75,136
#include <stdio.h> #include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <vector> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; #define mp make_pair #define Fi first #define Se second #define pb(x) push_back(x) #define szz(x) ((int)(x).size()) #define rep(i, n) for(int i=0;i<n;i++) #define all(x) (x).begin(), (x).end() #define ldb ldouble typedef tuple<int, int, int> t3; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair <ll, int> pli; typedef pair <db, db> pdd; int IT_MAX = 1 << 19; const ll MOD = 1000000007; const int INF = 0x3f3f3f3f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const db PI = acos(-1); const db ERR = 1e-10; pii indt[1100000]; void update(int p, pii v) { p += IT_MAX - 1; for (; p; p /= 2) indt[p] = min(indt[p], v); } pii getmn(int p1, int p2) { p1 += IT_MAX - 1; p2 += IT_MAX - 1; pii rv = pii(INF, INF); for (; p1 <= p2; p1 /= 2, p2 /= 2) { if (p1 % 2 == 1) rv = min(rv, indt[p1++]); if (p2 % 2 == 0) rv = min(rv, indt[p2--]); } return rv; } int in[500050]; int rin[500050]; vector <int> conn[500050]; vector <int> rconn[500050]; int deg[500050]; int ans[500050]; int main() { int N, K, i, j; scanf("%d %d", &N, &K); for (i = 1; i <= N; i++) { scanf("%d", &in[i]); rin[in[i]] = i; } memset(indt, 0x3f, sizeof(indt)); for (i = N; i >= 1; i--) { int p = rin[i]; int st = p + 1, en = min(N, p + K - 1); pii u = getmn(st, en); if (u.first != INF) { conn[p].push_back(u.second); rconn[u.second].push_back(p); } st = max(1, p - K + 1), en = p - 1; u = getmn(st, en); if (u.first != INF) { conn[p].push_back(u.second); rconn[u.second].push_back(p); } update(p, pii(in[p], p)); } for (i = 1; i <= N; i++) deg[i] = conn[i].size(); priority_queue <int> Hx; for (i = 1; i <= N; i++) if (!deg[i]) Hx.push(i); for (i = N; i >= 1; i--) { int t = Hx.top(); Hx.pop(); for (auto it : rconn[t]) { deg[it]--; if (deg[it] == 0) Hx.push(it); } ans[t] = i; } for (i = 1; i <= N; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
2,557
s069403429
p04052
1,583,453,963
cpp
Accepted
1,241
58,240
#include <bits/stdc++.h> using namespace std; int n,k; ///store position of outdeg0 int ansp[500005]; int fenwick[500005]; int ans[500005]; int arr[500005]; void update(int pos,int val){ while (pos<n+2){ fenwick[pos]+=val; pos += pos&(-pos); } } int query(int pos){ int ans = 0; while (pos>0){ ans += fenwick[pos]; pos-= pos&(-pos); } return ans; } struct node{ int s,e; int v=0,lazy=0,vp; node *l,*r; node(int _s, int _e){ s = _s; e = _e; vp = e; if (s!=e){ l = new node(s,(s+e)/2); r = new node((s+e)/2+1,e); } } void proc(){ if (lazy==0) return; v += lazy; if (s!=e){ l->lazy+=lazy; r->lazy+=lazy; } lazy = 0; } void update(int a, int b, int val){ proc(); if (a<=s&&b>=e){ lazy += val; proc(); return; } if (b<=(s+e)/2){ l->update(a,b,val); } else if (a>(s+e)/2){ r->update(a,b,val); } else{ l->update(a,b,val); r->update(a,b,val); } proc(); l->proc(); r->proc(); if (l->v<r->v){ v = l->v; vp = l->vp; } else { v = r->v; vp = r->vp; } } pair<int,int> query(int a, int b){ proc(); //printf("node %d %d, v %d\n",s,e,v); if (a<=s&&b>=e){ return {v,vp}; } if (b<=(s+e)/2){ return l->query(a,b); } if (a>(s+e)/2){ return r->query(a,b); } else{ pair<int,int> t1 = l->query(a,b); pair<int,int> t2 = r->query(a,b); if (t1.first<t2.first) return t1; return t2; } } }*root; int main(){ scanf("%d%d",&n,&k); root = new node(1,n); for (int x = 0; x<n; x++){ scanf("%d",&arr[x+1]); } for (int x = 1; x<=k; x++){ update(arr[x],1); } int sz = k; for (int x = 1; x<=n; x++){ //printf("num %d\n",sz-query(arr[x])); root->update(x,x,sz-query(arr[x])); int t = x-k+1; if (t>=1&&t<=n){ update(arr[t],-1); sz--; } t = x+k; if (t>=1&&t<=n){ update(arr[t],1); sz++; } } for (int x = n; x>=1; x--){ auto res = root->query(1,n); assert(res.first==0); ansp[x] = res.second; ans[res.second] = x; root->update(res.second,res.second,1000000); root->update(max(1,res.second-k+1),min(n,res.second+k-1),-1); } for (int x = 1; x<=n; x++){ printf("%d\n",ans[x]); } }
codenet
-1
2,559
s627136228
p04052
1,571,244,052
cpp
Accepted
645
42,624
#include<bits/stdc++.h> using namespace std; typedef int ll; typedef long long int li; const ll MAXN=1e6+51,inf=0x3f3f3f3f; struct Edge{ ll to,prev; }; struct SegmentTree{ ll l,r,minn; }; Edge ed[MAXN<<1]; SegmentTree tree[MAXN<<2]; priority_queue<ll,vector<ll>,greater<ll> >q; ll cnt,tot,kk,x,ptr; ll last[MAXN],rid[MAXN],deg[MAXN],res[MAXN]; inline ll read() { register ll num=0,neg=1; register char ch=getchar(); while(!isdigit(ch)&&ch!='-') { ch=getchar(); } if(ch=='-') { neg=-1; ch=getchar(); } while(isdigit(ch)) { num=(num<<3)+(num<<1)+(ch-'0'); ch=getchar(); } return num*neg; } template <class T> inline ll Min(T x,T y) { return x<y?x:y; } template <class T> inline ll Max(T x,T y) { return x>y?x:y; } inline void addEdge(ll from,ll to) { ed[++tot].prev=last[from]; ed[tot].to=to; last[from]=tot; } inline void update(ll node) { tree[node].minn=Min(tree[node<<1].minn,tree[(node<<1)|1].minn); } inline void create(ll l,ll r,ll node) { tree[node].l=l,tree[node].r=r; if(tree[node].l==tree[node].r) { tree[node].minn=inf; return; } ll mid=(l+r)>>1; create(l,mid,node<<1); create(mid+1,r,(node<<1)|1); update(node); } inline void changePoint(ll pos,ll val,ll node) { if(tree[node].l==tree[node].r) { tree[node].minn=val; return; } ll mid=(tree[node].l+tree[node].r)>>1; if(pos<=mid) { changePoint(pos,val,node<<1); } else { changePoint(pos,val,(node<<1)|1); } update(node); } inline ll queryMin(ll l,ll r,ll node) { if(l<=tree[node].l&&r>=tree[node].r) { return tree[node].minn; } ll mid=(tree[node].l+tree[node].r)>>1,res=inf; if(l<=mid) { res=Min(res,queryMin(l,r,node<<1)); } if(r>mid) { res=Min(res,queryMin(l,r,(node<<1)|1)); } return res; } inline void toposort() { ll top; for(register int i=1;i<=cnt;i++) { if(!deg[i]) { q.push(i); } } while(!q.empty()) { top=q.top(),q.pop(),res[top]=++ptr; for(register int i=last[top];i;i=ed[i].prev) { deg[ed[i].to]--; if(!deg[ed[i].to]) { q.push(ed[i].to); } } } } int main() { cnt=read(),kk=read(); for(register int i=1;i<=cnt;i++) { rid[read()]=i; } create(1,cnt,1); for(register int i=cnt;i;i--) { x=queryMin(rid[i]+1,Min(rid[i]+kk-1,cnt),1); if(x>=1&&x<=cnt) { addEdge(rid[i],rid[x]),deg[rid[x]]++; } x=queryMin(Max(rid[i]-kk+1,1),rid[i]-1,1); if(x>=1&&x<=cnt) { addEdge(rid[i],rid[x]),deg[rid[x]]++; } changePoint(rid[i],i,1); } toposort(); for(register int i=1;i<=ptr;i++) { printf("%d\n",res[i]); } }
codenet
-1
2,585
s179493843
p04052
1,558,649,600
cpp
Accepted
485
46,464
#include<bits/stdc++.h> using namespace std; #define IL inline #define rep(i,j,k) for(int i=j;i<=k;++i) #define repd(i,j,k) for(int i=j;i>=k;--i) #define pb push_back #define db double #define mp make_pair #define mp3(a,b,c) mp(mp(a,b),c) #define pii pair<int,int> #define piii pair<pii,int> #define fr first #define se second #define ll long long #define ull unsigned long long #define pbc(x) __builtin_popcount(x) #define clr(x) memset(x,0,sizeof x) #define SIZE(x) (int)(x.size()) const int mod=1e9+7; IL int pls(int x,int y){x+=y;return x>=mod?x-mod:x;} IL int dec(int x,int y){x-=y;return x<0?x+mod:x;} IL int mul(int x,int y){return 1ll*x*y%mod;} IL int mul(int x,int y,int z){return mul(mul(x,y),z);} IL int mul(int x,int y,int z,int p){return mul(mul(x,y),mul(z,p));} IL void add(int &x,int y){x+=y;x=(x>=mod)?x-mod:x;} IL int fpw(int x,int y,int r=1){for(;y;y>>=1,x=mul(x,x))if(y&1)r=mul(r,x);return r;} IL int inv(int x){return fpw(x,mod-2);} IL int gi(){int x;int _w=scanf("%d",&x);return x;} IL void gi(int &x){int _w=scanf("%d",&x);} IL void write(int x){printf("%d\n",x);} void chkmax(int &x,int y){x=(x>y)?x:y;} void chkmin(int &x,int y){x=(x<y)?x:y;} const int INF=0x3f3f3f3f; template<typename T>IL void debug(T x){cerr<<x;return;} /* --------------------------------------------------------------------------------------------------------- */ int n,k; const int maxn=5e5+10; int a[maxn],p[maxn],deg[maxn],b[maxn]; vector<int>e[maxn]; int mn[maxn<<2]; void add(int p,int x,int y,int pos,int d){ if(x==y){mn[p]=d;return;} int mid=(x+y)>>1; if(pos<=mid)add(p<<1,x,mid,pos,d); else add(p<<1|1,mid+1,y,pos,d); mn[p]=min(mn[p<<1],mn[p<<1|1]); } int qry(int p,int x,int y,int l,int r){ if(x==l&&y==r)return mn[p]; int mid=(x+y)>>1; if(r<=mid)return qry(p<<1,x,mid,l,r); else if(l>mid)return qry(p<<1|1,mid+1,y,l,r); else return min(qry(p<<1,x,mid,l,mid),qry(p<<1|1,mid+1,y,mid+1,r)); } int main(){ #ifdef LOCAL freopen("/home/noilinux/Desktop/input.txt","r",stdin); #endif memset(mn,0x3f,sizeof mn); n=gi();k=gi(); rep(i,1,n)p[i]=gi(),a[p[i]]=i; repd(i,n,1){ int pos; pos=qry(1,1,n,a[i],min(a[i]+k-1,n)); if(pos!=INF)e[i].pb(pos),deg[pos]++; pos=qry(1,1,n,max(1,a[i]-k+1),a[i]); if(pos!=INF)e[i].pb(pos),deg[pos]++; add(1,1,n,a[i],i); } priority_queue<pii,vector<pii>,greater<pii> >q; for(int i=1;i<=n;++i)if(!deg[i])q.push({a[i],i}); int cnt=0; while(q.size()){ int t=q.top().se;q.pop(); b[++cnt]=a[t]; for(auto v:e[t]){ deg[v]--; if(!deg[v])q.push({a[v],v}); } } rep(i,1,n)p[b[i]]=i; rep(i,1,n)printf("%d\n",p[i]); return 0; }
codenet
-1
2,590
s689904184
p04052
1,540,564,808
cpp
Accepted
672
50,688
#include<cstdio> #include<cstring> #include<algorithm> #define ls (k<<1) #define rs (k<<1|1) #define inf 0x3fffffff using std::min; struct node { int l,r,v;//维护区间最小值 node(int l,int r) { this->l=l; this->r=r; v=inf; } node() { v=inf; } }t[2010000]; void build(int k,int l,int r) { t[k]=node(l,r); if(l==r) return; int mid=(l+r)>>1; build(ls,l,mid); build(rs,mid+1,r); } void change(int k,int p,int x) { if(t[k].l==t[k].r) { t[k].v=x; return; } int mid=(t[k].l+t[k].r)>>1; if(p<=mid) change(ls,p,x); else change(rs,p,x); t[k].v=min(t[ls].v,t[rs].v); } int ask(int k,int l,int r) { if(r<t[k].l||l>t[k].r) return inf; if(l<=t[k].l&&r>=t[k].r) return t[k].v; return min(ask(ls,l,r),ask(rs,l,r)); } struct edge { int n,nxt; edge(int n,int nxt) { this->n=n; this->nxt=nxt; } edge(){} }e[1010000]; int in[1010000],head[1010000],ecnt=-1; void add(int from,int to) { e[++ecnt]=edge(to,head[from]); head[from]=ecnt; ++in[to]; } int a[500500],b[500500]; int heap[500500],l=0; void Push(int x) { heap[++l]=x; int i=l; while(i>1&&heap[i]<heap[i>>1]) { int tmp=heap[i]; heap[i]=heap[i>>1]; heap[i>>1]=tmp; i>>=1; } } void pop() { heap[1]=heap[l--]; int i=1; while(i<<1<=l&&(heap[i]>heap[i<<1]||heap[i]>heap[i<<1|1])) { if(heap[i<<1]<heap[i<<1|1]||i<<1==l) { int tmp=heap[i]; heap[i]=heap[i<<1]; heap[i<<1]=tmp; i<<=1; } else { int tmp=heap[i]; heap[i]=heap[i<<1|1]; heap[i<<1|1]=tmp; i=i<<1|1; } } } int ans[500500],cnt=0; int main() { memset(head,-1,sizeof(head)); int n,m,u; scanf("%d%d",&n,&m); build(1,1,n); for(int i=1;i<=n;++i) { scanf("%d",&u); a[u]=i; } for(int i=n;i;--i) { int tmp=ask(1,a[i]+1,a[i]+m-1); if(tmp!=inf) add(a[i],a[tmp]); tmp=ask(1,a[i]-m+1,a[i]-1); if(tmp!=inf) add(a[i],a[tmp]); change(1,a[i],i); } for(int i=1;i<=n;++i) if(!in[i]) Push(i); while(l) { int x=heap[1]; ans[x]=++cnt; pop(); for(int i=head[x];~i;i=e[i].nxt) { --in[e[i].n]; if(!in[e[i].n]) Push(e[i].n); } } for(int i=1;i<=n;++i) printf("%d\n",ans[i]); return 0; }
codenet
-1
2,639
s819021673
p04052
1,514,878,597
cpp
Accepted
840
33,024
#include "bits/stdc++.h" using namespace std; #define fi first #define se second #define ll long long #define dbg(v) cerr<<#v<<" = "<<v<<'\n' #define vi vector<int> #define vl vector <ll> #define pii pair<int,int> #define mp make_pair #define db long double #define pb push_back #define all(s) s.begin(),s.end() template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;} template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;} const int N = (int)(1e6) + 5; int p[N]; int q[N]; struct treap { treap *l,*r; int ans; int cnt; int pr; int v; treap(int vv) { l = r = 0; ans = v = vv; cnt = 1; pr = rand(); } void up(void) { cnt = 1; ans = v; if (l) smin(ans,l->ans),cnt += l->cnt; if (r) smin(ans,r->ans),cnt += r->cnt; } }; typedef treap * tr; tr merge(tr a,tr b) { if (!a) return b; if (!b) return a; if (a->pr > b->pr) { a->r = merge(a->r,b); a->up(); return a; } else { b->l = merge(a,b->l); b->up(); return b; } } void split(tr Root,int k,tr & L,tr & R) { if (!Root) return void(L = R = 0); int cnt = 1; if (Root->l) cnt += Root->l->cnt; if (cnt <= k) { split(Root->r,k - cnt,Root->r,R); L = Root; } else { split(Root->l,k,L,Root->l); R = Root; } Root->up(); } void add(tr &Root,int pos,int v) { tr L,R; split(Root,pos,L,R); Root = merge(L,merge(new treap(v),R)); } int sz = 0; int get(tr Root,int v) { if (!Root) return 0; int ans1 = !Root->r ? 2 * N : Root->r->ans; int cnt1 = !Root->r ? 0 : Root->r->cnt; int ans2 = !Root->l ? 2 * N : Root->l->ans; int cnt2 = !Root->l ? 0 : Root->l->cnt; if (v > ans1) return cnt2 + 1 + get(Root->r,v); else if (Root->v < v) return cnt2 + 1; else return get(Root->l,v); } void go(tr Root) { if (!Root) return; go(Root->l); q[Root->v] = ++sz; go(Root->r); } void print(tr Root) { if (!Root) return; print(Root->l); cerr << Root->v << ' '; print(Root->r); } int main(void) { srand(time(0)); ios_base :: sync_with_stdio(0); int n,k; cin>>n>>k; for (int i = 1;i <= n;++i) { int v; cin>>v; p[v] = i; } tr Root = 0; for (int i = 1;i <= n;++i) { int index = get(Root,p[i] + k); add(Root,index,p[i]); } go(Root); for (int i = 1;i <= n;++i) cout << q[i] << " \n"[i == n]; return 0; }
codenet
-1
2,671
s771994211
p04052
1,529,189,851
cpp
Accepted
646
28,672
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 10, INF = 1e9; int p[MAXN], q[MAXN]; int f[MAXN]; int find(int p) { ++p; p = max(p, 0); p = min(p, MAXN - 1); int res = 0; for (;p; p -= p&-p) res += f[p]; return res; } void add(int p) { ++p; for (;p < MAXN; p += p&-p) ++f[p]; } int deg[MAXN]; pair <int, int> segment[4 * MAXN]; int lazy[4 * MAXN]; void build(int v, int b, int e) { if (e - b == 1) { segment[v] = {deg[b], b}; return; } int mid = (b + e) / 2; build(2 * v, b, mid); build(2 * v + 1, mid, e); if (segment[2 * v + 1].first <= segment[2 * v].first) segment[v] = segment[2 * v + 1]; else segment[v] = segment[2 * v]; } void shift(int v) { lazy[2 * v] += lazy[v]; lazy[2 * v + 1] += lazy[v]; segment[2 * v].first -= lazy[v]; segment[2 * v + 1].first -= lazy[v]; lazy[v] = 0; } void clr(int v, int b, int e, int p){ if (p < b || e <= p) return; if (e - b == 1) { segment[v] = {INF, b}; return; } int mid = (b + e) / 2; shift(v); clr(2 * v, b, mid, p); clr(2 * v + 1, mid, e, p); if (segment[2 * v + 1].first <= segment[2 * v].first) segment[v] = segment[2 * v + 1]; else segment[v]= segment[2 * v]; } void upd(int v, int b, int e, int l, int r) { if (r <= b || e <= l) return; if (l <= b && e <= r) { ++lazy[v]; --segment[v].first; return; } int mid = (b + e) / 2; shift(v); upd(2 * v, b, mid, l, r); upd(2 * v + 1, mid, e, l, r); if (segment[2 * v + 1].first <= segment[2 * v].first) segment[v] = segment[2 * v + 1]; else segment[v]= segment[2 * v]; } int32_t main () { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, k; cin >> n >> k; --k; for (int i = 0; i < n; ++i) { cin >> p[i]; --p[i]; q[p[i]] = i; } for (int i = n - 1; i >= 0; --i) { deg[q[i]] = find(q[i] + k) - find(q[i] - k - 1); add(q[i]); } // for (int i = 0; i < n; ++i) // cout << deg[i] << ' '; build(1, 0, n); for (int i = n - 1; i >= 0; --i) { // for (int i = 1; i < 2* n; ++i, cout << ' ') { // if (__builtin_popcount(i) == 1) // cout << '\n'; // cout << segment[i].first << ':' << segment[i].second; // } p[segment[1].second] = i; q[i] = segment[1].second; clr(1, 0, n, q[i]); upd(1, 0, n, q[i] - k, q[i] + k + 1); } for (int i = 0; i < n; ++i) cout << p[i] + 1 << '\n'; return 0; }
codenet
-1
2,672
s958883651
p04052
1,569,047,316
cpp
Accepted
1,335
25,200
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #define D(a) cerr << #a << " = " << a << endl #else #define D(a) #define cerr false && cerr #endif #define fastio ios_base::sync_with_stdio(0); cin.tie(0) #define dforsn(i,s,n) for(int i=int(n-1);i>=int(s);i--) #define forsn(i,s,n) for(int i=int(s);i<int(n);i++) #define dforn(i,n) dforsn(i,0,n) #define forn(i,n) forsn(i,0,n) #define all(a) a.begin(),a.end() #define si(a) int((a).size()) #define pb emplace_back #define mp make_pair #define snd second #define fst first #define endl '\n' using pii = pair<int,int>; using vi = vector<int>; using ll = long long; const int N = 5e5; const int INF = 1e9; struct RMQ { int neutro = -1e9; int sz; pii t[4*N]; int &operator [](int p){ t[sz + p].snd = p; return t[sz + p].fst; } void init(int n) { // O(n) sz = 1 << (32 - __builtin_clz(n)); forn(i, 2*sz) t[i] = {neutro, -1}; } void updall() { //O(n) dforsn(i,0,sz) t[i] = max(t[2*i], t[2*i + 1]); } pii get(int i, int j){ return get(i, j, 1, 0, sz); } pii get(int i, int j, int n, int a, int b){ // O(lg n) if(j <= a || i >= b) return mp(neutro, -1); if(i <= a && b <= j) return t[n]; int c = (a + b)/2; return max(get(i, j, 2*n, a, c), get(i, j, 2*n + 1, c, b)); } void set(int p, int val){ // O(lg n) pii act = {val, p}; for (p += sz; p > 0 && t[p].fst != val;){ t[p] = act, p /= 2; act = max(t[p*2], t[p*2 + 1]); } } } rmq; int main() { fastio; int n, k; cin >> n >> k; vi p(n); for (int &x : p) { cin >> x; x--; } rmq.init(n); forn(i, n) rmq[i] = p[i]; rmq.updall(); auto is_maximum = [&p, k, n](int i) { int l = max(i - k + 1, 0), r = min(i + k, n); return p[i] == rmq.get(l, r).fst; }; priority_queue<int> local_maximums; forn(i, n) if (is_maximum(i)) local_maximums.push(i); vi ans(n); dforn(next_id, n) { int local_maximum = -1; while (local_maximum == -1 || !is_maximum(local_maximum)) local_maximum = local_maximums.top(), local_maximums.pop(); ans[local_maximum] = next_id; rmq.set(local_maximum, -INF); auto update = [is_maximum, &local_maximums, n, k](int block) { if (block < 0 || block * k >= n) return; int l = block * k, r = min((block + 1) * k, n); int candidate = rmq.get(l, r).snd; if (candidate >= 0 && is_maximum(candidate)) local_maximums.push(candidate); }; int block_number = local_maximum / k; forsn(block, block_number - 1, block_number + 2) update(block); } for (int &x : ans) cout << x + 1 << endl; return 0; }
codenet
-1
2,690
s754502499
p04052
1,537,311,718
cpp
Accepted
288
30,960
#include <bits/stdc++.h> #define N 500020 #define ll long long using namespace std; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar()); while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); return f?x:-x; } int ins[N]; int p[N], q[N]; int to[N<<1], nxt[N<<1], head[N], cnt; void insert(int x, int y) { // printf("%d(%d) --> %d(%d)\n", x, q[x], y, q[y]); ++ ins[x]; to[++ cnt] = x; nxt[cnt] = head[y]; head[y] = cnt; } // 单点加,查询区间max namespace sgt { int mx[N<<2]; void update(int x, int k, int L, int R, int v) { if (L == R) { mx[x] = max(mx[x], v); return; } int mid = L + R >> 1; if (k <= mid) update(x << 1, k, L, mid, v); else update(x<<1|1, k, mid + 1, R, v); mx[x] = max(mx[x << 1], mx[x<<1|1]); } int query(int x, int l, int r, int L, int R) { if (l > r) return 0; if (l == L && r == R) return mx[x]; int mid = L + R >> 1; if (r <= mid) return query(x << 1, l, r, L, mid); else if (l > mid) return query(x<<1|1, l, r, mid + 1, R); return max(query(x << 1, l, mid, L, mid), query(x<<1|1, mid + 1, r, mid + 1, R)); } } typedef pair<int, int> pii; priority_queue<pii, vector<pii>, greater<pii>> que; int main(int argc, char const *argv[]) { int n = read(), k = read(); for (int i = 1; i <= n; ++ i) { q[p[i] = read()] = i; } // for (int i = 1; i <= n; ++ i) { // printf("%d ", q[i]); // } puts(""); for (int i = 1; i <= n; ++ i) { // int ql = max(1, q[i] - k + 1); int ql = q[i] + 1; int qr = min(n, q[i] + k - 1); int mn = sgt::query(1, ql, qr, 1, n); if (mn) { insert(i, mn); } sgt::update(1, q[i], 1, n, i); } for (int i = 1; i <= n; ++ i) { if (!ins[i]) { que.push({ q[i], i }); } } vector<int> res; while (!que.empty()) { int x = que.top().second; que.pop(); for (int i = head[x]; i; i = nxt[i]) { if (!-- ins[to[i]]) { que.push({ q[to[i]], to[i] }); } } res.push_back(x); } // for (int i = 0; i < n; ++ i) { // printf("%d\n", q[res[i]]); // } puts("===="); for (size_t i = 0; i < res.size(); ++ i) { p[q[res[i]]] = i + 1; } for (int i = 1; i <= n; ++ i) { printf("%d\n", p[i]); } return 0; } /* 先求原序列的逆序q(q[p[i]]=i) 发现原序列中的操作在逆序中即为交换两个相邻的差值>=k的元素。 不难发现逆序中的最小字典序即为原序列的最小字典序。(猜的 发现所有差值<k的元素的相对位置不会发生变化。 例如第 3 个样例,逆序为 6 7 5 1 2 8 3 4 + ^-+ ^-^-+ + ^-+ ^-^-------+ ^-^-^---+ ^---^---^---^-+ (图为每个点不能越过的元素的限制) 直接求拓扑序即可。 但是直接做是 n^2 的,于是只要将每个点与前面最后一个限制连线即可。 6 7 5 1 2 8 3 4 + ^-+ ^-+ + ^-+ ^-------+ ^-------+ ^-------------+ 总复杂度 O(nlogn) */
codenet
-1
2,690
s486243623
p04052
1,468,730,935
cpp
Accepted
2,862
169,324
// wtf ... forgot to participate >_< #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <cassert> #include <queue> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head const int N=501000; struct node { int l,r,ind; }pool[N*22]; int cur=1,rt[N]; priority_queue<pair<int,int> > Q; map<int,int> hs; int n,k,p[N],pos[N]; VI ret; int build(int l,int r) { int p=cur++; if (l!=r) { int md=(l+r)>>1; pool[p].l=build(l,md); pool[p].r=build(md+1,r); pool[pool[p].l].ind++; pool[pool[p].r].ind++; } return p; } void adde(int p,int q,int l,int r,int tl,int tr) { if (l==tl&&r==tr) { pool[q].ind++; } else { int md=(l+r)>>1; if (tr<=md) adde(p,pool[q].l,l,md,tl,tr); else if (tl>md) adde(p,pool[q].r,md+1,r,tl,tr); else adde(p,pool[q].l,l,md,tl,md),adde(p,pool[q].r,md+1,r,md+1,tr); } } void dfs(int p) { if (pool[p].l==0) { Q.push(mp(hs[p],p)); } else { if ((--pool[pool[p].l].ind)==0) dfs(pool[p].l); if ((--pool[pool[p].r].ind)==0) dfs(pool[p].r); } } void dele(int p,int q,int l,int r,int tl,int tr) { if (l==tl&&r==tr) { if ((--pool[q].ind)==0) dfs(q); } else { int md=(l+r)>>1; if (tr<=md) dele(p,pool[q].l,l,md,tl,tr); else if (tl>md) dele(p,pool[q].r,md+1,r,tl,tr); else dele(p,pool[q].l,l,md,tl,md),dele(p,pool[q].r,md+1,r,md+1,tr); } } int insert(int p,int l,int r,int x,int v) { if (l==r) { return v; } else { int q=cur++; int md=(l+r)>>1; if (x<=md) pool[q].l=insert(pool[p].l,l,md,x,v),pool[q].r=pool[p].r; else pool[q].r=insert(pool[p].r,md+1,r,x,v),pool[q].l=pool[p].l; pool[pool[q].l].ind++; pool[pool[q].r].ind++; return q; } } int main() { scanf("%d%d",&n,&k); --k; rep(i,1,n+1) { scanf("%d",p+i); pos[p[i]]=i; } rt[0]=build(1,n); rep(i,1,n+1) { int q=cur++; hs[q]=pos[i]; adde(q,rt[i-1],1,n,max(1,pos[i]-k),min(n,pos[i]+k)); rt[i]=insert(rt[i-1],1,n,pos[i],q); } pool[rt[0]].ind=-1; per(i,1,n+1) if (pool[rt[i]].ind==0) dfs(rt[i]); while (!Q.empty()) { int id=Q.top().fi; int q=Q.top().se; Q.pop(); ret.pb(id); dele(q,rt[p[id]-1],1,n,max(1,id-k),min(n,id+k)); } // printf("%d\n",SZ(ret)); reverse(all(ret)); rep(i,0,n) p[ret[i]]=i+1; rep(i,1,n+1) printf("%d\n",p[i]); }
codenet
-1
2,692
s077102406
p04052
1,527,717,211
cpp
Accepted
658
28,672
#include <bits/stdc++.h> using namespace std; constexpr int N = 5e5 + 10; constexpr int MOD = 1e9 + 7; int n, k, a[N], fen[N], p[N], d[N], lazy[4 * N]; pair <int, int> tree[4 * N]; void upd (int x, int val) { for (; x < N; x += x & (-x)) fen[x] += val; } int get (int x) { int ret = 0; for (; x; x -= x & (-x)) ret += fen[x]; return ret; } void build (int b, int e, int idx) { if (b + 1 == e) { tree[idx] = {d[b], b}; return; } int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1; build (b, mid, lidx); build (mid, e, ridx); if (tree[ridx].first <= tree[lidx].first) tree[idx] = tree[ridx]; else tree[idx] = tree[lidx]; } void shift (int b, int e, int idx) { int lidx = idx << 1, ridx = lidx | 1; tree[lidx].first -= lazy[idx]; tree[ridx].first -= lazy[idx]; lazy[ridx] += lazy[idx]; lazy[lidx] += lazy[idx]; lazy[idx] = 0; } void dec (int b, int e, int l, int r, int idx) { if (b >= r || e <= l) return; if (b >= l && e <= r) { tree[idx].first--; lazy[idx]++; return; } int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1; shift (b, e, idx); dec (b, mid, l, r, lidx); dec (mid, e, l, r, ridx); if (tree[ridx].first <= tree[lidx].first) tree[idx] = tree[ridx]; else tree[idx] = tree[lidx]; } void change (int b, int e, int pos, int idx) { if (b + 1 == e) { tree[idx] = {1e9, b}; return; } int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1; shift (b, e, idx); if (pos >= mid) change (mid, e, pos, ridx); else change (b, mid, pos, lidx); if (tree[ridx].first <= tree[lidx].first) tree[idx] = tree[ridx]; else tree[idx] = tree[lidx]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { upd (a[i], 1); if (i > k) upd (a[i - k], -1); d[i] = get (N - 1) - get (a[i]); } memset (fen, 0, sizeof (fen)); for (int i = n; i; i--) { upd (a[i], 1); if (i <= n - k) upd (a[i + k], -1); d[i] += get (N - 1) - get (a[i]); } build (1, n + 1, 1); int now = n; while (now) { int pos = tree[1].second; p[pos] = now; now--; dec (1, n + 1, max (1, pos - k + 1), min (n + 1, pos + k), 1); change (1, n + 1, pos, 1); } for (int i = 1; i <= n; i++) cout << p[i] << '\n'; return 0; }
codenet
-1
2,707
s377593435
p04052
1,527,717,112
cpp
Accepted
634
28,672
#include <bits/stdc++.h> using namespace std; constexpr int N = 5e5 + 10; constexpr int MOD = 1e9 + 7; int n, k, a[N], fen[N], p[N], d[N], lazy[4 * N]; pair <int, int> tree[4 * N]; void upd (int x, int val) { for (; x < N; x += x & (-x)) fen[x] += val; } int get (int x) { int ret = 0; for (; x; x -= x & (-x)) ret += fen[x]; return ret; } void build (int b, int e, int idx) { if (b + 1 == e) { tree[idx] = {d[b], b}; return; } int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1; build (b, mid, lidx); build (mid, e, ridx); if (tree[ridx].first <= tree[lidx].first) tree[idx] = tree[ridx]; else tree[idx] = tree[lidx]; } void shift (int b, int e, int idx) { int lidx = idx << 1, ridx = lidx | 1; tree[lidx].first -= lazy[idx]; tree[ridx].first -= lazy[idx]; lazy[ridx] += lazy[idx]; lazy[lidx] += lazy[idx]; lazy[idx] = 0; } void dec (int b, int e, int l, int r, int idx) { if (b >= r || e <= l) return; if (b >= l && e <= r) { tree[idx].first--; lazy[idx]++; return; } int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1; shift (b, e, idx); dec (b, mid, l, r, lidx); dec (mid, e, l, r, ridx); if (tree[ridx].first <= tree[lidx].first) tree[idx] = tree[ridx]; else tree[idx] = tree[lidx]; } void change (int b, int e, int pos, int idx) { if (b + 1 == e) { tree[idx] = {1e9, b}; return; } int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1; shift (b, e, idx); if (pos >= mid) change (mid, e, pos, ridx); else change (b, mid, pos, lidx); if (tree[ridx].first <= tree[lidx].first) tree[idx] = tree[ridx]; else tree[idx] = tree[lidx]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { upd (a[i], 1); if (i > k) upd (a[i - k], -1); d[i] = get (N - 1) - get (a[i]); } memset (fen, 0, sizeof (fen)); for (int i = n; i; i--) { upd (a[i], 1); if (i <= n - k) upd (a[i + k], -1); d[i] += get (N - 1) - get (a[i]); } build (1, n + 1, 1); int now = n; while (now) { int pos = tree[1].second; p[pos] = now; now--; dec (1, n + 1, max (1, pos - k + 1), min (n + 1, pos + k), 1); change (1, n + 1, pos, 1); } for (int i = 1; i <= n; i++) cout << p[i] << '\n'; return 0; }
codenet
-1
2,707
s675845709
p04052
1,507,559,972
cpp
Accepted
1,079
45,440
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<set> #define For(i , j , k) for (int i = (j) , _##end_ = (k) ; i <= _##end_ ; ++ i) #define Fordown(i , j , k) for (int i = (j) , _##end_ = (k) ; i >= _##end_ ; -- i) #define Set(a , b) memset(a , b , sizeof(a)) #define pb push_back #define INF (0x3f3f3f3f) #define Mod (1000000007) using namespace std; typedef long long LL; template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; } template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; } int _ , __; char c_; inline int read() { for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1; for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48); return _ * __; } inline void file() { #ifdef hany01 freopen("permutation.in" , "r" , stdin); freopen("permutation.out" , "w" , stdout); #endif } const int maxn = 500010 , maxm = 10000000; int v[maxm] , in[maxn] , nex[maxm] , beg[maxn] , n , k , p[maxn] , e , ans[maxn]; inline void add(int uu , int vv) { // printf("%d %d\n" , uu , vv); v[++ e] = vv; ++ in[vv]; nex[e] = beg[uu]; beg[uu] = e; } struct Item { int key , pos; bool operator < (const Item &item) const { return key < item.key; } }; typedef set<Item>::iterator It; It it; inline void init() { n = read(); k = read(); For(i , 1 , n) p[i] = read(); set<Item> st; For(i , 1 , n) { if (i >= k + 1) st.erase((Item){p[i - k] , i - k}); it = st.upper_bound((Item){p[i] , i}); if (it != st.end()) add(i , (*it).pos); /* printf("%d:" , p[i]); for (it = st.begin() ; it != st.end() ; ++ it) printf(" %d" , (*it).key); putchar('\n');*/ st.insert((Item){p[i] , i}); } st.clear(); For(i , 1 , k - 1) st.insert((Item){p[i] , i}); // putchar('\n'); For(i , 1 , n) { st.erase((Item){p[i] , i}); if (i + k - 1 <= n) st.insert((Item){p[i + k - 1] , i + k - 1}); it = st.upper_bound((Item){p[i] , i}); if (it != st.end()) add(i , (*it).pos); /* printf("%d:" , p[i]); for (it = st.begin() ; it != st.end() ; ++ it) printf(" %d" , (*it).key); putchar('\n');*/ st.erase((Item){p[i - k + 1] , i - k + 1}); } } inline void toposort() { priority_queue<int , vector<int> , greater<int> > q; For(i , 1 , n) if (!in[i]) q.push(i); int now = 0; while (!q.empty()) { int u = q.top(); q.pop(); ans[u] = ++ now; for (int i = beg[u] ; i ; i = nex[i]) { -- in[v[i]]; if (!in[v[i]]) q.push(v[i]); } } } inline void print() { For(i , 1 , n) printf("%d\n" , ans[i]); } int main() { file(); init(); toposort(); print(); return 0; }
codenet
-1
2,749
s174289663
p04052
1,519,978,744
cpp
Accepted
919
33,152
#include <bits/stdc++.h> #define endl '\n' using namespace std; template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; } template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; } const int MAXN = (1 << 20); random_device rd; mt19937 mt(rd()); struct node { int prior, val, mn, sz; node *l, *r, *par; node() { prior = val = mn = sz = 0; par = l = r = nullptr; } node(int v) { prior = mt(); val = mn = v; sz = 1; par = l = r = nullptr; } }; typedef node* pnode; int size(pnode t) { return t ? t->sz : 0; } void update_size(pnode &t) { if(t) t->sz = size(t->l) + size(t->r) + 1; } void reset(pnode &t) { if(t) t->mn = t->val; } void update_par(pnode &t) { if(!t) return; if(t->l) t->l->par = t; if(t->r) t->r->par = t; t->par = nullptr; } void combine(pnode &t, pnode l, pnode r) { if(!l) { t = r; return; } if(!r) { t = l; return; } t->mn = min(l->mn, r->mn); } void operation(pnode &t) { if(!t) return; reset(t); combine(t, t->l, t); combine(t, t, t->r); } void merge(pnode &t, pnode l, pnode r) { if(!l) { t = r; return; } if(!r) { t = l; return; } if(l->prior > r->prior) merge(l->r, l->r, r), t = l; else merge(r->l, l, r->l), t = r; update_par(t); update_size(t); operation(t); } void split(pnode t, pnode &l, pnode &r, int k, int add = 0) { if(!t) { l = r = nullptr; return; } int idx = add + size(t->l); if(idx <= k) split(t->r, t->r, r, k, idx + 1), l = t; else split(t->l, l, t->l, k, add), r = t; update_par(t); update_size(t); operation(t); } int get_pos(pnode it) { if(!it) return -1; pnode u = it; int answer = size(it->l); while(true) { if(!u->par) break; if(u->par->r == u) answer += size(u->par->l) + 1; u = u->par; } return answer; } int find_leftmost(pnode it, int v) { if(!it) return 0; int MN = it->val; if(it->r) chkmin(MN, it->r->mn); if(MN < v) return size(it->l) + 1 + find_leftmost(it->r, v); else return find_leftmost(it->l, v); } int n, k; int a[MAXN], p[MAXN]; void read() { cin >> n >> k; for(int i = 1; i <= n; i++) cin >> p[i]; } pnode root, pin[MAXN]; int answer[MAXN]; void dfs(pnode t, int add = 0) { if(!t) return; dfs(t->l, add); dfs(t->r, add + size(t->l) + 1); answer[t->val - 1] = 1 + size(t->l) + add; } void solve() { for(int i = 1; i <= n; i++) a[p[i]] = i; root = nullptr; for(int vv = 1; vv <= n; vv++) { int v = a[vv]; int po = find_leftmost(root, v + k); pnode l, r, curr = new node(v); split(root, l, r, po - 1); merge(root, l, curr); merge(root, root, r); } dfs(root); for(int i = 0; i < n; i++) cout << answer[i] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
codenet
-1
2,751
s174289663
p04052
1,519,978,744
cpp
Accepted
919
33,152
#include <bits/stdc++.h> #define endl '\n' using namespace std; template<class T, class T2> inline void chkmax(T &x, const T2 &y) { if(x < y) x = y; } template<class T, class T2> inline void chkmin(T &x, const T2 &y) { if(x > y) x = y; } const int MAXN = (1 << 20); random_device rd; mt19937 mt(rd()); struct node { int prior, val, mn, sz; node *l, *r, *par; node() { prior = val = mn = sz = 0; par = l = r = nullptr; } node(int v) { prior = mt(); val = mn = v; sz = 1; par = l = r = nullptr; } }; typedef node* pnode; int size(pnode t) { return t ? t->sz : 0; } void update_size(pnode &t) { if(t) t->sz = size(t->l) + size(t->r) + 1; } void reset(pnode &t) { if(t) t->mn = t->val; } void update_par(pnode &t) { if(!t) return; if(t->l) t->l->par = t; if(t->r) t->r->par = t; t->par = nullptr; } void combine(pnode &t, pnode l, pnode r) { if(!l) { t = r; return; } if(!r) { t = l; return; } t->mn = min(l->mn, r->mn); } void operation(pnode &t) { if(!t) return; reset(t); combine(t, t->l, t); combine(t, t, t->r); } void merge(pnode &t, pnode l, pnode r) { if(!l) { t = r; return; } if(!r) { t = l; return; } if(l->prior > r->prior) merge(l->r, l->r, r), t = l; else merge(r->l, l, r->l), t = r; update_par(t); update_size(t); operation(t); } void split(pnode t, pnode &l, pnode &r, int k, int add = 0) { if(!t) { l = r = nullptr; return; } int idx = add + size(t->l); if(idx <= k) split(t->r, t->r, r, k, idx + 1), l = t; else split(t->l, l, t->l, k, add), r = t; update_par(t); update_size(t); operation(t); } int get_pos(pnode it) { if(!it) return -1; pnode u = it; int answer = size(it->l); while(true) { if(!u->par) break; if(u->par->r == u) answer += size(u->par->l) + 1; u = u->par; } return answer; } int find_leftmost(pnode it, int v) { if(!it) return 0; int MN = it->val; if(it->r) chkmin(MN, it->r->mn); if(MN < v) return size(it->l) + 1 + find_leftmost(it->r, v); else return find_leftmost(it->l, v); } int n, k; int a[MAXN], p[MAXN]; void read() { cin >> n >> k; for(int i = 1; i <= n; i++) cin >> p[i]; } pnode root, pin[MAXN]; int answer[MAXN]; void dfs(pnode t, int add = 0) { if(!t) return; dfs(t->l, add); dfs(t->r, add + size(t->l) + 1); answer[t->val - 1] = 1 + size(t->l) + add; } void solve() { for(int i = 1; i <= n; i++) a[p[i]] = i; root = nullptr; for(int vv = 1; vv <= n; vv++) { int v = a[vv]; int po = find_leftmost(root, v + k); pnode l, r, curr = new node(v); split(root, l, r, po - 1); merge(root, l, curr); merge(root, root, r); } dfs(root); for(int i = 0; i < n; i++) cout << answer[i] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
codenet
-1
2,751
s297636353
p04052
1,541,704,775
cpp
Accepted
556
45,568
/*#pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/ #include<bits/stdc++.h> #define ll long long #define inf 1000000005 #define mod 1000000007 #define put putchar('\n') #define F(i,a,b) for (int i=(a);i<=(b);i++) #define D(i,a,b) for (int i=(a);i>=(b);i--) #define go(i,t) for (int i=head[t];i;i=Next[i]) #define sqr(x) ((x)*(x)) #define re register #define mp make_pair #define fi first #define se second #define pa pair<int,int> #define pb push_back #define be begin() #define en end() #define ret return puts("-1"),0; #define N 500005 //#define int ll using namespace std; inline char gc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } #define gc getchar inline int read(){char c=getchar();int tot=1;while ((c<'0'|| c>'9')&&c!='-') c=getchar();if (c=='-'){tot=-1;c=getchar();} int sum=0;while (c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}return sum*tot;} inline void wr(int x){if (x<0) {putchar('-');wr(-x);return;}if(x>=10)wr(x/10);putchar(x%10+'0');} inline void wrn(int x){wr(x);put;}inline void wri(int x){wr(x);putchar(' ');} inline void wrn(int x,int y){wri(x);wrn(y);}inline void wrn(int a,int b,int c){wri(a);wrn(b,c);} int n,m,ans,k,a[N],b[N],tot; int nedge,head[N],to[N*2],Next[N*2],in1[N]; #define V to[i] void add(int a,int b){ Next[++nedge]=head[a];head[a]=nedge;to[nedge]=b;in1[b]++; } void add_ne(int a,int b){add(a,b);add(b,a);} struct tre{ struct t_r{ int l,r,min; }t[N*4]; void build(int x,int l,int r){ t[x].l=l;t[x].r=r;t[x].min=n+1; if (l==r){ return; } int mid=(t[x].l+t[x].r)>>1; build(x*2,l,mid);build(x*2+1,mid+1,r); } void change(int x,int k,int p){ if (t[x].l==t[x].r){ t[x].min=p;return; } int mid=(t[x].l+t[x].r)>>1; if (k<=mid) change(x*2,k,p); else change(x*2+1,k,p); t[x].min=min(t[x*2].min,t[x*2+1].min); } int query(int x,int l,int r){ if (t[x].l==l&&t[x].r==r) return t[x].min; int mid=(t[x].l+t[x].r)>>1; if (r<=mid) return query(x*2,l,r); else if (l>mid) return query(x*2+1,l,r); else return min(query(x*2,l,mid),query(x*2+1,mid+1,r)); } }tr; int query(int l,int r){ l=max(l,1);r=min(r,n);if (l>r) return n+1; return tr.query(1,l,r); } set <int> q; signed main(){ n=read();k=read(); F(i,1,n) a[i]=read(),b[a[i]]=i; tr.build(1,1,n); D(i,n,1){ int t=query(b[i]-k+1,b[i]); if (t!=n+1) add(b[i],b[t]); t=query(b[i],b[i]+k-1); if (t!=n+1) add(b[i],b[t]); tr.change(1,b[i],i); } F(i,1,n) if (in1[i]==0) q.insert(i); while (!q.empty()){ int t=*q.be;q.erase(t);a[t]=++tot; go(i,t){ in1[V]--; if (in1[V]==0) q.insert(V); } } F(i,1,n) wri(a[i]);put; return 0; }
codenet
-1
2,755
s512712985
p04052
1,554,429,524
cpp
Accepted
678
36,848
#include<bits/stdc++.h> using namespace std; typedef pair<int, int> pii; const int MN = 500010; struct Fenwick { vector<int> tree; void init() { tree = vector<int>(MN + 1, 0); } void upd(int x, int v) { for(int i = x + 1; i <= MN; i += (i & -i)) tree[i] += v; } int quer(int a) { if(a >= MN) a = MN - 1; int ret = 0; for(int i = a + 1; i >= 1; i -= (i & -i)) ret += tree[i]; return ret; } int quer(int a, int b) { return quer(b) - quer(a - 1); } } fw; int N, K; int P[MN], deg[MN], ans[MN]; struct BIT { vector<pii> tree; vector<int> lazy; void init() { tree = vector<pii>(4 * N); lazy = vector<int>(4 * N, 0); build(0, N - 1, 1); } void build(int l, int r, int n) { if(l == r) { tree[n] = pii(deg[l], l); return; } int m = (l + r)>>1; build(l, m, 2*n); build(m + 1, r, 2*n + 1); tree[n] = min(tree[2*n], tree[2*n + 1]); } void prop(int l, int r, int n) { if(l != r) { tree[2*n].first += lazy[n]; lazy[2*n] += lazy[n]; tree[2*n + 1].first += lazy[n]; lazy[2*n + 1] += lazy[n]; lazy[n] = 0; } } void upd(int a, int b, int d, int l, int r, int n) { if(b < l || r < a) return; if(a <= l && r <= b) { tree[n].first += d; lazy[n] += d; return; } prop(l, r, n); int m = (l + r)>>1; upd(a, b, d, l, m, 2*n); upd(a, b, d, m + 1, r, 2*n + 1); tree[n] = min(tree[2*n], tree[2*n + 1]); } pii quer(int a, int b, int l, int r, int n) { if(b < l || r < a) return pii(1e9, 1e9); if(a <= l && r <= b) return tree[n]; prop(l, r, n); int m = (l + r)>>1; pii L = quer(a, b, l, m, 2*n); pii R = quer(a, b, m + 1, r, 2*n + 1); return min(L, R); } } bit; priority_queue<int> pq; int main() { scanf("%d %d", &N, &K); for(int i = 0; i < N; i++) { int t; scanf("%d", &t); t--; P[t] = i; } fw.init(); for(int i = N - 1; i >= 0; i--) { deg[ P[i] ] = fw.quer(P[i] - K + 1, P[i] + K - 1); fw.upd(P[i], 1); } bit.init(); for(int i = N - 1; i >= 0; i--) { while(1) { pii t = bit.tree[1]; if(t.first) break; bit.upd(t.second, t.second, 1e9, 0, N - 1, 1); pq.push(t.second); } int t = pq.top(); pq.pop(); ans[t] = i; bit.upd(max(0, t - K + 1), min(N - 1, t + K - 1), -1, 0, N - 1, 1); } for(int i = 0; i < N; i++) { printf("%d\n", ans[i] + 1); } }
codenet
-1
2,776
s202685930
p04052
1,542,874,435
cpp
Accepted
1,825
31,072
#ifdef DEBUG #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; typedef long double ld; #ifdef DEBUG #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #else #define eprintf(...) ; #endif #define sz(x) ((int) (x).size()) #define TASK "text" const int inf = (int) 1.01e9; const long long infll = (long long) 1.01e18; const ld eps = 1e-9; const ld pi = acos((ld) -1); #ifdef DEBUG mt19937 mrand(300); #else mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); #endif int rnd(int x) { return mrand() % x; } void precalc() { } const int maxn = (int) 5e5 + 5; int n, k; int q[maxn]; bool read() { if (scanf("%d%d", &n, &k) < 2) { return false; } k--; for (int i = 0; i < n; i++) { scanf("%d", &q[i]); q[i]--; } return true; } int fen[maxn]; void add(int i, int val) { for (; i < n; i |= i + 1) { fen[i] += val; } } int get(int i) { int res = 0; for (; i >= 0; i = (i & (i + 1)) - 1) { res += fen[i]; } return res; } void addSeg(int l, int r, int val) { l = max(l, 0); r = min(r, n); add(l, val); add(r, -val); } void qsort(vector<int> &p) { int n = sz(p); if (n <= 1) { return; } int x = rnd(n); vector<int> todel; addSeg(p[x] - k, p[x] + k + 1, 1); todel.push_back(p[x]); vector<int> l, r; for (int i = x - 1; i >= 0; i--) { if (p[i] > p[x] && !get(p[i])) { r.push_back(p[i]); } else { l.push_back(p[i]); addSeg(p[i] - k, p[i] + k + 1, 1); todel.push_back(p[i]); } } reverse(l.begin(), l.end()); reverse(r.begin(), r.end()); for (int i = 0; i < sz(todel); i++) { addSeg(todel[i] - k, todel[i] + k + 1, -1); } todel.clear(); addSeg(p[x] - k, p[x] + k + 1, 1); todel.push_back(p[x]); for (int i = x + 1; i < n; i++) { if (p[i] < p[x] && !get(p[i])) { l.push_back(p[i]); } else { r.push_back(p[i]); addSeg(p[i] - k, p[i] + k + 1, 1); todel.push_back(p[i]); } } for (int i = 0; i < sz(todel); i++) { addSeg(todel[i] - k, todel[i] + k + 1, -1); } todel.clear(); qsort(l); qsort(r); int val = p[x]; for (int i = 0; i < sz(l); i++) { p[i] = l[i]; } p[sz(l)] = val; for (int i = 0; i < sz(r); i++) { p[sz(l) + 1 + i] = r[i]; } } void solve() { vector<int> p(n); for (int i = 0; i < n; i++) { p[q[i]] = i; } memset(fen, 0, sizeof(fen)); qsort(p); for (int i = 0; i < n; i++) { q[p[i]] = i; } for (int i = 0; i < n; i++) { printf("%d\n", q[i] + 1); } } int main() { precalc(); #ifdef DEBUG assert(freopen(TASK ".in", "r", stdin)); assert(freopen(TASK ".out", "w", stdout)); #endif while (read()) { solve(); #ifdef DEBUG eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC); #endif } return 0; }
codenet
-1
2,820
s202685930
p04052
1,542,874,435
cpp
Accepted
1,825
31,072
#ifdef DEBUG #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; typedef long double ld; #ifdef DEBUG #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #else #define eprintf(...) ; #endif #define sz(x) ((int) (x).size()) #define TASK "text" const int inf = (int) 1.01e9; const long long infll = (long long) 1.01e18; const ld eps = 1e-9; const ld pi = acos((ld) -1); #ifdef DEBUG mt19937 mrand(300); #else mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); #endif int rnd(int x) { return mrand() % x; } void precalc() { } const int maxn = (int) 5e5 + 5; int n, k; int q[maxn]; bool read() { if (scanf("%d%d", &n, &k) < 2) { return false; } k--; for (int i = 0; i < n; i++) { scanf("%d", &q[i]); q[i]--; } return true; } int fen[maxn]; void add(int i, int val) { for (; i < n; i |= i + 1) { fen[i] += val; } } int get(int i) { int res = 0; for (; i >= 0; i = (i & (i + 1)) - 1) { res += fen[i]; } return res; } void addSeg(int l, int r, int val) { l = max(l, 0); r = min(r, n); add(l, val); add(r, -val); } void qsort(vector<int> &p) { int n = sz(p); if (n <= 1) { return; } int x = rnd(n); vector<int> todel; addSeg(p[x] - k, p[x] + k + 1, 1); todel.push_back(p[x]); vector<int> l, r; for (int i = x - 1; i >= 0; i--) { if (p[i] > p[x] && !get(p[i])) { r.push_back(p[i]); } else { l.push_back(p[i]); addSeg(p[i] - k, p[i] + k + 1, 1); todel.push_back(p[i]); } } reverse(l.begin(), l.end()); reverse(r.begin(), r.end()); for (int i = 0; i < sz(todel); i++) { addSeg(todel[i] - k, todel[i] + k + 1, -1); } todel.clear(); addSeg(p[x] - k, p[x] + k + 1, 1); todel.push_back(p[x]); for (int i = x + 1; i < n; i++) { if (p[i] < p[x] && !get(p[i])) { l.push_back(p[i]); } else { r.push_back(p[i]); addSeg(p[i] - k, p[i] + k + 1, 1); todel.push_back(p[i]); } } for (int i = 0; i < sz(todel); i++) { addSeg(todel[i] - k, todel[i] + k + 1, -1); } todel.clear(); qsort(l); qsort(r); int val = p[x]; for (int i = 0; i < sz(l); i++) { p[i] = l[i]; } p[sz(l)] = val; for (int i = 0; i < sz(r); i++) { p[sz(l) + 1 + i] = r[i]; } } void solve() { vector<int> p(n); for (int i = 0; i < n; i++) { p[q[i]] = i; } memset(fen, 0, sizeof(fen)); qsort(p); for (int i = 0; i < n; i++) { q[p[i]] = i; } for (int i = 0; i < n; i++) { printf("%d\n", q[i] + 1); } } int main() { precalc(); #ifdef DEBUG assert(freopen(TASK ".in", "r", stdin)); assert(freopen(TASK ".out", "w", stdout)); #endif while (read()) { solve(); #ifdef DEBUG eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC); #endif } return 0; }
codenet
-1
2,820
s044141824
p04052
1,580,274,529
cpp
Accepted
535
56,704
// Written by newbiechd #include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> // Delete the debugging information! #define debug(x) std::cerr << #x << " = " << x << std::endl; const int BUFFER_SIZE = 1 << 25 | 1; struct InputOutputStream { char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE], *s, *oh; InputOutputStream() : s(ibuf), oh(obuf) { ibuf[fread(ibuf, 1, BUFFER_SIZE, stdin)] = '\0'; } ~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); } template <typename T> inline InputOutputStream &operator>>(T &x) { while (!isdigit(*s)) ++s; for (x = 0; isdigit(*s); ++s) x = x * 10 + (*s ^ '0'); return *this; } template <typename T> inline InputOutputStream &operator<<(T x) { static char buf[23]; register char *top = buf; if (x) { for (T t; x; ) t = x / 10, *top++ = x - t * 10 + 48, x = t; while (top != buf) *oh++ = *--top; } else *oh++ = '0'; *oh++ = '\n'; return *this; } }IO; template <typename lhs, typename rhs> inline lhs min(const lhs &x, const rhs &y) { return x < y ? x : y; } template <typename lhs, typename rhs> inline lhs max(const lhs &x, const rhs &y) { return x > y ? x : y; } const int MAXN = 500003; int n, k, p[MAXN], q[MAXN]; const int MAXT = 2000003; #define mid ((l + r) >> 1) #define lSon (k << 1) #define rSon (k << 1 | 1) struct SegmentTree { int mx[MAXT]; void modify(int k, int l, int r, int x, int v) { mx[k] = max(mx[k], v); if (l == r) return ; x <= mid ? modify(lSon, l, mid, x, v) : modify(rSon, mid + 1, r, x, v); } int query(int k, int l, int r, int x, int y) { if (x <= l && r <= y) return mx[k]; int o = 0; if (x <= mid) o = max(o, query(lSon, l, mid, x, y)); if (mid < y) o = max(o, query(rSon, mid + 1, r, x, y)); return o; } }tree; #undef mid #undef lSon #undef rSon std::vector<int> g[MAXN]; int deg[MAXN]; std::priority_queue<int> que; int main() { IO >> n >> k, --k; // k = k - 1 for (int i = 1; i <= n; ++i) IO >> p[i], q[p[i]] = i; for (int i = 1; i <= n; ++i) { int temp = tree.query(1, 1, n, q[i] - k, q[i]); if (temp) g[q[i]].push_back(q[temp]), ++deg[q[temp]]; temp = tree.query(1, 1, n, q[i], q[i] + k); if (temp) g[q[i]].push_back(q[temp]), ++deg[q[temp]]; tree.modify(1, 1, n, q[i], i); } static int ans[MAXN], cnt = 0; for (int i = 1; i <= n; ++i) if (!deg[i]) que.push(i); while (!que.empty()) { int x = que.top(); que.pop(), ans[++cnt] = x; for (int y : g[x]) if (--deg[y] == 0) que.push(y); } std::reverse(ans + 1, ans + n + 1); for (int i = 1; i <= n; ++i) p[ans[i]] = i; for (int i = 1; i <= n; ++i) IO << p[i]; return 0; }
codenet
-1
2,847
s740920852
p04052
1,570,350,403
cpp
Accepted
1,830
244,736
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define mp make_pair #define ff first #define ss second #define pb push_back #define pf push_front #define popb pop_back #define popf pop_front #define all(v) v.begin(),v.end() const int maxn = 5e5 + 100, maxlog = 27; const ll inf = 2e18, mod = 1e9 + 7; int n, k, p[maxn], q[maxn], seg[4 * maxn], deg[maxlog * maxn], vertcnt, ans[maxn]; pair<int, int> segE[maxlog * maxn]; vector<int> graph[maxn]; priority_queue<int, vector<int>, greater<int> > pq; bitset<maxlog * maxn> vis; inline void handle(int a, int b) { if (segE[a].ff == -1) segE[a].ff = b; else segE[a].ss = b; deg[b]++; } void add(int u, int l, int r, int x) { if (seg[u] != -1) handle(vertcnt, seg[u]); seg[u] = vertcnt++; //cerr << "ADD " << seg[u] << ' ' << l << ' ' << r << endl; if (r - l == 1) { //cerr << "ADD FINISHED TO " << x << endl; handle(seg[u], x); return; } handle(seg[u], vertcnt); int mid = (l + r) / 2; if (x < mid) add(2 * u, l, mid, x); else add(2 * u + 1, mid, r, x); } void connect(int u, int l, int r, int b, int e, int vert) { if (seg[u] == -1) return; if (b <= l && r <= e) { graph[vert].pb(seg[u]); deg[seg[u]]++; //cerr << "CONNECT " << vert << " TO " << seg[u] << ' ' << l << ' ' << r << endl; return; } int mid = (l + r) / 2; if (b < mid) connect(2 * u, l, mid, b, e, vert); if (e > mid) connect(2 * u + 1, mid, r, b, e, vert); } void dfs(int u) { if (vis[u]) return; vis[u] = true; //cerr << "DFS " << u << endl; if (u < n) { for (int i : graph[u]) { deg[i]--; if (!deg[i]) { if (i < n) pq.push(i); else dfs(i); } } } else { int i = segE[u].ff; if (i != -1) { deg[i]--; if (!deg[i]) { if (i < n) pq.push(i); else dfs(i); } } i = segE[u].ss; if (i != -1) { deg[i]--; if (!deg[i]) { if (i < n) pq.push(i); else dfs(i); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); memset(seg, -1, sizeof(seg)); for (int i = 0; i < maxlog * maxn; i++) segE[i] = mp(-1, -1); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; q[p[i]] = i; } /*for (int i = 0; i < n; i++) cerr << '_' << q[i]; cerr << endl;*/ vertcnt = n; for (int i = n - 1; i >= 0; i--) { int now = q[i]; //cerr << "### NOW AT " << now << endl; int L = max(0, now - k + 1), R = min(n, now + k); connect(1, 0, n, L, R, now); add(1, 0, n, now); } for (int i = 0; i < vertcnt; i++) if (!deg[i]) { if (i < n) pq.push(i); else dfs(i); } int now, ind = 0; while (!pq.empty()) { now = pq.top(); //cerr << "TOPOL SORT AT" << ' ' << now << endl; pq.pop(); ans[now] = ++ind; dfs(now); } for (int i = 0; i < n; i++) cout << ans[i] << endl; }
codenet
-1
2,858
s457396118
p04052
1,550,787,335
cpp
Accepted
532
43,776
#include <bits/stdc++.h> #define IL __inline__ __attribute__((always_inline)) #define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++ i) #define FOR(i, a, b) for (int i = a, i##end = b; i < i##end; ++ i) #define Rep(i, a, b) for (int i = a, i##end = b; i >= i##end; -- i) #define REP(i, a, b) for (int i = (a) - 1, i##end = b; i >= i##end; -- i) typedef long long LL; template<class T> IL bool chkmax(T &a, const T &b) { return a < b ? ((a = b), 1) : 0; } template<class T> IL bool chkmin(T &a, const T &b) { return a > b ? ((a = b), 1) : 0; } template<class T> IL T mymax(const T &a, const T &b) { return a > b ? a : b; } template<class T> IL T mymin(const T &a, const T &b) { return a < b ? a : b; } template<class T> IL T myabs(const T &a) { return a > 0 ? a : -a; } const int INF = 0X3F3F3F3F; const double EPS = 1E-10, PI = acos(-1.0); #define DEBUG(...) fprintf(stderr, __VA_ARGS__) #define OK DEBUG("Passing [%s] in LINE %d...\n", __FUNCTION__, __LINE__) /*------------------------------header------------------------------*/ const int MAXN = 500000 + 5; struct SegmentTree { int max[MAXN * 4]; #define lc (o << 1) #define rc (o << 1 | 1) void push_up(int o) { max[o] = mymax(max[lc], max[rc]); } void modify(int o, int l, int r, int p, int x) { if (l == r) { max[o] = x; return; } int mid = (l + r) >> 1; if (p <= mid) { modify(lc, l, mid, p, x); } else { modify(rc, mid + 1, r, p, x); } push_up(o); } int query(int o, int l, int r, int a, int b) { if (a > b) { return 0; } if (l >= a && r <= b) { return max[o]; } int mid = (l + r) >> 1, answer = 0; if (a <= mid) { chkmax(answer, query(lc, l, mid, a, b)); } if (b > mid) { chkmax(answer, query(rc, mid + 1, r, a, b)); } return answer; } } seg; int p[MAXN], a[MAXN], deg[MAXN], answer[MAXN]; std::vector<int> edge[MAXN]; int main() { int n, k; scanf("%d%d", &n, &k); For(i, 1, n) { scanf("%d", &p[i]); a[p[i]] = i; } if (k == 1) { For(i, 1, n) { printf("%d\n", i); } puts(""); exit(0); } For(i, 1, n) { int x = a[i] == n ? 0 : seg.query(1, 1, n, a[i], mymin(a[i] + k - 1, n)), y = a[i] == 1 ? 0 : seg.query(1, 1, n, mymax(a[i] - k + 1, 1), a[i]); if (x) { edge[i].push_back(x); ++ deg[x]; } if (y) { edge[i].push_back(y); ++ deg[y]; } seg.modify(1, 1, n, a[i], i); } std::priority_queue<int> q; For(i, 1, n) { if (!deg[i]) { q.push(a[i]); } } Rep(i, n, 1) { int u = q.top(); q.pop(); answer[i] = u; for (auto &x : edge[p[u]]) { if (!-- deg[x]) { q.push(a[x]); } } } For(i, 1, n) { a[answer[i]] = i; } For(i, 1, n) { printf("%d\n", a[i]); } return 0; }
codenet
-1
2,864
s606418733
p04052
1,509,459,623
cpp
Accepted
937
39,296
#include <iostream> #include <sstream> #include <iomanip> #include <cstdio> #include <cstdlib> #include <cmath> #include <cctype> #include <cstring> #include <vector> #include <list> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #include <algorithm> #include <iterator> #include <bitset> #include <ctime> #include <fstream> #include <complex> using namespace std; #define FOR(i,a,b) for(int i = (a); i <(b); i++) #define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--) #define ITER(it,a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++) #define FILL(a, value) memset(a,value, sizeof(a)) #define ALL(a) a.begin(),a.end() #define SZ(a) (int)a.size() #define PB push_back #define MP make_pair typedef long long LL; typedef vector<LL> VI; typedef pair<int, LL> PII; const double PI = acos(-1.0); const int INF = 1000 * 1000 * 1000 + 7; const LL LINF = INF * (LL)INF; const int MOD = 1000 * 1000 * 1000 + 7; const int MAX = 1000 * 500 + 7; struct RMQ { int T[MAX << 2]; void upd(int v, int tl, int tr, int p, int x) { if (tl == tr) { T[v] = x; return ; } int tm = (tl + tr)/2; if(p <= tm)upd(v + v, tl, tm, p, x); else upd(v + v + 1, tm+1, tr, p, x); T[v] = max(T[v+v], T[v+v+1]); } int get(int v, int tl, int tr, int l, int r) { if (r < l)return -INF; if (l == tl && r == tr)return T[v]; int tm = tl + tr >> 1; return max(get(v + v, tl, tm, l, min(r, tm)), get(v + v + 1, tm + 1, tr, max(tm+1, l), r)); } } T; int P[MAX], Q[MAX], R[MAX], C[MAX]; int k, n; set<int> S; void updBlock(int block) { if (C[block] != -1) { S.erase(-C[block]); C[block] = -1; } int L = block * k; int R = min(L + k - 1, n-1); int x = T.get(1, 0, n-1, L, R); // cout << "-" << x << endl; if (x == -INF)return ; if (T.get(1, 0,n-1, max(0, Q[x] - k + 1), min(n-1, Q[x] + k + -1)) == x) S.insert(-Q[x]), C[block] = Q[x]; } int main() { //freopen("input.txt", "r", stdin); //ios::sync_with_stdio(false);cin.tie(NULL); scanf("%d %d", &n, &k); FOR(i, 0, n) { scanf("%d", P + i); P[i]--; Q[P[i]] = i; T.upd(1, 0, n-1, i, P[i]); } int cnt = (n + k - 1) / k; FILL(C, -1); FOR(i, 0, cnt) updBlock(i); // cout << "*" << endl; RFOR(i, n, 0) { // cout << i << " " << SZ(S) << endl; int x = -*S.begin(); S.erase(S.begin()); R[x] = i; T.upd(1, 0, n-1, x, -INF); updBlock(x / k); if (x / k - 1 >= 0)updBlock(x / k - 1); if (x / k + 1 < cnt)updBlock(x / k + 1); } FOR(i, 0, n) printf("%d ", R[i]+1); return 0; }
codenet
-1
2,866
s376326706
p04052
1,537,558,041
cpp
Accepted
582
32,632
//2018.9.21 by ljz #include<bits/stdc++.h> using namespace std; #define res register int #define LL long long #define inf 0x3f3f3f3f #define eps 1e-15 inline int read(){ res s=0; bool w=0; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=1;ch=getchar();} while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar(); return w?-s:s; } inline void _swap(res &x,res &y){ x^=y^=x^=y; } inline int _abs(const res &x){ return x>0?x:-x; } inline int _max(const res &x,const res &y){ return x>y?x:y; } inline int _min(const res &x,const res &y){ return x<y?x:y; } const int N=1e6+10; namespace MAIN{ int a[N],n,k; namespace Segtree{ int mn[N<<2]; inline void pushup(const res &rt){ mn[rt]=_min(mn[rt<<1],mn[rt<<1|1]); } void build(const res &rt,const res &l,const res &r){ mn[rt]=inf; if(l==r)return; res mid=(l+r)>>1; build(rt<<1,l,mid),build(rt<<1|1,mid+1,r); } void update(const res &rt,const res &l,const res &r,const res &p,const res &va){ if(l==r){mn[rt]=va;return;} res mid=(l+r)>>1; if(p<=mid)update(rt<<1,l,mid,p,va); else update(rt<<1|1,mid+1,r,p,va); pushup(rt); } int query(const res &rt,const res &l,const res &r,const res &L,const res &R){ if(L>R)return 0; if(L<=l&&r<=R)return mn[rt]; res mid=(l+r)>>1,ans=inf; if(L<=mid)ans=_min(ans,query(rt<<1,l,mid,L,R)); if(R>mid)ans=_min(ans,query(rt<<1|1,mid+1,r,L,R)); return ans; } } struct E{ int next,to; E() {} E(res next,res to):next(next),to(to) {} }edge[N]; int head[N],cnt,du[N]; inline void addedge(const res &u,const res &v){ edge[++cnt]=E(head[u],v),head[u]=cnt,du[v]++; } int x; priority_queue<int,vector<int>,greater<int> >Q; int id[N],idx; inline void topsort(){ for(res i=1;i<=n;i++)if(!du[i])Q.push(i); while(!Q.empty()){ res u=Q.top(); Q.pop(); id[u]=++idx; for(res i=head[u];~i;i=edge[i].next){ res tox=edge[i].to; if(--du[tox]==0)Q.push(tox); } } } inline void MAIN(){ memset(head,-1,sizeof(head)); n=read(),k=read(); for(res i=1;i<=n;i++)a[read()]=i; Segtree::build(1,1,n); for(res i=n;i;i--){ x=Segtree::query(1,1,n,a[i]+1,_min(a[i]+k-1,n)); if(x>=1&&x<=n)addedge(a[i],a[x]); x=Segtree::query(1,1,n,_max(1,a[i]-k+1),a[i]-1); if(x>=1&&x<=n)addedge(a[i],a[x]); Segtree::update(1,1,n,a[i],i); } topsort(); for(res i=1;i<=n;i++)printf("%d\n",id[i]); } } int main(){ MAIN::MAIN(); return 0; }
codenet
-1
2,894
s932551583
p04052
1,543,161,849
cpp
Accepted
737
31,072
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <queue> const int N=5e5+5; int n,q,pos[N],ans[N],to_s[N],to_b[N]; inline int nxi(){ int x=0; char c; while((c=getchar())>'9'||c<'0'); while(x=x*10-48+c,(c=getchar())>='0'&&c<='9'); return x; } template <class T> inline void apn(T &x,const T y){ if(x>y) x=y; } template <class T> inline void apx(T &x,const T y){ if(x<y) x=y; } bool cmp_pos(const int a,const int b){ return pos[a]<pos[b]; } struct cmp_pos_greater{ bool operator () (const int a,const int b){ return pos[a]>pos[b]; } }; namespace G{ int cnt,fir[N],in[N],buk[N]; struct edge{ int to,nx; }eg[N<<1]; inline void add(const int a,const int b){ eg[++cnt]=(edge){b,fir[a]}; fir[a]=cnt; ++in[b]; } inline void init(){ for(int i=1;i<=n;++i){ if(to_s[i]<=n) add(i,to_s[i]); if(to_b[i]<=n) add(i,to_b[i]); } } inline void topo(){ std::priority_queue <int,std::vector <int>,cmp_pos_greater> pq; int top=0; for(int i=1;i<=n;++i){ if(!in[i]) pq.push(i); } while(!pq.empty()){ const int x=pq.top(); pq.pop(); buk[++top]=x; for(int i=fir[x];i;i=eg[i].nx){ const int y=eg[i].to; if(!--in[y]) pq.push(y); } } } } namespace T{ int x,y,v,tr[N<<1],buk[N]; inline int idx(const int l,const int r){ return (l+r)|(l!=r); } inline void upd(const int l,const int r){ const int mid=(l+r)>>1; tr[idx(l,r)]=tr[idx(l,mid)]+tr[idx(mid+1,r)]; } void mod_t(const int l,const int r){ if(l==r){ tr[idx(l,r)]+=v; return; } const int mid=(l+r)>>1; if(x<=mid) mod_t(l,mid); else mod_t(mid+1,r); upd(l,r); } int ask_l_t(const int l,const int r){ if(!tr[idx(l,r)]) return n+1; if(l==r) return l; int tp,mid=(l+r)>>1; if(x<=mid&&(tp=ask_l_t(l,mid))<=n) return tp; return ask_l_t(mid+1,r); } inline void mod(const int x,const int v){ T::x=x,T::v=v; mod_t(1,n); } inline int ask_l(const int x,const int y){ if(x>y) return n+1; T::x=x,T::y=y; return ask_l_t(1,n); } inline void init(){ for(int i=1;i<=n;++i){ buk[pos[i]]=i; } std::sort(buk+1,buk+n+1,cmp_pos); } inline void solve1(){ for(int i=1;i<=n;++i){ if(i-q>0) mod(buk[i-q],-1); apn(to_s[buk[i]],ask_l(buk[i]+1,n)); mod(buk[i],1); } } inline void solve2(){ for(int i=n;i;--i){ if(i+q<=n) mod(buk[i+q],-1); apn(to_b[buk[i]],ask_l(buk[i]+1,n)); mod(buk[i],1); } } } int main(){ #ifndef ONLINE_JUDGE // freopen("f.in","r",stdin); #endif memset(to_s,1,sizeof(to_s)); memset(to_b,1,sizeof(to_b)); n=nxi(),q=nxi(); for(int i=1;i<=n;++i){ pos[nxi()]=i; } T::init(); T::solve1(); memset(T::tr,0,sizeof(T::tr)); T::solve2(); // for(int i=1;i<=n;++i){ // printf("%d %d\n",to_l[i],to_r[i]); // } G::init(); G::topo(); for(int i=1;i<=n;++i){ ans[pos[G::buk[i]]]=i; } for(int i=1;i<=n;++i){ printf("%d\n",ans[i]); } return 0; }
codenet
-1
2,914
s954829217
p04052
1,489,464,118
cpp
Accepted
741
57,728
#include <set> #include <map> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <vector> #include <bitset> #include <string> #include <cstdio> #include <cassert> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #define mk make_pair #define pb push_back #define fi first #define se second #define REP(i, x, y) for(int i = (int)x; i <= (int)y; i ++) #define FOR(i, x, y) for(int i = (int)x; i < (int)y; i ++) #define PER(i, x, y) for(int i = (int)x; i >= (int)y; i --) #define trace(x) cerr << #x << " " << x << endl; #define dprintf(...) fprintf(stderr, __VA__ARGS__) #define dln() fprintf(stderr, "\n") using namespace std; typedef long long LL; typedef long double db; typedef pair<int,int> PII; typedef vector<int> VI; typedef vector<PII> VPI; const int N = 500005; const int P = 1e9 + 7; const int inf = 1e9; const LL Inf = 1e15; inline int IN(){ char ch = getchar(); int x = 0, f = 0; while(ch < '0' || ch > '9') ch = getchar(), f = (ch == '-'); while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + ch - 48; ch = getchar(); } return f ? (-x) : x; } inline int Pow(int x, int y, int p){ int an = 1; for(; y; y >>= 1, x = (LL)x * x % p) if(y & 1) an = (LL)an * x % p; return an; } void renew(int &x, int y){ x += y; if(x < 0) x += P; else if(x >= P) x -= P; } template<typename T> inline void chkmin(T &a, const T &b) {if(a > b) a = b;} template<typename T> inline void chkmax(T &a, const T &b) {if(a < b) a = b;} int deg[N]; int n, k, a[N], b[N]; VI e[N]; struct seg{ int sum[N << 2]; int ql, qr, qx, qv; #define ls ((x) << 1) #define rs ((ls) | 1) #define md (((L) + (R)) >> 1) void M(int x, int L, int R){ if(L == R){ sum[x] = 1; return; } if(qx <= md) M(ls, L, md); else M(rs, md + 1, R); sum[x] = sum[ls] + sum[rs]; } void D(int x, int L, int R){ if(!sum[x]) return; if(L == R){ e[L].pb(qv); deg[qv] ++; sum[x] = 0; return; } if(ql <= L && R <= qr){ D(ls, L, md); D(rs, md + 1, R); sum[x] = sum[ls] + sum[rs]; return; } if(ql <= md) D(ls, L, md); if(md < qr) D(rs, md + 1, R); sum[x] = sum[ls] + sum[rs]; } void Modify(int x){ qx = x; M(1, 1, n); } void Delete(int L, int R, int v){ if(L > R) return; ql = L; qr = R; qv = v; D(1, 1, n); } }L, R; set<int> fres; int lab[N], tim; int main(){ scanf("%d%d", &n, &k); REP(i, 1, n) scanf("%d", a + i); REP(i, 1, n) b[a[i]] = i; REP(i, 1, n){ int x = b[i]; L.Delete(max(1, x - k + 1), x - 1, x); R.Delete(x + 1, min(n, x + k - 1), x); L.Modify(x); R.Modify(x); } REP(i, 1, n) if(!deg[i]) fres.insert(i); while(!fres.empty()){ int x = *fres.begin(); lab[x] = ++ tim; fres.erase(x); for(int v : e[x]){ -- deg[v]; if(!deg[v]) fres.insert(v); } } REP(i, 1, n) printf("%d\n", lab[i]); return 0; }
codenet
-1
2,919
s440583616
p04052
1,509,274,950
cpp
Accepted
961
19,560
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<vector> #include<string> #include<iostream> #include<algorithm> #include<map> #include<iterator> #include<set> #include<stack> #include<queue> #include<fstream> #include<iomanip> #include <unordered_map> #include <unordered_set> #include <numeric> #include<cmath> #include<list> #include <sstream> #include<unordered_map> #include<cstring> using namespace std; #define FOR(i,a,b) for(int i = (a); i < (b); i++) #define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--) #define ITER(it,a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++) #define FILL(a,value) memset(a, value, sizeof(a)) #define ALL(a) a.begin(),a.end() #define SZ(a) (int)a.size() #define PB push_back #define MP make_pair typedef long long LL; typedef vector<int> VI; typedef pair<int, int> PII; const double PI = acos(-1.0); const int INF = 1000 * 1000 * 1000 + 7; const LL LINF = INF * (LL)INF; const int MAX = 5 * 1000 * 100 + 47; int P[MAX]; int ANS[MAX]; int M[MAX]; priority_queue<int> q; struct RMQ { int T[MAX * 4]; int n; int Max(int a, int b) { return P[a] > P[b] ? a : b; } void build(int v, int tl, int tr) { if (tl == tr) { T[v] = tl; return; } int tm = (tl + tr) / 2; build(2 * v, tl, tm); build(2 * v + 1, tm + 1, tr); T[v] = Max(T[2 * v], T[2 * v + 1]); } void init(int n) { this->n = n; build(1, 0, n - 1); } int get1(int v, int tl, int tr, int l, int r) { if (l > r) return n - 1; if (tl == l && tr == r) return T[v]; int tm = (tl + tr) / 2; int v1 = get1(2 * v, tl, tm, l, min(r, tm)); int v2 = get1(2 * v + 1, tm + 1, tr, max(l, tm + 1), r); return Max(v1, v2); } int get(int l, int r) { return get1(1, 0, n - 1, l, r); } void update(int v, int tl, int tr, int ind, int val) { if (tl == tr) { T[v] = tl; P[tl] = val; return; } int tm = (tl + tr) / 2; if (tm >= ind) { update(2 * v, tl, tm, ind, val); } else { update(2 * v + 1, tm + 1, tr, ind, val); } T[v] = Max(T[2 * v + 1], T[2 * v]); } void update(int ind, int val) { update(1, 0, n - 1, ind, val); } } R; int n, k; void upd(int block) { int l = block * k; int r = min((block + 1) * k - 1, n - 1); int ind = R.get(l, r); if (ind == M[block] || ind == n) return; int v = R.get(max(ind - k + 1, 0), min(ind + k - 1, n - 1)); if (v == ind) { q.push(ind); M[block] = ind; } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; FOR(i, 0, n) { cin >> P[i]; P[i]--; } P[n] = -INF; R.init(n + 1); int sz = (n + k - 1) / k; FILL(M, -1); FOR(i, 0, sz) { upd(i); } int ind = n - 1; while (!q.empty()) { int pos = q.top(); q.pop(); ANS[pos] = ind; R.update(pos, -INF); int block = pos / k; upd(block); if (block) upd(block - 1); if (block + 1 != sz) upd(block + 1); ind--; } FOR(i, 0, n) cout << ANS[i] + 1 << "\n"; system("pause"); }
codenet
-1
3,000
s931187630
p04052
1,570,726,502
cpp
Accepted
442
29,216
#include <cstdio> #include <iostream> #include <queue> #include <vector> #define root 1, n, 1 #define ls l, mid, k << 1 #define rs mid + 1, r, k << 1 | 1 using namespace std; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() \ (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), \ (iS == iT ? EOF : *iS++)) \ : *iS++) // print the remaining part inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } // input a signed integer template <class I> inline void read(I &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; } // print a signed integer template <class I> inline void print(I x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); } // no need to call flush at the end manually! struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io::print; using io::putc; using io::read; int n, k; int p[500005]; struct Seg { int tmn[2000006]; void build(int l, int r, int k) { tmn[k] = n + 1; if (l == r) return; int mid = l + r >> 1; build(ls), build(rs); } int query(int l, int r, int k, int st, int en) { if (st > r || en < l) return n + 1; if (st <= l && en >= r) return tmn[k]; int mid = l + r >> 1; return min(query(ls, st, en), query(rs, st, en)); } void ins(int l, int r, int k, int pos, int v) { tmn[k] = v; if (l == r) return; int mid = l + r >> 1; (pos <= mid) ? ins(ls, pos, v) : ins(rs, pos, v); } } seg; int hd[500005], nx[1000006], to[1000006], cnt; int deg[500005]; void add(int f, int t) { nx[++cnt] = hd[f], hd[f] = cnt, to[cnt] = t; } priority_queue<int, vector<int>, greater<int> > q; int res[500005], rc; void work() { for (int i = 1; i <= n; ++i) !deg[i] ? q.push(i) : void(); while (!q.empty()) { int u = q.top(); q.pop(), res[u] = ++rc; for (int i = hd[u]; i; i = nx[i]) !(--deg[to[i]]) ? q.push(to[i]) : void(); } return; } int main() { read(n), read(k); for (int i = 1, x; i <= n; ++i) read(x), p[x] = i; seg.build(root); for (int i = n, j; i >= 1; --i) { j = seg.query(root, p[i] + 1, p[i] + k - 1); (j > n) ? void() : (deg[p[j]]++, add(p[i], p[j])); j = seg.query(root, p[i] - k + 1, p[i] - 1); (j > n) ? void() : (deg[p[j]]++, add(p[i], p[j])); seg.ins(root, p[i], i); } work(); for (int i = 1; i <= n; ++i) print(res[i]), putc('\n'); return 0; }
codenet
-1
3,044
s285229352
p04052
1,493,241,033
cpp
Accepted
725
17,408
#include <cstdio> #include <algorithm> #define MAXN 500009 #define MAXP 1050000 int aint[MAXP]; int heap[MAXN], osal[MAXN], sef[MAXN]; int v[MAXN], ans[MAXN]; int poz, left, right, val; int n, m, k; void dfs(int p, int st, int dr){ if(st==dr) aint[p]=st; else{ int m=(st+dr)/2; dfs(2*p, st, m); dfs(2*p+1, m+1, dr); if(v[aint[2*p]]>v[aint[2*p+1]]) aint[p]=aint[2*p]; else aint[p]=aint[2*p+1]; } } void update(int p, int st, int dr){ if(st==dr) aint[p]=0; else{ int m=(st+dr)/2; if(poz<=m) update(2*p, st, m); else update(2*p+1, m+1, dr); if(v[aint[2*p]]>v[aint[2*p+1]]) aint[p]=aint[2*p]; else aint[p]=aint[2*p+1]; } } void query(int p, int st, int dr){ if((left<=st)&&(dr<=right)){ if(v[aint[p]]>v[val]) val=aint[p]; }else{ int m=(st+dr)/2; if(left<=m) query(2*p, st, m); if(m<right) query(2*p+1, m+1, dr); } } inline int tata(int p){ return p/2; } inline int fiust(int p){ return 2*p; } inline int fiudr(int p){ return 2*p+1; } inline void mySwap(int p, int q){ int aux=heap[p]; heap[p]=heap[q]; heap[q]=aux; sef[heap[p]]=p; sef[heap[q]]=q; } inline void urcare(int p){ if(p>heap[0]) return ; while((p>1)&&(heap[p]>heap[tata(p)])){ mySwap(p, tata(p)); p=tata(p); } } inline void coborare(int p){ int q; bool f=1; while((f)&&(fiust(p)<=heap[0])){ q=fiust(p); if((fiudr(p)<=heap[0])&&(heap[fiudr(p)]>heap[q])) q=fiudr(p); if(heap[q]>heap[p]){ mySwap(p, q); p=q; }else f=0; } } inline void scoate(int x){ if(sef[x]!=0){ int a=heap[heap[0]]; heap[sef[x]]=heap[heap[0]]; sef[heap[sef[x]]]=sef[x]; heap[0]--; sef[x]=0; urcare(sef[a]); coborare(sef[a]); } } inline void baga(int x){ sef[x]=heap[0]+1; heap[++heap[0]]=x; urcare(heap[0]); } inline void calc(int u){ if(osal[u]!=0){ left=osal[u]-k+1; right=osal[u]+k-1; val=0; query(1, 1, n); if(val==osal[u]) baga(osal[u]); } } int main(){ scanf("%d%d", &n, &k); for(int i=1; i<=n; i++) scanf("%d", &v[i]); dfs(1, 1, n); m=(n+k-1)/k; for(int i=1; i<=m; i++){ osal[i]=(i-1)*k+1; for(int j=(i-1)*k+2; j<=i*k; j++) if(v[j]>v[osal[i]]) osal[i]=j; } for(int i=m; i>0; i--) calc(i); for(int i=n; i>0; i--){ int x=heap[1]; scoate(x); ans[x]=i; poz=x; update(1, 1, n); int u=(x-1)/k+1; left=(u-1)*k+1; right=u*k; scoate(osal[u-1]); scoate(osal[u+1]); val=0; query(1, 1, n); osal[u]=val; calc(u); calc(u-1); calc(u+1); } for(int i=1; i<=n; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
3,050
s341336743
p04052
1,600,506,901
cpp
Accepted
731
45,860
#include <bits/stdc++.h> using namespace std; #define Gene template< class #define Rics printer& operator, Gene c> struct rge{c b, e;}; Gene c> rge<c> range(c i, c j){ return {i, j};} struct printer{ ~printer(){cerr<<endl;} Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;} Rics(string x){cerr<<x;return *this;} Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";} Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));} Gene c >Rics(rge<c> x){ *this,"["; for(auto it = x.b; it != x.e; ++it) *this,(it==x.b?"":", "),*it; return *this,"]";} }; #define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer() #define dbg(x) "[",#x,": ",(x),"] " mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int my_rand(int l, int r) { return uniform_int_distribution<int>(l, r) (rng); } const int N = 5e5+100; const int inf = 1e9; int tr[4*N]; void update(int cn, int b, int e, int idx, int x) { if(b == e) { tr[cn] = x; return; } int mid = (b+e)/2, lc = 2*cn, rc = 2*cn+1; if(idx <= mid) update(lc, b, mid, idx, x); else update(rc, mid+1, e, idx, x); tr[cn] = min(tr[lc], tr[rc]); } int query(int cn, int b, int e, int i, int j) { if(b > j or e < i or b > e) return inf; if(b >= i && e <= j) return tr[cn]; int mid = (b+e)/2, lc = 2*cn, rc = 2*cn+1; return min(query(lc, b, mid, i, j), query(rc, mid+1, e, i, j)); } int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(0); cin.tie(0); int n, K; cin >> n >> K; vector<int> v(n), pos(n); for(int i = 0; i < n; i++) { cin >> v[i]; v[i]--; pos[v[i]] = i; } for(int i = 0; i < n; i++) { update(1, 0, n-1, i, inf); } vector<int> right_bad(n); priority_queue<int> candidate; vector<vector<int>> edges(n); for(int i = n-1; i >= 0; i--) { int l = pos[i], r = min(n-1, pos[i]+K-1); int min_idx = query(1, 0, n-1, l, r); if(min_idx != inf) { right_bad[i]++; edges[min_idx].push_back(i); } l = max(0, pos[i]-(K-1)), r = pos[i]; min_idx = query(1, 0, n-1, l, r); if(min_idx != inf) { right_bad[i]++; edges[min_idx].push_back(i); } if(right_bad[i] == 0) { candidate.push(pos[i]); } update(1, 0, n-1, pos[i], i); } // debug(), dbg(edges), dbg(right_bad); vector<int> res_pos; while(candidate.size() > 0) { int y = candidate.top(); int x = v[candidate.top()]; // debug(), dbg(x), dbg(y); candidate.pop(); res_pos.push_back(pos[x]); for(auto i : edges[x]) { right_bad[i]--; if(right_bad[i] == 0) candidate.push(pos[i]); } } reverse(res_pos.begin(), res_pos.end()); vector<int> res(n); for(int i = 0; i < n; i++) { res[res_pos[i]] = i; } for(int x : res) cout << x+1 << " "; cout << endl; }
codenet
-1
3,083
s773476490
p04052
1,523,546,802
cpp
Accepted
1,704
203,248
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define all(v) (v).begin(),(v).end() #define fi first #define se second typedef vector<int>vint; typedef pair<int,int>pint; typedef vector<pint>vpint; template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} const int INF=1001001001001001001ll; struct segtree{ static const int SEG=1<<19; vint dat; segtree():dat(SEG*2,INF){} void update(int k,int x){ k+=SEG-1; dat[k]=x; while(k){ k=(k-1)/2; dat[k]=min(dat[k*2+1],dat[k*2+2]); } } int query(int a,int b,int k=0,int l=0,int r=SEG){ if(r<=a||b<=l)return INF; if(a<=l&&r<=b)return dat[k]; return min(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r)); } }; int N,K; int S[555555]; vint G[555555]; vint X[555555]; int par[20][555555]; int dep[555555]; vint latte; void dfs(int v){ sort(all(X[v])); for(auto u:X[v]){ dfs(u); } latte.pb(v); } signed main(){ scanf("%lld%lld",&N,&K); rep(i,N){ int p;scanf("%lld",&p);p--;S[p]=i+1; } segtree seg; for(int i=N-1;i>=0;i--){ int tmp=seg.query(S[i]+1,S[i]+K); if(tmp<N)G[S[i]].pb(S[tmp]); tmp=seg.query(S[i]-K+1,S[i]); if(tmp<N)G[S[i]].pb(S[tmp]); seg.update(S[i],i); } for(int i=1;i<=N;i++)G[i].pb(0); vint ord; vint deg(N+1); rep(i,N+1)for(auto u:G[i])deg[u]++; rep(i,N+1)if(deg[i]==0)ord.pb(i); rep(i,ord.size()){ int v=ord[i]; for(auto u:G[v]){ if(--deg[u]==0)ord.pb(u); } } rep(i,20)par[i][0]=-1; reverse(all(ord)); for(auto v:ord){ if(v==0)continue; vint lis; for(auto u:G[v]){ if(u==0)continue; if(u<v){ lis.pb(u); continue; } for(int i=19;i>=0;i--){ if(par[i][u]>v)u=par[i][u]; } lis.pb(par[0][u]); } if(lis.size()==0){ dep[v]=dep[0]+1; par[0][v]=0; } else if(lis.size()==1){ dep[v]=dep[lis[0]]+1; par[0][v]=lis[0]; } else{ if(dep[lis[0]]<dep[lis[1]])swap(lis[0],lis[1]); int a=lis[0]; int b=lis[1]; rep(i,20)if((dep[a]-dep[b])>>i&1)a=par[i][a]; for(int i=19;i>=0;i--)if(par[i][a]!=par[i][b])a=par[i][a],b=par[i][b]; if(a<=b){ par[0][v]=lis[0]; } else{ par[0][v]=lis[1]; } dep[v]=dep[par[0][v]]+1; } for(int i=0;i<19;i++){ if(par[i][v]==-1)par[i+1][v]=-1; else par[i+1][v]=par[i][par[i][v]]; } } for(int i=1;i<=N;i++)X[par[0][i]].pb(i); dfs(0); vint ans(N); rep(i,N){ ans[latte[i]-1]=i; } rep(i,N)printf("%lld\n",ans[i]+1); return 0; }
codenet
-1
3,125
s586590522
p04052
1,468,730,601
cpp
Accepted
2,434
87,672
#include <iostream> #include <cstdio> #include <cassert> #include <cstring> #include <vector> #include <valarray> #include <array> #include <queue> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <algorithm> #include <cmath> #include <complex> #include <random> using namespace std; using ll = long long; using ull = unsigned long long; constexpr int TEN(int n) {return (n==0)?1:10*TEN(n-1);} struct Node { using NP = Node*; int ma = -1; int get(int a, int b) { if (b <= 0 or sz <= a) return -1; if (a <= 0 and sz <= b) { return ma; } return max(l->get(a, b), r->get(a-sz/2, b-sz/2)); } void set(int k, int x) { if (sz == 1) { ma = x; return; } if (k < sz/2) { l->set(k, x); } else { r->set(k-sz/2, x); } ma = max(l->ma, r->ma); } NP l, r; int sz; Node(int sz) : sz(sz) { if (sz == 1) return; l = new Node(sz/2); r = new Node(sz - sz/2); } }; int main() { int n, k; scanf("%d %d", &n, &k); int p[n], q[n]; for (int i = 0; i < n; i++) { scanf("%d", p+i); p[i]--; } for (int i = 0; i < n; i++) { q[p[i]] = i; } /* for (int i = 0; i < n; i++) { printf("%d, ", q[i]); } printf("\n");*/ Node* rmq = new Node(n); int mi[n]; vector<vector<int>> g(n); for (int i = 0; i < n; i++) { { int v = rmq->get(max(0, q[i]-k+1), q[i]); if (v != -1) { int qv = q[v]; // printf("ed %d %d\n", q[i], qv); g[q[i]].push_back(qv); } } { int v = rmq->get(q[i], min(n, q[i]+k)); if (v != -1) { int qv = q[v]; // printf("ed %d %d\n", q[i], qv); g[q[i]].push_back(qv); } rmq->set(q[i], i); } rmq->set(q[i], i); } /* Node* rrmq = new Node(n); for (int i = n-1; i >= 0; i--) { int v = n-1-rrmq->get(max(0, q[i]-k+1), min(n, q[i]+k)); if (v != n) { int qv = q[v]; printf("ed %d %d\n", qv, q[i]); g[qv].push_back(q[i]); } rrmq->set(q[i], n-1-i); }*/ int deg[n]; fill_n(deg, n, 0); for (int i = 0; i < n; i++) { for (int j: g[i]) { deg[j]++; } } priority_queue<int> que; for (int i = 0; i < n; i++) { if (deg[i] == 0) { que.push(i); } } vector<int> v; while (que.size()) { int p = que.top(); que.pop(); v.push_back(p); for (int d: g[p]) { deg[d]--; if (deg[d] == 0) { que.push(d); } } } reverse(v.begin(), v.end()); int ans[n]; for (int i = 0; i < n; i++) { // printf("%d, ", v[i]); ans[v[i]] = i+1; } // printf("\n"); for (int i = 0; i < n; i++) { printf("%d\n", ans[i]); } return 0; }
codenet
-1
3,125
s986366011
p04052
1,589,109,426
cpp
Accepted
743
19,968
///A drunk man will find his way home, but a drunk bird may get lost forever... #include <bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; //template <typename T> //using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; typedef tree<double, null_type, less_equal<double>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //#define int long long typedef long long ll; typedef double dd; typedef long double ldd; typedef pair <int, int> pii; typedef pair <pii , int> ppii; #define pb push_back #define pf push_front #define ppb pop_back() #define ppf pop_front() #define f first #define s second #define sz(x) (int)x.size() #define smin(x , y) x = min(x , y) #define smax(x , y) x = max(x , y) #define mp make_pair #define all(x) x.begin() , x.end() #define debug(x) cerr<< #x << " = " << x << endl #define ddebug(x, y) cerr<< #x << " = " << x << ", " << #y << " = " << y << endl #define uop(x, y) pii(min(x, y), max(x, y)) #define mem(a, b) memset(a, b, sizeof a) #define sq(x) (x) * (x) #define out return cout << -1 << endl, 0 const int mx = 5e5 + 10; int n, seg[4 * mx], lazy[4 * mx], fen[mx], ind[mx], ans[mx]; void shift(int v, int lc, int rc){ seg[lc] += lazy[v]; seg[rc] += lazy[v]; lazy[lc] += lazy[v]; lazy[rc] += lazy[v]; lazy[v] = 0; return; } void add(int l , int r , int val, int b = 0, int e = n, int ind = 1){ // cout << "df" << endl; if(r <= b || e <= l){ // cout << "out1" << endl; return; } if(l <= b && e <= r){ // cout << "out2" << endl; seg[ind] += val; lazy[ind] += val; return; } shift(ind, 2 * ind, 2 * ind + 1); int mid = (e + b) / 2; add(l, r, val, b, mid, 2 * ind); add(l, r, val, mid, e, 2 * ind + 1); seg[ind] = min(seg[2 * ind] , seg[2 * ind + 1]); // cout << ind << " " << seg[ind] << endl; return; } int minimum(int l = 0, int r = n, int ind = 1){ // cout << l << " " << r << " " << ind << " " << seg[ind] << " " << seg[2 * ind + 1] << " " << lazy[ind] << endl; if(r - l == 1) return l; shift(ind , 2 * ind, 2 * ind + 1); int mid = (l + r) / 2; if(!seg[2 * ind + 1]){ // cout << "ah" << endl; return minimum(mid, r, 2 * ind + 1); } return minimum(l, mid, 2 * ind); } void add(int p){ for(p++; p <= n; p += (p & -p)) fen[p]++; return; } int get(int p){ int ret = 0; for( ; p; p -= (p & -p)) ret += fen[p]; return ret; } int get(int l, int r){ return get(r) - get(l); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k, a; cin >> n >> k; for(int i = 0; i < n; i++){ cin >> a; ind[a - 1] = i; } for(int i = n - 1; ~i; i--){ // cout << "gi" << " " << ind[i] << " " << ind[i] + 1 <<" " << get(max(0 , ind[i] - k + 1) , min(n , ind[i] + k)) << endl; add(ind[i] , ind[i] + 1, get(max(0 , ind[i] - k + 1) , min(n , ind[i] + k))); add(ind[i]); } // cout << endl; for(int i = 0; i < n; i++){ int mini = minimum(); // debug(mini); add(mini , mini + 1, INT_MAX); add(max(0 , mini - k + 1) , min(n , mini + k) , -1); ans[mini] = n - i; // for(int i = 1; i <= 7; i++) // cout << "h " << seg[i] << " " << lazy[i] << endl; } for(int i = 0; i < n; i++) cout << ans[i] << '\n'; return 0; }
codenet
-1
3,262
s058336009
p04052
1,567,208,807
cpp
Accepted
427
28,920
#include <bits/stdc++.h> // Defination {{{ #define PI pair<int, int> #define mk make_pair #define reg register #define ll long long #define rep(i, a, b) for(reg int i = a; i <= b; ++i) #define per(i, a, b) for(reg int i = a; i >= b; --i) #define pb push_back // }}} using namespace std; int min(reg int a, reg int b) { return a < b ? a : b; } int max(reg int a, reg int b) { return a > b ? a : b; } template<typename T> void read(T &x) { x = 0; reg char ch = getchar(); reg int f = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); x *= f; } #define N 500005 int n, k, cnt; int a[N], b[N], l[N], r[N]; class Segment_Tree { public: int tree[N << 2]; void pushup(int x) { tree[x] = min(tree[x << 1], tree[x << 1 | 1]); } void build(int l, int r, int x) { if (l == r) { tree[x] = n + 1; return; } reg int mid = l + r >> 1; build(l, mid, x << 1); build(mid + 1, r, x << 1 | 1); pushup(x); } void modify(int l, int r, int pos, int val, int x) { if (l == r) { tree[x] = val; return; } int mid = l + r >> 1; if (pos <= mid) modify(l, mid, pos, val, x << 1); else modify(mid + 1, r, pos, val, x << 1 | 1); pushup(x); } int query(int l, int r, int la, int ra, int x) { if (l >= la && r <= ra) return tree[x]; int mid = l + r >> 1; int res = n + 1; if (la <= mid) res = min(res, query(l, mid, la, ra, x << 1)); if (ra > mid) res = min(res, query(mid + 1, r, la, ra, x << 1 | 1)); return res; } } seg; int fir[N], nx[N << 2], to[N << 2], in[N]; inline void add(int _, int __) { nx[++cnt] = fir[_]; fir[_] = cnt; to[cnt] = __; } inline void topo() { priority_queue<int, vector<int>, greater<int> > q; rep (i, 1, n) if (in[i] == 0) q.push(i); int res = 0; while (!q.empty()) { int x = q.top(); a[x] = ++res; q.pop(); for (int i = fir[x]; i; i = nx[i]) { int v = to[i]; --in[v]; if (in[v] == 0) q.push(v); } } rep (i, 1, n) printf("%d\n", a[i]); } int main() { read(n), read(k); rep (i, 1, n) read(a[i]), b[a[i]] = i; if (k <= 1) { sort(a + 1, a + n + 1); rep (i, 1, n) printf("%d\n", a[i]); return 0; } seg.build(1, n, 1); per (i, n, 1) { int x = seg.query(1, n, min(n, b[i] + 1), min(n, b[i] + k - 1), 1); if (x != n + 1) add(b[i], b[x]), in[b[x]]++; x = seg.query(1, n, max(1, b[i] - k + 1), max(1, b[i] - 1), 1); if (x != n + 1) add(b[i], b[x]), in[b[x]]++; seg.modify(1, n, b[i], i, 1); } topo(); return 0; } /* _____ ____ _____ |__ / | _ \ |__ / / / | |_) | / / / /_ | _ < / /_ /____| |_| \_\ /____| _______________________________________ / permutation.cpp is created by zrz who \ \ is weak / --------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || */
codenet
-1
3,267
s270338097
p04052
1,584,060,184
cpp
Accepted
723
54,400
/* be name khoda */ // #define stream_enable #define long_enable #include <iostream> #include <algorithm> #include <cstring> #include <numeric> #include <iomanip> #include <sstream> #include <vector> #include <queue> #include <set> #include <map> using namespace std; #ifdef stream_enable #define cin sss #endif #ifdef long_enable typedef long long int ll; #else typedef int ll; #endif typedef pair<ll, ll> pii; typedef pair<pii, ll> ppi; typedef pair<ll, pii> pip; typedef vector<ll> vi; typedef vector<pii> vpii; const ll MOD = 1000000007; const long long BIG = 1446803456761533460; const int Big = 336860180; #define F first #define S second #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define debug(x) cout << #x << " -> " << (x) << endl #define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl #define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl #define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl #define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl #define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl #define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl #define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl #define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl #define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl #define forifrom(i, s, n) for(ll i = (s); i < (n); ++i) #define forirto(i, n, e) for(ll i = (n) - 1; i >= (e); --i) #define fori(i, n) forifrom (i, 0, n) #define forir(i, n) forirto (i, n, 0) #define smin(a, b) a = min(a, (b)) #define smax(a, b) a = max(a, (b)) #define Add(a, b) a = ((a) + (b)) % MOD #define Mul(a, b) a = (1LL * (a) * (b)) % MOD stringstream sss; // ----------------------------------------------------------------------- const ll maxn = 500010; ll n, k; ll A[maxn]; vi subt[maxn]; set<pii> st; priority_queue<ll, vi, greater<ll>> pq; ll res[maxn]; void MAIN() { cin >> n >> k; fori (i, n) cin >> A[i]; st.insert({0, 0}); fori (i, k - 1) { st.insert({A[i], i + 1}); } fori (i, n) { if (i + k - 1 < n) st.insert({A[i + k - 1], i + k}); st.erase({A[i], i + 1}); auto it = st.lower_bound({A[i], -1}); pii x = *(--it); subt[x.S].eb(i + 1); } pq.push(0); fori (i, n + 1) { ll x = pq.top(); pq.pop(); for (auto y : subt[x]) pq.push(y); if (x > 0) res[x - 1] = i; } fori (i, n) cout << res[i] << '\n'; } // ----------------------------------------------------------------------- int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cout << fixed << setprecision(10); sss << R"( 8 6 4 5 7 8 3 1 2 6 )"; MAIN(); return 0; }
codenet
-1
3,314
s934279825
p04052
1,468,576,936
cpp
Accepted
1,858
17,392
#include <cstdio> #include <algorithm> #include <stack> #include <queue> #include <deque> #include <vector> #include <string> #include <string.h> #include <cstdlib> #include <ctime> #include <cmath> #include <map> #include <set> #include <iostream> #include <sstream> #include <numeric> #include <cctype> #include <bitset> #include <cassert> #define fi first #define se second #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next) #define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++) #define rng(a) a.begin(),a.end() #define maxs(x,y) x = max(x,y) #define mins(x,y) x = min(x,y) #define pb push_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcount #define uni(x) x.erase(unique(rng(x)),x.end()) #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define df(x) int x = in() #define dame { puts("0"); return 0;} #define show(x) cout<<#x<<" = "<<x<<endl; using namespace std; typedef long long int ll; typedef pair<int,int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; inline int in() { int x; scanf("%d",&x); return x;} inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');} template<typename T>istream& operator>>(istream&i,vector<T>&v) {rep(j,sz(v))i>>v[j];return i;} template<typename T>string join(vector<T>&v) {stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);} template<typename T>ostream& operator<<(ostream&o,vector<T>&v) {if(sz(v))o<<join(v);return o;} template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v) {return i>>v.fi>>v.se;} template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v) {return o<<v.fi<<","<<v.se;} const int MX = 100005, INF = 1001001001; const ll LINF = 1e18; const double eps = 1e-10; int n, k; vi p, a, ans; priority_queue<int> q; struct seg { vi d, e; int x2; seg(){} seg(int mx) { x2 = 1; while(x2 < mx) x2 <<= 1; d = vi(x2<<1); e = vi(x2<<1, INF); } void sp(int i) { d[i<<1] += d[i]; e[i<<1] += d[i]; d[i<<1|1] += d[i]; e[i<<1|1] += d[i]; d[i] = 0; } // 0 -> reset, otherwise -> add void add(int a, int b, int x) { add(max(0,a), min(b,n), x, 1, 0, x2); } void add(int a, int b, int x, int i, int l, int r){ if(a <= l && r <= b) { if (x) { d[i] += x; e[i] += x; } else { d[i] = e[i] = 0; } return; } sp(i); int c = (l+r)>>1; if(a < c) add(a,b,x,i<<1,l,c); if(c < b) add(a,b,x,i<<1|1,c,r); e[i] = min(e[i<<1], e[i<<1|1]); } void push(int i=1) { if (e[i]) return; if (i >= x2) { q.push(i-x2); d[i] = e[i] = INF; return; } sp(i); push(i<<1); push(i<<1|1); e[i] = min(e[i<<1], e[i<<1|1]); } }; void solve() { seg t(n+2); rep(i,n) { t.add(a[i]-k+1, a[i]+k, 1); t.add(a[i], a[i]+1, 0); } t.push(); while (sz(q)) { int i = q.top(); q.pop(); ans.pb(i); t.add(i-k+1, i+k, -1); t.push(); } } int main() { scanf("%d%d",&n,&k); p = a = vi(n); rep(i,n) scanf("%d",&p[i]), --p[i]; rep(i,n) a[p[i]] = i; solve(); assert(sz(ans) == n); reverse(rng(ans)); rep(i,n) p[ans[i]] = i; for (int x : p) printf("%d\n",x+1); return 0; }
codenet
-1
3,430
s729183734
p04052
1,508,625,144
cpp
Accepted
455
32,896
// Copyright (C) 2017 SYCstudio. // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; version 3 // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; If not, see <http://www.gnu.org/licenses/>. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define ll long long #define mem(Arr,x) memset(Arr,x,sizeof(Arr)) #define mcp(Arr,Brr) memcpy(Arr,Brr,sizeof(Arr)) const int maxN=500100; const int inf=2147483647; int n,K; int cnt=0; int Head[maxN]; int Next[maxN*4]; int V[maxN*4]; int Arr[maxN]; int Pos[maxN];//Pos[i]表示数i在Arr中的位置 int New_Pos[maxN]; int Seg[maxN*4]; int Degree[maxN]; priority_queue<int,vector<int>,less<int> > Q; int read(); int Query(int now,int l,int r,int ql,int qr); void Update(int now,int l,int r,int pos,int key); void Add_Edge(int u,int v); void Outp(int now,int l,int r); int main() { mem(Seg,127); mem(Head,-1); n=read(); K=read(); for (int i=1;i<=n;i++) { Arr[i]=read(); Pos[Arr[i]]=i; } for (int i=n;i>=1;i--) { //Outp(1,1,n); //cout<<endl; int mn=inf; //if (Pos[i]!=1) //cout<<max(1,Pos[i]-K+1)<<" "<<Pos[i]<<endl; mn=Query(1,1,n,max(1,Pos[i]-K+1),Pos[i]); //cout<<mn<<endl; if (mn<=n) Add_Edge(Pos[mn],Pos[i]); mn=inf; //if (Pos[i]!=n) mn=Query(1,1,n,Pos[i],min(Pos[i]+K-1,n)); if (mn<=n) Add_Edge(Pos[mn],Pos[i]); //cout<<mn<<endl; Update(1,1,n,Pos[i],i); } for (int i=1;i<=n;i++) if (Degree[i]==0) Q.push(i); //cout<<Q.empty()<<endl; for (int i=n;i>=1;i--) { int x=Q.top(); //cout<<x<<endl; Q.pop(); New_Pos[i]=x; for (int j=Head[x];j!=-1;j=Next[j]) { Degree[V[j]]--; if (Degree[V[j]]==0) Q.push(V[j]); } } /* for (int i=1;i<=n;i++) cout<<New_Pos[i]<<" "; cout<<endl; //*/ for (int i=1;i<=n;i++) Arr[New_Pos[i]]=i; for (int i=1;i<=n;i++) printf("%d\n",Arr[i]); fclose(stdin); fclose(stdout); return 0; } int read() { int x=0,k=1; char ch=getchar(); while (((ch<'0')||(ch>'9'))&&(ch!='-')) ch=getchar(); if (ch=='-') { k=-1; ch=getchar(); } while ((ch>='0')&&(ch<='9')) { x=x*10+ch-48; ch=getchar(); } return x*k; } int Query(int now,int l,int r,int ql,int qr) { //cout<<now<<" "<<l<<" "<<r<<" "<<ql<<" "<<qr<<" "<<Seg[now]<<endl; if ((l==ql)&&(r==qr)) return Seg[now]; int mid=(l+r)/2; if (qr<=mid) return Query(now*2,l,mid,ql,qr); if (ql>=mid+1) return Query(now*2+1,mid+1,r,ql,qr); return min(Query(now*2,l,mid,ql,mid),Query(now*2+1,mid+1,r,mid+1,qr)); } void Update(int now,int l,int r,int pos,int key) { if (l==r) { Seg[now]=key; return; } int mid=(l+r)/2; if (pos<=mid) Update(now*2,l,mid,pos,key); if (pos>=mid+1) Update(now*2+1,mid+1,r,pos,key); Seg[now]=min(Seg[now*2],Seg[now*2+1]); return; } void Add_Edge(int u,int v) { cnt++; Next[cnt]=Head[u]; Head[u]=cnt; V[cnt]=v; Degree[v]++; } void Outp(int now,int l,int r) { if (l==r) { cout<<Seg[now]<<" "; return; } int mid=(l+r)/2; Outp(now*2,l,mid); Outp(now*2+1,mid+1,r); return; }
codenet
-1
3,437
s786237686
p04052
1,471,361,191
cpp
Accepted
1,936
95,984
#include <bits/stdc++.h> // iostream is too mainstream #include <cstdio> // bitch please #include <iostream> #include <algorithm> #include <cstdlib> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <list> #include <cmath> #include <iomanip> #include <time.h> #define dibs reserve #define OVER9000 1234567890 #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++) #define tisic 47 #define soclose 1e-8 #define chocolate win // so much chocolate #define patkan 9 #define ff first #define ss second #define abs(x) ((x < 0)?-(x):x) #define uint unsigned int #define dbl long double #define pi 3.14159265358979323846 using namespace std; // mylittledoge #ifdef DONLINE_JUDGE // palindromic tree is better than splay tree! #define lld I64d #endif void solve(vector< vector<int> > E, vector<int> &pos, vector<int> v, int pos_add) { int N =v.size(); if(N == 0) return; vector<int> sub(N,0); queue<int> q; q.push(0); vector<int> v1,v2; while(!q.empty()) { ALL_THE(E[q.front()],it) if(sub[*it] == 0) { sub[*it] =1; q.push(*it);} q.pop();} for(int i =1; i < N; i++) { if(sub[i] == 0) v2.push_back(v[i]); else v1.push_back(v[i]);} vector<int> num(N); int x1 =0, x2 =0; for(int i =1; i < N; i++) { if(sub[i] == 0) num[i] =x2++; else num[i] =x1++;} vector< vector<int> > E1,E2; for(int i =1; i < N; i++) { if(sub[i] == 1) { vector<int> w; for(int j =0; j < (int)E[i].size(); j++) if(E[i][j] > 0 && sub[E[i][j]] == 1) w.push_back(num[E[i][j]]); E1.push_back(w);} else { vector<int> w; for(int j =0; j < (int)E[i].size(); j++) if(E[i][j] > 0 && sub[E[i][j]] == 0) w.push_back(num[E[i][j]]); E2.push_back(w);} } pos[pos_add+v1.size()] =v[0]; solve(E1,pos,v1,pos_add); solve(E2,pos,v2,pos_add+v1.size()+1); } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N,K; cin >> N >> K; vector<int> A(N); for(int i =0; i < N; i++) cin >> A[i]; vector<int> lfte(N,-1),rte(N,-1); set< pair<int,int> > S; for(int i =0; i < N; i++) { if(i > 0) S.insert(make_pair(A[i-1],i-1)); if(i >= K) S.erase(make_pair(A[i-K],i-K)); auto it =S.lower_bound(make_pair(A[i],-1)); if(it != S.begin()) { it--; lfte[i] =it->ss;} } S.clear(); for(int i =N-1; i >= 0; i--) { if(i < N-1) S.insert(make_pair(A[i+1],i+1)); if(i+K < N) S.erase(make_pair(A[i+K],i+K)); auto it =S.lower_bound(make_pair(A[i],-1)); if(it != S.begin()) { it--; rte[i] =it->ss;} } vector< vector<int> > E(N); for(int i =0; i < N; i++) { // cout << lfte[i] << " " << rte[i] << " " << i << "\n"; if(lfte[i] != -1) E[i].push_back(lfte[i]); if(rte[i] != -1) E[i].push_back(rte[i]);} // for(int i =0; i < N; i++) ALL_THE(E[i],it) cout << i << " " << *it << "\n"; vector<int> pos(N),v; for(int i =0; i < N; i++) v.push_back(i); // solve(E,pos,v,0); vector< vector<int> > E2(N); vector<int> D(N,0); for(int i =0; i < N; i++) { D[i] =E[i].size(); ALL_THE(E[i],it) E2[*it].push_back(i);} set<int> S2; for(int i =0; i < N; i++) if(D[i] == 0) S2.insert(i); for(int i =0; i < N; i++) { int x =*S2.begin(); pos[i] =x; S2.erase(x); ALL_THE(E2[x],it) { D[*it]--; if(D[*it] == 0) S2.insert(*it);} } vector<int> ans(N); for(int i =0; i < N; i++) ans[pos[i]] =i+1; for(int i =0; i < N; i++) cout << ans[i] << "\n"; return 0;} // look at my code // my code is amazing
codenet
-1
3,441
s526902232
p04052
1,533,172,499
cpp
Accepted
548
46,592
/// {{{ Author: Wang, Yen-Jen // include #include <bits/stdc++.h> // using using namespace std; // types typedef long long ll; typedef pair<int,int> pii; // macro #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin() , (x).end() #define REP(i , n) for(int i = 0; i < int(n); i++) #define REP1(i , a , b) for(int i = a; i <= int(b); i++) #define F first #define S second #define MP make_pair #define PB push_back #define LC o<<1 , l , m #define RC o<<1|1 , m + 1 , r #define MS(x , v) memset(x , (v) , sizeof(x)) // input inline bool SR(int &x) { return scanf("%d",&x) == 1; } inline bool SR(ll &x) { return scanf("%lld",&x) == 1; } inline bool SR(double &x) { return scanf("%lf",&x) == 1; } inline bool SR(char *s) { return scanf("%s",s) == 1; } inline bool RI() { return true; } template<typename I , typename... T> inline bool RI(I &x , T&... tail) { return SR(x) && RI(tail...); } // output inline void SP(const int x) { printf("%d",x); } inline void SP(const ll x) { printf("%lld",x); } inline void SP(const double x) { printf("%.16lf",x); } inline void SP(const char *s) { printf("%s",s); } inline void PL() { puts(""); } template<typename I , typename... T> inline void PL(const I x , const T... tail) { SP(x); if(sizeof...(tail)) putchar(' '); PL(tail...); } // debug #define WangYenJen #ifdef WangYenJen template<typename I> void _DOING(const char *s , I&& x) { cerr << s << " = " << x << endl; } template<typename I , typename... T> void _DOING(const char *s , I&& x , T&&... tail) { int c = 0; while(*s != ',' || c != 0) { if(*s == '(' || *s == '[' || *s == '{') c++; if(*s == ')' || *s == ']' || *s == '}') c--; cerr << *s++; } cerr << " = " << x << " , "; _DOING(s + 1 , tail...); } #define DEBUG(...) \ do {\ fprintf(stderr , "%s: Line %d - ",__PRETTY_FUNCTION__,__LINE__);\ _DOING(#__VA_ARGS__ , __VA_ARGS__);\ } while(0); #else #define DEBUG(...) #endif // constant number const int INF = 0x3f3f3f3f; const ll INF64 = 0x3f3f3f3f3f3f3f3fll; // random function inline int RAND() { static int x = 880301; return (x = x * 0xdefaced + 1) % 0x7fffffff; } /// }}} const int MAX_N = 500000 + 7; int tr[MAX_N<<2]; void modify(int o, int l, int r, int p, int v) { if (l == r) tr[o] = v; else { int m = (l + r) >> 1; if (p <= m) modify(LC, p, v); else modify(RC, p, v); tr[o] = min(tr[o<<1], tr[o<<1|1]); } } int query(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return tr[o]; int m = (l + r) >> 1; if (qr <= m) return query(LC, ql, qr); else if (m < ql) return query(RC, ql, qr); else return min(query(LC, ql, qr), query(RC, ql, qr)); } int ans[MAX_N]; int deg[MAX_N]; vector<int> G[MAX_N]; int P[MAX_N], Q[MAX_N]; int main() { int N, K; RI(N, K); REP1(i, 1, N) { RI(P[i]); Q[P[i]] = i; } MS(tr, INF); for (int i = N; i >= 1; i--) { int p = query(1, 1, N, max(1, Q[i] - K + 1), Q[i]); if (p != INF) { G[Q[i]].PB(Q[p]); deg[Q[p]]++; } p = query(1, 1, N, Q[i], min(N, Q[i] + K - 1)); if (p != INF) { G[Q[i]].PB(Q[p]); deg[Q[p]]++; } modify(1, 1, N, Q[i], i); } priority_queue<int, vector<int>, greater<int> > pq; REP1(i, 1, N) { if (deg[i] == 0) pq.push(i); } int now_id = 0; while (!pq.empty()) { int u = pq.top(); ans[u] = ++now_id; pq.pop(); for (int v : G[u]) { if (--deg[v] == 0) pq.push(v); } } REP1(i, 1, N) PL(ans[i]); return 0; }
codenet
-1
3,508
s937080153
p04052
1,518,699,838
cpp
Accepted
685
60,160
#ifndef LOCAL #pragma GCC optimize("Ofast") #endif #include "bits/stdc++.h" using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; using ld = long double; constexpr int nax = 1000 * 1000 + 105; constexpr int oo = numeric_limits<int>::max(); constexpr int mod = 1000 * 1000 * 1000 + 7; template <const int&(*MinMax)(const int&, const int&), int neutral> struct Drzewo { int n2; vector<int> drz; Drzewo(int n) { n2 = 2; while (n2 <= n) { n2 *= 2; } drz.resize(n2 * 2, neutral); } void Ustaw(int w, int val) { assert(0 <= w and w < n2); w += n2; drz[w] = val; w /= 2; while (w) { drz[w] = MinMax(drz[w * 2], drz[w * 2 + 1]); w /= 2; } } int Odczyt(int a, int b) { assert(0 <= a and a <= b and b < n2); a += n2; b += n2; int wynik = MinMax(drz[a], drz[b]); while ((a / 2) != (b / 2)) { if (a % 2 == 0) wynik = MinMax(wynik, drz[a + 1]); if (b % 2 == 1) wynik = MinMax(wynik, drz[b - 1]); a /= 2; b /= 2; } return wynik; } }; int n, k; int tab[nax]; int odp[nax]; vector<int> graf[nax]; int stop[nax]; void Toposort() { for (int i = 1; i <= n; i++) { for (int x : graf[i]) { stop[x]++; } } set<int> moge; for (int i = 1; i <= n; i++) { if (stop[i] == 0) { moge.insert(i); } } for (int i = 1; i <= n; i++) { assert(!moge.empty()); tab[i] = *moge.begin(); moge.erase(moge.begin()); for (int x : graf[tab[i]]) { stop[x]--; if (stop[x] == 0) { moge.insert(x); } } } assert(moge.empty()); } void BudujGraf() { debug() << imie(range(tab + 1, tab + n + 1)); Drzewo<std::min<int>, +oo> prawo(n); for (int i = n; i >= 1; i--) { const int above = prawo.Odczyt(tab[i], min(tab[i] + k - 1, n)); const int below = prawo.Odczyt(max(tab[i] - k + 1, 1), tab[i]); prawo.Ustaw(tab[i], i); if (above != +oo) { graf[tab[i]].push_back(tab[above]); } if (below != +oo) { graf[tab[i]].push_back(tab[below]); } } Drzewo<std::max<int>, -oo> lewo(n); for (int i = 1; i <= n; i++) { const int above = lewo.Odczyt(tab[i], min(tab[i] + k - 1, n)); const int below = lewo.Odczyt(max(tab[i] - k + 1, 1), tab[i]); lewo.Ustaw(tab[i], i); if (above != -oo) { graf[tab[above]].push_back(tab[i]); } if (below != -oo) { graf[tab[below]].push_back(tab[i]); } } } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); tab[x] = i; } BudujGraf(); Toposort(); for (int i = 1; i <= n; i++) { odp[tab[i]] = i; } for (int i = 1; i <= n; i++) { printf("%d\n", odp[i]); } return 0; }
codenet
-1
3,537
s448383078
p04052
1,583,968,851
cpp
Accepted
1,529
167,592
#pragma GCC optimize("Ofast,fast-math,unroll-loops") #include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define int ll //#define double long double #define endl '\n' #define all(C) (C).begin(), (C).end() #define rall(C) (C).rbegin(), (C).rend() #define mp make_pair #define pb emplace_back #define dbg(x) cerr << #x << " : " << x << endl //#define PI 3.141592653589 using namespace std; //using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair <int, int>; using pll = pair <ll, ll>; using pld = pair <ld, ld>; /* const ll MAX_MEM = 5e8; char MEM[MAX_MEM]; ll MEM_POS = 0; void* operator new(size_t x) { auto ret = MEM + MEM_POS; MEM_POS += x; assert(MEM_POS < MAX_MEM); return ret; } void operator delete(void*) {} */ template <class T> istream& operator>> (istream &in, vector <T> &a) { for (auto &i : a) in >> i; return in; } template <class T> ostream& operator<< (ostream &out, vector <T> a) { for (auto &i : a) out << i << ' '; return out; } template <class T, class U> istream& operator>> (istream &in, pair <T, U> &p) { in >> p.first >> p.second; return in; } template <class T, class U> ostream& operator<< (ostream &out, pair <T, U> p) { out << p.first << " " << p.second << " "; return out; } inline void Start() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); //freopen("circlecover.in", "r", stdin); //freopen("circlecover.out", "w", stdout); } struct node { pii val; int vl, vr; node *l = 0, *r = 0; node() {} node(int tl, int tr, vector <int> &a) { vl = tl, vr = tr; if (vr - vl == 1) { val = {a[vl], vl}; return; } l = new node(vl, (vl + vr) >> 1, a), r = new node((vl + vr) >> 1, vr, a); val = max(l->val, r->val); } void change(int pos, int x) { if (vr - vl == 1) { val = {x, pos}; return; } int vm = (vl + vr) >> 1; if (pos < vm) l->change(pos, x); else r->change(pos, x); val = max(l->val, r->val); } pii get(int ql, int qr) { if (vl == ql && vr == qr) return val; int vm = (vl + vr) >> 1; if (qr <= vm) return l->get(ql, qr); if (ql >= vm) return r->get(ql, qr); return max(l->get(ql, vm), r->get(vm, qr)); } }; signed main() { Start(); int n, k; cin >> n >> k; vector <int> a(n); cin >> a; vector <vector <int>> b; for (int i = 0; i < n; ++i) { if (i % k == 0) b.pb(0); b.back().pb(a[i]); } int m = b.size(); vector <node*> t; for (int i = 0; i < m; ++i) t.pb(new node(0, b[i].size(), b[i])); node *f = new node(0, n, a); set <pii> q; auto recalc = [&] (int i) -> void { auto g = t[i]->get(0, b[i].size()); if (g.first == -1) return; if (g.first == f->get(max(0ll, g.second + i * k - k + 1), min(n, g.second + i * k + k)).first) { q.emplace(-(g.second + i * k), g.first); } }; for (int i = 0; i < m; ++i) recalc(i); vector <int> ans(n); for (int i = n - 1; i >= 0; --i) { int pos = -q.begin()->first; q.erase(q.begin()); int j = pos / k; t[j]->change(pos - j * k, -1); f->change(pos, -1); ans[pos] = i + 1; for (int l = j - 1; l <= j + 1; ++l) if (0 <= l && l < m) recalc(l); } for (auto &i : ans) cout << i << endl; return 0; }
codenet
-1
3,833
s777660235
p04052
1,468,853,447
cpp
Accepted
3,913
47,092
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i,x) for(int i=0;i<(int)(x);i++) #define REPS(i,x) for(int i=1;i<=(int)(x);i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(int i=((int)(x));i>0;i--) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a,b) make_pair(a, b) #define pb push_back #define eb emplace_back #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os; } template<class T> ostream& operator<<(ostream &os, const set<T> &t) { os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os; } template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);} template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);} const int INF = 1<<28; const double EPS = 1e-8; const int MOD = 1000000007; struct handler { typedef pii val_t; typedef pii opr_t; handler() {} val_t def_val() { return pii(-1, -1); } static val_t update(const val_t &l, const opr_t &r) { return r; } static val_t merge(const val_t &l, const val_t &r) { return max(l, r); } }; template<typename Handler> struct SegTree { typedef typename Handler::val_t val_t; typedef typename Handler::opr_t opr_t; vector<val_t> val; Handler hdl; int n; SegTree(int size) :hdl() { n = 1; while (n<size) n <<= 1; val = vector<val_t>(2 * n, hdl.def_val()); } SegTree(const vector<val_t> &in) :hdl() { n = 1; while (n<in.size()) n <<= 1; val = vector<val_t>(2 * n, hdl.def_val()); for (int i = n - 1 + in.size() - 1; i >= 0; i--) { if (n - 1 <= i) val[i] = in[i - (n - 1)]; else val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]); } } void update(int i, const opr_t& a) { i += n - 1; val[i] = hdl.update(val[i], a); while (i > 0) { i = (i - 1) / 2; val[i] = hdl.merge(val[i * 2 + 1], val[i * 2 + 2]); } } val_t query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return hdl.def_val(); if (a <= l&&r <= b) return val[k]; return hdl.merge(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r) ); } val_t query(int a, int b) { return query(a, b, 0, 0, n); } val_t operator[](size_t i) { return query(i, i + 1); } friend ostream& operator<<(ostream &os, SegTree<Handler> &t) { REP(i, t.n) os << (i ? ", " : "[") << t.query(i, i + 1); return os << "]"; } }; int T, n, m; int main(int argc, char *argv[]){ ios::sync_with_stdio(false); cin >> n >> m; vi p(n); REP(i, n) { int x; cin >> x; x--; p[x] = i; } SegTree<handler> seg(n); vector<vi> g(n); vi deg(n); REP(i, n) { for (auto ret : { seg.query(p[i], min(n, p[i] + m)) }) { if (ret.first != -1) { g[ret.second].eb(p[i]); deg[p[i]] ++; } } seg.update(p[i], pii(i, p[i])); } priority_queue<int, vi, greater<int>> pq; REP(i, n) if (deg[i] == 0) pq.push(i); vi ans; while (!pq.empty()) { int u = pq.top(); pq.pop(); ans.push_back(u); for (int v : g[u]) if (--deg[v] == 0) { pq.push(v); } } assert(ans.size() == n); vi inv(n); REP(i, n) inv[ans[i]] = i; for (int x : inv) cout << x + 1 << endl; return 0; }
codenet
-1
4,154
s120844326
p04052
1,545,840,110
cpp
Accepted
1,155
93,680
/* Author: CNYALI_LK LANG: C++ PROG: f.cpp Mail: cnyalilk@vip.qq.com */ #include<bits/stdc++.h> #define debug(...) fprintf(stderr,__VA_ARGS__) #define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define all(x) x.begin(),x.end() #define x first #define y second using namespace std; typedef long long ll; typedef pair<int,int> pii; const signed inf=0x3f3f3f3f; const double eps=1e-8; const double pi=acos(-1.0); template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;} template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;} template<class T>T sqr(T a){return a*a;} template<class T>T mmin(T a,T b){return a<b?a:b;} template<class T>T mmax(T a,T b){return a>b?a:b;} template<class T>T aabs(T a){return a<0?-a:a;} template<class T>int dcmp(T a,T b){return a>b;} template<int *a>int cmp_a(int x,int y){return a[x]<a[y];} #define min mmin #define max mmax #define abs aabs namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) // print the remaining part inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); } // input a signed integer inline void read (signed &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; } inline void read (long long &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; } inline void read (char &x) { x=gc(); } inline void read(char *x){ while((*x=gc())=='\n' || *x==' '||*x=='\r'); while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc(); } template<typename A,typename ...B> inline void read(A &x,B &...y){ read(x);read(y...); } // print a signed integer inline void write (signed x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); } inline void write (long long x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); } inline void write (char x) { putc(x); } inline void write(const char *x){ while(*x){putc(*x);++x;} } inline void write(char *x){ while(*x){putc(*x);++x;} } template<typename A,typename ...B> inline void write(A x,B ...y){ write(x);write(y...); } //no need to call flush at the end manually! struct Flusher_ {~Flusher_(){flush();}}io_flusher_; } using io :: read; using io :: putc; using io :: write; int a[500005],p[500005]; struct smt{ int ls,rs,mx; smt *l,*r; smt(int la,int ra){ ls=la;rs=ra; mx=0; if(la==ra){ l=r=0; } else{ int mid=(ls+rs)>>1; l=new smt(ls,mid); r=new smt(mid+1,rs); } } int query(int la,int ra){ if(la<=ls && rs<=ra)return mx; int mx=0; if(la<=l->rs)chkmax(mx,l->query(la,ra)); if(r->ls<=ra)chkmax(mx,r->query(la,ra)); return mx; } void upd(int x,int y){ mx=y; if(ls==rs)return; if(x<=l->rs)l->upd(x,y); else r->upd(x,y); } }; smt *rt; int ind[500005],c[500005]; vector<int> to[500005]; priority_queue<pii> pq; int main(){ #ifdef cnyali_lk freopen("f.in","r",stdin); freopen("f.out","w",stdout); #endif int n,k,x; read(n,k); for(int i=1;i<=n;++i){ read(a[i]); p[a[i]]=i; } rt=new smt(1,n); for(int i=1;i<=n;++i){ to[i].push_back(x=rt->query(p[i]-k+1,p[i]));++ind[x]; to[i].push_back(x=rt->query(p[i],p[i]+k-1));++ind[x]; rt->upd(p[i],i); } for(int i=1;i<=n;++i)if(!ind[i])pq.push(make_pair(p[i],i)); int t=n+1; while(!pq.empty()){ c[--t]=pq.top().x; int x=pq.top().y; pq.pop(); for(auto i:to[x]){ if(!--ind[i]){ pq.push(make_pair(p[i],i)); } } } for(int i=1;i<=n;++i)a[i]=i; sort(a+1,a+n+1,cmp_a<c>); for(int i=1;i<=n;++i)printf("%d\n",a[i]); return 0; }
codenet
-1
4,165
s405941122
p04052
1,537,646,593
cpp
Accepted
817
42,624
#include <bits/stdc++.h> #define ADD(a, b) a = (a + ll(b)) % mod #define MUL(a, b) a = (a * ll(b)) % mod #define MAX(a, b) a = max(a, b) #define MIN(a, b) a = min(a, b) #define rep(i, a, b) for(int i = int(a); i < int(b); i++) #define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--) #define all(a) (a).begin(), (a).end() #define sz(v) (int)(v).size() #define pb push_back #define sec second #define fst first #define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<int, pi> ppi; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vl> mat; typedef complex<double> comp; void Debug() {cout << '\n'; } template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){ cout<<arg<<" ";Debug(rest...);} template<class T>ostream& operator<<(ostream& out,const vector<T>& v) { out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;} template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){ out<<"("<<v.first<<", "<<v.second<<")";return out;} const int MAX_N = 500010; const int MAX_V = 100010; const double eps = 1e-6; const ll mod = 1000000007; const int inf = 1 << 29; const ll linf = 1LL << 60; const double PI = 3.14159265358979323846; /////////////////////////////////////////////////////////////////////////////////////////////////// struct BIT { //0-origin!!! update and get just like lazy_segtree int n; vl bit0, bit1; void init(int mx) { n = mx; bit0 = vl(n + 1, 0); bit1 = vl(n + 1, 0); } BIT(int mx = 0) { init(mx); } ll ga(vl& bit, int i) { ll s = 0; while(i > 0) { s += bit[i]; i -= i & -i; } return s; } void app(vl& bit, int i, ll x) { while(i <= n) { bit[i] += x; i += i & -i; } } void update(int a, int b, ll x) { //[a, b) a++; app(bit0, a, -x * (a - 1)); app(bit1, a, x); app(bit0, b + 1, x * b); app(bit1, b + 1, -x); } ll get(int a, int b) { //[a, b) a++; return (ga(bit1, b) * b + ga(bit0, b)) - (ga(bit1, a - 1) * (a - 1) + ga(bit0, a - 1)); } }; struct BITB { //1-origin!!! int n, b; vl bit; void init(int mx) { n = mx; b = 1; while(b * 2 <= n) b *= 2; bit = vl(n + 1, 0); } BITB(int mx = 0) { init(mx); } ll get(int i) { ll s = 0; while(i > 0) { s += bit[i]; i -= i & -i; } return s; } void update(int i, ll x) { while(i <= n) { bit[i] += x; i += i & -i; } } int get_index(ll x) { //something like lower_bound(all(sum), a). O(logn) x--; int r = b; int res = 0; while(r > 0) { if((r | res) <= n) { int rv = bit[r | res]; if(x >= rv) { x -= rv; res += r; } } r >>= 1; } return res + 1; } int range_sum(int a, int b) { //[a, b] return get(b) - get(a - 1); } }; int N, K; int A[MAX_N]; int G[MAX_N]; int D[MAX_N]; int F[MAX_N]; pi Q[MAX_N]; int loop(int v) { if(F[v] != -1) return F[v]; int res = D[v]; if(G[v] != v) { res += loop(G[v]); } return F[v] = res; } void solve() { cin >> N >> K; rep(i, 0, N) { cin >> A[i]; A[i]--; } set<pi> S; rep(i, 0, K) S.insert(pi(-A[i], i)); rep(i, 0, N) { auto it = S.upper_bound(pi(-A[i], i)); if(it == S.end()) { G[i] = i; Q[A[i]] = pi(i, i + 1); } else { G[i] = (*it).sec; Q[A[i]] = pi(i, G[i]); } S.erase(pi(-A[i], i)); if(i + K < N) S.insert(pi(-A[i + K], i + K)); } BIT bit(N); rep(i, 0, N) { bit.update(Q[i].fst, Q[i].fst + 1, 1); D[Q[i].fst] = bit.get(Q[i].fst, Q[i].sec); } memset(F, -1, sizeof(F)); rep(i, 0, N) loop(i); // debug(vi(G, G + N)); // debug(vi(D, D + N)); // debug(vi(F, F + N)); BITB bb(N); rep(i, 0, N) bb.update(i + 1, 1); rep(i, 0, N) { int a = bb.get_index(F[i]); cout << a << "\n"; bb.update(a, -1); } } int main() { #ifndef LOCAL ios::sync_with_stdio(false); cin.tie(0); #endif cout << fixed; cout.precision(20); srand((unsigned int)time(NULL)); #ifdef LOCAL //freopen("in.txt", "wt", stdout); //for tester freopen("in.txt", "rt", stdin); #endif solve(); #ifdef LOCAL cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
codenet
-1
4,191
s819562916
p04052
1,552,449,332
cpp
Accepted
599
14,064
#include<queue> #include<set> #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } template<class T> struct RXQ { static const int npos = -1; int n, m; vector<T> d; RXQ() {} RXQ(int n_, const T init=T()) : n(n_), m(2<<__lg(max(1, n))), d(2*m, init) {} template<class Iter> void build(Iter begin, Iter end, const T init=T()) { n = end - begin; m = 2<<__lg(max(1, n)); d.resize(2*m); for (int i=0; begin!=end; ++begin, i++) d[i+m] = *begin; for (int i=n; i<m; i++) d[i+m] = init; for (int i=m; --i; ) d[i] = max(d[i*2+1], d[i*2]); }; const T at(int i) const { return d[i+m]; } void modify(int i, const T v) { i += m; d[i] = v; for (i>>=1; i; i>>=1) d[i] = max(d[i*2+1], d[i*2]); } T max_v(int l, int r) const { l += m; r += m; T ret = d[l++]; for (; l<r; l>>=1, r>>=1) { if (l & 1) { amax(ret, d[l]); l++; } if (r & 1) { r--; amax(ret, d[r]); } } return ret; } int max_i(int l, int r) const { return first_more_equal(l, max_v(l, r)); } // for i in [l .. n-1]: // if at(i) > v: return i; // return npos; int first_more_than(int l, const T v) const { int r = 2*m; for (l+=m; l<r; l>>=1, r>>=1) { if (l & 1) { if (v < d[l]) break; l++; } } if (l >= r) return npos; for (; l<m;) { if (v < d[l*2]) l = l*2; else l = l*2+1; } return l - m; } // for i in [l .. n-1]: // if v <= at(i): return i; // return npos; int first_more_equal(int l, const T v) const { int r = 2*m; for (l+=m; l<r; l>>=1, r>>=1) { if (l & 1) { if (v <= d[l]) break; l++; } } if (l >= r) return npos; for (; l<m;) { if (v <= d[l*2]) l = l*2; else l = l*2+1; } return l - m; } // for i in [r-1 .. 0]: // if v < at(i): return i; // return npos; int last_more_than(int r, const T v) const { if (r < m) { for (r+=m; 1<r; r>>=1) { if (r & 1) { r--; if (v < d[r]) break; } } if (r <= 1) return npos; } else { if (v < d[1]) r = 1; else return npos; } for (; r<m; ) { if (v < d[r*2+1]) r = r*2+1; else r = r*2; } return r - m; } // for i in [r-1 .. 0]: // if v <= at(i): return i; // return npos; int last_more_equal(int r, const T v) const { if (r < m) { for (r+=m; 1<r; r>>=1) { if (r & 1) { r--; if (v <= d[r]) break; } } if (r <= 1) return npos; } else { if (v <= d[1]) r = 1; else return npos; } for (; r<m; ) { if (v <= d[r*2+1]) r = r*2+1; else r = r*2; } return r - m; } }; int N, K; int P[500011]; int ans[500011]; bool in[500011]; void MAIN() { scanf("%d%d", &N, &K); REP (i, N) { scanf("%d", P+i); P[i]--; } priority_queue<int> Q; RXQ<int> rxq; rxq.build(P, P+N); auto update = [&](int i) { int nxt = min(N, i+K); int pos = rxq.max_i(i, nxt); if (rxq.at(pos) == -1 || in[pos]) return; int left = max(0, pos - K + 1); int right = min(N, pos + K); if (rxq.max_i(left, right) == pos) { Q.emplace(pos); in[pos] = true; } }; for (int i=0; i<N; i+=K) { update(i); } REP (t, N) { int p = Q.top(); Q.pop(); ans[p] = N-t; rxq.modify(p, -1); int i = p / K * K; update(i); if (i > 0) update(i-K); if (i+K < N) update(i+K); } REP (i, N) printf("%d\n", ans[i]); } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
codenet
-1
4,218
s214562216
p04052
1,589,745,553
cpp
Accepted
718
21,504
// #pragma GCC target("avx") // CPU 処理並列化 // #pragma GCC optimize("O3") // CPU 処理並列化 // #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす // #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024)); // #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_); #include<stdio.h> #include<math.h> #include<algorithm> #include<queue> #include<deque> #include<stack> #include<string> #include<string.h> #include<vector> #include<set> #include<map> #include<bitset> #include<stdlib.h> #include<cassert> #include<time.h> #include<bitset> #include<numeric> #include<unordered_set> #include<unordered_map> #include<complex> using namespace std; const long long mod=1000000007; const long long inf=mod*mod; const long long d2=(mod+1)/2; const double EPS=1e-11; const double INF=1e+10; const double PI=acos(-1.0); const int C_SIZE = 3100000; const int UF_SIZE = 3100000; namespace{ long long fact[C_SIZE]; long long finv[C_SIZE]; long long inv[C_SIZE]; inline long long Comb(int a,int b){ if(a<b||b<0)return 0; return fact[a]*finv[b]%mod*finv[a-b]%mod; } void init_C(int n){ fact[0]=finv[0]=inv[1]=1; for(int i=2;i<n;i++){ inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod; } for(int i=1;i<n;i++){ fact[i]=fact[i-1]*i%mod; finv[i]=finv[i-1]*inv[i]%mod; } } long long pw(long long a,long long b){ if(a<0LL)return 0; if(b<0LL)return 0; long long ret=1; while(b){ if(b%2)ret=ret*a%mod; a=a*a%mod; b/=2; } return ret; } long long pw_mod(long long a,long long b,long long M){ if(a<0LL)return 0; if(b<0LL)return 0; long long ret=1; while(b){ if(b%2)ret=ret*a%M; a=a*a%M; b/=2; } return ret; } int pw_mod_int(int a,int b,int M){ if(a<0)return 0; if(b<0)return 0; int ret=1; while(b){ if(b%2)ret=(long long)ret*a%M; a=(long long)a*a%M; b/=2; } return ret; } int ABS(int a){return max(a,-a);} long long ABS(long long a){return max(a,-a);} double ABS(double a){return max(a,-a);} int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } int UF[UF_SIZE]; void init_UF(int n){ for(int i=0;i<n;i++)UF[i]=-1; } int FIND(int a){ if(UF[a]<0)return a; return UF[a]=FIND(UF[a]); } void UNION(int a,int b){ a=FIND(a);b=FIND(b);if(a==b)return; if(UF[a]>UF[b])swap(a,b); UF[a]+=UF[b];UF[b]=a; } } // ここから編集しろ int segtree[1<<20]; int query(int a,int b,int c,int d,int e,int f){ if(d<a||b<c){ return -1; } if(c<=a&&b<=d){ return f; } return max(query(a,(a+b)/2,c,d,e*2,f+segtree[e*2]),query((a+b)/2+1,b,c,d,e*2+1,f+segtree[e*2+1])); } void add(int a,int b,int c,int d,int e,int f){ if(d<a||b<c)return; if(c<=a&&b<=d){ segtree[e]+=f; }else{ add(a,(a+b)/2,c,d,e*2,f); add((a+b)/2+1,b,c,d,e*2+1,f); int t=max(segtree[e*2],segtree[e*2+1]); segtree[e]+=t; segtree[e*2]-=t; segtree[e*2+1]-=t; } } int segtree2[1<<20]; int f(int a,int b,int c,int d){ if(a==b)return a; if(segtree2[c*2]>d){ return f(a,(a+b)/2,c*2,d); }else{ return f((a+b)/2+1,b,c*2+1,d-segtree2[c*2]); } } void del(int a){ a+=524288; while(a){ segtree2[a]--; a/=2; } } int p[510000]; int q[510000]; int ret[510000]; int tmp[510000]; int ans[510000]; int main(){ int a,b;scanf("%d%d",&a,&b); for(int i=0;i<a;i++){ scanf("%d",p+i);p[i]--; } for(int i=0;i<a;i++)q[p[i]]=i; segtree[1]=-1; for(int i=0;i<a;i++){ int at=query(0,524287,0,q[i]+b-1,1,segtree[1])+1; tmp[i]=at; // printf("%d: %d\n",i,at); add(0,524287,q[i],q[i],1,at-query(0,524287,q[i],q[i],1,segtree[1])); add(0,524287,q[i]+1,524287,1,1); } for(int i=0;i<a;i++){ segtree2[524288+i]=1; } for(int i=524287;i>0;i--){ segtree2[i]=segtree2[i*2]+segtree2[i*2+1]; } for(int i=a-1;i>=0;i--){ ret[q[i]]=f(0,524287,1,tmp[i]); // printf("%d: %d %d\n",i,tmp[i],ret[i]); del(ret[q[i]]); } for(int i=0;i<a;i++){ ans[i]=ret[i]; //ans[ret[i]]=i; } for(int i=0;i<a;i++){ printf("%d\n",ans[i]+1); } }
codenet
-1
4,272
s273085445
p04052
1,593,151,325
cpp
Accepted
396
48,884
#include "bits/stdc++.h" using namespace std; #ifndef LOCAL #define endl '\n' #endif #define fr(i, a, b) for(int i = a; i <= b; i++) #define rf(i, a, b) for(int i = a; i >= b; i--) #define pf push_front #define pb push_back #define eb emplace_back #define fi first #define se second #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() #define lb lower_bound #define ub upper_bound typedef long long ll; typedef long double f80; typedef pair<int,int> pii; typedef pair<ll,ll> pll; int pct(int x) { return __builtin_popcount(x); } int pct(ll x) { return __builtin_popcountll(x); } int bt(int x) { return 31 - __builtin_clz(x); } // floor(log2(x)) int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x)) int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); } ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); } int nxt_C(int x) { int c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } ll nxt_C(ll x) { ll c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } vector<int> get_bits(int mask) { vector<int> bb; while(mask) { int b = bt(mask); bb.pb(b); mask ^= (1 << b); } reverse(all(bb)); return bb; } int get_mask(vector<int> v) { int mask = 0; for(int x : v) { mask ^= (1 << x); } return mask; } template<typename T> void uniq(vector<T> &v) { sort(all(v)); v.resize(unique(all(v)) - v.begin()); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r){ uniform_int_distribution<ll> uid(l, r); return uid(rng); } void sc() {} template <typename Head, typename... Tail> void sc(Head &H, Tail &... T) { cin >> H; sc(T...); } #ifdef LOCAL #define debug(...) cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // #ifndef LOCAL // string to_string(__int128 x) { // string s = ""; // bool neg = 0; // if(x < 0) { s += "-"; neg = 1; x = -x; } // if(!x) s += '0'; // while(x) { // int rem = x % 10; // s += to_string(rem); // x /= 10; // } // reverse(s.begin() + neg, s.end()); // return s; // } // #endif const int mod = 1e9 + 7; // 998244353; int pwr(int a,ll b) { int ans = 1; while(b) { if(b & 1) ans = (ans * 1LL * a) % mod; a = (a * 1LL * a) % mod; b >>= 1; } return ans; } /* Lookout for overflows!! Check array sizes!! Clear before test cases!! Use the correct modulo!! Check for corner cases!! Are you forgetting something?! Read problem statement carefully!!! */ template<class T, size_t MAXN> struct RMQ { int n; T st[MAXN << 1]; void build(int n) { this->n = n; for (int i = n - 1; i > 0; i--) { st[i] = max(st[i << 1], st[i << 1 | 1]); } } void upd(int p, int val) { st[p += n] = val; for(; p > 1; p >>= 1) { st[p >> 1] = max(st[p], st[p ^ 1]); } } T query(int l, int r) { // [l, r] l = max(l, 1); r = min(r, n - 1); l += n, r += n + 1; T res = -1e9; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) res = max(res, st[l++]); if (r & 1) res = max(res, st[--r]); } return res; } }; const int N = 5e5 + 5; int a[N]; vector<int> g[N]; int deg[N]; int pos[N]; void solve() { RMQ<int, N> seg; int n, k; sc(n, k); fr(i, 1, n) { sc(a[i]); pos[a[i]] = i; seg.st[n + 1 + i] = -1e9; } seg.build(n + 1); fr(i, 1, n) { int idx = seg.query(pos[i] - k + 1, pos[i] - 1); if(idx != -1e9) { g[pos[idx]].pb(pos[i]); deg[pos[i]]++; } idx = seg.query(pos[i] + 1, pos[i] + k - 1); if(idx != -1e9) { g[pos[idx]].pb(pos[i]); deg[pos[i]]++; } seg.upd(pos[i], i); } set<int> q; fr(i, 1, n) { if(!deg[i]) { q.insert(i); } } int c = 0; vector<int> ans(n + 1); while(!q.empty()) { int u = *q.begin(); q.erase(q.begin()); ans[u] = ++c; for(int v : g[u]) { deg[v]--; if(!deg[v]) q.insert(v); } } fr(i, 1, n) { cout << ans[i] << endl; } } int main() { ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; for(int tt = 1; tt <= t; tt++) { solve(); } return 0; }
codenet
-1
4,390
s908346503
p04052
1,597,632,040
cpp
Accepted
914
102,040
#include <bits/stdc++.h> #define int long long #define ll long long using ull = unsigned long long; using namespace std; #define dump(x) \ if (dbg) { \ cerr << #x << " = " << (x) << endl; \ } #define overload4(_1, _2, _3, _4, name, ...) name #define FOR1(n) for (ll i = 0; i < (n); ++i) #define FOR2(i, n) for (ll i = 0; i < (n); ++i) #define FOR3(i, a, b) for (ll i = (a); i < (b); ++i) #define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c)) #define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define FORR(i, a, b) for (int i = (a); i <= (b); ++i) #define bit(n, k) ((n >> k) & 1) /*nのk bit目*/ namespace mydef { const int INF = 1ll << 60; const int MOD = 1e9 + 7; template <class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } else return 0; } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } else return 0; } void Yes(bool flag = true) { if (flag) cout << "Yes" << endl; else cout << "No" << endl; } void No(bool flag = true) { Yes(!flag); } void YES(bool flag = true) { if (flag) cout << "YES" << endl; else cout << "NO" << endl; } void NO(bool flag = true) { YES(!flag); } template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T& val) { std::fill((T*)array, (T*)(array + N), val); } bool dbg = false; } // namespace mydef using namespace mydef; #define pb push_back #define mp make_pair #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(), (v).end() #define SZ(x) ((int)(x).size()) #define vi vector<int> #define vvi vector<vector<int>> #define vp vector<pair<int, int>> #define vvp vector<vector<pair<int, int>>> #define pi pair<int, int> //#define P pair<int, int> //#define V vector<int> //#define S set<int> #define asn ans //SegmentTree //How to use //SegmentTree(n, f, M1):= サイズ n の初期化。ここで f は2つの区間の要素をマージする二項演算, M1はモノイドの単位元である。 //set(k , x):= k 番目の要素に x を代入する。 //build():= セグメント木を構築する。 //query(a , b):= 区間 [a,b) に対して二項演算した結果を返す。 //update(k , x):= k 番目の要素を x に変更する。 //operator[k] : = k 番目の要素を返す。 template <typename Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid& x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid& x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int& k) const { return seg[k + sz]; } }; //example /* int main() { int N, Q; scanf("%d %d", &N, &Q); SegmentTree< int > seg(N, [](int a, int b) { return min(a, b); }, INT_MAX); while(Q--) { int T, X, Y; scanf("%d %d %d", &T, &X, &Y); if(T == 0) seg.update(X, Y); else printf("%d\n", seg.query(X, Y + 1)); } } */ int N, K, P[505050]; int seen[505050]; int now = 0; int ans[505050]; using Seg = SegmentTree<int>; void f(Seg& seg, int idx) { while (true) { int MIN = seg.query(0, P[idx]); if (MIN >= idx + K) break; f(seg, MIN); } ans[idx] = now++; seg.update(P[idx], INF); seen[idx] = 1; } void solve() { Seg seg(N, [](int a, int b) { return min(a, b); }, INF); for (int i = 0; i < N; i++) { seg.set(P[i], i); } seg.build(); for (int i = 0; i < N; i++) { if (!seen[i]) f(seg, i); } for (int i = 0; i < N; i++) { cout << ans[i] + 1 << endl; } } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin >> N >> K; for (int i = 0; i < N; i++) { cin >> P[i]; P[i]--; } solve(); return 0; }
codenet
-1
4,495
s216052948
p04052
1,585,906,540
cpp
Accepted
1,751
171,504
#include <iostream> #include <vector> #include <cstdio> #include <algorithm> #include <set> #include <map> #include <cassert> #include <numeric> #include <string> #include <cstring> #include <cmath> #include <queue> using namespace std; #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define eprintf(...) 42 #endif typedef long long int int64; const int N = (int) 5e5 + 100; #ifdef LOCAL const int SZ = N; #else const int SZ = (int) 1e7 + 100; #endif int sz; pair <int, int> edges[SZ]; int l[SZ], r[SZ]; int edSz; //int *degIn = r; int degIn[SZ]; int goodSt[SZ]; int goodStSize; int addV(int v, int tl, int tr, int pos, int val) { if (tr == tl) return val; int cl = -1, cr = -1; if (v != -1) { cl = l[v]; cr = r[v]; } int nv = sz; l[nv] = cl; r[nv] = cr; sz++; int tm = (tl + tr) / 2; if (pos <= tm) l[nv] = addV(cl, tl, tm, pos, val); else r[nv] = addV(cr, tm + 1, tr, pos, val); // for (int to : {l[nv], r[nv]}) // if (to != -1) // edges[edSz++] = make_pair(nv, to); return nv; } void addEdges(int v, int tl, int tr, int sl, int sr, int from) { if (v == -1 || sr < tl || tr < sl) return; //eprintf("addEdges(v = %d, tl = %d, tr = %d, sl = %d, sr = %d, from = %d)\n", v, tl, tr, sl, sr, from); if (sl <= tl && tr <= sr) { edges[edSz++] = make_pair(from, v); return; } int tm = (tl + tr) / 2; addEdges(l[v], tl, tm, sl, sr, from); addEdges(r[v], tm + 1, tr, sl, sr, from); } int p[N], pos[N]; int pans[N]; int main(int, char **) { #ifdef LOCAL freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &p[i]); p[i]--; pos[p[i]] = i; } sz = n; memset(l, -1, sizeof l); memset(r, -1, sizeof r); int root = sz; //eprintf("init end\n"); k--; for (int i = n - 1; i >= -k; i--) { //eprintf("i = %d\n", i); if (i >= 0) { int j = p[i]; root = addV(root, 0, n - 1, j, i); //eprintf("v added\n"); } if (k > 0 && i + k < n) addEdges(root, 0, n - 1, p[i + k] + 1, n - 1, i + k); } for (int i = 0; i < sz; i++) { if (l[i] != -1) degIn[l[i]]++; //edges[edSz++] = make_pair(i, l[i]); if (r[i] != -1) degIn[r[i]]++; //edges[edSz++] = make_pair(i, r[i]); } // memset(r, 0, sizeof r); if (sz > SZ || edSz > 2 * SZ) throw; //eprintf("g built\n"); //for (int i = 0; i < edSz; i++) // swap(edges[i].first, edges[i].second); for (int i = 0; i < edSz; i++) { //eprintf("%d -> %d\n", edges[i].first, edges[i].second); degIn[edges[i].second]++; } sort(edges, edges + edSz); priority_queue <int> good; for (int i = 0; i < sz; i++) if (degIn[i] == 0) { if (i < n) //eprintf("init good = %d\n", i); good.push(-i); else goodSt[goodStSize++] = i; } vector <int> ans; while (!good.empty() || goodStSize) { int v; if (goodStSize) v = goodSt[--goodStSize]; else { v = -good.top(); good.pop(); } // int v = *good.rbegin(); // if (v < n) // v = *good.begin(); // good.erase(v); // eprintf("v = %d\n", v); if (v < n) { ans.push_back(v); } int it = lower_bound(edges, edges + edSz, make_pair(v, -1)) - edges; while (it < edSz && edges[it].first == v) { int to = edges[it++].second; //for (int to : g[v]) if (to != -1) { degIn[to]--; if (degIn[to] == 0) { if (to < n) good.push(-to); else goodSt[goodStSize++] = to; } } } for (int to : {l[v], r[v]}) if (to != -1) { degIn[to]--; if (degIn[to] == 0) { if (to < n) good.push(-to); else goodSt[goodStSize++] = to; } } } //reverse(ans.begin(), ans.end()); for (int i = 0; i < n; i++) pans[ans[i]] = i; for (int i = 0; i < n; i++) printf("%d ", pans[i] + 1); printf("\n"); //for (int x : ans) // printf("%d ", x + 1); //printf("\n"); return 0; }
codenet
-1
4,855
s789733448
p04052
1,587,329,315
cpp
Accepted
1,072
21,760
/* Look at inverse permutation: can make adjacent swaps i, i+1 iff |Q[i]-Q[i+1]| >= K. Then if |Q[i]-Q[j]| < K, we cannot change the relative order. All other permutations are possible: we can always find an adjacent swap and decrease # inversions by 1. (if we cannot swap i, i+1, this means |Q[i]-Q[i+1]| < K, contradiction.) So back to original permutation P, this means we want a lex min labeling such that: - if |i-j| < K, then P[i] < P[j] or P[i] > P[j] needs to hold true. Draw i->j if P[i] < P[j] must be true. We want a lex least topo sort. Algo: find outdeg=0 nodes, and assign rightmost to n. outdeg[i] = 0 means i is a local maximum in P. (P[i] >= P[i-K+1..i+K-1]). Maintain # in window that is > P[i] for each i. */ #include <bits/stdc++.h> using namespace std; //#define cerr if (false) cerr #define db(x) cerr << #x << "=" << x << endl #define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl #define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl #define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& x) { return os << "(" << x.first << "," << x.second << ")"; } typedef long long ll; typedef long double ld; struct SegTree { struct Node { pair<int, int> mn; // Set default value here int prop = 0; // Used for updates and propagation. void apply(int s, int e, int v) { mn.first += v; prop += v; } }; inline Node combine(const Node& a, const Node& b) { Node res; res.mn = min(a.mn, b.mn); return res; } inline void push(int sn, int s, int e) { if (tree[sn].prop) { int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1; tree[lsn].apply(s, m, tree[sn].prop); tree[rsn].apply(m + 1, e, tree[sn].prop); tree[sn].prop = 0; } } int start, end; vector<Node> tree; void build(int sn, int s, int e) { if (s == e) { // initialize here, possibly. tree[sn].mn = {0, -s}; return; } int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1; build(lsn, s, m); build(rsn, m + 1, e); tree[sn] = combine(tree[lsn], tree[rsn]); } template <typename T> void build(int sn, int s, int e, vector<T>& v) { if (s == e) { // tree[sn].x = v[s]; return; } int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1; build(lsn, s, m, v); build(rsn, m + 1, e, v); tree[sn] = combine(tree[lsn], tree[rsn]); } template <typename... T> void update(int sn, int s, int e, int qs, int qe, const T&... v) { if (qs <= s && e <= qe) { tree[sn].apply(s, e, v...); return; } int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1; push(sn, s, e); if (qs <= m) update(lsn, s, m, qs, qe, v...); if (qe > m) update(rsn, m + 1, e, qs, qe, v...); tree[sn] = combine(tree[lsn], tree[rsn]); } Node query(int sn, int s, int e, int qs, int qe) { if (qs <= s && e <= qe) return tree[sn]; int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1; push(sn, s, e); if (qe <= m) return query(lsn, s, m, qs, qe); if (qs > m) return query(rsn, m + 1, e, qs, qe); return combine(query(lsn, s, m, qs, qe), query(rsn, m + 1, e, qs, qe)); } void query(int sn, int s, int e, int qs, int qe, const function<void(Node&, int, int)>& f) { if (qs <= s && e <= qe) return f(tree[sn], s, e); int m = (s + e) >> 1, lsn = sn * 2, rsn = sn * 2 + 1; push(sn, s, e); if (qs <= m) query(lsn, s, m, qs, qe, f); if (qe > m) query(rsn, m + 1, e, qs, qe, f); } SegTree(int n) : SegTree(0, n - 1) {} SegTree(int _start, int _end) : start(_start), end(_end) { int n = end - start + 1; int maxs = n == 1 ? 2 : 1 << (33 - __builtin_clz(n - 1)); tree.resize(maxs); build(1, start, end); } template <typename T> SegTree(vector<T>& v) { int n = v.size(); int maxs = n == 1 ? 2 : 1 << (33 - __builtin_clz(n - 1)); tree.resize(maxs); start = 0; end = n - 1; build(1, start, end, v); } Node query(int qs, int qe) { return query(1, start, end, qs, qe); } Node query(int p) { return query(1, start, end, p, p); } void query(int qs, int qe, const function<void(Node&, int, int)>& f) { if (qs > qe) return; query(1, start, end, qs, qe, f); } template <typename... T> void update(int qs, int qe, const T&... v) { update(1, start, end, qs, qe, v...); } }; const int INF = 1e9; int main() { int n, k; scanf("%d%d", &n, &k); vector<int> P(n), ind(n + 1), ans(n); for (int i = 0; i < n; ++i) { scanf("%d", &P[i]); ind[P[i]] = i; } SegTree st(n); for (int i = 1; i <= n; ++i) { int p = ind[i]; st.update(max(0, p - k + 1), min(n - 1, p + k - 1), 1); st.update(p, p, -st.query(p, p).mn.first); } for (int i = n; i >= 1; --i) { auto x = st.query(0, n - 1).mn; assert(x.first == 0); int p = -x.second; ans[p] = i; st.update(max(0, p - k + 1), min(n - 1, p + k - 1), -1); st.update(p, p, INF); } for (int x : ans) printf("%d\n", x); }
codenet
-1
5,034
s459429556
p04052
1,468,875,927
cpp
Accepted
2,427
33,280
#include<stdio.h> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<bitset> #include<utility> #include<functional> #include<iomanip> #include<sstream> #include<ctime> #include<cassert> using namespace std; #define y0 y0z #define y1 y1z #define yn ynz #define j0 j0z #define j1 j1z #define jn jnz #define tm tmz #define buli(x) (__builtin_popcountll(x)) #define bur0(x) (__builtin_ctzll(x)) #define bul2(x) (63-__builtin_clzll(x)) #define mp make_pair #define pb push_back #define fi first #define se second #define fil(a,b) memset((a),(b),sizeof(a)) #define cl(a) fil(a,0) #define siz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++) #define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--) #define pw(x) ((ll(1))<<(x)) #define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a)) #define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a)) void getre(){int x=0;printf("%d\n",1/x);} void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);} typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<pii> vpii; template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;} template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;} template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;} template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);} template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;} template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);} #if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L) #define lld "%I64d" #else #define lld "%lld" #endif inline void gn(long long&x){ int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0'); while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg; } inline void gn(int&x){long long t;gn(t);x=t;} inline void gn(unsigned long long&x){long long t;gn(t);x=t;} inline void gn(double&x){double t;scanf("%lf",&t);x=t;} inline void gn(long double&x){double t;scanf("%lf",&t);x=t;} inline void gs(char *s){scanf("%s",s);} inline void gc(char &c){while((c=getchar())>126 || c<33);} inline void pc(char c){putchar(c);} #ifdef JCVB #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif typedef long long ll; typedef double db; inline ll sqr(ll a){return a*a;} inline db sqrf(db a){return a*a;} const int inf=0x3f3f3f3f; const db pi=3.14159265358979323846264338327950288L; const db eps=1e-6; //const int mo=0; //int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;} int n; int a[555555]; int b[555555]; // single point update, range query // index 1..n // first call seginit(n) typedef int seg_nu; typedef int seg_tag; const int SEG_MAXN=500000+5; seg_nu seg[SEG_MAXN*4]; inline void segpu(int x){ seg[x]=min(seg[x<<1],seg[x<<1|1]); //modify } void seginit_in(int l,int r,int x){ if(l==r){ //seg[x]=a[l]; seg[x]=inf; //modify }else{ int mid=l+r>>1; seginit_in(l,mid,x<<1); seginit_in(mid+1,r,x<<1|1); segpu(x); } } int l1,r1,I; seg_nu sans; bool ans_bo; seg_tag stag; void segupd_in(int l,int r,int x){ if(l==r){ seg[x]=stag; //modify }else{ int mid=l+r>>1; if(I<=mid)segupd_in(l,mid,x<<1); else segupd_in(mid+1,r,x<<1|1); segpu(x); } } void segque_in(int l,int r,int x){ //if(l1>r1)return; if(l1<=l && r<=r1){ if(!ans_bo)ans_bo=1,sans=seg[x]; else sans=min(sans,seg[x]); //modify }else{ int mid=l+r>>1; if(l1<=mid)segque_in(l,mid,x<<1); if(r1>mid)segque_in(mid+1,r,x<<1|1); } } int segn; void segupd(int i,seg_tag v){ stag=v,I=i; segupd_in(1,segn,1); } seg_nu segque(int l,int r){ if(l>r)return inf; //modify ans_bo=0,l1=l,r1=r; segque_in(1,segn,1); return sans; } void seginit(int n){ segn=n; seginit_in(1,segn,1); } struct edge{ int v,next; }e[2222222];int g[555555];int etot=0; int inde[555555]; void ae(int u,int v){ e[etot].v=v;e[etot].next=g[u];g[u]=etot++; inde[v]++; } priority_queue<int> qu; int ans[555555]; int main() { int k; #ifdef JCVB //freopen("1.in","r",stdin); //freopen("1.out","w",stdout); int _time_jc=clock(); #endif fil(g,-1); gn(n);gn(k); rep(i,1,n+1){ gn(a[i]); b[a[i]]=i; } seginit(n); rep(i,1,n+1){ segupd(a[i],a[i]); if(i>k)segupd(a[i-k],inf); int t=segque(a[i]+1,n); if(t!=inf){ ae(b[t],i); } } seginit(n); rep(i,1,n+1){ segupd(a[i],-a[i]); if(i>k)segupd(a[i-k],inf); int t=segque(1,a[i]-1); if(t!=inf){ ae(i,b[-t]); } } seginit(n); per(i,1,n+1){ segupd(a[i],a[i]); if(i+k<=n)segupd(a[i+k],inf); int t=segque(a[i]+1,n); if(t!=inf){ ae(b[t],i); } } seginit(n); per(i,1,n+1){ segupd(a[i],-a[i]); if(i+k<=n)segupd(a[i+k],inf); int t=segque(1,a[i]-1); if(t!=inf){ ae(i,b[-t]); } } rep(i,1,n+1)if(inde[i]==0)qu.push(i); int too=n; while(!qu.empty()){ int x=qu.top();qu.pop(); ans[x]=too--; for (int i=g[x];~i;i=e[i].next)if((--inde[e[i].v])==0){ qu.push(e[i].v); } } rep(i,1,n+1)printf("%d\n",ans[i]); #ifdef JCVB debug("time: %d\n",int(clock()-_time_jc)); #endif return 0; }
codenet
-1
5,305
s538763121
p04052
1,508,529,272
cpp
Accepted
1,265
70,272
#ifdef DEBUG #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <cassert> #include <sstream> #include <fstream> #include <functional> #include <set> #include <bitset> #include <string> #include <utility> #include <queue> #include <deque> #include <vector> #include <map> #else #include <bits/stdc++.h> #endif #ifdef DEBUG #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif #define rep(i, n) for (int i = 0, i##_end_ = (n); i < i##_end_; ++i) #define per(i, n) for (int i = (n) - 1; i >= 0; --i) #define forn(i, l, r) for (int i = (l), i##_end_ = (r); i <= i##_end_; ++i) #define nrof(i, r, l) for (int i = (r), i##_end_ = (l); i >= i##_end_; --i) #define X first #define Y second #define mp make_pair #define pb push_back #define SZ(x) (int)((x).size()) #define ALL(x) (x).begin(), (x).end() using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long LL; template<typename T> inline bool chkmax(T &x, const T &y) { return x < y ? x = y, 1 : 0; } template<typename T> inline bool chkmin(T &x, const T &y) { return x > y ? x = y, 1 : 0; } #ifdef DEBUG char *input_file, *output_file; #endif struct IO { static const int maxn = (1 << 25) + 10; char a[maxn], *s, b[maxn], *t; void INPUT() { s = a; t = b; #ifdef DEBUG FILE *f = fopen(input_file, "r"); a[fread(a, 1, sizeof a, f)] = 0; #else a[fread(a, 1, sizeof a, stdin)] = 0; #endif } void OUTPUT() { #ifdef DEBUG FILE *f = fopen(output_file, "w"); fwrite(b, 1, t - b, f); #else fwrite(b, 1, t - b, stdout); #endif } operator int() { int x = 0; while(*s != '-' && (*s < '0' || *s > '9')) { ++s; } bool f = 0; if(*s == '-') { f = 1; ++s; } while(*s >= '0' && *s <= '9') { (x *= 10) += *s - '0'; ++s; } if(f) { x = -x; } return x; } operator LL() { LL x = 0; while(*s != '-' && (*s < '0' || *s > '9')) { ++s; } bool f = 0; if(*s == '-') { f = 1; ++s; } while(*s >= '0' && *s <= '9') { (x *= 10) += *s - '0'; ++s; } if(f) { x = -x; } return x; } operator char() { while(*s <= 32) { ++s; } char ret = *s; ++s; return ret; } inline void out(int x) { if(!x) { *t++ = '0'; return; } if(x < 0) { *t++ = '-'; x = -x; } static char c[20], *i; i = c; while(x) { int y = x / 10; *i++ = x - y * 10 + '0'; x = y; } while(i != c) { *t++ = *--i; } return; } inline void out(int x, char C) { if(!x) { *t++ = '0'; *t++ = C; return; } if(x < 0) { *t++ = '-'; x = -x; } static char c[20], *i; i = c; while(x) { int y = x / 10; *i++ = x - y * 10 + '0'; x = y; } while(i != c) { *t++ = *--i; } *t++ = C; return; } inline void out(LL x) { if(!x) { *t++ = '0'; return; } if(x < 0) { *t++ = '-'; x = -x; } static char c[20], *i; i = c; while(x) { LL y = x / 10; *i++ = x - y * 10 + '0'; x = y; } while(i != c) { *t++ = *--i; } return; } inline void out(LL x, char C) { if(!x) { *t++ = '0'; *t++ = C; return; } if(x < 0) { *t++ = '-'; x = -x; } static char c[20], *i; i = c; while(x) { LL y = x / 10; *i++ = x - y * 10 + '0'; x = y; } while(i != c) { *t++ = *--i; } *t++ = C; return; } inline void out(char c) { *t++ = c; return; } inline void out(char *s) { while(*s >= ' ') { *t++ = *s++; } return; } }io; void Main(); int main(int argc, char *argv[]) { #ifdef DEBUG input_file = argv[1]; output_file = argv[2]; #endif io.INPUT(); Main(); io.OUTPUT(); return 0; } //---------------------------------------------------------------------------------------head--------------------------------------------------------------------------------------- int n, k, *p, *ans; void update(int &x, int y) { x = (x == n || y < n && p[y] < p[x]) ? y : x; return; } struct node { node *ch[2]; int num; node() { memset(ch, 0, sizeof num); } void pull() { num = n; rep(i, 2) { update(num, ch[i]->num); } return; } }*root; int seg_a, seg_b, seg_q; set<int> heap; void B(node* &u = root, int l = 0, int r = n - 1) { u = new node; if(l == r) { u->num = l; return; } int mid = l + r >> 1; B(u->ch[0], l, mid); B(u->ch[1], mid + 1, r); return u->pull(); } void M(node* &u = root, int l = 0, int r = n - 1) { if(l == r) { u->num = seg_q; return; } int mid = l + r >> 1; if(seg_a <= mid) { M(u->ch[0], l, mid); } else { M(u->ch[1], mid + 1, r); } return u->pull(); } void Q(node* &u = root, int l = 0, int r = n - 1) { if(seg_a <= l && r <= seg_b) { update(seg_q, u->num); return; } int mid = l + r >> 1; if(seg_a <= mid) { Q(u->ch[0], l, mid); } if(mid < seg_b) { Q(u->ch[1], mid + 1, r); } return; } void Main() { n = io; k = io; p = new int[n]; ans = new int[n]; rep(i, n) { p[i] = io; } B(); for (int i = 0; i < n; i += k) { seg_a = i, seg_b = min(i + k, n) - 1; seg_q = n; Q(); if(seg_q < n) { int i = seg_q; seg_a = max(0, i - k + 1), seg_b = min(n - 1, i + k - 1); seg_q = n; Q(); if(i == seg_q) { heap.insert(i); } } } int cnt = 0; while(SZ(heap)) { int i = *heap.begin(); ans[i] = ++cnt; heap.erase(heap.begin()); seg_a = i; seg_q = n; M(); i = i / k * k; seg_a = i, seg_b = min(i + k, n) - 1; seg_q = n; Q(); if(seg_q < n) { int i = seg_q; seg_a = max(0, i - k + 1), seg_b = min(n - 1, i + k - 1); seg_q = n; Q(); if(i == seg_q) { heap.insert(i); } } i -= k; if(i >= 0) { seg_a = i, seg_b = min(i + k, n) - 1; seg_q = n; Q(); if(seg_q < n) { int i = seg_q; seg_a = max(0, i - k + 1), seg_b = min(n - 1, i + k - 1); seg_q = n; Q(); if(i == seg_q) { heap.insert(i); } } } i += k + k; if(i >= 0 && i < n) { seg_a = i, seg_b = min(i + k, n) - 1; seg_q = n; Q(); if(seg_q < n) { int i = seg_q; seg_a = max(0, i - k + 1), seg_b = min(n - 1, i + k - 1); seg_q = n; Q(); if(i == seg_q) { heap.insert(i); } } } } rep(i, n) { io.out(ans[i], '\n'); } return; }
codenet
-1
6,221
s182723715
p04052
1,561,821,205
cpp
Accepted
500
17,904
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) for (int i = 0; i < (a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a, x) for (auto& a : x) #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rsz resize const int MOD = 1000000007; // 998244353 const ll INF = 1e18; const int MX = 1<<19; const ld PI = 4*atan((ld)1); template<class T> void ckmin(T &a, T b) { a = min(a, b); } template<class T> void ckmax(T &a, T b) { a = max(a, b); } namespace input { template<class T> void re(complex<T>& x); template<class T1, class T2> void re(pair<T1,T2>& p); template<class T> void re(vector<T>& a); template<class T, size_t SZ> void re(array<T,SZ>& a); template<class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } void re(ld& x) { string t; re(t); x = stold(t); } template<class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); } template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); } template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); } template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); } } using namespace input; namespace output { template<class T1, class T2> void pr(const pair<T1,T2>& x); template<class T, size_t SZ> void pr(const array<T,SZ>& x); template<class T> void pr(const vector<T>& x); template<class T> void pr(const set<T>& x); template<class T1, class T2> void pr(const map<T1,T2>& x); template<class T> void pr(const T& x) { cout << x; } template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template<class T1, class T2> void pr(const pair<T1,T2>& x) { pr("{",x.f,", ",x.s,"}"); } template<class T> void prContain(const T& x) { pr("{"); bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool> pr("}"); } template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); } template<class T> void pr(const vector<T>& x) { prContain(x); } template<class T> void pr(const set<T>& x) { prContain(x); } template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); } void ps() { pr("\n"); } template<class Arg> void ps(const Arg& first) { pr(first); ps(); // no space at end of line } template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first," "); ps(rest...); // print w/ spaces } } using namespace output; namespace io { void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO } } using namespace io; template<class T> T invGeneral(T a, T b) { a %= b; if (a == 0) return b == 1 ? 0 : -1; T x = invGeneral(b,a); return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b; } template<class T> struct modular { T val; explicit operator T() const { return val; } modular() { val = 0; } template<class U> modular(const U& v) { val = (-MOD <= v && v <= MOD) ? v : v % MOD; if (val < 0) val += MOD; } friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; } friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; } friend bool operator!=(const modular& a, const modular& b) { return !(a == b); } modular operator-() const { return modular(-val); } modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; } modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; } modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; } friend modular exp(modular a, ll p) { modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend modular inv(const modular& a) { auto i = invGeneral(a.val,MOD); assert(i != -1); return i; } // equivalent to return exp(b,MOD-2) if MOD is prime modular& operator/=(const modular& m) { return (*this) *= inv(m); } friend modular operator+(modular a, const modular& b) { return a += b; } friend modular operator-(modular a, const modular& b) { return a -= b; } friend modular operator*(modular a, const modular& b) { return a *= b; } friend modular operator/(modular a, const modular& b) { return a /= b; } }; typedef modular<int> mi; typedef pair<mi,mi> pmi; typedef vector<mi> vmi; typedef vector<pmi> vpmi; int N,K,P[MX],Q[MX],ans[MX],ret[MX]; bool used[MX]; template<class T, int SZ> struct Seg { // SZ should be power of 2 T seg[2*SZ], ID = 0; Seg() { memset(seg,0,sizeof seg); } T comb(T a, T b) { return Q[a] > Q[b] ? a : b; } // easily change this to min or max // comb(ID,b) must equal b void build() { F0Rd(i,SZ) seg[i] = comb(seg[2*i],seg[2*i+1]); } void upd(int p) { // set value at position p int pos = p+SZ; for (seg[pos] = p; pos > 1; pos >>= 1) seg[pos>>1] = comb(seg[(pos|1)^1],seg[pos|1]); // make sure non-commutative operations work } T query(int l, int r) { // sum on interval [l, r] T res1 = ID, res2 = ID; r++; for (l += SZ, r += SZ; l < r; l >>= 1, r >>= 1) { if (l&1) res1 = comb(res1,seg[l++]); if (r&1) res2 = comb(seg[--r],res2); } return comb(res1,res2); } }; Seg<int,1<<19> S; priority_queue<int> pq; void test(int ind) { int l = max(1,ind-K+1), r = min(N,ind+K-1); // ps("TEST",ind); if (S.query(l,r) == ind) { pq.push(ind); // ps("HA",ind); } } void recalc(int x) { int l = (x-1)*K+1, r = x*K; if (r < 1 || l > N) return; ckmin(r,N); // ps("OOPS",x,l,r); int ind = S.query(l,r); if (Q[ind] <= 0) return; test(ind); } int main() { setIO(); re(N,K); FOR(i,1,N+1) re(Q[i]); FOR(i,1,N+1) S.upd(i); /*FOR(i,1,N+1) ps(i,Q[i]); Q[1] = -MOD; S.upd(1); ps(S.query(1,N)); exit(0);*/ for (int i = K; ; i += K) { recalc(i/K); if (i >= N) break; } /*FOR(i,1,N+1) ps(i,Q[i]); exit(0);*/ // ps(sz(pq),pq.top()); exit(0); FORd(i,1,N+1) { if (!sz(pq)) { ps("??",i); exit(0); } while (used[pq.top()]) pq.pop(); int x = pq.top(); pq.pop(); used[x] = 1; Q[x] = -MOD; S.upd(x); ans[i] = x; // ps("WUT",i,x); recalc((x+K-1)/K-1), recalc((x+K-1)/K), recalc((x+K-1)/K+1); } FOR(i,1,N+1) ret[ans[i]] = i; FOR(i,1,N+1) ps(ret[i]); } /* stuff you should look for * int overflow, array bounds * special cases (n=1?), set tle * do smth instead of nothing and stay organized */
codenet
-1
8,000
s512166186
p04052
1,580,027,572
cpp
Accepted
929
38,784
/** * author: tourist * created: 25.01.2020 21:28:03 **/ #include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif mt19937 rng(58); class node { public: int id; node* l; node* r; node* p; bool rev; int sz; // declare extra variables: int P; int mn; node(int _id) { id = _id; l = r = p = nullptr; rev = false; sz = 1; // init extra variables: P = rng(); mn = id; } void unsafe_reverse() { rev ^= 1; swap(l, r); pull(); } // apply changes: void unsafe_apply() { } void push() { if (rev) { if (l != nullptr) { l->unsafe_reverse(); } if (r != nullptr) { r->unsafe_reverse(); } rev = 0; } // now push everything else: } void pull() { sz = 1; // now init from self: mn = id; if (l != nullptr) { l->p = this; sz += l->sz; // now pull from l: mn = min(mn, l->mn); } if (r != nullptr) { r->p = this; sz += r->sz; // now pull from r: mn = min(mn, r->mn); } } }; void debug_node(node* v, string pref = "") { #ifdef LOCAL if (v != nullptr) { debug_node(v->r, pref + " "); cerr << pref << "-" << " " << v->id << '\n'; debug_node(v->l, pref + " "); } else { cerr << pref << "-" << " " << "nullptr" << '\n'; } #endif } namespace treap { pair<node*, int> find(node* v, const function<int(node*)> &go_to) { // go_to returns: 0 -- found; -1 -- go left; 1 -- go right // find returns the last vertex on the descent and its go_to if (v == nullptr) { return {nullptr, 0}; } int dir; while (true) { v->push(); dir = go_to(v); if (dir == 0) { break; } node* u = (dir == -1 ? v->l : v->r); if (u == nullptr) { break; } v = u; } return {v, dir}; } node* get_leftmost(node* v) { return find(v, [&](node*) { return -1; }).first; } node* get_rightmost(node* v) { return find(v, [&](node*) { return 1; }).first; } node* get_kth(node* v, int k) { // 0-indexed pair<node*, int> p = find(v, [&](node* u) { if (u->l != nullptr) { if (u->l->sz > k) { return -1; } k -= u->l->sz; } if (k == 0) { return 0; } k--; return 1; }); return (p.second == 0 ? p.first : nullptr); } int get_position(node* v) { // 0-indexed int k = (v->l != nullptr ? v->l->sz : 0); while (v->p != nullptr) { if (v == v->p->r) { k++; if (v->p->l != nullptr) { k += v->p->l->sz; } } v = v->p; } return k; } node* get_bst_root(node* v) { while (v->p != nullptr) { v = v->p; } return v; } pair<node*, node*> split(node* v, const function<bool(node*)> &is_right) { if (v == nullptr) { return {nullptr, nullptr}; } v->push(); if (is_right(v)) { pair<node*, node*> p = split(v->l, is_right); if (p.first != nullptr) { p.first->p = nullptr; } v->l = p.second; v->pull(); return {p.first, v}; } else { pair<node*, node*> p = split(v->r, is_right); v->r = p.first; if (p.second != nullptr) { p.second->p = nullptr; } v->pull(); return {v, p.second}; } } pair<node*, node*> split_leftmost_k(node* v, int k) { return split(v, [&](node* u) { int left_and_me = (u->l != nullptr ? u->l->sz : 0) + 1; if (k >= left_and_me) { k -= left_and_me; return false; } return true; }); } node* merge(node* v, node* u) { if (v == nullptr) { return u; } if (u == nullptr) { return v; } if (v->P > u->P) { // if (rng() % (v->sz + u->sz) < (unsigned int) v->sz) { v->push(); v->r = merge(v->r, u); v->pull(); return v; } else { u->push(); u->l = merge(v, u->l); u->pull(); return u; } } int count_left(node* v, const function<bool(node*)> &is_right) { if (v == nullptr) { return 0; } v->push(); if (is_right(v)) { return count_left(v->l, is_right); } return (v->l != nullptr ? v->l->sz : 0) + 1 + count_left(v->r, is_right); } node* add(node* r, node* v, const function<bool(node*)> &go_left) { pair<node*, node*> p = split(r, go_left); return merge(p.first, merge(v, p.second)); } node* remove(node* v) { // returns the new root v->push(); node* x = v->l; node* y = v->r; node* p = v->p; v->l = v->r = v->p = nullptr; v->push(); v->pull(); // now v might be reusable... node* z = merge(x, y); if (p == nullptr) { if (z != nullptr) { z->p = nullptr; } return z; } if (p->l == v) { p->l = z; } if (p->r == v) { p->r = z; } while (true) { p->push(); p->pull(); if (p->p == nullptr) { break; } p = p->p; } return p; } node* next(node* v) { if (v->r == nullptr) { while (v->p != nullptr && v->p->r == v) { v = v->p; } return v->p; } v->push(); v = v->r; while (v->l != nullptr) { v->push(); v = v->l; } return v; } node* prev(node* v) { if (v->l == nullptr) { while (v->p != nullptr && v->p->l == v) { v = v->p; } return v->p; } v->push(); v = v->l; while (v->r != nullptr) { v->push(); v = v->r; } return v; } int get_size(node* v) { return (v != nullptr ? v->sz : 0); } template<typename... T> void apply(node* v, T... args) { v->unsafe_apply(args...); } void reverse(node* v) { v->unsafe_reverse(); } } // namespace treap using namespace treap; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> p(n); for (int i = 0; i < n; i++) { int x; cin >> x; --x; p[x] = i; } node* r = nullptr; for (int i = 0; i < n; i++) { auto q = split(r, [&](node* nd) { return nd->id >= p[i] + k && (nd->r == nullptr || nd->r->mn >= p[i] + k); }); r = merge(q.first, merge(new node(p[i]), q.second)); } vector<int> res(n); r = get_leftmost(r); for (int i = 0; i < n; i++) { res[r->id] = i; r = next(r); } for (int i = 0; i < n; i++) { cout << res[i] + 1 << '\n'; } return 0; }
codenet
-1
8,082
s584467211
p04052
1,489,640,086
csharp
Accepted
1,051
93,740
using System; using System.Linq; using System.Collections.Generic; using Debug = System.Diagnostics.Debug; using StringBuilder = System.Text.StringBuilder; using Number = System.Int64; namespace Program { public class Solver { public void Solve() { var n = sc.Integer(); var k = sc.Integer(); var p = Enumerate(n, x => sc.Integer()); var q = new int[n]; for (int i = 0; i < n; i++) q[p[i] - 1] = i; var G = Enumerate(n, x => new List<int>()); var bit = new FenwickTree(n); for (int i = n - 1, j = n - 1; i >= 0; i--) { while (j >= 0 && i - j < k) { bit.Add(p[j], 1); j--; } var pos = p[i]; var sum = bit[pos]; var next = bit.LowerBound(sum + 1); if (next <= n) G[i].Add(q[next - 1]); var prev = bit.LowerBound(sum - 1); if (prev > 0 && prev < pos) G[q[prev - 1]].Add(i); bit.Add(pos, -1); } var ord = Graph.TopologicalSort(G, true); var ans = new int[n]; for (int i = 0; i < n; i++) ans[ord[i]] = i + 1; foreach (var x in ans) IO.Printer.Out.WriteLine(x); } public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput()); static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; } static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; } } } #region main static class Ex { static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); } static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); } static public void Main() { var solver = new Program.Solver(); solver.Solve(); Program.IO.Printer.Out.Flush(); } } #endregion #region Ex namespace Program.IO { using System.IO; using System.Text; using System.Globalization; public class Printer: StreamWriter { static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; } public static Printer Out { get; set; } public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } } public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { } public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { } public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); } public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); } } public class StreamScanner { public StreamScanner(Stream stream) { str = stream; } public readonly Stream str; private readonly byte[] buf = new byte[1024]; private int len, ptr; public bool isEof = false; public bool IsEndOfStream { get { return isEof; } } private byte read() { if (isEof) return 0; if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } } return buf[ptr++]; } public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; } public string Scan() { var sb = new StringBuilder(); for (var b = Char(); b >= 33 && b <= 126; b = (char)read()) sb.Append(b); return sb.ToString(); } public string ScanLine() { var sb = new StringBuilder(); for (var b = Char(); b != '\n'; b = (char)read()) if (b == 0) break; else if (b != '\r') sb.Append(b); return sb.ToString(); } public long Long() { if (isEof) return long.MinValue; long ret = 0; byte b = 0; var ng = false; do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b)); if (b == 0) return long.MinValue; if (b == '-') { ng = true; b = read(); } for (; true; b = read()) { if (b < '0' || '9' < b) return ng ? -ret : ret; else ret = ret * 10 + b - '0'; } } public int Integer() { return (isEof) ? int.MinValue : (int)Long(); } public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; } private T[] enumerate<T>(int n, Func<T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(); return a; } public char[] Char(int n) { return enumerate(n, Char); } public string[] Scan(int n) { return enumerate(n, Scan); } public double[] Double(int n) { return enumerate(n, Double); } public int[] Integer(int n) { return enumerate(n, Integer); } public long[] Long(int n) { return enumerate(n, Long); } } } #endregion #region FenwickTree public class FenwickTree { int n; Number[] bit; int max = 1; public FenwickTree(int size) { n = size; bit = new Number[n + 1]; while ((max << 1) <= n) max <<= 1; } /// <summary>sum[a,b]</summary> public Number this[int i, int j] { get { return this[j] - this[i - 1]; } } /// <summary>sum[0,i]</summary> public Number this[int i] { get { Number s = 0; for (; i > 0; i -= i & -i) s += bit[i]; return s; } } public int LowerBound(Number w) { if (w <= 0) return 0; int x = 0; for (int k = max; k > 0; k >>= 1) if (x + k <= n && bit[x + k] < w) { w -= bit[x + k]; x += k; } return x + 1; } /// <summary>add v to bit[i]</summary> public void Add(int i, Number v) { if (i == 0) System.Diagnostics.Debug.Fail("BIT is 1 indexed"); for (; i <= n; i += i & -i) bit[i] += v; } public Number[] Items { get { var ret = new Number[n + 1]; for (int i = 0; i < ret.Length; i++) ret[i] = this[i, i]; return ret; } } } #endregion #region TopologicalSort static partial class Graph { //順序をいじりたい場合はQueueから変える //使わない奴がある場合はcountのその位置に予め-1を入れて渡す static public int[] TopologicalSort(List<int>[] G, bool isdirected, int[] count = null) { int Capacity = isdirected ? 0 : 1;//有向ならば0無向ならば1 var n = G.Length; if (count == null) { count = new int[n]; foreach (var l in G) foreach (var x in l) count[x]++; } var ret = new List<int>(); var q = new PriorityQueue<int>(); for (int i = 0; i < n; i++) if (count[i] == Capacity) q.Enqueue(i); while (q.Count > 0) { var p = q.Dequeue(); ret.Add(p); foreach (var x in G[p]) if (--count[x] == Capacity) q.Enqueue(x); } return ret.ToArray(); } } #endregion #region BinaryHeap public class PriorityQueue<T> { readonly List<T> heap = new List<T>(); readonly Comparison<T> cmp; public PriorityQueue() : this(Comparer<T>.Default) { } public PriorityQueue(IComparer<T> comparer) : this(comparer.Compare) { } public PriorityQueue(Comparison<T> comparison) { cmp = comparison; } public void Enqueue(T item) { var i = heap.Count; heap.Add(item); while (i > 0) { var p = (i - 1) / 2; if (cmp(heap[p], item) <= 0) break; heap[i] = heap[p]; i = p; } heap[i] = item; } public T Dequeue() { var ret = heap[0]; var i = 0; var x = heap[heap.Count - 1]; while ((i * 2) + 1 < heap.Count - 1) { var a = i * 2 + 1; var b = i * 2 + 2; if (b < heap.Count - 1 && cmp(heap[b], heap[a]) < 0) a = b; if (cmp(heap[a], x) >= 0) break; heap[i] = heap[a]; i = a; } heap[i] = x; heap.RemoveAt(heap.Count - 1); return ret; } public T Peek() { return heap[0]; } public int Count { get { return heap.Count; } } public bool Any() { return heap.Count > 0; } public T[] Items { get { var ret = heap.ToArray(); Array.Sort(ret, cmp); return ret; } } } #endregion
codenet
-1
9,006
s508270000
p04052
1,483,425,521
java
Accepted
4,394
173,360
import java.util.*; public class Main { static boolean[] vis; static SegTree seg; static int n, k; static int idx = 0; static int[] ans; static int[] p; static int[] q; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); k = sc.nextInt(); p = new int[n]; vis = new boolean[n]; ans = new int[n]; for (int i = 0; i < n; ++i) { p[i] = sc.nextInt() - 1; } q = new int[n]; for (int i = 0; i < n; ++i) { q[p[i]] = i; } seg = new SegTree(n); for (int i = 0; i < n; ++i) { seg.val[i + seg.n - 1] = q[i]; } for (int i = seg.n - 2; i >= 0; --i) { seg.val[i] = Math.min(seg.val[2 * i + 1], seg.val[2 * i + 2]); } for (int i = 0; i < n; ++i) { if (!vis[i]) { dfs(i); } } StringBuilder sb = new StringBuilder(); int[] t = new int[n]; for (int i = 0; i < n; ++i) { t[ans[i]] = i; } ans = Arrays.copyOf(t, t.length); for (int i : ans) { sb.append((i + 1) + "\n"); } System.out.println(sb); } static void dfs(int i) { while (true) { int min = seg.query(0, p[i]); if (min < i + k) { dfs(min); } else break; } ans[idx++] = i; vis[i] = true; seg.setVal(p[i], Integer.MAX_VALUE / 10); } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } static class SegTree { int n = 1; int[] val; public SegTree(int n_) { while (n < n_) { n *= 2; } val = new int[2 * n - 1]; } void setVal(int k, int v) { k += n - 1; val[k] = v; while (k > 0) { k = (k - 1) / 2; val[k] = Math.min(val[2 * k + 1], val[2 * k + 2]); } } int query(int a, int b) { return query(a, b, 0, n, 0); } int query(int a, int b, int l, int r, int k) { if (a <= l && r <= b) { return val[k]; } else if (r <= a || b <= l) { return Integer.MAX_VALUE / 10; } else { int vl = query(a, b, l, (l + r) / 2, 2 * k + 1); int vr = query(a, b, (l + r) / 2, r, 2 * k + 2); return Math.min(vl, vr); } } } }
codenet
-1
2,032
s289085063
p04052
1,510,877,893
java
Accepted
2,438
126,888
import java.util.*; import java.io.*; public class Main { static void checker(boolean x) throws Exception { if(!x) { Exception e = new Exception(); throw e; } } static int [] t = new int [500010 * 4]; static int [] a = new int [500010]; static int [] p = new int [500010]; static int n, k; final static int inf = 100000000; static void build(int c, int b, int e) { if(b == e) { t[c] = a[b]; return ; } int m = (b + e) >> 1; int l = c << 1; int r = l + 1; build(l, b, m); build(r, m+1, e); t[c] = Math.min(t[l], t[r]); } static void update(int x, int val, int c, int b, int e) { if(b == e) { t[c] = val; return ; } int m = (b + e) >> 1; int l = c << 1; int r = l + 1; if(x <= m) update(x, val, l, b, m); else update(x, val, r, m +1, e); t[c] = Math.min(t[l], t[r]); } static int query(int x, int y, int c, int b, int e) { if(x <= b && e <= y) { return t[c]; } if(y < b || e < x) return inf; int m = (b + e) >> 1; int l = c << 1; int r = l + 1; return Math.min(query(x, y, l, b, m), query(x, y, r, m+1, e)); } static int query(int x, int y) { return query(x, y, 1, 1, n); } static void update(int x, int val) { update(x, val, 1, 1, n); } static int left(int block) { return block * k + 1; } static int right(int block) { return Math.min(n, block * k + k); } static int idx(int pos) { return (pos - 1) / k; } static boolean check(int pos) { return query(Math.max(1, pos - k + 1), Math.min(n, pos + k - 1)) == a[pos]; } static int candidate(int b) { int x = query(left(b), right(b)); if(x == inf) return -1; else return p[x]; } public static void main(String [] args) throws Exception { Reader in = new Reader (); Writer out = new Writer (); n = in.nextInt(); k = in.nextInt(); int [] deg = new int [n + 5]; for(int i = 1; i <= n; i++) { a[i] = in.nextInt(); p[a[i]] = i; } build(1, 1, n); TreeSet <Integer> foo = new TreeSet <> (); for(int i = 0; i <= idx(n); i++) { int can = candidate(i); if(can != -1 && check(can)) { foo.add(can); } } int pointer = 0; int ans [] = new int [n + 5]; while(!foo.isEmpty()) { int x = foo.first(); foo.remove(x); update(x, inf); for(int i = idx(x) - 1; i <= idx(x) + 1; i++) { if(0 <= i && i <= idx(n)) { int can = candidate(i); if(can != -1 && check(can)) { foo.add(can); } } } ans[x] = ++pointer; } for(int i = 1; i <= n; i++) { out.writeln(ans[i]); } out.flush(); } static class Reader { private StringTokenizer a; private BufferedReader b; Reader () { a = null; b = new BufferedReader (new InputStreamReader (System.in)); } public String next () { while(a == null || !a.hasMoreTokens()) { try { a = new StringTokenizer (b.readLine()); } catch (IOException e) { e.printStackTrace(); } } return a.nextToken(); } public int nextInt() { return Integer.parseInt(this.next()); } public long nextLong () { return Long.parseLong(this.next()); } public double nextDouble () { return Double.parseDouble(this.next()); } public String nextLine() { try { return b.readLine(); } catch (IOException e) { e.printStackTrace(); } return ""; } } static class Writer { private PrintWriter a; private StringBuffer b; Writer () { a = new PrintWriter (System.out); b = new StringBuffer (""); } public void write (Object s) { b.append(s); } public void writeln(Object s) { b.append(s).append('\n'); } public void flush () { a.print(b); a.flush(); a.close(); } } static class Pair implements Comparator <Pair> { int first; int second; Pair (int a, int b) { this.first = a; this.second = b; } Pair (Pair a) { this.first = a.first; this.second = a.second; } Pair () {} public String toString () { return "[" + first + ", " + second + "]"; } public int compare (Pair a, Pair b) { if(a.first == b.first) { return a.second - b.second; } else { return a.first - b.first; } } } }
codenet
-1
5,362
s670377825
p04052
1,468,737,546
java
Accepted
1,569
42,200
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(), K = ni(); int[] a = na(n); for(int i = 0;i < n;i++)a[i]--; int[] ia = new int[n]; for(int i = 0;i < n;i++){ ia[a[i]] = i; } int[] indeg = new int[n]; int[] ft = new int[n+3]; for(int i = 0;i < n;i++){ indeg[ia[i]] = sumFenwick(ft, Math.min(n-1, ia[i]+K-1)) - sumFenwick(ft, ia[i]-K); addFenwick(ft, ia[i], 1); } StarrySkyTreePos sst = new StarrySkyTreePos(indeg); int[] ord = new int[n]; for(int i = 0;i < n;i++){ int argmin = sst.pos[1]; ord[argmin] = i; sst.add(argmin, argmin + 1, 99999999); sst.add(Math.max(0, argmin-K+1), Math.min(argmin+K-1, n-1)+1, -1); } for(int i = 0;i < n;i++){ out.println(ord[i] + 1); } } public static class StarrySkyTreePos { public int M, H, N; public int[] st; public int[] plus; public int[] pos; public int I = Integer.MAX_VALUE/4; // I+plus<int public StarrySkyTreePos(int n) { N = n; M = Integer.highestOneBit(Math.max(n-1, 1))<<2; H = M>>>1; st = new int[M]; plus = new int[H]; pos = new int[M]; for(int i = M-1;i >= H;i--)pos[i] = i-H; for(int i = H-1;i >= 0;i--)propagate(i); } public StarrySkyTreePos(int[] a) { N = a.length; M = Integer.highestOneBit(Math.max(N-1, 1))<<2; H = M>>>1; st = new int[M]; for(int i = 0;i < N;i++){ st[H+i] = a[i]; } plus = new int[H]; Arrays.fill(st, H+N, M, I); pos = new int[M]; for(int i = M-1;i >= H;i--)pos[i] = i-H; for(int i = H-1;i >= 1;i--)propagate(i); } private void propagate(int i) { if(st[2*i] <= st[2*i+1]){ st[i] = st[2*i] + plus[i]; pos[i] = pos[2*i]; }else{ st[i] = st[2*i+1] + plus[i]; pos[i] = pos[2*i+1]; } } public void add(int l, int r, int v){ if(l < r)add(l, r, v, 0, H, 1); } private void add(int l, int r, int v, int cl, int cr, int cur) { if(l <= cl && cr <= r){ if(cur >= H){ st[cur] += v; }else{ plus[cur] += v; propagate(cur); } }else{ int mid = cl+cr>>>1; if(cl < r && l < mid){ add(l, r, v, cl, mid, 2*cur); } if(mid < r && l < cr){ add(l, r, v, mid, cr, 2*cur+1); } propagate(cur); } } public int min(int l, int r){ return l >= r ? I : min(l, r, 0, H, 1);} private int min(int l, int r, int cl, int cr, int cur) { if(l <= cl && cr <= r){ return st[cur]; }else{ int mid = cl+cr>>>1; int ret = I; if(cl < r && l < mid){ ret = Math.min(ret, min(l, r, cl, mid, 2*cur)); } if(mid < r && l < cr){ ret = Math.min(ret, min(l, r, mid, cr, 2*cur+1)); } return ret + plus[cur]; } } public int[] minpos(int l, int r){ return l >= r ? null : minpos(l, r, 0, H, 1);} private int[] minpos(int l, int r, int cl, int cr, int cur) { if(l <= cl && cr <= r){ return new int[]{st[cur], pos[cur]}; }else{ int mid = cl+cr>>>1; int[] L = null, R = null; if(cl < r && l < mid){ L = minpos(l, r, cl, mid, 2*cur); } if(mid < r && l < cr){ R = minpos(l, r, mid, cr, 2*cur+1); } if(L == null && R == null)return null; if(R == null || L[0] <= R[0]){ L[0] += plus[cur]; return L; }else{ R[0] += plus[cur]; return R; } } } public int[] toArray() { return toArray(1, 0, H, new int[H]); } private int[] toArray(int cur, int l, int r, int[] ret) { if(r-l == 1){ ret[cur-H] = st[cur]; }else{ toArray(2*cur, l, l+r>>>1, ret); toArray(2*cur+1, l+r>>>1, r, ret); for(int i = l;i < r;i++)ret[i] += plus[cur]; } return ret; } } public static int sumFenwick(int[] ft, int i) { int sum = 0; if(i < 0)return 0; for (i++; i > 0; i -= i & -i) sum += ft[i]; return sum; } public static void addFenwick(int[] ft, int i, int v) { if (v == 0 || i < 0) return; int n = ft.length; for (i++; i < n; i += i & -i) ft[i] += v; } public static int findGFenwick(int[] ft, int v) { int i = 0; int n = ft.length; for (int b = Integer.highestOneBit(n); b != 0 && i < n; b >>= 1) { if (i + b < n) { int t = i + b; if (v >= ft[t]) { i = t; v -= ft[t]; } } } return v != 0 ? -(i + 1) : i - 1; } public static int valFenwick(int[] ft, int i) { return sumFenwick(ft, i) - sumFenwick(ft, i - 1); } public static int[] restoreFenwick(int[] ft) { int n = ft.length - 1; int[] ret = new int[n]; for (int i = 0; i < n; i++) ret[i] = sumFenwick(ft, i); for (int i = n - 1; i >= 1; i--) ret[i] -= ret[i - 1]; return ret; } public static int before(int[] ft, int x) { int u = sumFenwick(ft, x - 1); if (u == 0) return -1; return findGFenwick(ft, u - 1) + 1; } public static int after(int[] ft, int x) { int u = sumFenwick(ft, x); int f = findGFenwick(ft, u); if (f + 1 >= ft.length - 1) return -1; return f + 1; } public static int[] buildFenwick(int[] a) { int n = a.length; int[] ft = new int[n + 1]; System.arraycopy(a, 0, ft, 1, n); for (int k = 2, h = 1; k <= n; k *= 2, h *= 2) { for (int i = k; i <= n; i += k) { ft[i] += ft[i - h]; } } return ft; } public static int[] buildFenwick(int n, int v) { int[] ft = new int[n + 1]; Arrays.fill(ft, 1, n + 1, v); for (int k = 2, h = 1; k <= n; k *= 2, h *= 2) { for (int i = k; i <= n; i += k) { ft[i] += ft[i - h]; } } return ft; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
codenet
-1
8,652
s332524056
p04052
1,532,730,793
python
Accepted
4,339
61,572
def invert(p, q): for i, pi in enumerate(p): q[pi] = i def sort_insertion(k, data, first, last): length = last - first if length <= 2: if length == 2 and data[first] - data[first + 1] >= k: data[first], data[first + 1] = data[first + 1], data[first] return for i in range(first + 1, last): v = data[i] for t in range(i - 1, first - 1, -1): if data[t] - v < k: t += 1 break data[t + 1:i + 1] = data[t:i] data[t] = v def sort_merge(k, data, first, last): if last - first < 10: sort_insertion(k, data, first, last) return middle = (first + last) // 2 sort_merge(k, data, first, middle) sort_merge(k, data, middle, last) bounds = data[first:middle] for i in range(len(bounds) - 2, -1, -1): bounds[i] = min(bounds[i + 1], bounds[i]) tmp = data[first:middle] first_len = middle - first head1 = 0 head2 = middle for ohead in range(first, last): if head1 == first_len or head2 == last: data[ohead:ohead + first_len - head1] = tmp[head1:first_len] return elif bounds[head1] - data[head2] >= k: data[ohead] = data[head2] head2 += 1 else: data[ohead] = tmp[head1] head1 += 1 n, k = (int(s) for s in input().split(' ')) p = [int(s) - 1 for s in input().split(' ')] q = list(p) invert(p, q) sort_merge(k, q, 0, n) invert(q, p) for pi in p: print(pi + 1)
codenet
-1
1,532
s039140354
p04052
1,576,776,154
rust
Accepted
624
51,704
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes .by_ref() .map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr, ) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => { read_value!($next, usize) - 1 }; ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }}; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } #[allow(unused)] macro_rules! debug { ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap()); } #[allow(unused)] macro_rules! debugln { ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap()); } /** * Segment Tree. This data structure is useful for fast folding on intervals of an array * whose elements are elements of monoid I. Note that constructing this tree requires the identity * element of I and the operation of I. * Verified by: yukicoder No. 259 (http://yukicoder.me/submissions/100581) * AGC015-E (http://agc015.contest.atcoder.jp/submissions/1461001) */ struct SegTree<I, BiOp> { n: usize, dat: Vec<I>, op: BiOp, e: I, } impl<I, BiOp> SegTree<I, BiOp> where BiOp: Fn(I, I) -> I, I: Copy { pub fn new(n_: usize, op: BiOp, e: I) -> Self { let mut n = 1; while n < n_ { n *= 2; } // n is a power of 2 SegTree {n: n, dat: vec![e; 2 * n - 1], op: op, e: e} } /* ary[k] <- v */ pub fn update(&mut self, idx: usize, v: I) { let mut k = idx + self.n - 1; self.dat[k] = v; while k > 0 { k = (k - 1) / 2; self.dat[k] = (self.op)(self.dat[2 * k + 1], self.dat[2 * k + 2]); } } /* [a, b) (note: half-inclusive) * http://proc-cpuinfo.fixstars.com/2017/07/optimize-segment-tree/ */ pub fn query(&self, mut a: usize, mut b: usize) -> I { let mut left = self.e; let mut right = self.e; a += self.n - 1; b += self.n - 1; while a < b { if (a & 1) == 0 { left = (self.op)(left, self.dat[a]); } if (b & 1) == 0 { right = (self.op)(self.dat[b - 1], right); } a = a / 2; b = (b - 1) / 2; } (self.op)(left, right) } } // This solution was written after the author read the editorial. // Topological sorting of DAG whose edges are not explicitly given. fn solve() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts { ($($format:tt)*) => (write!(out,$($format)*).unwrap()); } input! { n: usize, k: usize, p: [i64; n], } let mut p = p; let mut label = vec![n; n]; let mut buckets = vec![(0, 0); (n + k - 1) / k]; let mut st = SegTree::new(n, max, (0, 0)); for i in 0..n { st.update(i, (p[i], i)); } let mut que = BinaryHeap::new(); for i in 0..n { buckets[i / k] = max(buckets[i / k], (p[i], i)); } for i in 0..buckets.len() { let idx = buckets[i].1; if st.query(max(idx, k - 1) - (k - 1), min(idx + k, n)) == buckets[i] { let (a, b) = buckets[i]; que.push((b, a)); } } let mut cur = n; while let Some((idx, _)) = que.pop() { if p[idx] == 0 { continue; } label[idx] = cur; p[idx] = 0; st.update(idx, (0, idx)); cur -= 1; let bidx = idx / k; let mut aff = vec![]; if bidx > 0 { aff.push(bidx - 1); } aff.push(bidx); if (bidx + 1) * k < n { aff.push(bidx + 1); } for b in aff { buckets[b] = st.query(b * k, min(n, b * k + k)); let idx = buckets[b].1; let vic = st.query(max(idx, k - 1) - (k - 1), min(idx + k, n)); if buckets[b] == vic { if buckets[b].0 != 0 { let (x, y) = buckets[b]; que.push((y, x)); } } } } for l in label { puts!("{}\n", l); } } fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); }
codenet
-1
5,483