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
s015617292
p04052
1,597,270,373
cpp
Accepted
661
44,976
#include<cstdio> #include<algorithm> #include<queue> #include<cstring> #include<iostream> #include<cstdio> #include<cmath> #include<map> using namespace std; #define N 500005 #define INF 0x3f3f3f3f #define LL long long int rd() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48); c=getchar();} return f*x; } priority_queue<int,vector<int>,greater<int> > Q;//小根堆 保证字典序最小 int n,k,m; int ind[N]; vector<int>G[N]; int q[N],ans[N]; int tree[N<<2]; //权值线段树 维护区间最小值(因为是从右到左 就是离他最近 void PushUp(int i) { tree[i]=min(tree[i<<1],tree[i<<1|1]); } void Update(int i,int l,int r,int pos,int val) { if(l==r) { tree[i]=val; return ; } int mid=(l+r)>>1; if(pos<=mid) Update(i<<1,l,mid,pos,val); else Update(i<<1|1,mid+1,r,pos,val); PushUp(i); } int Query(int i,int l,int r,int ql,int qr) { if(r<ql||qr<l) return INF; if(ql<=l&&qr>=r) return tree[i]; int mid=(l+r)>>1; return min(Query(i<<1,l,mid,ql,qr),Query(i<<1|1,mid+1,r,ql,qr)); } void Topo() { for(int i=1;i<=n;i++) if(!ind[i]) Q.push(i); while(!Q.empty()) { int u=Q.top(); Q.pop(); q[++m]=u; for(int i=0;i<G[u].size();i++) { int v=G[u][i]; ind[v]--; if(!ind[v]) Q.push(v); } } } int main() { memset(tree,INF,sizeof(tree)); n=rd(),k=rd(); for(int i=1;i<=n;i++) q[rd()]=i; for(int i=n;i>=1;i--) { int j=Query(1,1,n,q[i]+1,q[i]+k-1);//q[j]-q[i]<k if(j!=INF) ind[q[j]]++,G[q[i]].push_back(q[j]); j=Query(1,1,n,q[i]-k+1,q[i]-1);//q[i]-q[j]<k if(j!=INF) ind[q[j]]++,G[q[i]].push_back(q[j]); Update(1,1,n,q[i],i); } Topo(); for(int i=1;i<=n;i++) ans[q[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,697
s068874154
p04052
1,535,752,420
cpp
Accepted
543
50,432
#include<iostream> #include<cstring> #include<vector> #include<queue> #include<set> #include<cstdio> #include<algorithm> using namespace std; #define LL long long inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c=='-') f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f; } const int INF=9999999; const int MAXN=500010; int N,K; int p[MAXN+1],q[MAXN+1]; set<int> s; vector<int> vec[MAXN+1]; int tr[MAXN<<2]; int deg[MAXN+1]; inline void Add(int rt,int l,int r,int k,int x){ if(l==r){tr[rt]=min(tr[rt],x); return ;} int mid=(l+r)>>1; if(k<=mid) Add(rt<<1,l,mid,k,x); else Add(rt<<1|1,mid+1,r,k,x); tr[rt]=min(tr[rt<<1],tr[rt<<1|1]); return ; } inline int Query(int rt,int l,int r,int L,int R){ if(L<=l&&R>=r) return tr[rt]; int 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; } inline void insert(int u,int v){ ++deg[v]; vec[u].push_back(v); return ; } int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); N=read(),K=read(); for(int i=1;i<=N;i++){ p[i]=read(); q[p[i]]=i; } memset(tr,127,sizeof(tr)); for(int i=N;i>=1;i--){ int x=Query(1,1,N,max(1,q[i]-K+1),q[i]); int y=Query(1,1,N,q[i],min(N,q[i]+K-1)); if(x<=N) insert(q[i],q[x]); if(y<=N) insert(q[i],q[y]); Add(1,1,N,q[i],i); } for(int i=1;i<=N;i++) if(!deg[i]) s.insert(i); int cnt=0; while(s.size()){ int k=(*s.begin()); q[++cnt]=k; s.erase(s.begin()); for(int i=0;i<vec[k].size();i++){ int v=vec[k][i]; if(!deg[v]) continue; deg[v]--; if(!deg[v]) s.insert(v); } } for(int i=1;i<=N;i++) p[q[i]]=i; for(int i=1;i<=N;i++) printf("%d\n",p[i]); return 0; }
codenet
-1
1,703
s835291101
p04052
1,514,193,824
cpp
Accepted
1,279
42,496
// +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+ \\ #include <bits/stdc++.h> #define F first #define S second #define _sz(x) (int)x.size() using namespace std ; using ll = long long ; using ld = long double ; using pii = pair <int , int> ; int const N = 5e5 + 20 ; int n , k , a[N] , res[N] ; set <int> st ; pii seg[N << 2] ; void modify (int p , int val , int v = 1 , int s = 0 , int e = n) { if (s + 1 == e) { seg[v] = pii(val , s) ; return ; } int mid = (s + e) >> 1 , lc = v << 1 , rc = lc | 1 ; if (p < mid) modify(p , val , lc , s , mid) ; else modify(p , val , rc , mid , e) ; seg[v] = max(seg[lc] , seg[rc]) ; } pii get (int l , int r , int v = 1 , int s = 0 , int e = n) { if (s >= r || e <= l) return pii(-1 , 0) ; if (s >= l && e <= r) return seg[v] ; int mid = (s + e) >> 1 , lc = v << 1 , rc = lc | 1 ; return max( get(l , r , lc , s , mid) , get(l , r , rc , mid , e) ) ; } inline void rel (int b) { int xl = b * k , xr = min(n , xl + k) ; pii p = get(xl , xr) ; int index = p.S ; if (a[index] && p == get(max(0 , index - k + 1) , min(n , index + k))) st.insert(index) ; } int main(){ ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ; cin >> n >> k ; for (int i = 0 ; i < n ; i ++) cin >> a[i] ; for (int i = 0 ; i < n ; i ++) modify(i , a[i]) ; for (int i = 0 ; i < n ; i += k) rel(i / k) ; for (int i = 0 ; i < n ; i ++) { int index = *st.rbegin() ; res[index] = n - i ; st.erase(index) ; a[index] = 0 ; modify(index , a[index]) ; int b = index / k ; rel(b) ; if (b) rel(b - 1) ; if (b + 1 <= (n - 1) / k) rel(b + 1) ; } for (int i = 0 ; i < n ; i ++) cout << res[i] << '\n' ; }
codenet
-1
1,706
s835291101
p04052
1,514,193,824
cpp
Accepted
1,279
42,496
// +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+ \\ #include <bits/stdc++.h> #define F first #define S second #define _sz(x) (int)x.size() using namespace std ; using ll = long long ; using ld = long double ; using pii = pair <int , int> ; int const N = 5e5 + 20 ; int n , k , a[N] , res[N] ; set <int> st ; pii seg[N << 2] ; void modify (int p , int val , int v = 1 , int s = 0 , int e = n) { if (s + 1 == e) { seg[v] = pii(val , s) ; return ; } int mid = (s + e) >> 1 , lc = v << 1 , rc = lc | 1 ; if (p < mid) modify(p , val , lc , s , mid) ; else modify(p , val , rc , mid , e) ; seg[v] = max(seg[lc] , seg[rc]) ; } pii get (int l , int r , int v = 1 , int s = 0 , int e = n) { if (s >= r || e <= l) return pii(-1 , 0) ; if (s >= l && e <= r) return seg[v] ; int mid = (s + e) >> 1 , lc = v << 1 , rc = lc | 1 ; return max( get(l , r , lc , s , mid) , get(l , r , rc , mid , e) ) ; } inline void rel (int b) { int xl = b * k , xr = min(n , xl + k) ; pii p = get(xl , xr) ; int index = p.S ; if (a[index] && p == get(max(0 , index - k + 1) , min(n , index + k))) st.insert(index) ; } int main(){ ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) ; cin >> n >> k ; for (int i = 0 ; i < n ; i ++) cin >> a[i] ; for (int i = 0 ; i < n ; i ++) modify(i , a[i]) ; for (int i = 0 ; i < n ; i += k) rel(i / k) ; for (int i = 0 ; i < n ; i ++) { int index = *st.rbegin() ; res[index] = n - i ; st.erase(index) ; a[index] = 0 ; modify(index , a[index]) ; int b = index / k ; rel(b) ; if (b) rel(b - 1) ; if (b + 1 <= (n - 1) / k) rel(b + 1) ; } for (int i = 0 ; i < n ; i ++) cout << res[i] << '\n' ; }
codenet
-1
1,706
s217286924
p04052
1,588,150,323
cpp
Accepted
942
24,064
#include <bits/stdc++.h> using namespace std; const int MN = 501234, INF = 1012345678; int se[MN * 4], seg[MN * 4], lz[MN * 4], n, a[MN], g[MN], b[MN]; void boom(int v) { seg[v * 2] += lz[v]; seg[v * 2 + 1] += lz[v]; lz[v * 2] += lz[v]; lz[v * 2 + 1] += lz[v]; lz[v] = 0; } void add(int l, int r, int val, int s = 0, int e = n, int v = 1) { if (r <= s || e <= l) return; if (l <= s && e <= r) { seg[v] += val; lz[v] += val; return; } boom(v); int mid = (s + e) / 2; add(l, r, val, s, mid, v * 2); add(l, r, val, mid, e, v * 2 + 1); seg[v] = min(seg[v * 2], seg[v * 2 + 1]); } int get_min(int s = 0, int e = n, int v = 1) { if (e - s == 1) return s; boom(v); int mid = (s + e) / 2; if (seg[v * 2 + 1] == 0) return get_min(mid, e, v * 2 + 1); else return get_min(s, mid, v * 2 ); } void add(int p) { for (p++; p < MN; p += p & -p) se[p]++; } int get(int p) { int sum = 0; for (; p > 0; p -= p & -p) sum += se[p]; return sum; } int get(int l, int r) { return get(r) - get(l); } int main() { int k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> g[i]; for (int i = 0; i < n; i++) a[g[i] - 1] = i; for (int i = n - 1; i >= 0; i--) { // int x; // cerr << max(0, a[i] - k + 1) << ' ' << min(n, a[i] + k) << endl; add(a[i], a[i] + 1, get(max(0, a[i] - k + 1), min(n, a[i] + k))); // cerr << x << endl; // for (int i = 0; i < n; i++) // cerr << get(i, i + 1) << ' '; // cerr << endl; add(a[i]); } // cerr << endl; for (int i = 0; i < n; i++) { int v = get_min(); // cerr << v << endl; add(v, v + 1, INF); add(max(v - k + 1, 0), min(v + k, n), -1); b[v] = n - i; } for (int i = 0; i < n; i++) cout << b[i] << ' '; }
codenet
-1
1,709
s716688635
p04052
1,549,163,417
cpp
Accepted
897
52,592
#include<bits/stdc++.h> using namespace std; #define nn 65536*8 #define ll long long #define mod 1000000007 vector<int> v[nn]; int p[nn],q[nn]; int n,k;int in[nn]; void solve() { for(int i=1;i<=n;i++) { for(int j=0;j<v[i].size();j++) in[v[i][j]]++; } priority_queue<int> pq; for(int i=1;i<=n;i++) if(in[i]==0) pq.push(-i); vector<int> ans; while(pq.size()) { int x=-pq.top();pq.pop();ans.push_back(x); for(int i=0;i<v[x].size();i++) { in[v[x][i]]--; if(!in[v[x][i]]) pq.push(-v[x][i]); } } for(int i=1;i<=n;i++) p[ans[i-1]]=i; for(int i=1;i<=n;i++) printf("%d\n",p[i]); } struct segmenttree{ int pos[nn*2+10]; int ls[nn*2+10],rs[nn*2+10];int tot; #define lc ls[id] #define rc rs[id] #define mid (l+r)/2 void build(int l,int r,int &id) { if(!id) id=++tot; if(l==r) { pos[id]=0; return ; } build(l,mid,lc);build(mid+1,r,rc); pos[id]=max(pos[lc],pos[rc]); } void update(int x,int l,int r,int id,int a) { if(l==r) { pos[id]=a; return ; } if(x<=mid) update(x,l,mid,lc,a); else update(x,mid+1,r,rc,a);pos[id]=max(pos[lc],pos[rc]); } int query(int a,int b,int l,int r,int id) { if(l>=a and r<=b) return pos[id]; int sm=0; if(a<=mid) sm=max(sm,query(a,b,l,mid,lc)); if(b>mid) sm=max(sm,query(a,b,mid+1,r,rc)); return sm; } }trcyx; int check(int i,int j) { if(j&&i&&(i-j)) if(abs(q[i]-q[j])<k) v[q[i]].push_back(q[j]); } int main() { cin>>n>>k; for(int i=1;i<=n;i++) scanf("%d",&p[i]),q[p[i]]=i; int rt=0;trcyx.build(1,n,rt); for(int i=1;i<=n;i++) { int j; j=trcyx.query(q[i]-k+1,q[i],1,n,rt); check(j,i); j=trcyx.query(q[i],q[i]+k-1,1,n,rt); check(j,i); trcyx.update(q[i],1,n,rt,i); } solve(); return 0; }
codenet
-1
1,717
s700547729
p04052
1,558,926,941
cpp
Accepted
512
42,748
#include<iostream> #include<cstdio> #include<queue> using namespace std; const int N=3000005; int n,k,a[N],p[N],h[N],cnt,d[N],tot; struct xds { int l,r,p; }t[N]; struct qwe { int ne,to; }e[N]; int read() { int r=0,f=1; char p=getchar(); while(p>'9'||p<'0') { if(p=='-') f=-1; p=getchar(); } while(p>='0'&&p<='9') { r=r*10+p-48; p=getchar(); } return r*f; } void build(int ro,int l,int r) { t[ro].l=l,t[ro].r=r,t[ro].p=n+1; if(l==r) return; int mid=(l+r)>>1; build(ro<<1,l,mid); build(ro<<1|1,mid+1,r); } void update(int ro,int p,int v) { if(t[ro].l==t[ro].r) { t[ro].p=v; return; } int mid=(t[ro].l+t[ro].r)>>1; if(p<=mid) update(ro<<1,p,v); else update(ro<<1|1,p,v); t[ro].p=min(t[ro<<1].p,t[ro<<1|1].p); } int ques(int ro,int l,int r) { if(t[ro].l==l&&t[ro].r==r) return t[ro].p; int mid=(t[ro].l+t[ro].r)>>1; if(r<=mid) return ques(ro<<1,l,r); else if(l>mid) return ques(ro<<1|1,l,r); else return min(ques(ro<<1,l,mid),ques(ro<<1|1,mid+1,r)); } void add(int u,int v) {//cerr<<u<<" "<<v<<endl; cnt++; e[cnt].ne=h[u]; e[cnt].to=v; d[v]++; h[u]=cnt; } int main() { n=read(),k=read(); for(int i=1;i<=n;i++) a[i]=read(),p[a[i]]=i; build(1,1,n); for(int i=n;i>=1;i--) { int x=ques(1,p[i],min(n,p[i]+k-1)),y=ques(1,max(1,p[i]-k+1),p[i]); if(x<=n) add(p[i],p[x]); if(y<=n) add(p[i],p[y]); update(1,p[i],i); } priority_queue<int,vector<int>,greater<int> >q; for(int i=1;i<=n;i++) if(!d[i]) q.push(i); while(!q.empty()) { int u=q.top(); p[++tot]=u; q.pop(); for(int i=h[u];i;i=e[i].ne) if(!(--d[e[i].to])) q.push(e[i].to); } for(int i=1;i<=n;i++) a[p[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",a[i]); return 0; }
codenet
-1
1,721
s582744953
p04052
1,594,079,233
cpp
Accepted
405
25,560
#include <cstdio> #include <queue> int N, K; int a[500001], ref[500001], ans[500001]; int d[500001]; int T[1048576]; int head[500001], next[1000001], to[1000001], E; int GL(int p, int l, int r, int x) { if (!T[p]) return 0; if (r <= x) { while (l < r) { int m = l + r >> 1; if (T[p << 1 | 1]) p = p << 1 | 1, l = m + 1; else p <<= 1, r = m; } return l; } int m = l + r >> 1; int t = m + 1 <= x ? GL(p << 1 | 1, m + 1, r, x) : 0; return t ? t : GL(p << 1, l, m, x); } int GR(int p, int l, int r, int x) { if (!T[p]) return 0; if (l >= x) { while (l < r) { int m = l + r >> 1; if (T[p << 1]) p <<= 1, r = m; else p = p << 1 | 1, l = m + 1; } return l; } int m = l + r >> 1; int t = m >= x ? GR(p << 1, l, m, x) : 0; return t ? t : GR(p << 1 | 1, m + 1, r, x); } void P(int x, int w) { int p = 1, l = 1, r = N; T[p] += w; while (l < r) { int m = l + r >> 1; if (x <= m) p <<= 1, r = m; else p = p << 1 | 1, l = m + 1; T[p] += w; } } void add(int u, int v) { d[u]++; next[++E] = head[v]; to[E] = u; head[v] = E; } std::priority_queue < int > Q; int main() { scanf("%d%d", &N, &K); for (int i = 1; i <= N; i++) scanf("%d", a + i); for (int i = 1; i <= N; i++) { int L = GL(1, 1, N, a[i]), R = GR(1, 1, N, a[i]); if (L) add(ref[L], i); if (R) add(i, ref[R]); ref[a[i]] = i; P(a[i], 1); if (i >= K) P(a[i - K + 1], -1); } for (int i = 1; i <= N; i++) if (d[i] == 0) Q.push(i); for (int w = N; w; w--) { int pos = Q.top(); Q.pop(); ans[pos] = w; for (int e = head[pos]; e; e = next[e]) if (!--d[to[e]]) Q.push(to[e]); } for (int i = 1; i <= N; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,724
s582744953
p04052
1,594,079,233
cpp
Accepted
405
25,560
#include <cstdio> #include <queue> int N, K; int a[500001], ref[500001], ans[500001]; int d[500001]; int T[1048576]; int head[500001], next[1000001], to[1000001], E; int GL(int p, int l, int r, int x) { if (!T[p]) return 0; if (r <= x) { while (l < r) { int m = l + r >> 1; if (T[p << 1 | 1]) p = p << 1 | 1, l = m + 1; else p <<= 1, r = m; } return l; } int m = l + r >> 1; int t = m + 1 <= x ? GL(p << 1 | 1, m + 1, r, x) : 0; return t ? t : GL(p << 1, l, m, x); } int GR(int p, int l, int r, int x) { if (!T[p]) return 0; if (l >= x) { while (l < r) { int m = l + r >> 1; if (T[p << 1]) p <<= 1, r = m; else p = p << 1 | 1, l = m + 1; } return l; } int m = l + r >> 1; int t = m >= x ? GR(p << 1, l, m, x) : 0; return t ? t : GR(p << 1 | 1, m + 1, r, x); } void P(int x, int w) { int p = 1, l = 1, r = N; T[p] += w; while (l < r) { int m = l + r >> 1; if (x <= m) p <<= 1, r = m; else p = p << 1 | 1, l = m + 1; T[p] += w; } } void add(int u, int v) { d[u]++; next[++E] = head[v]; to[E] = u; head[v] = E; } std::priority_queue < int > Q; int main() { scanf("%d%d", &N, &K); for (int i = 1; i <= N; i++) scanf("%d", a + i); for (int i = 1; i <= N; i++) { int L = GL(1, 1, N, a[i]), R = GR(1, 1, N, a[i]); if (L) add(ref[L], i); if (R) add(i, ref[R]); ref[a[i]] = i; P(a[i], 1); if (i >= K) P(a[i - K + 1], -1); } for (int i = 1; i <= N; i++) if (d[i] == 0) Q.push(i); for (int w = N; w; w--) { int pos = Q.top(); Q.pop(); ans[pos] = w; for (int e = head[pos]; e; e = next[e]) if (!--d[to[e]]) Q.push(to[e]); } for (int i = 1; i <= N; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,724
s532619177
p04052
1,583,095,078
cpp
Accepted
599
50,812
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> using namespace std; typedef long long ll; #define N 1000002 const int p=1e9+7; struct quji{int l,r,mid,mn;}c[N<<2]; struct node{int x,va;friend bool operator <(node n1,node n2){return n1.va<n2.va;}}; struct edge{int to,nxxt;}e[N<<1]; int n,a[N],b[N],head[N],cnt=1,rd[N],s[N],tot,k; priority_queue<node>q; inline void ins(int u,int v){e[cnt].to=v;e[cnt].nxxt=head[u];head[u]=cnt++;} void bt(int ind,int l,int r) { c[ind].l=l;c[ind].r=r;c[ind].mid=(l+r)>>1;c[ind].mn=1e9; if(l==r)return;bt(ind<<1,l,c[ind].mid);bt(ind<<1|1,c[ind].mid+1,r); } void chng(int ind,int x,int y) { if(c[ind].l==c[ind].r){c[ind].mn=min(c[ind].mn,y);return;} if(x<=c[ind].mid)chng(ind<<1,x,y);else chng(ind<<1|1,x,y); c[ind].mn=min(c[ind<<1].mn,c[ind<<1|1].mn); } int ask(int ind,int l,int r) { if(l<=c[ind].l&&c[ind].r<=r)return c[ind].mn; int ret=1e9; if(l<=c[ind].mid)ret=min(ret,ask(ind<<1,l,r)); if(r>c[ind].mid)ret=min(ret,ask(ind<<1|1,l,r)); return ret; } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[a[i]]=i; for(int i=1,j=n;i<j;i++,j--)swap(b[i],b[j]); bt(1,1,n); for(int i=n;i;i--) { int x=ask(1,b[i],min(n,b[i]+k-1)); if(x<=n)ins(i,x),rd[x]++; x=ask(1,max(1,b[i]-k+1),b[i]); if(x<=n)ins(i,x),rd[x]++; chng(1,b[i],i); } for(int i=1;i<=n;i++)if(!rd[i])q.push((node){i,b[i]}); while(!q.empty()) { node a1=q.top();q.pop();s[++tot]=a1.va;; for(int i=head[a1.x];i;i=e[i].nxxt) { int t1=e[i].to;rd[t1]--;if(!rd[t1])q.push((node){t1,b[t1]}); } } for(int i=1,j=n;i<j;i++,j--)swap(s[i],s[j]); for(int i=1;i<=n;i++)a[s[i]]=i; for(int i=1;i<=n;i++)printf("%d\n",a[i]); }
codenet
-1
1,725
s151123813
p04052
1,575,719,180
cpp
Accepted
708
60,800
#include <bits/stdc++.h> #pragma GCC optimize ("O2") #pragma GCC optimize ("unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr<<#x<<'='<<(x)<<endl; #define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl; #define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl; #define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl; #define all(x) x.begin(), x.end() #define pb push_back #define kill(x) return cout<<x<<'\n', 0; const ld eps=1e-7; const int inf=1000000010; const ll INF=10000000000000010LL; const int mod = 1000000007; const int MAXN = 500010, LOG=20; int n, m, k, u, v, x, y, t, a, b; int P[MAXN], ans[MAXN]; int deg[MAXN]; vector<int> G[MAXN]; set<pii> st; priority_queue<int, vector<int>, greater<int>> pq; void add_edge(int u, int v){ if (!u || v==n+1) return ; G[u].pb(v); deg[v]++; //debug2(u, v) } int main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin>>n>>k; st.insert({0, 0}); st.insert({n+1, n+1}); for (int i=1; i<=n; i++){ cin>>P[i]; if (i>k) st.erase({P[i-k], i-k}); auto it=st.lower_bound({P[i], 0}); add_edge(i, it->second); it--; add_edge(it->second, i); st.insert({P[i], i}); } for (int i=1; i<=n; i++) if (!deg[i]) pq.push(i); /*while (pq.size())*/for (int i=1; i<=n; i++){ int v=pq.top(); pq.pop(); ans[v]=i; for (int u:G[v]) if (!--deg[u]) pq.push(u); } for (int i=1; i<=n; i++) cout<<ans[i]<<'\n'; return 0; }
codenet
-1
1,725
s608453835
p04052
1,571,177,860
cpp
Accepted
681
38,492
#include<bits/stdc++.h> using namespace std; const int N=5e5+5,inf=1e9+5; int n,K; int head[N],ce,deg[N]; struct edge{int to,nxt;} e[N<<1]; int p[N],a[N],ord[N],ans[N],cnt; struct Segtree{int l,r,minx;} tr[N<<2]; void Build(int l,int r,int k) { tr[k].l=l,tr[k].r=r,tr[k].minx=inf; if(l==r) return; int mid=(l+r)>>1; Build(l,mid,k<<1); Build(mid+1,r,k<<1|1); } void Insert(int x,int k) { if(tr[k].l==tr[k].r) { tr[k].minx=p[x]; return; } int mid=(tr[k].l+tr[k].r)>>1; if(mid>=x) Insert(x,k<<1); else Insert(x,k<<1|1); tr[k].minx=min(tr[k<<1].minx,tr[k<<1|1].minx); } int Query(int l,int r,int k) { if(l>r) return inf; if(l<=tr[k].l && tr[k].r<=r) return tr[k].minx; int ret=inf,mid=(tr[k].l+tr[k].r)>>1; if(l<=mid) ret=min(ret,Query(l,r,k<<1)); if(r>mid) ret=min(ret,Query(l,r,k<<1|1)); return ret; } void topsort() { priority_queue<pair<int,int> > Q; for(int i=1;i<=n;++i) if(!deg[i]) Q.push(make_pair(-a[i],i)); while(!Q.empty()) { int u=Q.top().second; Q.pop(); ord[++cnt]=u; for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(!--deg[v]) Q.push(make_pair(-a[v],v)); } } } void adde(int a,int b) { e[++ce].to=b,e[ce].nxt=head[a],head[a]=ce,++deg[b]; } int main() { //freopen("permutation.in","r",stdin); freopen("permutation.out","w",stdout); scanf("%d%d",&n,&K); for(int i=1;i<=n;++i) scanf("%d",&p[i]); for(int i=1;i<=n;++i) a[p[i]]=i; Build(1,n,1); for(int i=n;i>=1;--i) { int x=Query(a[i]+1,min(a[i]+K-1,n),1); if(x<inf) adde(i,x); x=Query(max(a[i]-K+1,1),a[i]-1,1); if(x<inf) adde(i,x); Insert(a[i],1); } topsort(); for(int i=1;i<=n;++i) ord[i]=a[ord[i]]; for(int i=1;i<=n;++i) ans[ord[i]]=i; for(int i=1;i<=n;++i) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,725
s776920309
p04052
1,579,471,075
cpp
Accepted
1,353
21,120
//IOI 2021 #include <bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second typedef long long ll; typedef pair<int, int> pii; const int MAXN = 5 * 100 * 1000 + 17; int n, k, a[MAXN], seg[MAXN << 2], p[MAXN]; set<int> s; void upd(int pos, int v = 1, int l = 1, int r = n + 1) { if (pos < l || r <= pos) return; if (l + 1 == r) { seg[v] = pos; return; } upd(pos, v << 1, l, l + r >> 1); upd(pos, v << 1 | 1, l + r >> 1, r); if (a[seg[v << 1]] > a[seg[v << 1 | 1]]) seg[v] = seg[v << 1]; else seg[v] = seg[v << 1 | 1]; } int get(int lp, int rp, int v = 1, int l = 1, int r = n + 1) { if (rp <= l || r <= lp) return 0; if (lp <= l && r <= rp) return seg[v]; int lg = get(lp, rp, v << 1, l, l + r >> 1), rg = get(lp, rp, v << 1 | 1, l + r >> 1, r); if (a[lg] > a[rg]) return lg; else return rg; } int getST(int x) { return x * (k - 1) + 1; } int getEN(int x) { return min((x + 1) * (k - 1) + 1, n + 1); } void addMAX(int x) { int pos = get(getST(x), getEN(x)); if (get(pos - k + 1, pos + k) == pos && a[pos] > 0) s.insert(pos); } 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], upd(i); if (k == 1) { for (int i = 1; i <= n; i++) cout << i << '\n'; return 0; } for (int i = 0; i < (n + k - 2) / (k - 1); i++) addMAX(i); int label = n; while (s.size()) { int u = *s.rbegin(); s.erase(u); a[u] = -1; upd(u); p[u] = label--; int bl = (u - 1) / (k - 1); addMAX(bl); if (bl == 0) addMAX(bl + 1); else if (bl + 1 == (n + k - 2) / (k - 1)) addMAX(bl - 1); else addMAX(bl - 1), addMAX(bl + 1); } for (int i = 1; i <= n; i++) cout << p[i] << '\n'; return 0; }
codenet
-1
1,730
s318517052
p04052
1,537,135,104
cpp
Accepted
777
45,168
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> #define MAXN 500000 using namespace std; priority_queue<int,vector<int>,greater<int> > que; int N,K; int p[MAXN+5],q[MAXN+5]; int tree[MAXN*4],d[MAXN+5]; vector<int> G[MAXN+5],ans; void Build(int i,int l,int r) { tree[i]=0; if(l==r) return; int mid=(l+r)/2; Build(i*2,l,mid); Build(i*2+1,mid+1,r); } void PushUp(int i) { tree[i]=max(tree[i*2],tree[i*2+1]); } void Insert(int i,int l,int r,int p,int val) { if(l==r) { tree[i]=val; return; } int mid=(l+r)/2; if(p<=mid) Insert(i*2,l,mid,p,val); else Insert(i*2+1,mid+1,r,p,val); PushUp(i); } int Query(int i,int l,int r,int ql,int qr) { if(qr<l||ql>r) return 0; if(ql<=l&&r<=qr) return tree[i]; int mid=(l+r)/2; return max(Query(i*2,l,mid,ql,qr),Query(i*2+1,mid+1,r,ql,qr)); } void BFS() { for(int i=1;i<=N;i++) if(d[i]==0) que.push(i); while(que.empty()==false) { int u=que.top(); que.pop(); ans.push_back(u); for(int i=0;i<(int)G[u].size();i++) { int v=G[u][i]; d[v]--; if(d[v]==0) que.push(v); } } } int main() { // freopen("swap.in","r",stdin); // freopen("swap.out","w",stdout); scanf("%d %d",&N,&K); for(int i=1;i<=N;i++) { scanf("%d",&p[i]); q[p[i]]=i; } Build(1,1,N); for(int i=1;i<=N;i++) { int pos=Query(1,1,N,max(q[i]-K+1,1),q[i]); if(pos!=0) { G[q[pos]].push_back(q[i]); d[q[i]]++; } pos=Query(1,1,N,q[i],min(N,q[i]+K-1)); if(pos!=0) { G[q[pos]].push_back(q[i]); d[q[i]]++; } Insert(1,1,N,q[i],i); } for(int i=1;i<=N;i++) sort(G[i].begin(),G[i].end()); BFS(); for(int i=0;i<(int)ans.size();i++) p[ans[i]]=i+1; for(int i=1;i<=N;i++) printf("%d\n",p[i]); return 0; }
codenet
-1
1,730
s788896035
p04052
1,577,836,364
cpp
Accepted
2,067
52,596
#include <bits/stdc++.h> #define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0); #define F first #define S second #define V vector #define PB push_back #define MP make_pair #define EB emplace_back #define ALL(v) (v).begin(), (v).end() #define debug(x) cerr << #x << " is " << x << endl using namespace std; typedef long long ll; typedef pair<int, int> pi; typedef V<int> vi; const int INF = 0x3f3f3f3f, N = 500005; int p[N], nxt[N][2], in[N]; pi t[4*N]; int n, k; void upd(int pos, pi val, int v = 1, int tl = 1, int tr = n) { if(tl == tr) { t[v] = val; return; } int tm = (tl + tr) / 2; if(pos <= tm) upd(pos, val, v*2, tl, tm); else upd(pos, val, v*2+1, tm+1, tr); t[v] = min(t[v*2], t[v*2+1]); } pi qry(int l, int r, int v = 1, int tl = 1, int tr = n) { if(l > r) return MP(INF, INF); if(l == tl && r == tr) return t[v]; int tm = (tl + tr) / 2; return min(qry(l, min(r, tm), v*2, tl, tm), qry(max(l, tm+1), r, v*2+1, tm+1, tr)); } signed main() { IO_OP; memset(t, 0x3f, sizeof t); cin >> n >> k; for(int i=1;i<=n;i++) { int t; cin >> t; p[t] = i; } for(int i=n;i>=1;i--) { nxt[p[i]][0] = qry(max(1, p[i]), min(p[i]+k-1, n)).S; nxt[p[i]][1] = qry(max(1, p[i]-k+1), min(p[i], n)).S; upd(p[i], MP(i, p[i])); for(int j=0;j<2;j++) if(nxt[p[i]][j] != INF) in[nxt[p[i]][j]]++; } set<pi> s; for(int i=1;i<=n;i++) s.insert(MP(in[i], i)); vi v; while(s.size()) { pi p = *s.begin(); s.erase(s.begin()); v.PB(p.S); for(int i=0;i<2;i++) if(nxt[p.S][i] != INF) { int pp = nxt[p.S][i]; s.erase(MP(in[pp], pp)); in[pp]--; s.insert(MP(in[pp], pp)); } } for(int i=0;i<n;i++) p[v[i]] = i + 1; for(int i=1;i<=n;i++) cout << p[i] << endl; }
codenet
-1
1,732
s084354950
p04052
1,508,691,107
cpp
Accepted
867
42,624
#include<cstdio> #include<cstdlib> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<set> #include<queue> #define IL inline #define RG register #define db double #define inf 0x3f3f3f3f #define LL long long #define N 500005 using namespace std; IL int gi() { RG int res=0,s=1;RG char ch; for(ch=getchar(); (ch<'0'||ch>'9')&&ch!='-' ;ch=getchar()); if(ch=='-') s=-1,ch=getchar(); for(;ch>='0'&&ch<='9';ch=getchar()) res=10*res+ch-48; return res*s; } int n,k,a[N],ans[N]; int head[N],to[N<<1],ne[N<<1],num,du[N]; struct node { int p,v; node(RG int pp=0,RG int vv=0){p=pp,v=vv;} bool operator < (const node &o) const { return v<o.v; } bool operator == (const node &o) const { return p==o.p&&o==o.v; } }; set<node>s; set<node>::iterator it; priority_queue<int>h; //建反图 IL void adde(RG int u,RG int v) { to[++num]=v,ne[num]=head[u],head[u]=num; du[v]++; } int main() { //freopen("permutation.in","r",stdin); //freopen("permutation.out","w",stdout); n=gi(),k=gi(); for(RG int i=1;i<=n;++i) a[i]=gi(); for(RG int i=1;i<=n;++i) { if(i-k>0) s.erase(node(i-k,a[i-k])); s.insert(node(i,a[i])); it=s.upper_bound(node(i,a[i])); if(it!=s.end()) adde((*it).p,i); } s.erase(s.begin(),s.end()); for(RG int i=n;i>=1;--i) { if(i+k<=n) s.erase(node(i+k,a[i+k])); s.insert(node(i,a[i])); it=s.upper_bound(node(i,a[i])); if(it!=s.end()) adde((*it).p,i); } for(RG int i=1;i<=n;++i) if(!du[i]) h.push(i); RG int pos,tot=n; while(!h.empty()) { pos=h.top(); h.pop(); ans[pos]=tot; tot--; for(RG int i=head[pos];i;i=ne[i]) { du[to[i]]--; if(!du[to[i]]) h.push(to[i]); } } for(RG int i=1;i<=n;++i) printf("%d\n",ans[i]); //fclose(stdin);fclose(stdout); return 0; }
codenet
-1
1,732
s384668341
p04052
1,567,307,491
cpp
Accepted
465
38,528
#include<bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; const int maxn=2000002; int n,k,num,res,p[maxn],in[maxn],to[maxn],ans[maxn],from[maxn],head[maxn],tree[maxn]; priority_queue < int , vector < int > , greater < int > > Q; inline void add (int u,int v) { from[++num]=head[u]; ++in[v]; to[num]=v; head[u]=num; } inline void Update (int x,int l,int r,int pos,int val) { if(l==pos&&r==pos) { tree[x]=val; return ; } register int mid=(l+r)>>1; if(pos<=mid) Update(x<<1,l,mid,pos,val); else Update(x<<1|1,mid+1,r,pos,val); tree[x]=min(tree[x<<1],tree[x<<1|1]); } inline void Query (int x,int l,int r,int L,int R) { if(L<=l&&r<=R) { res=min(res,tree[x]); return ; } register int mid=(l+r)>>1; if(L<=mid) Query(x<<1,l,mid,L,R); if(R>mid) Query(x<<1|1,mid+1,r,L,R); } inline void Topo () { register int now=0; for(register int i=1;i<=n;++i) if(!in[i]) Q.push(i); while(!Q.empty()) { register int u=Q.top(); Q.pop(); ans[u]=++now; for(register int i=head[u];i;i=from[i]) if(!--in[to[i]]) Q.push(to[i]); } for(register int i=1;i<=n;++i) printf("%d\n",ans[i]); } inline int read () { char ch=getchar(); register int num=0; while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)) num=(num<<3)+(num<<1)+(ch^'0'),ch=getchar(); return num; } int main () { n=read(),k=read(); register int l,r; for(register int i=1;i<=n;++i) p[read()]=i; memset(tree,INF,sizeof(tree)); for(register int i=n;i;--i) { l=p[i]+1,r=min(p[i]+k-1,n); res=INF; if(l<=r) Query(1,1,n,l,r); if(res!=INF) add(p[i],p[res]); l=max(1,p[i]-k+1),r=p[i]-1; res=INF; if(l<=r) Query(1,1,n,l,r); if(res!=INF) add(p[i],p[res]); Update(1,1,n,p[i],i); } Topo(); return 0; }
codenet
-1
1,733
s297459729
p04052
1,562,986,655
cpp
Accepted
718
45,440
/* * Author : YangDavid * Created Time : 2019年07月12日 星期五 20时51分56秒 */ #include<bits/stdc++.h> #define rep(i, n) for(int i = 1; i <= n; ++i) using namespace std; typedef long long ll; typedef pair<int, int> pii; const int maxn = 555555; int mx[maxn << 1], n, k, p[maxn], q[maxn], ans[maxn]; #define lson (o << 1) #define rson (o << 1 | 1) void modify(int o, int l, int r, int d, int v) { if(l == r) { mx[o] = v; return; } int mid = (l + r) >> 1; if(d <= mid) modify(lson, l, mid, d, v); else modify(rson, mid + 1, r, d, v); mx[o] = max(mx[lson], mx[rson]); } int query(int o, int l, int r, int ql, int qr) { if(ql <= l && r <= qr) return mx[o]; int mid = (l + r) >> 1, res = 0; if(ql <= mid) res = max(res, query(lson, l, mid, ql, qr)); if(qr > mid) res = max(res, query(rson, mid + 1, r, ql, qr)); return res; } vector<int> G[maxn]; int indeg[maxn]; void add(int from, int to) { indeg[to]++; G[from].push_back(to); } void toposort() { int cnt = 0; priority_queue<int, vector<int>, greater<int> > pq; for(int i = 1; i <= n; ++i) if(indeg[i] == 0) pq.push(i); while(!pq.empty()) { int f = pq.top(); pq.pop(); ans[f] = ++cnt; for(auto g : G[f]) if(--indeg[g] == 0) pq.push(g); } } int main() { scanf("%d%d", &n, &k); rep(i, n) scanf("%d", &p[i]), q[p[i]] = i; for(int i = 1; i <= n; ++i) { int small = query(1, 1, n, max(1, q[i]-k+1), q[i]); if(small) add(q[small], q[i]); int big = query(1, 1, n, q[i], min(n, q[i] + k - 1)); if(big) add(q[big], q[i]); modify(1, 1, n, q[i], i); } toposort(); rep(i, n) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,733
s453384383
p04052
1,588,820,437
cpp
Accepted
763
27,764
#include<bits/stdc++.h> #define INF 0x3f3f3f3f #define re register #define LL long long #define MOD 1000000007 #define MAXN 500005 #define lson (now<<1) #define rson ((now<<1)|1) using namespace std; struct Edge{ int to,nex;Edge(int _=0,int __=0){to=_,nex=__;} }e[MAXN<<1]; int n,k,cnt,tot,hd[MAXN],du[MAXN],p[MAXN],ans[MAXN],t[MAXN<<2]; priority_queue<int,vector<int>,greater<int> >q; int rd(){ int x=0,tp=1;char c;c=getchar(); while(c<'0' || c>'9'){if(c=='-')tp=-1;c=getchar();} while(c>='0' && c<='9'){x=(x*10+c-'0');c=getchar();} return x*tp; } void adde(int x,int y){ e[++cnt]=Edge(y,hd[x]); hd[x]=cnt,du[y]++; } void update(int now,int l,int r,int pos,int V){ if(l==r){t[now]=V;return;} int mid=(l+r)>>1; if(pos<=mid)update(lson,l,mid,pos,V); else update(rson,mid+1,r,pos,V); t[now]=min(t[lson],t[rson]); } int query(int now,int l,int r,int L,int R){ if(L<=l && r<=R)return t[now]; int mid=(l+r)>>1,res=INF; if(L<=mid)res=query(lson,l,mid,L,R); if(mid<R)res=min(res,query(rson,mid+1,r,L,R)); return res; } void topo(){ for(int i=1;i<=n;i++)if(!du[i])q.push(i); while(!q.empty()){ int now=q.top();q.pop(); ans[now]=++tot; for(int i=hd[now];i;i=e[i].nex){ du[e[i].to]--; if(!du[e[i].to])q.push(e[i].to); } } } int main(){ // freopen("testdata.in","r",stdin); // freopen("shit.out","w",stdout); int tmp; memset(t,INF,sizeof(t)); n=rd(),k=rd(); for(int i=1;i<=n;i++)p[rd()]=i; for(int i=n;i>=1;i--){ tmp=query(1,1,n,p[i],min(n,p[i]+k-1)); if(tmp<INF)adde(p[i],p[tmp]); tmp=query(1,1,n,max(1,p[i]-k+1),p[i]); if(tmp<INF)adde(p[i],p[tmp]); update(1,1,n,p[i],i); } topo(); for(int i=1;i<=n;i++)printf("%d\n",ans[i]); // printf("%.2lf\n",(double)clock()/CLOCKS_PER_SEC); return 0; }
codenet
-1
1,736
s428126499
p04052
1,524,684,301
cpp
Accepted
871
49,408
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() const int MN = 500010; const int SZ = 1 << 19; struct segtree_max { vector<pii> dat; void init() { dat.resize(SZ * 2); rep(i, SZ * 2) dat[i] = mp(INT_MIN, -1); } void update(int pos, int x) { pos += SZ - 1; dat[pos] = mp(x, pos - (SZ - 1)); while (pos > 0) { pos = (pos - 1) / 2; dat[pos] = max(dat[pos * 2 + 1], dat[pos * 2 + 2]); } } pii get(int a, int b, int k = 0, int l = 0, int r = SZ) { if (b <= l || r <= a) return mp(INT_MIN, -1); if (a <= l && r <= b) return dat[k]; return max(get(a, b, k * 2 + 1, l, (l + r) / 2), get(a, b, k * 2 + 2, (l + r) / 2, r)); } } seg; int N, K; int P[MN], Q[MN]; vi g[MN]; int in[MN]; int main() { scanf("%d %d", &N, &K); rep(i, N) { scanf("%d", &P[i]); --P[i]; Q[P[i]] = i; } seg.init(); rep(i, N) { int p = Q[i]; pii r = seg.get(p, min(N, p + K)); pii l = seg.get(max(0, p + 1 - K), p); if (r.fi != INT_MIN) { ++in[p]; g[r.se].pb(p); } if (l.fi != INT_MIN) { ++in[p]; g[l.se].pb(p); } seg.update(p, i); } priority_queue<int, vi, greater<int>> que; rep(i, N) { if (in[i] == 0) { que.push(i); } } vi res(N), ans(N); int now = 0; while (!que.empty()) { int v = que.top(); que.pop(); res[now++] = v; for (int to : g[v]) { if (--in[to] == 0) { que.push(to); } } } rep(i, N) ans[res[i]] = i; rep(i, N) printf("%d\n", ans[i] + 1); return 0; }
codenet
-1
1,737
s380007927
p04052
1,509,136,716
cpp
Accepted
947
42,360
#include<iostream> #include<cstdio> #include<queue> using std::min; const int MAXN=5e5+5,INF=~0U>>1; int n,K; int seq[MAXN],pos[MAXN]; const int rt=1; struct SEGTN{int l,r,lc,rc;int mnv;} t[MAXN<<2];int tcnt=rt; inline void upd(int o){t[o].mnv=min(t[t[o].lc].mnv,t[t[o].rc].mnv);} void buildTree(int o,int l,int r) { t[o].l=l,t[o].r=r; if(l==r) {t[o].mnv=INF;return;} int mid=(l+r)>>1; t[o].lc=++tcnt;buildTree(t[o].lc,l,mid); t[o].rc=++tcnt;buildTree(t[o].rc,mid+1,r); upd(o); } void ch(int o,int p,int v) { if(t[o].l==t[o].r) {t[o].mnv=v;return;} int mid=(t[o].l+t[o].r)>>1; if(p<=mid) ch(t[o].lc,p,v); else ch(t[o].rc,p,v); upd(o); } int calMin(int o,int l,int r) { if(l<=t[o].l&&t[o].r<=r) return t[o].mnv; int mid=(t[o].l+t[o].r)>>1; int res=INF; if(l<=mid) res=min(res,calMin(t[o].lc,l,r)); if(r>mid) res=min(res,calMin(t[o].rc,l,r)); return res; } int inDgr[MAXN]; struct E{int next,to;} e[MAXN<<1];int ecnt,G[MAXN]; void addEdge(int u,int v){e[++ecnt]=(E){G[u],v};G[u]=ecnt;inDgr[v]++;} inline void buildGraph() { buildTree(rt,1,n); int u,v; for(v=n;v;v--) { u=calMin(rt,pos[v]-K+1,pos[v]); if(u<=n) addEdge(pos[u],pos[v]); u=calMin(rt,pos[v],pos[v]+K-1); if(u<=n) addEdge(pos[u],pos[v]); ch(rt,pos[v],v); } } std::priority_queue<int> heap; inline void toposort() { int i; for(i=1;i<=n;i++) if(!inDgr[i]) heap.push(i); for(int v=n;v;v--) { int u=pos[v]=heap.top();heap.pop(); for(i=G[u];i;i=e[i].next) if(--inDgr[e[i].to]==0) heap.push(e[i].to); } } int main() { int i; scanf("%d%d",&n,&K); for(i=1;i<=n;i++) scanf("%d",seq+i); for(i=1;i<=n;i++) pos[seq[i]]=i; buildGraph(); toposort(); for(i=1;i<=n;i++) seq[pos[i]]=i; for(i=1;i<=n;i++) printf("%d\n",seq[i]); return 0; }
codenet
-1
1,745
s094534156
p04052
1,508,380,317
cpp
Accepted
1,346
87,672
#include <iostream> #include <vector> #include <queue> using namespace std; typedef long long ll; typedef unsigned long long ull; 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; 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]; g[q[i]].push_back(qv); } } { int v = rmq->get(q[i], min(n, q[i]+k)); if (v != -1) { int qv = q[v]; g[q[i]].push_back(qv); } rmq->set(q[i], i); } rmq->set(q[i], 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++) ans[v[i]] = i+1; for (int i = 0; i < n; i++) printf("%d\n", ans[i]); }
codenet
-1
1,752
s901013716
p04052
1,579,492,737
cpp
Accepted
456
29,684
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dep(i,a,b) for(int i=a;i>=b;i--) #define lb(a) lower_bound(a) #define is(a) insert(a) #define ps(a) push(a) const int N=5e5+10; const int inf=0x3f3f3f3f; int in[N],loc[N],p[N],ans[N],tot,n,k; std::priority_queue <int,std::vector<int>,std::greater<int> > q; int head[N],to[N<<1],Next[N<<1],cnt; void add(int u,int v) { to[++cnt]=v,Next[cnt]=head[u],head[u]=cnt; } #define ls id<<1 #define rs id<<1|1 int min(int x,int y){return x<y?x:y;} int max(int x,int y){return x>y?x:y;} int mi[N<<2]; void change(int id,int l,int r,int p,int d) { if(l==r) {mi[id]=d;return;} int mid=l+r>>1; if(p<=mid) change(ls,l,mid,p,d); else change(rs,mid+1,r,p,d); mi[id]=min(mi[ls],mi[rs]); } int query(int id,int L,int R,int l,int r) { if(mi[id]==inf) return inf; if(l==L&&r==R) return mi[id]; int Mid=L+R>>1; if(r<=Mid) return query(ls,L,Mid,l,r); else if(l>Mid) return query(rs,Mid+1,R,l,r); else return min(query(ls,L,Mid,l,Mid),query(rs,Mid+1,R,Mid+1,r)); } int main() { scanf("%d%d",&n,&k); rep(i,1,n) scanf("%d",p),p[p[0]]=i; memset(mi,0x3f,sizeof(mi)); dep(i,n,1) { int loc=query(1,1,n,p[i],min(n,p[i]+k-1)); if(loc!=inf) add(p[i],p[loc]),++in[p[loc]]; loc=query(1,1,n,max(1,p[i]-k+1),p[i]); if(loc!=inf) add(p[i],p[loc]),++in[p[loc]]; change(1,1,n,p[i],i); } rep(i,1,n) if(!in[i]) q.ps(i); while(!q.empty()) { int now=q.top(); ans[now]=++tot; q.pop(); for(int i=head[now];i;i=Next[i]) { int v=to[i]; --in[v]; if(!in[v]) q.ps(v); } } rep(i,1,n) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,757
s931599651
p04052
1,580,356,298
cpp
Accepted
565
27,008
#include<bits/stdc++.h> #define LL long long using namespace std; const int N=5e5+10,M=1e8+10; int rd() { int x=0,w=1;char ch=0; while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();} return x*w; } int to[N<<1],nt[N<<1],dg[N],hd[N],tot=1; void adde(int x,int y){++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot,++dg[y];} int n,m,a[N],an[N]; int mi[N<<2]; void psup(int o){mi[o]=min(mi[o<<1],mi[o<<1|1]);} void modif(int o,int l,int r,int lx,int x) { if(l==r){mi[o]=x;return;} int mid=(l+r)>>1; if(lx<=mid) modif(o<<1,l,mid,lx,x); else modif(o<<1|1,mid+1,r,lx,x); psup(o); } int quer(int o,int l,int r,int ll,int rr) { if(ll>rr) return n+1; if(ll<=l&&r<=rr) return mi[o]; int an=n+1,mid=(l+r)>>1; if(ll<=mid) an=min(an,quer(o<<1,l,mid,ll,rr)); if(rr>mid) an=min(an,quer(o<<1|1,mid+1,r,ll,rr)); psup(o); return an; } void bui(int o,int l,int r) { if(l==r){mi[o]=n+1;return;} int mid=(l+r)>>1; bui(o<<1,l,mid),bui(o<<1|1,mid+1,r); psup(o); } struct node { int x; bool operator < (const node &bb) const {return a[x]>a[bb.x];} }; priority_queue<node> q; int main() { //// n=rd(),m=rd(); for(int i=1;i<=n;++i) a[rd()]=i; bui(1,1,n); for(int i=n;i;--i) { int n1=quer(1,1,n,max(a[i]-m+1,1),a[i]-1),n2=quer(1,1,n,a[i]+1,min(a[i]+m-1,n)); if(n1<=n) adde(i,n1); if(n2<=n) adde(i,n2); modif(1,1,n,a[i],i); } for(int i=1;i<=n;++i) if(!dg[i]) q.push((node){i}); for(int h=1;h<=n;++h) { int x=q.top().x; q.pop(); an[a[x]]=h; for(int i=hd[x];i;i=nt[i]) { int y=to[i]; --dg[y]; if(!dg[y]) q.push((node){y}); } } for(int i=1;i<=n;++i) printf("%d\n",an[i]); return 0; }
codenet
-1
1,759
s369816309
p04052
1,519,339,770
cpp
Accepted
449
38,656
// #include {{{ #include <iostream> #include <cassert> #include <cstring> #include <cstdlib> #include <cstdio> #include <cctype> #include <cmath> #include <ctime> #include <queue> #include <set> #include <map> #include <stack> #include <string> #include <bitset> #include <vector> #include <complex> #include <algorithm> using namespace std; // }}} // #define {{{ typedef long long ll; typedef double db; typedef pair<int,int> pii; typedef vector<int> vi; #define de(x) cout << #x << "=" << x << endl #define rep(i,a,b) for(int i=a;i<(b);++i) #define per(i,a,b) for(int i=(b)-1;i>=(a);--i) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define fi first #define se second // }}} const int N = 5e5 + 10 , inf = 1e9 + 7; int n , K , p[N] , a[N << 2]; int Min(int c,int l,int r,int L,int R) { if(L > R) return inf; if(L <= l && r <= R) return a[c]; int m = (l + r) >> 1; int res = inf; if(L <= m) res = min(res , Min(c << 1 , l , m , L , R)); if(R > m) res = min(res , Min(c << 1 | 1 , m + 1 , r , L , R)); return res; } void Update(int c,int l,int r,int p,int x) { if(l == r) a[c] = x; else { int m = (l + r) >> 1; if(p <= m) Update(c << 1 , l , m , p , x); else Update(c << 1 | 1 , m + 1 , r , p , x); a[c] = min(a[c << 1] , a[c << 1 | 1]); } } int tot , vis[N] , ans[N]; void solve(int c) { while(true) { int t = Min(1 , 0 , n - 1 , 0 , p[c] - 1); if(t >= c + K) break; solve(t); } ans[c] = ++tot; vis[c] = true; Update(1 , 0 , n - 1 , p[c] , inf); } int main(){ scanf("%d%d",&n,&K); rep(i,0,n) scanf("%d",p+i) , Update(1 , 0 , n - 1 , --p[i] , i); rep(i,0,n) if(!vis[i]) solve(i); rep(i,0,n) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,762
s040537873
p04052
1,541,704,457
cpp
Accepted
759
42,624
#include<bits/stdc++.h> using namespace std; #define ll long long #define PI pair<int,int> #define mp make_pair inline ll read(){ bool pos=1; ll x=0; char ch=getchar(); for(;!isdigit(ch);ch=getchar())if(ch=='-')pos=0; for(;isdigit(ch);ch=getchar())x=x*10+ch-'0'; return pos?x:-x; } void write(ll x){ if(x<0){ x=-x; putchar('-'); } if(x>=10)write(x/10); putchar(x%10+'0'); } void writeln(ll x){ write(x); puts(""); } const int N=500005; int n,k,rd[N],ans[N],pos[N],tree[N<<2]; priority_queue<int> q; vector<int> v[N]; void insert(int l,int r,int pos,int de,int nod){ if(l==r){ tree[nod]=de; return; } int mid=(l+r)>>1; if(pos<=mid)insert(l,mid,pos,de,nod<<1); else insert(mid+1,r,pos,de,nod<<1|1); tree[nod]=max(tree[nod<<1],tree[nod<<1|1]); } int ask(int l,int r,int i,int j,int nod){ if(i>j)return 0; if(l==i&&r==j)return tree[nod]; int mid=(l+r)>>1; if(j<=mid)return ask(l,mid,i,j,nod<<1); else if(i>mid)return ask(mid+1,r,i,j,nod<<1|1); else return max(ask(l,mid,i,mid,nod<<1),ask(mid+1,r,mid+1,j,nod<<1|1)); } int main(){ n=read(); k=read(); for(int i=1;i<=n;i++)pos[read()]=i; for(int i=1;i<=n;i++){ int x=ask(1,n,max(pos[i]-k+1,1),pos[i]-1,1); if(x){v[pos[i]].push_back(pos[x]); rd[pos[x]]++;} x=ask(1,n,pos[i]+1,min(pos[i]+k-1,n),1); if(x){v[pos[i]].push_back(pos[x]); rd[pos[x]]++;} insert(1,n,pos[i],i,1); } for(int i=1;i<=n;i++)if(rd[i]==0)q.push(i); for(int i=n;i;i--){ int t=q.top(); q.pop(); ans[t]=i; for(unsigned j=0;j<v[t].size();j++){ if(--rd[v[t][j]]==0){ q.push(v[t][j]); } } } for(int i=1;i<=n;i++){ write(ans[i]); putchar(' '); } } /* 每次找一个最大位置使得可以和当前所有数换 to1 to2 to3 to4 to5 to6 4 6 x->y 连边当且仅当 x>y并且abs(ax-ay)<k 发现只需要连O(n)条边 线段树+堆支持即可 //为出题人点赞 8 3 4 5 7 8 3 1 2 6 */
codenet
-1
1,763
s847470685
p04052
1,567,027,697
cpp
Accepted
1,383
82,048
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e6+50; int n, k, a[N], pos[N], deg[N], f[N]; vector<int> g[N]; int main() { scanf("%d%d", &n, &k); REP(i,1,n) scanf("%d", a+i); REP(i,1,n) pos[a[i]]=i; set<int> L, R; REP(i,1,k) R.insert(a[i]); REP(i,1,n) { auto p = L.upper_bound(a[i]); if (p!=L.end()) { g[pos[*p]].pb(i); ++deg[i]; } p = R.upper_bound(a[i]); if (p!=R.end()) { g[pos[*p]].pb(i); ++deg[i]; } L.insert(a[i]), R.erase(a[i]); if (i-k+1>0) L.erase(a[i-k+1]); if (i+k<=n) R.insert(a[i+k]); } priority_queue<int> q; REP(i,1,n) if (!deg[i]) q.push(i); PER(i,1,n) { int u = q.top(); q.pop(); f[i] = u; for (int v:g[u]) { if (!--deg[v]) q.push(v); } } REP(i,1,n) a[f[i]] = i; REP(i,1,n) printf("%d\n",a[i]); }
codenet
-1
1,777
s847470685
p04052
1,567,027,697
cpp
Accepted
1,383
82,048
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e6+50; int n, k, a[N], pos[N], deg[N], f[N]; vector<int> g[N]; int main() { scanf("%d%d", &n, &k); REP(i,1,n) scanf("%d", a+i); REP(i,1,n) pos[a[i]]=i; set<int> L, R; REP(i,1,k) R.insert(a[i]); REP(i,1,n) { auto p = L.upper_bound(a[i]); if (p!=L.end()) { g[pos[*p]].pb(i); ++deg[i]; } p = R.upper_bound(a[i]); if (p!=R.end()) { g[pos[*p]].pb(i); ++deg[i]; } L.insert(a[i]), R.erase(a[i]); if (i-k+1>0) L.erase(a[i-k+1]); if (i+k<=n) R.insert(a[i+k]); } priority_queue<int> q; REP(i,1,n) if (!deg[i]) q.push(i); PER(i,1,n) { int u = q.top(); q.pop(); f[i] = u; for (int v:g[u]) { if (!--deg[v]) q.push(v); } } REP(i,1,n) a[f[i]] = i; REP(i,1,n) printf("%d\n",a[i]); }
codenet
-1
1,777
s093245846
p04052
1,539,023,947
cpp
Accepted
1,045
44,672
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<set> typedef long long ll; typedef double dd; #define For(i,j,k) for (int i=(int)j;i<=(int)k;++i) #define Forr(i,j,k) for (int i=(int)j;i>=(int)k;--i) #define Set(a,p) memset(a,p,sizeof(a)) using namespace std; template<typename T>bool chkmax(T &a,T b) { return a<b?a=b,1:0; } template<typename T>bool chkmin(T &a,T b) { return a>b?a=b,1:0; } const int maxn=5e5+1e2; struct Edge { int to,nxt; }e[maxn<<1]; int n,m; int tot; int a[maxn],p[maxn],Ans[maxn],cnt[maxn],head[maxn]; set<int>pre,suf; priority_queue<int,vector<int>,less<int> >q; inline int read() { int x=0,p=1; char c=getchar(); while (!isdigit(c)) { if (c=='-') p=-1; c=getchar();} while (isdigit(c)) { x=(x<<1)+(x<<3)+(c-'0'); c=getchar();} return x*p; } inline void Erase(set<int>&s,int p) { set<int>::iterator it=s.lower_bound(p); s.erase(it); } inline void add(int from,int to) { e[++tot]=(Edge){to,head[from]}; head[from]=tot; ++cnt[to]; } inline void init() { n=read(); m=read(); For (i,1,n) a[i]=read(),p[a[i]]=i; For (i,0,m-1) suf.insert(a[i]); For (i,1,n) { pre.insert(a[i]); Erase(suf,a[i-1]); if (i+m-1<=n) suf.insert(a[i+m-1]); if (i-m>=1) Erase(pre,a[i-m]); set<int>::iterator it1=pre.upper_bound(a[i]); set<int>::iterator it2=suf.upper_bound(a[i]); if (it1!=pre.end()) add(p[*it1],i); if (it2!=suf.end()) add(p[*it2],i); } } inline void solve() { For (i,1,n) if (!cnt[i]) q.push(i); int ans=0; while (!q.empty()) { int u=q.top(); q.pop(); Ans[u]=n-ans; ++ans; for (int i=head[u];i!=0;i=e[i].nxt) { int v=e[i].to; --cnt[v]; if (!cnt[v]) q.push(v); } } For (i,1,n) printf("%d\n",Ans[i]); } int main() { init(); solve(); return 0; }
codenet
-1
1,780
s809772772
p04052
1,590,021,135
cpp
Accepted
234
29,816
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> P; template<typename T> inline void chkmin(T &a, const T &b) { a = a < b ? a : b; } template<typename T> inline void chkmax(T &a, const T &b) { a = a > b ? a : b; } const int MAXN = 500005, MAXT = 1 << 21; namespace ST { int mn[MAXT], tn; void build(int n) { memset(mn, 0x3f, sizeof(mn)); for (tn = 1; tn <= n + 1; tn <<= 1); } void modify(int p, int x) { p += tn; for (; p; p >>= 1) chkmin(mn[p], x); } int query(int l, int r) { if (l > r) return 1e9; l += tn - 1, r += tn + 1; int ans = 1e9; for (; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) chkmin(ans, mn[l ^ 1]); if ( r & 1) chkmin(ans, mn[r ^ 1]); } return ans; } } struct Edge { int to, next; } edge[MAXN << 1]; int pp[MAXN], qq[MAXN], head[MAXN], deg[MAXN], ans[MAXN], n, K, tot; void add_edge(int u, int v) { edge[++tot] = Edge { v, head[u] }; head[u] = tot; ++deg[v]; } priority_queue<int> pq; int main() { scanf("%d%d", &n, &K); for (int i = 1; i <= n; i++) { scanf("%d", pp + i); qq[pp[i]] = i; } ST::build(n); for (int i = n; i > 0; i--) { int l = max(1, qq[i] - K + 1), r = min(qq[i] + K - 1, n); int p = ST::query(l, qq[i] - 1); if (p <= n) add_edge(qq[p], qq[i]); p = ST::query(qq[i] + 1, r); if (p <= n) add_edge(qq[p], qq[i]); ST::modify(qq[i], i); } int cnt = 0; for (int i = 1; i <= n; i++) if (!deg[i]) pq.push(i); while (!pq.empty()) { int u = pq.top(); pq.pop(); pp[++cnt] = u; for (int i = head[u]; i; i = edge[i].next) { int v = edge[i].to; if (!--deg[v]) pq.push(v); } } reverse(pp + 1, pp + cnt + 1); assert(cnt == n); for (int i = 1; i <= n; i++) ans[pp[i]] = i; for (int i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,783
s845491406
p04052
1,592,840,247
cpp
Accepted
1,443
16,308
#include<bits/stdc++.h> using namespace std; typedef unsigned long long int ull; typedef /*long long*/ int ll; typedef pair<ll,ll> pll; typedef long double D; //typedef complex<D> P; #define F first #define S second const ll MOD=1000000007; //const ll MOD=998244353; template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;} template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;} template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;} template<typename T>ostream & operator << (ostream &o,const vector<T> &A){int i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;} int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N,K; cin>>N>>K; vector<ll> A(N),B(N); cin>>A; vector<ll> bef(N); vector<ll> ans(N); for(int i=0;i<N;i++){A[i]--;B[A[i]]=i;} for(int i=1;i<N;i<<=1){ for(int j=0;j+i<N;j+=i<<1){ int lf=j,mid=j+i,rg=min<ll>(N,j+(i<<1)); priority_queue<pll,vector<pll>,greater<pll>> Q1,Q2; for(int k=lf;k<mid;k++){Q1.push({B[k],k-lf});} for(int k=mid;k<rg;k++){Q2.push({B[k],k-mid});} int mx=-1; while(!Q2.empty()){ pll a=Q2.top(); Q2.pop(); while(!Q1.empty() && Q1.top().F<a.F+K){ mx=max<ll>(mx,Q1.top().S); Q1.pop(); } bef[a.S]=mx; } for(int k=1;k<rg-mid;k++){bef[k]=max(bef[k],bef[k-1]);} int idx=0; int k=0; for(int l=0;l<mid-lf;l++){ while(k<rg-mid && bef[k]<l){ans[idx++]=B[k+mid]; k++;} ans[idx++]=B[l+lf]; } for(int i=0;i<idx;i++){B[lf+i]=ans[i];} } } for(int i=0;i<N;i++){A[B[i]]=i;} for(auto &I:A){cout<<I+1<<endl;} return 0; }
codenet
-1
1,788
s069087754
p04052
1,543,201,075
cpp
Accepted
458
27,764
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #include <queue> using namespace std; const int MAXN = 5e5 + 10; const int MAXM = 1e6 + 10; int N, K; int P[MAXN], A[MAXN]; int cnt = 1, Head[MAXN], Next[MAXM], To[MAXM]; int deg[MAXN]; int tr[MAXN << 2]; inline void add_edge(int x, int y) { Next[++cnt] = Head[x], To[cnt] = y, Head[x] = cnt; ++deg[y]; } inline void build(int rt, int l, int r) { tr[rt] = N + 1; if(l == r) return; int mid = (l + r) >> 1; build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r); } inline void upd(int rt, int l, int r, int x, int d) { if(l == r) return tr[rt] = d, void(); int mid = (l + r) >> 1; if(x <= mid) upd(rt << 1, l, mid, x, d); else upd(rt << 1 | 1, mid + 1, r, x, d); tr[rt] = min(tr[rt << 1], tr[rt << 1 | 1]); } inline int qry(int rt, int l, int r, int x, int y) { if(x <= l && r <= y) return tr[rt]; if(y < l || r < x) return N + 1; int mid = (l + r) >> 1, res = N + 1; if(x <= mid) res = min(res, qry(rt << 1, l, mid, x, y)); if(mid < y) res = min(res, qry(rt << 1 | 1, mid + 1, r, x, y)); return res; } int main() { register int i; scanf("%d%d", &N, &K); for(i = 1; i <= N; ++i) scanf("%d", P + i), A[P[i]] = i; build(1, 1, N); for(i = N; i; --i) { int tp1 = qry(1, 1, N, max(A[i] - K + 1, 1), A[i]); int tp2 = qry(1, 1, N, A[i], min(A[i] + K - 1, N)); if(tp1 <= N) add_edge(A[i], A[tp1]); if(tp2 <= N) add_edge(A[i], A[tp2]); upd(1, 1, N, A[i], i); } priority_queue<int> q; for(i = 1; i <= N; ++i) if(!deg[i]) q.push(-i); int t = 0; while(!q.empty()) { int x = -q.top(); q.pop(); P[x] = ++t; for(i = Head[x]; i; i = Next[i]) if(!(--deg[To[i]])) q.push(-To[i]); } for(i = 1; i <= N; ++i) printf("%d\n", P[i]); return 0; }
codenet
-1
1,790
s721201865
p04052
1,549,161,889
cpp
Accepted
475
31,352
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<climits> #include<queue> using namespace std; inline int read(){ int f=1,ans=0;char c; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();} return ans*f; } const int N=500001; int n,a[N],minn[N<<2],k; struct node{ int u,v,nex; }x[N<<1]; int head[N]; struct segment_tree{ void insert(int k,int l,int r,int x,int y,int val){ if(l==r){minn[k]=val;return;} int mid=l+r>>1; if(x<=mid) insert(k<<1,l,mid,x,y,val); if(mid<y) insert(k<<1|1,mid+1,r,x,y,val); minn[k]=min(minn[k<<1],minn[k<<1|1]); return; } int query(int k,int l,int r,int x,int y){ // printf("x:%d y:%d\n",x,y); if(x>y) return INT_MAX; if(x<=l&&r<=y) return minn[k]; int res=INT_MAX,mid=l+r>>1; if(x<=mid) res=min(res,query(k<<1,l,mid,x,y)); if(mid<y) res=min(res,query(k<<1|1,mid+1,r,x,y)); return res; } }seg; int cnt,du[N]; void add(int u,int v){ // printf("u:%d v:%d\n",u,v); x[cnt].u=u,x[cnt].v=v,x[cnt].nex=head[u],head[u]=cnt++; du[v]++; return; } priority_queue<int> que; int id[N]; int main(){ memset(head,-1,sizeof(head)); int inf=INT_MAX; memset(minn,127/3,sizeof(minn)); n=read(),k=read(); for(int i=1;i<=n;i++) a[read()]=i; for(int i=n;i>=1;i--){ int pos=seg.query(1,1,n,a[i]+1,min(n,a[i]+k-1)); if(pos>=1&&pos<=n) add(a[i],a[pos]); pos=seg.query(1,1,n,max(1,a[i]-k+1),a[i]-1); if(pos>=1&&pos<=n) add(a[i],a[pos]); seg.insert(1,1,n,a[i],a[i],i); } for(int i=1;i<=n;i++) if(!du[i]) que.push(-i); while(!que.empty()){ int xx=-que.top();que.pop(); id[xx]=++id[0]; for(int i=head[xx];i!=-1;i=x[i].nex){ du[x[i].v]--; if(!du[x[i].v]) que.push(-x[i].v); } } for(int i=1;i<=n;i++) printf("%d\n",id[i]); }
codenet
-1
1,791
s392018924
p04052
1,507,999,837
cpp
Accepted
665
42,368
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<set> using namespace std; int n,m,a[500010],x[500010],f[2000010],g[2000010],p; set<int> w; inline void add(int i,int j,int k,int p) { f[i]++; if(j<k) if(p<=(j+k>>1)) add(i<<1,j,j+k>>1,p); else add(i<<1|1,(j+k>>1)+1,k,p); } inline int sum(int i,int j,int k,int l,int r) { if(l<=j && k<=r) return f[i]; else { int p=0; if(l<=(j+k>>1)) p+=sum(i<<1,j,j+k>>1,l,r); if(r>(j+k>>1)) p+=sum(i<<1|1,(j+k>>1)+1,k,l,r); return p; } } inline void down(int i) { f[i<<1]-=g[i]; g[i<<1]+=g[i]; f[i<<1|1]-=g[i]; g[i<<1|1]+=g[i]; g[i]=0; } inline void upd(int i) { f[i]=min(f[i<<1],f[i<<1|1]); } inline void del(int i,int j,int k,int l,int r) { if(l<=j && k<=r) if(f[i]>1) { f[i]--; g[i]++; } else if(j<k) { down(i); del(i<<1,j,j+k>>1,l,r); del(i<<1|1,(j+k>>1)+1,k,l,r); upd(i); } else { f[i]=1e9; w.insert(-(i-p+1)); } else { down(i); if(l<=(j+k>>1)) del(i<<1,j,j+k>>1,l,r); if(r>(j+k>>1)) del(i<<1|1,(j+k>>1)+1,k,l,r); upd(i); } } int main() { //freopen(".in","r",stdin); //freopen(".out","w",stdout); int i,j,k; scanf("%d%d",&n,&m); m--; for(i=1;i<=n;i++) { scanf("%d",&j); a[j]=i; } for(p=1;p<n;p<<=1); for(i=n;i>0;i--) { x[a[i]]=sum(1,1,p,max(a[i]-m,1),min(a[i]+m,n)); add(1,1,p,a[i]); } for(i=1;i<=n;i++) f[p+i-1]=x[i]+1; for(i=p-1;i>0;i--) f[i]=min(f[i<<1],f[i<<1|1]); del(1,1,p,1,n); for(i=n;i>0;i--) { k=-*w.lower_bound(-1e9); x[k]=i; del(1,1,p,max(k-m,1),min(k+m,n)); w.erase(-k); } for(i=1;i<=n;i++) printf("%d\n",x[i]); return 0; }
codenet
-1
1,794
s454656374
p04052
1,573,527,822
cpp
Accepted
498
31,088
/* 筆者:DriverBen コメント: */ #include<bits/stdc++.h> #define inf 0x3f3f3f3f #define ls t[rt].l #define rs t[rt].r using namespace std; const int maxn=500000+100; int head[maxn]; int p[maxn],in[maxn],a[maxn]; struct edge{ int to,next; }e[maxn<<1]; struct segment_tree{ int l,r,mi; segment_tree(){mi=inf;} }t[maxn<<1]; int size=0,cnt,root,v,res,n,k; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return x*f; } void addedge(int u,int v){ e[++size].to=v;e[size].next=head[u];head[u]=size; } void build(int l,int r,int &rt){ if(!rt)rt=++cnt; if(l==r)return; int m=(l+r)>>1; build(l,m,ls); build(m+1,r,rs); } void query(int l,int r,int rt,int L,int R){ if(L<=l&&r<=R){ res=min(res,t[rt].mi); return; } int m=(l+r)>>1; if(L<=m)query(l,m,ls,L,R); if(m<R)query(m+1,r,rs,L,R); } void update(int l,int r,int rt,int pos,int c){ if(l==r){ t[rt].mi=c; return; } int m=(l+r)>>1; if(pos<=m)update(l,m,ls,pos,c); else update(m+1,r,rs,pos,c); t[rt].mi=min(t[ls].mi,t[rs].mi); } void topo(){ priority_queue<int,vector<int>,greater<int> >q; for(int i=1;i<=n;++i)if(!in[i])q.push(i); while(!q.empty()){ int u=q.top(); q.pop(); a[u]=++v; for(int i=head[u];i;i=e[i].next){ int to=e[i].to; in[to]--; if(!in[to])q.push(to); } } } int main(){ n=read();k=read(); for(int i=1;i<=n;++i)p[read()]=i; build(1,n,root); for(int i=n;i;--i){ res=inf; query(1,n,1,min(p[i]+1,n),min(p[i]+k-1,n)); if(res!=inf)addedge(p[i],p[res]),in[p[res]]++; res=inf; query(1,n,1,max(p[i]-k+1,1),max(p[i]-1,1)); if(res!=inf)addedge(p[i],p[res]),in[p[res]]++; update(1,n,1,p[i],i); } topo(); for(int i=1;i<=n;++i)printf("%d\n",a[i]); return 0; }
codenet
-1
1,795
s574752390
p04052
1,551,401,382
cpp
Accepted
2,040
73,856
#include <bits/stdc++.h> using namespace std; const int N = 500010; int n, k; int a[N]; int v[N]; vector<int> g[N]; int deg[N]; struct Node; typedef Node* NP; struct Node { NP l, r; int sz; int mi; Node(int _sz) : sz(_sz) { mi = N; if (sz == 1) return; l = new Node(sz/2); r = new Node(sz-sz/2); } void set(int k, int v) { if (sz == 1) { mi = v; return; } if (k < sz/2) { l->set(k, v); } else { r->set(k-sz/2, v); } mi = v; } int get(int a, int b) { if (b <= 0 || sz <= a) return N; if (a <= 0 && sz <= b) return mi; return min(l->get(a, b), r->get(a-sz/2, b-sz/2)); } }; void make(int x, int y) { if (y == N) return; y = a[y]; deg[y]++; g[x].push_back(y); } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> k; for (int i = 0; i < n; i++) { int x; cin >> x; x--; a[x] = i; } NP tr = new Node(n); for (int i = n-1; i >= 0; i--) { int cr = a[i]; make(cr, tr->get(max(0, cr-k+1), cr)); make(cr, tr->get(min(n, cr+1), min(n, cr+k))); tr->set(cr, i); } set<int> que; for (int i = 0; i < n; i++) { if (deg[i] == 0) { que.insert(i); } } int cr = 0; while (que.size()) { int c = *que.begin(); que.erase(que.begin()); v[c] = cr++; for (int i = 0; i < int(g[c].size()); i++) { int d = g[c][i]; deg[d]--; if (deg[d] == 0) { que.insert(d); } } } for (int i = 0; i < n; i++) { cout << v[i]+1 << endl; } return 0; }
codenet
-1
1,796
s635265651
p04052
1,588,052,564
cpp
Accepted
773
22,912
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, inf = 1e8; int b[N], a[N], n, k, fen[N], val, seg[N << 2], lazy[N << 2], ans[N]; // fen void add(int p) { for (p++; p <= n; p += p & -p) fen[p]++; } int getPref(int p) { int rtr = 0; for (; p > 0; p -= p & -p) rtr += fen[p]; return rtr; } int get(int l, int r) { return getPref(r) - getPref(l); } // fen // seg void relax(int id) { int lc = id * 2, rc = lc + 1; seg[lc] += lazy[id], seg[rc] += lazy[id]; lazy[lc] += lazy[id], lazy[rc] += lazy[id]; lazy[id] = 0; } void addseg(int l, int r, int id = 1, int s = 0, int e = n) { if (e <= l || r <= s) return; if (l <= s && e <= r) { seg[id] += val; lazy[id] += val; return; } int mid = (s + e) / 2, lc = id * 2, rc = lc + 1; relax(id); addseg(l, r, lc, s, mid); addseg(l, r, rc, mid, e); seg[id] = min(seg[lc], seg[rc]); } int getseg(int id = 1, int s = 0, int e = n) { if (e - s == 1) return s; relax(id); int mid = (s + e) / 2, lc = id * 2, rc = lc + 1; if (seg[rc] == 0) return getseg(rc, mid, e); return getseg(lc, s, mid); } // seg // main int main() { // input ios_base::sync_with_stdio (false), cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) cin >> b[i], b[i]--; for (int i = 0; i < n; i++) a[b[i]] = i; // input // getDegree for (int i = n - 1; i >= 0; i--) { val = get(max(0, a[i] - k + 1), min(n, a[i] + k)); addseg(a[i], a[i] + 1); add(a[i]); } // getDegree // get most right ziro for (int i = 0; i < n; i++) { int plc = getseg(); ans[plc] = n - i - 1; val = -1; addseg(max(0, plc - k + 1), min(n, plc + k)); val = inf; addseg(plc, plc + 1); } // getmost right ziro //outout for (int i = 0; i < n; i++) cout << ans[i] + 1 << "\n"; //output return 0; } // main
codenet
-1
1,798
s178544313
p04052
1,571,177,928
cpp
Accepted
653
58,880
#include<iostream> #include<cstdio> #include<queue> using namespace std; struct node { int l,r,data,miner; }; struct note { int v,next; }; node tree[5000001]; note edge[5000001]; long long a[5000001],p[5000001],s1,s2,ans[5000001],res[5000001],head[5000001],len,in[5000001],n,length; priority_queue<int>q; void add(int x,int y) { edge[++len].v=y; edge[len].next=head[x]; head[x]=len; in[y]++; return; } void build(int k,int l,int r) { tree[k].l=l; tree[k].r=r; tree[k].data=1e9; if (l==r) return; int mid=(l+r)/2; build(k*2,l,mid); build(k*2+1,mid+1,r); return; } void adder(int k,int x,int d) { if (tree[k].l==tree[k].r) { tree[k].data=d; return; } int mid=(tree[k].l+tree[k].r)/2; if (x<=mid) adder(k*2,x,d); else adder(k*2+1,x,d); tree[k].data=min(tree[k*2].data,tree[k*2+1].data); return; } int query(int k,int l,int r) { if (l>r) return 1e9; if (tree[k].l==l&&tree[k].r==r) return tree[k].data; int mid=(tree[k].l+tree[k].r)/2; if (l<=mid&&r<=mid) return query(k*2,l,r); if (l>=mid+1&&r>=mid+1) return query(k*2+1,l,r); return min(query(k*2,l,mid),query(k*2+1,mid+1,r)); } void top_sort() { int top; for (int i=1;i<=n;++i) if (in[i]==0) q.push(-i); while (!q.empty()) { top=-q.top(); q.pop(); ans[top]=++length; for (int i=head[top];i>0;i=edge[i].next) { in[edge[i].v]--; if (in[edge[i].v]==0) q.push(-edge[i].v); } } return; } int main() { int k; scanf("%d%d",&n,&k); for (int i=1;i<=n;++i) { scanf("%d",&a[i]); p[a[i]]=i; } build(1,1,n); for (int i=n;i>=1;--i) { s1=query(1,max(p[i]-k+1,1ll),p[i]-1); if (s1!=1e9) add(p[i],p[s1]); s2=query(1,p[i]+1,min(p[i]+k-1,n)); if (s2!=1e9) add(p[i],p[s2]); adder(1,p[i],i); } top_sort(); for (int i=1;i<=n;++i) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,807
s576113315
p04052
1,588,237,338
cpp
Accepted
1,065
9,584
//Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God; //#include<bits/stweight++.h> #include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <queue> #include <deque> #include <map> #include <stack> #include<bitset> #include<list> #include<cassert> #include<numeric> #include <stdio.h> #include <string.h> #include<iomanip> #include<unordered_map> #include<unordered_set> #include <iostream> #include <fstream> using namespace std; const int N = 5e5 + 5;; int p[N]; int q[N]; int c[N]; int n, k; void merge_sort(int l, int r) { if (r - l == 1) { return; } int mid = (l + r) / 2; merge_sort(l, mid); merge_sort(mid, r); for (int i = mid - 1, mn = N; i >= l; i--) { mn = min(mn, q[i]); c[i] = mn; } int pl = l; int pr = mid; int p = l; vector <int> vec; while (vec.size() != r - l) { if (pl == mid) { vec.push_back(q[pr++]); } else { if (pr == r) { vec.push_back(q[pl++]); } else { if (q[pr] + k <= c[pl]) { vec.push_back(q[pr++]); } else { vec.push_back(q[pl++]); } } } } for (int i = l; i < r; i++) { q[i] = vec[i - l]; } } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> p[i]; } for (int i = 0; i < n; i++) { q[p[i] - 1] = i; } merge_sort(0, n); for (int i = 0; i < n; i++) { p[q[i]] = i + 1; } for (int i = 0; i < n; i++) { cout << p[i] << endl; } }
codenet
-1
1,810
s576113315
p04052
1,588,237,338
cpp
Accepted
1,065
9,584
//Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God; //#include<bits/stweight++.h> #include<iostream> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <queue> #include <deque> #include <map> #include <stack> #include<bitset> #include<list> #include<cassert> #include<numeric> #include <stdio.h> #include <string.h> #include<iomanip> #include<unordered_map> #include<unordered_set> #include <iostream> #include <fstream> using namespace std; const int N = 5e5 + 5;; int p[N]; int q[N]; int c[N]; int n, k; void merge_sort(int l, int r) { if (r - l == 1) { return; } int mid = (l + r) / 2; merge_sort(l, mid); merge_sort(mid, r); for (int i = mid - 1, mn = N; i >= l; i--) { mn = min(mn, q[i]); c[i] = mn; } int pl = l; int pr = mid; int p = l; vector <int> vec; while (vec.size() != r - l) { if (pl == mid) { vec.push_back(q[pr++]); } else { if (pr == r) { vec.push_back(q[pl++]); } else { if (q[pr] + k <= c[pl]) { vec.push_back(q[pr++]); } else { vec.push_back(q[pl++]); } } } } for (int i = l; i < r; i++) { q[i] = vec[i - l]; } } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> p[i]; } for (int i = 0; i < n; i++) { q[p[i] - 1] = i; } merge_sort(0, n); for (int i = 0; i < n; i++) { p[q[i]] = i + 1; } for (int i = 0; i < n; i++) { cout << p[i] << endl; } }
codenet
-1
1,810
s220733659
p04052
1,506,888,026
cpp
Accepted
523
17,536
#include <cstdio> #include <algorithm> int N, K, a[500001], B[500001], T[1048576], A[1048576], O[500001]; int I() { int r = 0, c; do c = getchar(); while (c < 48 || c > 57); do r = (r << 3) + r + r + (c - 48), c = getchar(); while (c > 47 && c < 58); return r; } void P(int x, int d) { while (x <= N) { B[x] += d; x += x & -x; } } int G(int x) { int r = 0; while (x) { r += B[x]; x &= x - 1; } return r; } void build(int p, int l, int r) { if (l == r) { if (l + K - 1 <= N) P(a[l + K - 1], 1); if (l > K) P(a[l - K], -1); T[p] = G(a[l] - 1); return; } int m = l + r >> 1; build(p << 1, l, m); build(p << 1 | 1, m + 1, r); T[p] = std::min(T[p << 1], T[p << 1 | 1]); } void D(int p, int l, int r, int L, int R, int w) { if (L <= l && r <= R) { T[p] += w; A[p] += w; return; } int m = l + r >> 1; if (A[p]) { T[p << 1] += A[p]; T[p << 1 | 1] += A[p]; A[p << 1] += A[p]; A[p << 1 | 1] += A[p]; A[p] = 0; } if (L <= m) D(p << 1, l, m, L, R, w); if (R > m) D(p << 1 | 1, m + 1, r, L, R, w); T[p] = std::min(T[p << 1], T[p << 1 | 1]); } void F(int w) { int p = 1, l = 1, r = N; while (l < r) { int m = l + r >> 1; if (A[p]) { T[p << 1] += A[p]; T[p << 1 | 1] += A[p]; A[p << 1] += A[p]; A[p << 1 | 1] += A[p]; A[p] = 0; } if (T[p << 1] == 0) p <<= 1, r = m; else p = p << 1 | 1, l = m + 1; } T[p] = 1000000000; O[l] = w; while (p > 1) { p >>= 1; T[p] = std::min(T[p << 1], T[p << 1 | 1]); } D(1, 1, N, std::max(1, l - K + 1), std::min(N, l + K - 1), -1); } int main() { N = I(); K = I(); for (int i = 1; i <= N; i++) a[i] = I(); for (int i = 1; i < K; i++) P(a[i], 1); build(1, 1, N); for (int T = 1; T <= N; T++) F(T); for (int i = 1; i <= N; i++) printf("%d\n", O[i]); return 0; }
codenet
-1
1,810
s108141059
p04052
1,509,139,382
cpp
Accepted
443
27,512
#include <cstdio> #include <algorithm> #include <climits> #include <queue> #include <cstring> using std::min; using std::priority_queue; const int MAXN=500005; int n,k; int p[MAXN],id[MAXN]; int he[MAXN],inD[MAXN]; struct line{int to,nex;}ed[MAXN<<1]; class segT{ #define ls (u<<1) #define rs (ls|1) private: int minw[MAXN<<2]; void upd(int u){ minw[u]=min(minw[ls],minw[rs]); } void ist(int u,int l,int r,int pos,int x){ if(l==r){ minw[u]=min(minw[u],x); return; } int mid=l+r>>1; if(pos<=mid) ist(ls,l,mid,pos,x); else ist(rs,mid+1,r,pos,x); upd(u); } int qry(int u,int l,int r,int lp,int rp){ if(lp<=l && r<=rp) return minw[u]; int mid=l+r>>1,ret=INT_MAX; if(lp<=mid) ret=qry(ls,l,mid,lp,rp); if(rp>mid) ret=min(ret,qry(rs,mid+1,r,lp,rp)); return ret; } public: segT(){memset(minw,0x7f,sizeof(minw));} void ist(int pos,int x){ist(1,1,n,pos,x);} int qry(int lp,int rp){return qry(1,1,n,lp,rp);} #undef ls #undef rs }T; inline void addE(int u,int v){ static int cnt; ed[++cnt]=(line){v,he[u]}; he[u]=cnt; } inline void build(){ for(int i=n,v;i;--i){ v=T.qry(id[i]-k+1,id[i]); if(v<=n){ addE(id[i],id[v]); ++inD[id[v]]; } v=T.qry(id[i],id[i]+k-1); if(v<=n){ addE(id[i],id[v]); ++inD[id[v]]; } T.ist(id[i],i); } } inline void topo_sort(){ static priority_queue<int> pq; for(int i=1;i<=n;++i){ if(!inD[i]) pq.push(-i); } for(int i=1,u;i<=n;++i){ u=id[i]=-pq.top(); pq.pop(); for(int j=he[u],v;j;j=ed[j].nex){ v=ed[j].to; if(--inD[v]==0) pq.push(-v); } } } int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;++i) scanf("%d",&p[i]); for(int i=1;i<=n;++i) id[p[i]]=i; build(); topo_sort(); for(int i=1;i<=n;++i) p[id[i]]=i; for(int i=1;i<=n;++i) printf("%d\n",p[i]); return 0; }
codenet
-1
1,815
s108141059
p04052
1,509,139,382
cpp
Accepted
443
27,512
#include <cstdio> #include <algorithm> #include <climits> #include <queue> #include <cstring> using std::min; using std::priority_queue; const int MAXN=500005; int n,k; int p[MAXN],id[MAXN]; int he[MAXN],inD[MAXN]; struct line{int to,nex;}ed[MAXN<<1]; class segT{ #define ls (u<<1) #define rs (ls|1) private: int minw[MAXN<<2]; void upd(int u){ minw[u]=min(minw[ls],minw[rs]); } void ist(int u,int l,int r,int pos,int x){ if(l==r){ minw[u]=min(minw[u],x); return; } int mid=l+r>>1; if(pos<=mid) ist(ls,l,mid,pos,x); else ist(rs,mid+1,r,pos,x); upd(u); } int qry(int u,int l,int r,int lp,int rp){ if(lp<=l && r<=rp) return minw[u]; int mid=l+r>>1,ret=INT_MAX; if(lp<=mid) ret=qry(ls,l,mid,lp,rp); if(rp>mid) ret=min(ret,qry(rs,mid+1,r,lp,rp)); return ret; } public: segT(){memset(minw,0x7f,sizeof(minw));} void ist(int pos,int x){ist(1,1,n,pos,x);} int qry(int lp,int rp){return qry(1,1,n,lp,rp);} #undef ls #undef rs }T; inline void addE(int u,int v){ static int cnt; ed[++cnt]=(line){v,he[u]}; he[u]=cnt; } inline void build(){ for(int i=n,v;i;--i){ v=T.qry(id[i]-k+1,id[i]); if(v<=n){ addE(id[i],id[v]); ++inD[id[v]]; } v=T.qry(id[i],id[i]+k-1); if(v<=n){ addE(id[i],id[v]); ++inD[id[v]]; } T.ist(id[i],i); } } inline void topo_sort(){ static priority_queue<int> pq; for(int i=1;i<=n;++i){ if(!inD[i]) pq.push(-i); } for(int i=1,u;i<=n;++i){ u=id[i]=-pq.top(); pq.pop(); for(int j=he[u],v;j;j=ed[j].nex){ v=ed[j].to; if(--inD[v]==0) pq.push(-v); } } } int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;++i) scanf("%d",&p[i]); for(int i=1;i<=n;++i) id[p[i]]=i; build(); topo_sort(); for(int i=1;i<=n;++i) p[id[i]]=i; for(int i=1;i<=n;++i) printf("%d\n",p[i]); return 0; }
codenet
-1
1,815
s108141059
p04052
1,509,139,382
cpp
Accepted
443
27,512
#include <cstdio> #include <algorithm> #include <climits> #include <queue> #include <cstring> using std::min; using std::priority_queue; const int MAXN=500005; int n,k; int p[MAXN],id[MAXN]; int he[MAXN],inD[MAXN]; struct line{int to,nex;}ed[MAXN<<1]; class segT{ #define ls (u<<1) #define rs (ls|1) private: int minw[MAXN<<2]; void upd(int u){ minw[u]=min(minw[ls],minw[rs]); } void ist(int u,int l,int r,int pos,int x){ if(l==r){ minw[u]=min(minw[u],x); return; } int mid=l+r>>1; if(pos<=mid) ist(ls,l,mid,pos,x); else ist(rs,mid+1,r,pos,x); upd(u); } int qry(int u,int l,int r,int lp,int rp){ if(lp<=l && r<=rp) return minw[u]; int mid=l+r>>1,ret=INT_MAX; if(lp<=mid) ret=qry(ls,l,mid,lp,rp); if(rp>mid) ret=min(ret,qry(rs,mid+1,r,lp,rp)); return ret; } public: segT(){memset(minw,0x7f,sizeof(minw));} void ist(int pos,int x){ist(1,1,n,pos,x);} int qry(int lp,int rp){return qry(1,1,n,lp,rp);} #undef ls #undef rs }T; inline void addE(int u,int v){ static int cnt; ed[++cnt]=(line){v,he[u]}; he[u]=cnt; } inline void build(){ for(int i=n,v;i;--i){ v=T.qry(id[i]-k+1,id[i]); if(v<=n){ addE(id[i],id[v]); ++inD[id[v]]; } v=T.qry(id[i],id[i]+k-1); if(v<=n){ addE(id[i],id[v]); ++inD[id[v]]; } T.ist(id[i],i); } } inline void topo_sort(){ static priority_queue<int> pq; for(int i=1;i<=n;++i){ if(!inD[i]) pq.push(-i); } for(int i=1,u;i<=n;++i){ u=id[i]=-pq.top(); pq.pop(); for(int j=he[u],v;j;j=ed[j].nex){ v=ed[j].to; if(--inD[v]==0) pq.push(-v); } } } int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;++i) scanf("%d",&p[i]); for(int i=1;i<=n;++i) id[p[i]]=i; build(); topo_sort(); for(int i=1;i<=n;++i) p[id[i]]=i; for(int i=1;i<=n;++i) printf("%d\n",p[i]); return 0; }
codenet
-1
1,815
s568334953
p04052
1,573,009,555
cpp
Accepted
593
44,544
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; return x * f; } const int INF = 0x3f3f3f3f; const int N = 5e5 + 5; int seg[N << 2]; #define mid (l + r >> 1) #define lc (rt << 1) #define rc (rt << 1 | 1) void modify(int rt, int l, int r, int p, int v) { seg[rt] = min(seg[rt], v); if (l == r) return; if (p <= mid) modify(lc, l, mid, p, v); if (p > mid) modify(rc, mid + 1, r, p, v); } int query(int rt, int l, int r, int L, int R) { if (L > R) return INF; if (L <= l && R >= r) return seg[rt]; int ret = INF; if (L <= mid) ret = min(ret, query(lc, l, mid, L, R)); if (R > mid) ret = min(ret, query(rc, mid + 1, r, L, R)); return ret; } #undef mid #undef lc #undef rc vector<int> G[N]; int deg[N], ans[N], p[N], n, k, tot; void addedge(int u, int v) { G[u].push_back(v); ++deg[v]; } void Toposort() { priority_queue<int, vector<int>, greater<int> > Q; for (int i = 1; i <= n; ++i) if (!deg[i]) Q.push(i); while (!Q.empty()) { int u = Q.top(); Q.pop(); ans[u] = ++tot; for (int v : G[u]) if (!(--deg[v])) Q.push(v); } } int main() { n = read(); k = read(); memset(seg, 0x3f, sizeof(seg)); for (int i = 1; i <= n; ++i) p[read()] = i; for (int i = n, cur; i >= 1; --i) { cur = query(1, 1, n, p[i] + 1, min(n, p[i] + k - 1)); if (cur != INF) addedge(p[i], p[cur]); cur = query(1, 1, n, max(1, p[i] - k + 1), p[i] - 1); if (cur != INF) addedge(p[i], p[cur]); modify(1, 1, n, p[i], i); } Toposort(); for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,816
s760991547
p04052
1,588,655,122
cpp
Accepted
581
33,664
#include<bits/stdc++.h> using namespace std; #define ll long long #define ui unsigned int #define ull unsigned long long #define db double #define ld long double #define pii pair<ll,int> #define X first #define Y second #define mp make_pair #define pb push_back #define vi vector<int> #define vii vector<vi> #define lb lower_bound #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define per(i,b,a) for(int i=(b);i>=(a);--i) #define rep0(i,a,b) for(int i=(a);i<(b);++i) #define fore(i,a) for(int i=0;i<a.size();++i) #define gc() getchar() #define ls x<<1,l,m #define rs x<<1|1,m+1,r inline int rd() { int x=0;char c=gc();while(!isdigit(c))c=gc(); while(isdigit(c))x=x*10+c-48,c=gc();return x; } const int N=500005,inf=1e9+7; int n,k,nn,cc,a[N],b[N],d[N],hd[N],t[N],q[N]; priority_queue<int>Q; struct E{int v,nxt;}e[N*10]; inline void add(int u,int v){e[++cc]=(E){v,hd[u]};hd[u]=cc;} struct seg { int mn[N*4]; void build(int x,int l,int r){mn[x]=inf;if(l==r)return;int m=l+r>>1;build(ls);build(rs);} void upd(int x,int l,int r,int p,int v) { if(l==r){mn[x]=v;return;}int m=l+r>>1; p<=m?upd(ls,p,v):upd(rs,p,v);mn[x]=min(mn[x<<1],mn[x<<1|1]); } int qry(int x,int l,int r,int tl,int tr) { if(tl<=l&&r<=tr)return mn[x];int m=l+r>>1; return min(tl<=m?qry(ls,tl,tr):inf,tr>m?qry(rs,tl,tr):inf); } }T; int main() { n=rd();k=rd(); rep(i,1,n)a[i]=rd(),b[a[i]]=i; T.build(1,1,n); per(i,n,1) { int t=T.qry(1,1,n,max(1,b[i]-k+1),b[i]); if(t<=n)add(b[i],b[t]),d[b[t]]++; t=T.qry(1,1,n,b[i],min(n,b[i]+k-1)); if(t<=n)add(b[i],b[t]),d[b[t]]++; T.upd(1,1,n,b[i],i); } int cc=0; rep(i,1,n)if(!d[i])Q.push(-i); while(!Q.empty()) { int x=-Q.top();Q.pop();q[++cc]=x; for(int i=hd[x];i;i=e[i].nxt)if(!--d[e[i].v])Q.push(-e[i].v); } rep(i,1,n)a[q[i]]=i; rep(i,1,n)printf("%d\n",a[i]); return 0; }
codenet
-1
1,818
s470782353
p04052
1,490,406,983
cpp
Accepted
750
61,172
#include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #include<iostream> #include<queue> #include<vector> #include<algorithm> #define N 510000 #define pb push_back using namespace std; struct node{int l,r,lc,rc,mx;}lt[2*N]; int n,m,a[N],tl,ans[N],head[N],siz[N]; bool b[N]; priority_queue<int> p; vector<int> v[N]; void upd(int now) { int lc=lt[now].lc,rc=lt[now].rc; lt[now].mx=max(lt[lc].mx,lt[rc].mx); } void bt(int l,int r) { int now=++tl; lt[now].l=l;lt[now].r=r; if(l<r) { int mid=(l+r)/2,lc,rc; lc=lt[now].lc=tl+1;bt(l,mid); rc=lt[now].rc=tl+1;bt(mid+1,r); upd(now); } else lt[now].mx=a[l]; } bool cmp(int x,int y) { if(a[x]>a[y]) return 1; return 0; } void change(int now,int k) { int lc=lt[now].lc,rc=lt[now].rc,mid=(lt[now].l+lt[now].r)/2; if(lt[now].l==lt[now].r) {lt[now].mx=0;return;} if(mid>=k) change(lc,k); else change(rc,k); upd(now); } int find(int now,int l,int r) { int lc=lt[now].lc,rc=lt[now].rc,mid=(lt[now].l+lt[now].r)/2; if(lt[now].l==l && lt[now].r==r) return lt[now].mx; if(mid>=r) return find(lc,l,r); else if(l>mid) return find(rc,l,r); else return max(find(lc,l,mid),find(rc,mid+1,r)); } void insert(int x) { if(x<0 || b[x] || head[x]==siz[x]) return; int k=v[x][head[x]]; if(find(1,max(1,k-m+1),min(n,k+m-1))==a[k]) p.push(k),b[x]=1; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); bt(1,n); for(int i=1;i<=n;i++) v[i/m].pb(i); for(int i=0;i<=n/m;i++) sort(v[i].begin(),v[i].end(),cmp); for(int i=0;i<=n/m;i++) siz[i]=v[i].size(),head[i]=0; for(int i=0;i<=n/m;i++) insert(i); for(int k=n;k>=1;k--) { int t=p.empty(); int x=p.top();p.pop(); ans[x]=k; head[x/m]++;b[x/m]=0; change(1,x); insert(x/m); insert(x/m+1); insert(x/m-1); } for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,823
s037599502
p04052
1,591,757,417
cpp
Accepted
787
15,856
#include <bits/stdc++.h> using std::cerr; using std::endl; const int N = 5e5 + 233; int n, K, P[N], ans[N]; #define ls (p << 1) #define rs (ls | 1) #define mid ((L + R) >> 1) int max[N * 4]; void build(int p, int L, int R) { if (L == R) return max[p] = L, void(); build(ls, L, mid); build(rs, mid + 1, R); max[p] = P[max[ls]] > P[max[rs]] ? max[ls] : max[rs]; } void remove(int p, int x, int L, int R) { if (L == R) return max[p] = 0, void(); if (x <= mid) remove(ls, x, L, mid); else remove(rs, x, mid + 1, R); max[p] = P[max[ls]] > P[max[rs]] ? max[ls] : max[rs]; } int query(int p, int l, int r, int L, int R) { if (l <= L && r >= R) return max[p]; if (l <= mid && r > mid) { int x = query(ls, l, r, L, mid), y = query(rs, l, r, mid + 1, R); return P[x] > P[y] ? x : y; } if (l <= mid) return query(ls, l, r, L, mid); else return query(rs, l, r, mid + 1, R); } std::priority_queue<int> que; bool vis[N]; inline void check(int x) { if (!vis[x]) { int l = std::max(1, x - K + 1), r = std::min(n, x + K - 1); if (query(1, l, r, 1, n) == x) { que.push(x); vis[x] = 1; } } } int main() { // freopen("data", "r", stdin); scanf("%d %d", &n, &K); for (int i = 1; i <= n; ++i) scanf("%d", P + i); P[0] = -1e9; build(1, 1, n); for (int i = 1; i <= n; ++i) check(i); for (int i = n; i; --i) { int x = que.top(); que.pop(); ans[x] = i; remove(1, x, 1, n); int tmp, l, r; l = std::max(1, x - K + 1); r = std::max(l, x - 1); tmp = query(1, l, r, 1, n); if (tmp) check(tmp); r = std::min(n, x + K - 1); l = std::min(r, x + 1); tmp = query(1, l, r, 1, n); if (tmp) check(tmp); } for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,829
s777349081
p04052
1,536,165,329
cpp
Accepted
550
28,928
#include<bits/stdc++.h> #define neko 500010 #define feko 2000010 #define pb push_back #define f(i,a,b) for(register int i=(a);i<=(b);i=-(~(i))) #define rf(i,a,b) for(register int i=(a);i>=(b);i=~(-(i))) #define travel(i,u,v) for(register int i=head[u],v=e[i].v;i;i=e[i].nex,v=e[i].v) using namespace std; vector<int>vec; struct node {int v,nex;}e[neko<<1]; int n,k,t,p[neko],q[neko],dgr[neko],head[neko],ans[neko],inf=0x3f3f3f3f; int Min[feko]; int cmin(int x,int y) {return x<y?x:y;} int cmax(int x,int y) {return x>y?x:y;} void add(int x,int y){e[++t].v=y,e[t].nex=head[x],head[x]=t;} namespace Seg_Tree { #define mid ((l+r)>>1) #define ori tagl,tagr #define lson root<<1,l,mid #define rson root<<1|1,mid+1,r void pushup(int root) {Min[root]=cmin(Min[root<<1],Min[root<<1|1]);} void update(int root,int l,int r,int tag,int x) { if(l==r){Min[root]=x;return;} if(tag<=mid)update(lson,tag,x); else update(rson,tag,x); pushup(root); } int query(int root,int l,int r,int tagl,int tagr) { if(l>=tagl&&r<=tagr)return Min[root]; int tmp=inf; if(tagl<=mid)tmp=query(lson,ori); if(tagr>mid)tmp=cmin(tmp,query(rson,ori)); return tmp; } } void topsort() { priority_queue<int,vector<int>,greater<int> >q;int u,num=0; f(i,1,n)if(!dgr[i])q.push(i); while(!q.empty()) { u=q.top(),q.pop(); ans[u]=++num; travel(i,u,v)if(!(--dgr[v]))q.push(v); } f(i,1,n)printf("%d\n",ans[i]); } int main() { using namespace Seg_Tree; int x=0x3f3f3f3f,y=0x3f3f3f3f; scanf("%d%d",&n,&k); f(i,1,n)scanf("%d",&p[i]),q[p[i]]=i; memset(Min,0x3f,sizeof Min); rf(i,n,1) { if(q[i]>1) { x=query(1,1,n,cmax(1,q[i]-k+1),q[i]-1); if(x^inf)add(q[i],q[x]),++dgr[q[x]]; } if(q[i]<n) { y=query(1,1,n,q[i]+1,cmin(q[i]+k-1,n)); if(y^inf)add(q[i],q[y]),++dgr[q[y]]; } update(1,1,n,q[i],i); }return topsort(),0; }
codenet
-1
1,837
s854999934
p04052
1,579,910,112
cpp
Accepted
565
44,672
#include<bits/stdc++.h> using namespace std; #define REP(i,a,b) for(int i=(a),ed=(b);i<=ed;++i) #define DREP(i,a,b) for(int i=(a),ed=(b);i>=ed;--i) typedef long long ll; inline int read(){ register int x=0,f=1;register char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();} while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();} return f?x:-x; } const int N=5e5+10,inf=0x3f3f3f3f; int n,k,a[N],b[N],ind[N]; vector<int> E[N]; priority_queue<int,vector<int>,greater<int> > q; namespace SegmentTree{ #define ls(p) p<<1 #define rs(p) p<<1|1 int tr[N<<2]; inline void pushup(int p){tr[p]=min(tr[ls(p)],tr[rs(p)]);} inline void Build(int p,int l,int r){ tr[p]=inf;if(l==r)return; int mid=(l+r)>>1; Build(ls(p),l,mid),Build(rs(p),mid+1,r); } inline void Insert(int p,int l,int r,int pos,int v){ if(l==r){tr[p]=v;return;} int mid=(l+r)>>1; if(mid>=pos)Insert(ls(p),l,mid,pos,v); else Insert(rs(p),mid+1,r,pos,v); pushup(p); } inline int Query(int p,int l,int r,int L,int R){ if(L>R)return inf; if(L<=l&&r<=R)return tr[p]; int mid=(l+r)>>1,ans=inf; if(L<=mid)ans=min(ans,Query(ls(p),l,mid,L,R)); if(mid<R)ans=min(ans,Query(rs(p),mid+1,r,L,R)); return ans; } }using namespace SegmentTree; int main(){ //freopen("in.in","r",stdin); n=read(),k=read();REP(i,1,n)a[i]=read(),b[a[i]]=i; Build(1,1,n); DREP(i,n,1){ int p=Query(1,1,n,max(1,b[i]-k+1),b[i]-1); if(p^inf)E[b[i]].push_back(b[p]),++ind[b[p]]; p=Query(1,1,n,b[i]+1,min(n,b[i]+k-1)); if(p^inf)E[b[i]].push_back(b[p]),++ind[b[p]]; Insert(1,1,n,b[i],i); } REP(i,1,n)if(!ind[i])q.push(i); int cnt=0; while(q.size()){ int u=q.top();q.pop(); a[u]=++cnt; for(int v:E[u]) if(!--ind[v])q.push(v); } REP(i,1,n-1)printf("%d\n",a[i]); printf("%d",a[n]); return 0; }
codenet
-1
1,841
s686896193
p04052
1,468,943,096
cpp
Accepted
1,355
44,928
#include<vector> #include<cmath> #include<map> #include<cstdlib> #include<iostream> #include<sstream> #include<fstream> #include<string> #include<algorithm> #include<cstring> #include<cstdio> #include<set> #include<stack> #include<bitset> #include<functional> #include<ctime> #include<queue> #include<deque> #include<complex> using namespace std; #define pb push_back #define pf push_front typedef long long lint; typedef complex<double> P; #define mp make_pair #define fi first #define se second typedef pair<int,int> pint; #define All(s) s.begin(),s.end() #define rAll(s) s.rbegin(),s.rend() #define REP(i,a,b) for(int i=a;i<b;i++) #define rep(i,n) REP(i,0,n) #define N 524288 int a[N],b[N],ch[N],out[N]; vector<int> par[N]; int dat[N*2+10]; int inf=1145141919; priority_queue<int> q; //[a,b)の最小値 int query(int a,int b,int k=0,int l=0,int r=N){ if(r<=a || b<=l) return inf; if(a<=l && r<=b) return dat[k]; int vl=query(a,b,k*2+1,l,(l+r)/2); int vr=query(a,b,k*2+2,(l+r)/2,r); return min(vl,vr); } void update(int k,int a){ k+=N-1; dat[k]=a; while(k>0){ k=(k-1)/2; dat[k]=min(dat[k*2+1],dat[k*2+2]); } return; } int main() { int n,k; scanf("%d %d",&n,&k); rep(i,n){ scanf("%d",&a[i]);a[i]--; } rep(i,n) b[a[i]]=i; //rep(i,n) cout<<b[i]<<endl; rep(i,N*2+10) dat[i]=inf; rep(i,n){ int x=query(b[i],min(n,b[i]+k)); if(x<inf) ch[b[-x]]++,par[b[i]].pb(b[-x]); update(b[i],-i); } rep(i,N*2+10) dat[i]=inf; for(int i=n-1;i>=0;i--){ int x=query(max(0,b[i]-k+1),b[i]); if(x<inf) ch[b[i]]++,par[b[x]].pb(b[i]); update(b[i],i); } //rep(i,n) cout<<i<<' '<<par[i]<<' '<<ch[i]<<endl; rep(i,n){ if(ch[i]==0) q.push(i); } rep(i,n){ int p=q.top();q.pop(); out[p]=n-i-1; rep(j,par[p].size()){ ch[par[p][j]]--; if(ch[par[p][j]]<1) q.push(par[p][j]); } } rep(i,n) printf("%d\n",out[i]+1); }
codenet
-1
1,842
s520461860
p04052
1,598,662,472
cpp
Accepted
980
14,432
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { x = 0; bool flag = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') { flag = 1; } ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } if (flag) { x = -x; } } const int N = 500005; int n, k, p[N], ans[N]; int pos[N << 2]; bool inq[N]; inline int merge(int x, int y) { return p[x] > p[y] ? x : y; } inline void pushup(int p) { pos[p] = merge(pos[p << 1], pos[p << 1 | 1]); } void build(int p, int l, int r) { if (l == r) { pos[p] = l; return; } int mid = l + r >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); pushup(p); } int query(int p, int l, int r, int L, int R) { if (l > R || r < L) { return 0; } if (l >= L && r <= R) { return pos[p]; } int mid = l + r >> 1; return merge(query(p << 1, l, mid, L, R), query(p << 1 | 1, mid + 1, r, L, R)); } void modify(int p, int l, int r, int x) { if (l == r) { pos[p] = 0; return; } int mid = l + r >> 1; if (mid >= x) { modify(p << 1, l, mid, x); } else { modify(p << 1 | 1, mid + 1, r, x); } pushup(p); } int main() { read(n), read(k); for (int i = 1; i <= n; ++i) { read(p[i]); } build(1, 1, n); priority_queue<int> q; auto check = [&](int pos) { if (pos && !inq[pos]) { if (pos == query(1, 1, n, pos - k + 1, pos + k - 1)) { q.push(pos); inq[pos] = 1; } } }; for (int i = 1; i <= n; ++i) { check(i); } for (int i = n; i; --i) { int pos = q.top(); q.pop(); ans[pos] = i; modify(1, 1, n, pos); check(query(1, 1, n, pos - k + 1, pos - 1)); check(query(1, 1, n, pos + 1, pos + k - 1)); } for (int i = 1; i <= n; ++i) { printf("%d\n", ans[i]); } return 0; }
codenet
-1
1,845
s645795344
p04052
1,489,179,931
cpp
Accepted
224
25,856
#include<cstdio> #include<algorithm> using namespace std; int heap[500005],top; struct tree{ int max; }t[2000005]; struct link{ int next,to; }l[500005]; int n,sum,cnt,k; int a[500005],p[500005],pre[500005],ans[500005],tmp[500005],head[500005]; inline int read(){ int ret=0;char c=getchar(); while((c<'0')||(c>'9'))c=getchar(); while((c>='0')&&(c<='9'))ret=(ret<<1)+(ret<<3)+c-'0',c=getchar(); return ret; } int P[20],pcnt; void print(int x){ if(x==0){puts("0");return;} while(x)P[++pcnt]=x%10,x/=10; while(pcnt)putchar(P[pcnt--]+'0'); putchar('\n'); } #define mid ((l+r)>>1) #define lson index<<1 #define rson index<<1|1 inline int query(int L,int R,int l,int r,int index){ if((L<=l)&&(R>=r))return t[index].max; if(R<=mid)return query(L,R,l,mid,lson); if(L>mid)return query(L,R,mid+1,r,rson); return max(query(L,R,l,mid,lson),query(L,R,mid+1,r,rson)); } void updata(int l,int r,int index,int k,int x){ t[index].max=x; if(l==r)return; if(k<=mid)updata(l,mid,lson,k,x); else updata(mid+1,r,rson,k,x); } void add(int u,int v){l[++cnt]=(link){head[u],v};head[u]=cnt;} void ins(int x){ heap[++top]=x;int k=top; while(k>1){ if(heap[k>>1]>heap[k])swap(heap[k],heap[k>>1]); else break; k>>=1; } } void pop(){ heap[1]=heap[top];int k=1;top--; while((k<<1)<=top){ int v=k<<1; if(v<top)if(heap[v|1]<heap[v])v|=1; if(heap[v]<heap[k])swap(heap[k],heap[v]); else break; k=v; } } int main(){ n=read();k=read(); for(int i=1;i<=n;i++)p[i]=read(),a[p[i]]=i; for(int i=1;i<=n;i++){ int r=min(n,a[i]+k-1); pre[i]=a[query(a[i],r,1,n,1)]; updata(1,n,1,a[i],i); } for(int i=1;i<=n;i++)if(pre[i])add(pre[i],a[i]); for(int i=1;i<=n;i++)if(pre[i]==0)ins(a[i]); cnt=0; for(int i=1;i<=n;i++){ int u=heap[1];ans[u]=++cnt;pop(); for(int j=head[u];j;j=l[j].next)ins(l[j].to); } for(int i=1;i<=n;i++)print(ans[i]); }
codenet
-1
1,845
s091330728
p04052
1,579,378,818
cpp
Accepted
580
27,384
#include <bits/stdc++.h> using namespace std; const int maxn = 500005; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } int n, k; int p[maxn], q[maxn], r[maxn], cnt; int Min[maxn << 2]; #define mid ((l + r) >> 1) #define lch (s << 1) #define rch (s << 1 | 1) void build(int s, int l, int r) { Min[s] = n + 1; if (l == r) return ; build(lch, l, mid); build(rch, mid + 1, r); } void modify(int s, int l, int r, int p, int v) { if (l == r) return Min[s] = v, void(); if (p <= mid) modify(lch, l, mid, p, v); else modify(rch, mid + 1, r, p, v); Min[s] = min(Min[lch], Min[rch]); } int query(int s, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return Min[s]; int res = n + 1; if (ql <= mid) res = min(res, query(lch, l, mid, ql, qr)); if (qr >= mid + 1) res = min(res, query(rch, mid + 1, r, ql, qr)); return res; } struct edge { int to, next; } e[maxn << 1]; int h[maxn], deg[maxn], tot; priority_queue<int> que; void add(int u, int v) { e[++tot] = (edge) {v, h[u]}; h[u] = tot; ++deg[v]; } int main() { n = gi(); k = gi(); for (int i = 1; i <= n; ++i) q[p[i] = gi()] = i; build(1, 1, n); for (int i = n, x; i >= 1; --i) { x = query(1, 1, n, max(1, q[i] - k + 1), q[i]); if (x <= n) add(q[x], q[i]); x = query(1, 1, n, q[i], min(n, q[i] + k - 1)); if (x <= n) add(q[x], q[i]); modify(1, 1, n, q[i], i); } for (int i = 1; i <= n; ++i) if (!deg[i]) que.push(i); int u; while (!que.empty()) { r[++cnt] = u = que.top(); que.pop(); for (int i = h[u], v; v = e[i].to, i; i = e[i].next) if (!(--deg[v])) que.push(v); } reverse(r + 1, r + n + 1); for (int i = 1; i <= n; ++i) p[r[i]] = i; for (int i = 1; i <= n; ++i) printf("%d\n", p[i]); return 0; }
codenet
-1
1,846
s903263863
p04052
1,505,554,907
cpp
Accepted
705
56,704
#include <bits/stdc++.h> typedef long long ll; typedef long long llong; typedef long double ld; typedef unsigned long long ull; using namespace std; /* ll pw(ll a, ll b) { ll ans = 1; while (b) { while (!(b & 1)) b >>= 1, a = (a * a) % MOD; ans = (ans * a) % MOD, --b; } return ans; } */ const int MAXN = 510000; int a[MAXN]; int p[MAXN]; set<int> ss; int cc[MAXN]; vector<int> go[MAXN]; int rmq[MAXN * 4]; int n, k; int lg[MAXN]; int rg[MAXN]; int ans[MAXN]; void build(int v, int tl, int tr) { rmq[v] = n; if (tl + 1 == tr) return; int m = (tl + tr) >> 1; build(v * 2 + 1, tl, m); build(v * 2 + 2, m, tr); } void upd(int v, int tl, int tr, int x, int y) { if (tl + 1 == tr) { rmq[v] = y; return; } int m = (tl + tr) >> 1; if (x < m) upd(v * 2 + 1, tl, m, x, y); else upd(v * 2 + 2, m, tr, x, y); rmq[v] = min(rmq[v * 2 + 1], rmq[v * 2 + 2]); } int get(int v, int tl, int tr, int l, int r) { if (r <= tl || tr <= l) return n; if (l <= tl && tr <= r) return rmq[v]; int m = (tl + tr) >> 1; return min(get(v * 2 + 1, tl, m, l, r), get(v * 2 + 2, m, tr, l, r)); } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) scanf("%d", p + i), --p[i], a[p[i]] = i; build(0, 0, n); for (int i = n - 1; i >= 0; --i) { lg[i] = get(0, 0, n, max(0, a[i] - k + 1), a[i]); rg[i] = get(0, 0, n, a[i], min(n, a[i] + k)); if (lg[i] != n) go[lg[i]].push_back(i), ++cc[i]; if (rg[i] != n) go[rg[i]].push_back(i), ++cc[i]; upd(0, 0, n, a[i], i); if (lg[i] == n && rg[i] == n) ss.insert(a[i]); } int cur = n - 1; while (!ss.empty()) { int x = *ss.rbegin(); ss.erase(x); int y = p[x]; ans[x] = cur; --cur; for (int j: go[y]) { --cc[j]; if (cc[j] == 0) ss.insert(a[j]); } } for (int i = 0; i < n; ++i) printf("%d ", ans[i] + 1); printf("\n"); return 0; }
codenet
-1
1,846
s514894382
p04052
1,537,365,126
cpp
Accepted
392
44,416
#include<bits/stdc++.h> #define ll long long #define ull unsigned ll #define uint unsigned #define db long double #define pii pair<int,int> #define pll pair<ll,ll> #define IT iterator #define PB push_back #define MK make_pair #define LB lower_bound #define UB upper_bound #define EB emplace_back #define fi first #define se second #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) #define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--) #define UPD(x,y) (((x)+=(y))>=mo?x-=mo:233) #define CLR(a,v) memset(a,v,sizeof(a)); #define CPY(a,b) memcpy(a,b,sizeof(a)); #define LS3 k*2,l,mid #define RS3 k*2+1,mid+1,r #define LS5 k*2,l,mid,x,y #define RS5 k*2+1,mid+1,r,x,y #define GET pushdown(k);int mid=(l+r)/2 #define INF ((1ll<<60)-233) #define sqr(x) ((x)*(x)) #define debug puts("wzpkking") #define N M using namespace std; const int N=500005; int deg[N],p[N],q[N],n,k; int ans[N],mx[N*4]; priority_queue<pii,vector<pii >,greater<pii > > que; vector<int> e[N]; void change(int k,int l,int r,int p,int v){ mx[k]=max(mx[k],v); if (l==r) return; int mid=(l+r)/2; if (p<=mid) change(k*2,l,mid,p,v); else change(k*2+1,mid+1,r,p,v); } int ask(int k,int l,int r,int x,int y){ if (x>y) return 0; if (l==x&&r==y) return mx[k]; int mid=(l+r)/2; if (y<=mid) return ask(k*2,l,mid,x,y); if (x>mid) return ask(k*2+1,mid+1,r,x,y); return max(ask(k*2,l,mid,x,mid), ask(k*2+1,mid+1,r,mid+1,y)); } int main(){ scanf("%d%d",&n,&k); For(i,1,n) scanf("%d",&p[i]),q[p[i]]=i; For(i,1,n){ int v=ask(1,1,n,q[i]+1,min(n,q[i]+k-1)); if (v) e[v].PB(i),deg[i]++; change(1,1,n,q[i],i); } For(i,1,n) if (!deg[i]) que.push(pii(q[i],i)); while (!que.empty()){ int x=que.top().se; que.pop(); for (int i:e[x]) if (!--deg[i]) que.push(pii(q[i],i)); ans[++*ans]=x; } For(i,1,*ans) p[q[ans[i]]]=i; For(i,1,n) printf("%d\n",p[i]); }
codenet
-1
1,848
s327845707
p04052
1,524,335,764
cpp
Accepted
810
38,272
#include<cstdio> int k,tot,n,m,p[500001],q[500001],l[2000001],r[2000001],f[2000001],lz[2000001],dbd,fir[500001],nex[500001],sto[500001],ru[500001]; void addbian(int aa,int bb) { tot++; nex[tot]=fir[aa]; fir[aa]=tot; sto[tot]=bb; } int getmin(int aa,int bb) { if(aa<bb) return(aa); else return(bb); } void update(int num,int hh) { lz[num]=hh; f[num]=hh; } void built(int num,int x,int y) { l[num]=x; r[num]=y; f[num]=dbd; lz[num]=0; if(x==y) return; int mid=(x+y)/2; built(num*2,x,mid); built(num*2+1,mid+1,y); } int ask(int num,int x) { if(l[num]==r[num])return(f[num]); int mid=(l[num]+r[num])/2; if(lz[num]>0) { update(num*2,lz[num]); update(num*2+1,lz[num]); lz[num]=0; } if(x<=mid)return(ask(num*2,x)); else return(ask(num*2+1,x)); f[num]=getmin(f[num*2],f[num*2+1]); } void gai(int num,int x,int y) { if((x==l[num])&&(y==r[num])) { update(num,dbd); return; } int mid=(l[num]+r[num])/2; if(lz[num]>0) { update(num*2,lz[num]); update(num*2+1,lz[num]); lz[num]=0; } if(y<=mid)gai(num*2,x,y); else { if(x>mid) gai(num*2+1,x,y); else { gai(num*2,x,mid); gai(num*2+1,mid+1,y); } } f[num]=getmin(f[num*2],f[num*2+1]); } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",&p[i]); q[p[i]]=i; } dbd=0; built(1,1,n); for(int i=1;i<=n;i++) { int bb=ask(1,q[i]); if(bb!=0) { addbian(bb,q[i]); ru[q[i]]++; } int li=1,ri=q[i]; if(q[i]-k+1>1) li=q[i]-k+1; dbd=q[i]; gai(1,li,ri); } dbd=n+1; built(1,1,n); for(int i=1;i<=n;i++)if(ru[i]==0) { dbd=i; gai(1,i,i); } for(int i=1;i<=n;i++) { int bb=fir[f[1]],cc=f[1]; while(bb!=0) { ru[sto[bb]]--; if(ru[sto[bb]]==0) { dbd=sto[bb]; gai(1,dbd,dbd); } bb=nex[bb]; } p[cc]=i; dbd=n+1; gai(1,cc,cc); } for(int i=1;i<=n;i++)printf("%d\n",p[i]); }
codenet
-1
1,848
s000533364
p04052
1,507,839,925
cpp
Accepted
530
37,248
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<set> #define fo(i,j,l) for(int i=j;i<=l;i++) #define fd(i,j,l) for(int i=j;i>=l;i--) #define rep(i,j) for(int i=la[j];i;i=ne[i]) using namespace std; typedef long long ll; const ll N=5e5+1e3,M=2*N; int n,m,k,l,o,a,w1,w2,oo,kk; int q[N],ne[M],lb[M],la[N],t[M*2],d[N],ans[N],p[N]; set<int> op; void read(int &o) { o=0; char ch=' '; for(;ch<'0'||ch>'9';)ch=getchar(); for(;ch>='0'&&ch<='9';ch=getchar())o=o*10+ch-48; } void write(int o) { int kt=0; char ch[20]; for(;o;o/=10)ch[++kt]='0'+o%10; fd(i,kt,1)printf("%c",ch[i]); printf("\n"); } int min(int a,int b) {if(a<b)return a;else return b;} int max(int a,int b) {if(a>b)return a;else return b;} void jl(int o,int l,int r) { t[o]=n+1; if(l==r)return; int mid=(l+r)/2; jl(o*2,l,mid); jl(o*2+1,mid+1,r); } void change(int o,int l,int r) { if(l==r&&l==w1){t[o]=w2; return;} if(l>w1||r<w1)return; int mid=(l+r)/2; change(o*2,l,mid); change(o*2+1,mid+1,r); if(t[o*2]<t[o*2+1])t[o]=t[o*2];else t[o]=t[o*2+1]; } void question(int o,int l,int r) { if(l>=w1&&r<=w2){ if(t[o]<kk)kk=t[o]; return; } if(l>w2||r<w1)return; int mid=(l+r)/2; question(o*2,l,mid); question(o*2+1,mid+1,r); } void llb(int a,int b) {ne[++oo]=la[a]; lb[oo]=b; la[a]=oo; d[b]++;} void doing() { jl(1,1,n); fd(i,n,1){ kk=n+1; w1=p[i]; w2=min(p[i]+k-1,n); question(1,1,n); if(kk!=n+1)llb(p[i],p[kk]); kk=n+1; w1=max(1,p[i]-k+1); w2=p[i]; question(1,1,n); if(kk!=n+1)llb(p[i],p[kk]); w1=p[i]; w2=i; change(1,1,n); } fo(i,1,n)if(!d[i])op.insert(i); fo(i,1,n){ int y=ans[i]=*op.begin(); op.erase(y); rep(j,y) if(!(--d[lb[j]]))op.insert(lb[j]); } fo(i,1,n)p[ans[i]]=i; fo(i,1,n)write(p[i]); } void intt() { cin>>n>>k; fo(i,1,n) read(a),p[a]=i; } int main() { intt(); doing(); }
codenet
-1
1,848
s752730408
p04052
1,597,088,231
cpp
Accepted
586
23,412
/*Lucky_Glass*/ #include<queue> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; inline int Read(){ register int r=0,c=getchar(); while(c<'0' || '9'<c) c=getchar(); while('0'<=c && c<='9') r=(r<<1)+(r<<3)+c-'0',c=getchar(); return r; } #define cs const int & const int N=5e5+10; int n,m,num[N],ans[N]; priority_queue< int,vector<int>,greater<int> > que; struct SEGTREE{ int mn[N<<1],inf; SEGTREE(){memset(mn,0x3f,sizeof mn),inf=mn[0];} #define idx(l,r) (((l)+(r))|((l)!=(r))) inline void PushUp(cs le,cs ri){ int mi=(le+ri)>>1; mn[idx(le,ri)]=min(mn[idx(le,mi)],mn[idx(mi+1,ri)]); } inline void Modify(cs le,cs ri,cs po,cs key){ if(le==ri){mn[idx(le,ri)]=key;return;} int mi=(le+ri)>>1; if(po<=mi) Modify(le,mi,po,key); else Modify(mi+1,ri,po,key); PushUp(le,ri); } inline int Query(cs le,cs ri,cs Le,cs Ri){ if(Le>Ri || ri<Le || Ri<le) return inf; if(Le<=le && ri<=Ri) return mn[idx(le,ri)]; int mi=(le+ri)>>1; return min(Query(le,mi,Le,Ri),Query(mi+1,ri,Le,Ri)); } }Se; struct GRAPH{ int head[N],nxt[N<<1],to[N<<1],du[N],ncnt; void AddEdge(int u,int v){ ncnt++; nxt[ncnt]=head[u],to[ncnt]=v,head[u]=ncnt; du[v]++; } int operator [](const int &u){return head[u];} }Gr; int main(){ n=Read(),m=Read(); for(int i=1;i<=n;i++) num[Read()]=i; for(int i=n;i>=1;i--){ int j=Se.Query(1,n,num[i]+1,num[i]+m-1),k=Se.Query(1,n,num[i]-m+1,num[i]-1); if(j!=Se.inf) Gr.AddEdge(num[i],num[j]); if(k!=Se.inf) Gr.AddEdge(num[i],num[k]); Se.Modify(1,n,num[i],i); } for(int i=1;i<=n;i++) if(!Gr.du[i]) que.push(i); int nans=0; while(!que.empty()){ int u=que.top();que.pop(); ans[u]=++nans; for(int it=Gr[u];it;it=Gr.nxt[it]){ Gr.du[Gr.to[it]]--; if(!Gr.du[Gr.to[it]]) que.push(Gr.to[it]); } } for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,851
s613112148
p04052
1,539,652,682
cpp
Accepted
562
47,092
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <vector> #include <queue> using namespace std; const int Maxn = 500010; int n, K; int a[Maxn], ans[Maxn]; vector <int> vec[Maxn]; int no[Maxn]; bool v[Maxn]; int mx[Maxn<<2]; bool cmp(int x, int y) { return a[x] > a[y]; } int _max(int x, int y) { return x > y ? x : y; } int _min(int x, int y) { return x < y ? x : y; } priority_queue <int> q; void build_tree(int now, int L, int R) { if(L < R){ int mid = L+R>>1, lc = now<<1, rc = now<<1|1; build_tree(lc, L, mid); build_tree(rc, mid+1, R); mx[now] = _max(mx[lc], mx[rc]); } else mx[now] = a[L]; } void chg(int now, int L, int R, int x) { if(L == R){ mx[now] = 0; return; } int mid = L+R>>1, lc = now<<1, rc = now<<1|1; if(x <= mid) chg(lc, L, mid, x); else chg(rc, mid+1, R, x); mx[now] = _max(mx[lc], mx[rc]); } int qry(int now, int L, int R, int l, int r) { if(L == l && R == r) return mx[now]; int mid = L+R>>1, lc = now<<1, rc = now<<1|1; if(r <= mid) return qry(lc, L, mid, l, r); else if(l > mid) return qry(rc, mid+1, R, l, r); else return _max(qry(lc, L, mid, l, mid), qry(rc, mid+1, R, mid+1, r)); } void ins(int x) { if(x < 0 || x > n/K || no[x] == vec[x].size() || v[x] == true) return; int p = vec[x][no[x]]; if(qry(1, 1, n, _max(1,p-K+1), _min(n,p+K-1)) == a[p]){ no[x]++; v[x] = true; q.push(p); } } int main() { int i, j, k; scanf("%d%d", &n, &K); for(i = 1; i <= n; i++) scanf("%d", &a[i]); build_tree(1, 1, n); for(i = 1; i <= n; i++) vec[i/K].push_back(i); for(i = 0; i <= n/K; i++) sort(vec[i].begin(), vec[i].end(), cmp); for(i = 0; i <= n/K; i++) ins(i); for(i = n; i >= 1; i--){ int x = q.top(); q.pop(); ans[x] = i; chg(1, 1, n, x); v[x/K] = false; ins(x/K-1); ins(x/K); ins(x/K+1); } for(i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,853
s806309122
p04052
1,555,034,030
cpp
Accepted
648
25,192
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #define rep(i,l,r) for(int i=(l);i<=(r);++i) #define per(i,r,l) for(int i=(r);i>=(l);--i) #define For(i,u) for(int i=hed[u],v=e[i].to;i;i=e[i].nex,v=e[i].to) using namespace std; const int inf=1e9,maxn=5e5+10; int n,k,x,a[maxn],c[maxn<<2],hed[maxn],id[maxn],d[maxn],cnt,ed; struct edge{ int to,nex; edge(int _=0,int __=0):to(_),nex(__){} }e[maxn<<1]; void build(int l,int r,int cur){ if(l==r){c[cur]=inf;return;} int mid=(l+r)>>1; build(l,mid,cur<<1);build(mid+1,r,cur<<1|1); c[cur]=min(c[cur<<1],c[cur<<1|1]); } void upd(int l,int r,int cur,int x,int y){ if(l==r){c[cur]=y;return;} int mid=(l+r)>>1; if(x>mid) upd(mid+1,r,cur<<1|1,x,y); else upd(l,mid,cur<<1,x,y); c[cur]=min(c[cur<<1],c[cur<<1|1]); } int que(int l,int r,int cur,int ql,int qr){ if(ql>r||qr<l) return inf; if(ql<=l&&r<=qr) return c[cur]; int mid=(l+r)>>1; return min(que(l,mid,cur<<1,ql,qr),que(mid+1,r,cur<<1|1,ql,qr)); } inline void add_edge(int u,int v){++d[v];e[++ed]=edge(v,hed[u]);hed[u]=ed;} void topsort(){ priority_queue<int,vector<int>,greater<int> > q; rep(i,1,n) if(!d[i]) q.push(i); while(!q.empty()){ int u=q.top();q.pop();id[u]=++cnt; For(i,u){ --d[v]; if(!d[v]) q.push(v); } } } int main(){ scanf("%d%d",&n,&k); rep(i,1,n) scanf("%d",&x),a[x]=i; build(1,n,1); per(i,n,1){ int x=que(1,n,1,max(1,a[i]-k+1),a[i]-1),y=que(1,n,1,a[i]+1,min(a[i]+k-1,n)); if(x!=inf) add_edge(a[i],a[x]); if(y!=inf) add_edge(a[i],a[y]); upd(1,n,1,a[i],i); } topsort(); rep(i,1,n) printf("%d\n",id[i]); return 0; }
codenet
-1
1,857
s509452049
p04052
1,552,665,700
cpp
Accepted
676
46,464
#include<bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, k, a[N], pos[N], deg[N], ans[N]; vector<int> g[N]; #define LD (t << 1) #define RD (t << 1 | 1) int val[N << 2]; void build(int t, int l, int r) { val[t] = n + 1; if (l == r) return; int mid = (l + r) >> 1; build(LD, l, mid); build(RD, mid + 1, r); } void insert(int t, int l, int r, int pos, int vl) { val[t] = min(val[t], vl); if (l == r) return; int mid = (l + r) >> 1; if (pos <= mid) insert(LD, l, mid, pos, vl); else insert(RD, mid + 1, r, pos, vl); } int query(int t, int l, int r, int ql, int qr) { if (ql > qr) return n + 1; if (ql <= l && r <= qr) return val[t]; int mid = (l + r) >> 1, res = n + 1; if (ql <= mid) res = min(res, query(LD, l, mid, ql, qr)); if (qr > mid) res = min(res, query(RD, mid + 1, r, ql, qr)); return res; } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); pos[a[i]] = i; } build(1, 1, n); // for (int i = 1; i <= n; i++) { // printf("%d ", pos[i]); // } // puts(""); for (int i = n; i >= 1; i--) { int l = query(1, 1, n, max(1, pos[i] - k + 1), pos[i] - 1); int r = query(1, 1, n, pos[i] + 1, min(n, pos[i] + k - 1)); // cout<<i<<" "<<l<<" "<<r<<endl; if (r <= n) { g[pos[i]].push_back(pos[r]); deg[pos[r]]++; } if (l <= n) { g[pos[i]].push_back(pos[l]); deg[pos[l]]++; } insert(1, 1, n, pos[i], i); } int cur = 0; priority_queue<int, vector<int>, greater<int> > q; for (int i = 1; i <= n; i++) { if (!deg[i]) { q.push(i); } } while (q.size()) { int u = q.top(); q.pop(); ans[u] = ++cur; for (auto v : g[u]) { if (!(--deg[v])) { q.push(v); } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; }
codenet
-1
1,864
s163985143
p04052
1,572,922,665
cpp
Accepted
670
84,736
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <queue> typedef long long ll; #define Ls (u << 1) #define Rs (u << 1 | 1) #define inc(i) (++ (i)) #define dec(i) (-- (i)) #define Rep(i , a , b) for(int i = (a) , i##Limit = (b) ; i <= i##Limit ; inc(i)) #define rep(i , a , b) for(int i = (a) , i##Limit = (b) ; i >= i##Limit ; dec(i)) using namespace std; const int N = 2000000 + 7; int n , k , Ord[N] , Min[N] , Ans[N] , In[N]; vector <int> E[N]; void Update(int u , int l , int r , int Pos , int w) { if(l == r) { Min[u] = min(Min[u] , w); return; } int Mid = (l + r) >> 1; if(Mid >= Pos) Update(Ls , l , Mid , Pos , w); else Update(Rs , Mid + 1 , r , Pos , w); Min[u] = min(Min[Ls] , Min[Rs]); } int Query(int u , int l , int r , int L , int R) { if(L > R) return Min[0]; if(L <= l && R >= r) return Min[u]; int Mid = (l + r) >> 1 , Ret = Min[0]; if(Mid >= L) Ret = Query(Ls , l , Mid , L , R); if(Mid < R) Ret = min(Ret , Query(Rs , Mid + 1 , r , L , R)); return Ret; } priority_queue <int> Q; inline void TopSort() { int Sz = 0; Rep(i , 1 , n) if(!In[i]) Q.push(-i); while(!Q.empty()) { int u = Q.top(); Q.pop(); Ans[-u] = inc(Sz); for(auto v: E[-u]) if(!dec(In[v])) Q.push(-v); } } int main() { int x; scanf("%d%d" , &n , &k); memset(Min , 127 , sizeof(Min)); Rep(i , 1 , n) scanf("%d" , &x) , Ord[x] = i; rep(i , n , 1) { int t = Query(1 , 1 , n , max(1 , Ord[i] - k + 1) , Ord[i] - 1); if(t != Min[0]) E[Ord[i]].push_back(Ord[t]) , inc(In[Ord[t]]);// , printf("%d %d\n" , Ord[i] , Ord[t]); t = Query(1 , 1 , n , Ord[i] + 1 , min(n , Ord[i] + k - 1)); if(t != Min[0]) E[Ord[i]].push_back(Ord[t]) , inc(In[Ord[t]]);// , printf("%d %d\n" , Ord[i] , Ord[t]); Update(1 , 1 , n , Ord[i] , i); } TopSort(); Rep(i , 1 , n) printf("%d\n" , Ans[i]); return 0; }
codenet
-1
1,865
s275793221
p04052
1,537,476,495
cpp
Accepted
534
64,744
//By Don4ick //#define _GLIBCXX_DEBUG #include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned int ui; #define forn(i, n) for (int i = 1; i <= n; i++) #define pb push_back #define all(x) x.begin(), x.end() #define y1 qewr1234 const double PI = acos(-1.0); const int DIR = 4; const int X[] = {1, 0, -1, 0}; const int Y[] = {0, 1, 0, -1}; const int N = (int)5e5 + 228; using namespace std; int n, k, p[N], q[N], t1[N], t2[N], t[N << 1], res[N]; vector < pair < int, int > > g[N]; bool used[N]; int get(int l, int r) { l += n - 1, r += n - 1; int res = n + 1; while(l <= r) { if (l & 1) res = min(res, t[l++]); if (!(r & 1)) res = min(res, t[r--]); l >>= 1, r >>= 1; } return res; } void upd(int v, int val) { v += n - 1; t[v] = val; while(v != 1) { v >>= 1; t[v] = min(t[v << 1], t[v << 1 | 1]); } } int main() { //ios_base::sync_with_stdio(false); //cin.tie(); //cout.tie(); //freopen(".in", "r", stdin); //freopen(".out", "w", stdout); //~read scanf("%d%d", &n, &k); forn(i, n) scanf("%d", &p[i]); forn(i, n) q[p[i]] = i; //~calc_t1_t2 fill(t + 1, t + (n << 1), n + 1); for (int i = n; i >= 1; i--) { upd(q[i], i); t2[q[i]] = get(q[i] + 1, min(n, q[i] + k - 1)); t1[q[i]] = get(max(1, q[i] - k + 1), q[i] - 1); g[t1[q[i]]].pb({q[i], t2[q[i]]}); g[t2[q[i]]].pb({q[i], t1[q[i]]}); } //~solve set < int > st; used[n + 1] = true; for (auto it : g[n + 1]) { if (used[it.second]) st.insert(it.first); } vector < int > ans; while(!st.empty()) { int v = *st.rbegin(); st.erase(v); ans.pb(v); used[p[v]] = true; for (auto it : g[p[v]]) { if (used[it.second]) { st.insert(it.first); } } } reverse(all(ans)); for (int i = 0; i < (int)ans.size(); i++) res[ans[i]] = i + 1; forn(i, n) printf("%d\n", res[i]); return 0; }
codenet
-1
1,869
s581243870
p04052
1,580,010,159
cpp
Accepted
612
48,640
#include<queue> #include<cstdio> #include<cctype> #include<vector> #include<cstring> #include<functional> namespace IO { char ibuf[(1<<21)+1],obuf[(1<<21)+1],st[15],*iS,*iT,*oS=obuf,*oT=obuf+(1<<21); char Get(){return (iS==iT? (iT=(iS=ibuf)+fread(ibuf,1,(1<<21)+1,stdin),(iS==iT? EOF:*iS++)):*iS++);} void Flush(){fwrite(obuf,1,oS-obuf,stdout),oS=obuf;} void Put(char x){*oS++=x;if(oS==oT)Flush();} int read(){int x=0,c=Get();while(!isdigit(c))c=Get();while(isdigit(c))x=x*10+c-48,c=Get();return x;} void write(int x){int top=0;while(x)st[++top]=(x%10)+48,x/=10;while(top)Put(st[top--]);Put('\n');} }using namespace IO; using std::vector; using std::priority_queue; int min(int a,int b){return a<b? a:b;} const int N=500007,inf=0x7f7f7f7f; vector<int>e[N];priority_queue<int,vector<int>,std::greater<int>>Q; int n,k,p[N],q[N],mn[N<<2],deg[N]; #define ls p<<1 #define rs p<<1|1 #define mid ((l+r)>>1) void pushup(int p){mn[p]=min(mn[ls],mn[rs]);} void update(int p,int l,int r,int x,int v) { if(r==l) return (void)(mn[p]=v); (x<=mid? update(ls,l,mid,x,v):update(rs,mid+1,r,x,v)),pushup(p); } int query(int p,int l,int r,int L,int R) { if(r<L||R<l) return inf; if(L<=l&&r<=R) return mn[p]; return min(query(ls,l,mid,L,R),query(rs,mid+1,r,L,R)); } int main() { int n=read(),k=read(),m=0;memset(mn,0x7f,sizeof mn); for(int i=1;i<=n;++i) q[read()]=i; for(int i=n,j;i;--i) { if((j=query(1,1,n,q[i]+1,q[i]+k-1))^inf) ++deg[q[j]],e[q[i]].push_back(q[j]); if((j=query(1,1,n,q[i]-k+1,q[i]-1))^inf) ++deg[q[j]],e[q[i]].push_back(q[j]); update(1,1,n,q[i],i); } for(int i=1;i<=n;++i) if(!deg[i]) Q.push(i); for(int u;!Q.empty();) { q[++m]=u=Q.top(),Q.pop(); for(int v:e[u]) if(!--deg[v]) Q.push(v); } for(int i=1;i<=n;++i) p[q[i]]=i; for(int i=1;i<=n;++i) write(p[i]); return Flush(),0; }
codenet
-1
1,873
s116600724
p04052
1,507,766,550
cpp
Accepted
632
42,240
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define pb push_back #define rep(i,a,b) for(i=a;i<=b;i++) #define lid node<<1 #define rid node<<1|1 #define lch lid,l,mid #define rch rid,mid+1,r using namespace std; const int N=500010; const int INF=1e9; int seg[N<<2]; int p[N],mp[N];int in[N]; vector<int> vec[N]; int n,K; priority_queue<int> q; void update(int node){ seg[node]=min(seg[lid],seg[rid]); } void build(int node,int l,int r){ if(l==r){seg[node]=INF;return;} int mid=l+r>>1; build(lch);build(rch); update(node); } void modify(int node,int l,int r,int x,int w){ if(l>x||r<x) return; if(l==r){seg[node]=w;return;} int mid=l+r>>1; modify(lch,x,w);modify(rch,x,w); update(node); } int query(int node,int l,int r,int fl,int fr){ if(fl<=l&&r<=fr) return seg[node]; int mid=l+r>>1; if(mid>=fr) return query(lch,fl,fr); else if(mid+1<=fl) return query(rch,fl,fr); else return min(query(lch,fl,fr),query(rch,fl,fr)); } int main(){ // freopen("a.in","r",stdin); // freopen("a.out","w",stdout); int i,j,now,x,y; scanf("%d%d",&n,&K); rep(i,1,n) scanf("%d",&p[i]); rep(i,1,n) mp[p[i]]=i; //rep(i,1,n) printf("%d ",mp[i]);printf("\n"); build(1,1,n); for(i=n;i;i--){ //printf("%d\n",mp[i]); x=query(1,1,n,max(1,mp[i]-K+1),mp[i]); if(x!=INF) {x=mp[x];vec[x].pb(mp[i]);in[mp[i]]++;}//printf("%d->%d\n",mp[i],x);} x=query(1,1,n,mp[i],min(n,mp[i]+K-1)); if(x!=INF) {x=mp[x];vec[x].pb(mp[i]);in[mp[i]]++;}//printf("%d->%d\n",mp[i],x);} modify(1,1,n,mp[i],i); } rep(i,1,n) if(!in[i]) q.push(i); now=n; while(!q.empty()){ x=q.top();q.pop(); mp[now]=x;now--; rep(i,0,(int)vec[x].size()-1){ y=vec[x][i]; in[y]--; if(!in[y]) q.push(y); } } rep(i,1,n) p[mp[i]]=i; rep(i,1,n) printf("%d\n",p[i]); }
codenet
-1
1,874
s422561218
p04052
1,486,628,572
cpp
Accepted
840
16,112
#include<bits/stdc++.h> using namespace std; int QueryAns, N, K, answer[500009], P[500009], arr[500009], aint[2000017]; bool in_q[500009]; priority_queue < int > PQ; void Update (int nod, int st, int dr, int pos, int val) { if (st == dr) { arr[pos] = val, aint[nod] = pos; return ; } int mij = (st + dr) >> 1, f1 = nod << 1, f2 = f1 | 1; if (pos <= mij) Update (f1, st, mij, pos, val); else Update (f2, mij + 1, dr, pos, val); int p1 = aint[f1], p2 = aint[f2]; if (arr[p1] > arr[p2]) aint[nod] = p1; else aint[nod] = p2; } void Query (int nod, int st, int dr, int x, int y) { if (x <= st && dr <= y) { if (x == st) QueryAns = aint[nod]; else if (arr[aint[nod]] > arr[QueryAns]) QueryAns = aint[nod]; return ; } int mij = (st + dr) >> 1, f1 = nod << 1, f2 = f1 | 1; if (x <= mij) Query (f1, st, mij, x, y); if (mij < y) Query (f2, mij + 1, dr, x, y); } void Refresh (int i) { int st = max (i * K, 1), dr = min ((i + 1) * K - 1, N); if (st > dr) return ; Query (1, 1, N, st, dr); int pos = QueryAns; if (in_q[pos]) return ; st = max (pos - K + 1, 1), dr = min (pos + K - 1, N); Query (1, 1, N, st, dr); if (pos == QueryAns) { in_q[pos] = 1; PQ.push (pos); } } int main () { //freopen ("input", "r", stdin); //freopen ("output", "w", stdout); scanf ("%d %d", &N, &K); for (int i=1; i<=N; i++) scanf ("%d", &P[i]); for (int i=1; i<=N; i++) Update (1, 1, N, i, P[i]); for (int i=0; i * K <= N; i++) Refresh (i); for (int curr_pos = N; curr_pos >= 1; curr_pos --) { int val = PQ.top (); PQ.pop (); answer[val] = curr_pos; Update (1, 1, N, val, 0); Refresh (val / K - 1), Refresh (val / K), Refresh (val / K + 1); } for (int i=1; i<=N; i++) printf ("%d\n", answer[i]); return 0; }
codenet
-1
1,881
s935061598
p04052
1,573,573,584
cpp
Accepted
515
30,196
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #define MAXN 510000 #define INF 2147483647 #define ls now*2 #define rs now*2+1 using namespace std; int n,k; int sit[MAXN],val[MAXN]; int tree[MAXN*4]; int in[MAXN],next[MAXN*2],head[MAXN],to[MAXN*2]; int ans[MAXN]; inline int read(){ int x = 0 , f = 1 ; char c = getchar(); while(c<'0'||c>'9'){ if(c=='-')f=-1; c=getchar(); } while(c>='0'&&c<='9'){ x=x*10+c-48; c=getchar(); } return x * f ; } inline void up(int now){ tree[now]=min(tree[now],min(tree[ls],tree[rs])); } void change(int now,int l,int r,int sit,int val){ if (l==r){ tree[now]=min(tree[now],val); return; } int mid=(l+r)/2; if (sit<=mid) change(ls,l,mid,sit,val); else change(rs,mid+1,r,sit,val); up(now); } int ask(int now,int l,int r,int ll,int rr){ if (l>=ll&&r<=rr){ return tree[now]; } int mid=(l+r)/2,tmp=INF; if (ll<=mid) tmp=min(tmp,ask(ls,l,mid,ll,rr)); if (rr>mid) tmp=min(tmp,ask(rs,mid+1,r,ll,rr)); return tmp; } int cnt=0; void link(int u,int v){ cnt++; to[cnt]=v; next[cnt]=head[u]; head[u]=cnt; in[v]++; } priority_queue<int, vector<int>, greater<int> > q; void topo(){ int cnt=0; for(int i=1;i<=n;i++) if (!in[i]) q.push(i); while(!q.empty()){ int now=q.top(); q.pop(); ans[now]=++cnt; for(int i=head[now];i;i=next[i]){ in[to[i]]--; if (!in[to[i]]) q.push(to[i]); } } } int main(){ cin>>n>>k; for(int i=1;i<=n;i++){ val[i]=read(); sit[val[i]]=i; } fill(tree+1,tree+4*MAXN,INF); for(int i=n;i>=1;i--){ int v; if (sit[i]!=n){ v=ask(1,1,n,sit[i]+1,min(sit[i]+k-1,n)); if (v!=INF) link(sit[i],sit[v]); } if (sit[i]!=1){ v=ask(1,1,n,max(sit[i]-k+1,1),sit[i]-1); if (v!=INF) link(sit[i],sit[v]); } change(1,1,n,sit[i],i); } topo(); for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,883
s913832273
p04052
1,599,755,264
cpp
Accepted
772
18,516
#include <bits/stdc++.h> #define maxn 500086 using namespace std; struct Node{ int sum, id; }t[maxn << 2]; #define ls(x) (x << 1) #define rs(x) (x << 1 | 1) inline void up(int x){ t[x].sum = max(t[ls(x)].sum, t[rs(x)].sum); t[x].id = t[x].sum == t[ls(x)].sum ? t[ls(x)].id : t[rs(x)].id; } void del(int x, int l, int r, int pos){ if(l == r){ t[x].sum = -1e9; return; } int mid = l + r >> 1; if(mid >= pos) del(ls(x), l, mid, pos); else del(rs(x), mid + 1, r, pos); up(x); } Node query(int x, int l, int r, int ll, int rr){ if(ll <= l && r <= rr) return t[x]; int mid = l + r >> 1; if(mid < ll) return query(rs(x), mid + 1, r, ll, rr); if(mid >= rr) return query(ls(x), l, mid, ll, rr); Node a = query(ls(x), l, mid, ll, rr), b = query(rs(x), mid + 1, r, ll, rr); return (Node){max(a.sum, b.sum), a.sum > b.sum ? a.id : b.id}; } int a[maxn]; void build(int x, int l, int r){ if(l == r){ t[x].sum = a[l], t[x].id = l; return; } int mid = l + r >> 1; build(ls(x), l, mid), build(rs(x), mid + 1, r); up(x); } int n, k; int ans[maxn]; bool inq[maxn]; priority_queue<int> q; int main(){ scanf("%d%d", &n, &k), k--; for(int i = 1;i <= n;i++) scanf("%d", &a[i]); build(1, 1, n); for(int i = 1;i <= n;i++) if(query(1, 1, n, max(1, i - k), min(n, i + k)).id == i) q.push(i), inq[i] = true; for(int i = n;i;i--){ int x = q.top();q.pop(); ans[x] = i; del(1, 1, n, x); if(!k) continue; Node node = query(1, 1, n, max(1, x - k), max(1, x - 1)); if(node.sum > 0){ int id = node.id; if(!inq[id] && query(1, 1, n, max(1, id - k), min(n, id + k)).id == id) q.push(id), inq[id] = true; } node = query(1, 1, n, min(n, x + 1), min(n, x + k)); if(node.sum > 0){ int id = node.id; if(!inq[id] && query(1, 1, n, max(1, id - k), min(n, id + k)).id == id) q.push(id), inq[id] = true; } } for(int i = 1;i <= n;i++) printf("%d\n", ans[i]); }
codenet
-1
1,891
s799760485
p04052
1,539,734,714
cpp
Accepted
492
31,604
#include<bits/stdc++.h> using namespace std; const int N = 5e5 + 10; const int inf = 0x3f3f3f3f; int n, k, E; int p[N], q[N]; int tr[N << 2], ans1[N], ans2[N]; int fir[N], deg[N], nex[N << 1], arr[N << 1]; priority_queue<int, vector<int>, greater<int> > Q; inline void Add_Edge(int x, int y) { // cout << x << ' ' << y << endl; nex[++E] = fir[x]; fir[x] = E; arr[E] = y; deg[y]++; } void Updata(int x, int l, int r, int pos, int v) { if (l == r) return (void) (tr[x] = v); int mid = (l + r) >> 1; if (mid >= pos) Updata(x << 1, l, mid, pos, v); if (mid < pos) Updata(x << 1 | 1, mid + 1, r, pos, v); tr[x] = min(tr[x << 1], tr[x << 1 | 1]); } int Query(int x, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return tr[x]; int mid = (l + r) >> 1, ans = 0x3f3f3f3f; if (mid >= ql) ans = min(ans, Query(x << 1, l, mid, ql, qr)); if (mid < qr) ans = min(ans, Query(x << 1 | 1, mid + 1, r, ql, qr)); return ans; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", &p[i]); q[p[i]] = i; } /* for (int i = 1; i <= n; ++i) cout << q[i] << ' '; puts("");*/ memset(tr, 0x3f, sizeof tr); for (int i = n; i >= 1; --i) { int x = Query(1, 1, n, min(n, q[i] + 1), min(n, q[i] + k - 1)); int y = Query(1, 1, n, max(1, q[i] - k + 1), max(1, q[i] - 1)); // cout << i << ' ' << x << ' ' << y << endl; if (x != inf) Add_Edge(q[i], q[x]); if (y != inf) Add_Edge(q[i], q[y]); Updata(1, 1, n, q[i], i); } for (int i = 1; i <= n; ++i) if (!deg[i]) Q.push(i); int cnt = 0; while (!Q.empty()) { int x = Q.top(); Q.pop(); for (int i = fir[x]; i; i = nex[i]) { if (--deg[arr[i]] == 0) Q.push(arr[i]); } ans1[++cnt] = x; } for (int i = 1; i <= n; ++i) ans2[ans1[i]] = i; /* for (int i = 1; i <= n; ++i) cout << ans1[i] << ' '; puts("");*/ for (int i = 1; i <= n; ++i) printf("%d\n", ans2[i]); return 0; }
codenet
-1
1,891
s126247448
p04052
1,557,211,232
cpp
Accepted
733
40,952
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int MAXN=500005; int read(){int x;scanf("%d",&x);return x;} struct node{ int ch[2]; int val,id; }t[MAXN<<2]; int tot,N,K,num; int a[MAXN],b[MAXN]; void PU(int u){ t[u].val=max(t[t[u].ch[0]].val,t[t[u].ch[1]].val); t[u].id=t[t[u].ch[0]].val==t[u].val?t[t[u].ch[0]].id:t[t[u].ch[1]].id; } void insert(int u,int tl,int tr,int p,int val){ if(tl==tr){t[u].val=val,t[u].id=p;return;} int mid=(tl+tr)>>1; if(p<=mid){ if(!t[u].ch[0]) t[u].ch[0]=++tot; insert(t[u].ch[0],tl,mid,p,val); } else{ if(!t[u].ch[1]) t[u].ch[1]=++tot; insert(t[u].ch[1],mid+1,tr,p,val); } PU(u); } pair<int,int>Query(int u,int tl,int tr,int l,int r){ // cout<<u<<" "<<tl<<" "<<tr<<" "<<t[u].val<<endl; if(l>r) return make_pair(0,0); if(!u) return make_pair(0,0); if(tl>=l&&tr<=r) return make_pair(t[u].val,t[u].id); if(tr<l||tl>r) return make_pair(0,0); int mid=(tl+tr)>>1; pair<int,int>x,y; x=Query(t[u].ch[0],tl,mid,l,r),y=Query(t[u].ch[1],mid+1,tr,l,r); return x.first>y.first?x:y; } int head[MAXN],du[MAXN]; priority_queue<int>Q; struct line{int nxt,to;}e[MAXN<<1]; int Ans[MAXN],p[MAXN],cnt; void add(int u,int v){e[num]=(line){head[u],v};head[u]=num,num++,du[v]++;} int main(){ memset(head,-1,sizeof(head)); N=read(),K=read(),tot=1; for(int i=1;i<=N;i++) a[i]=read(),b[a[i]]=i; for(int i=1;i<=N;i++){ insert(1,1,N,b[i],i); int x=Query(1,1,N,b[i]-K+1,b[i]-1).second,y=Query(1,1,N,b[i]+1,b[i]+K-1).second; // cout<<x<<" "<<y<<" "<<b[i]<<endl; if(x) add(b[i],x);if(y) add(b[i],y); } for(int i=1;i<=N;i++) if(!du[i]) Q.push(i); cnt=N; while(!Q.empty()){ int x=Q.top();Q.pop(); Ans[cnt--]=x; for(int i=head[x];~i;i=e[i].nxt){ du[e[i].to]--; if(!du[e[i].to]) Q.push(e[i].to); } } for(int i=1;i<=N;i++) p[Ans[i]]=i; for(int i=1;i<=N;i++) printf("%d\n",p[i]); return 0; }
codenet
-1
1,897
s671796548
p04052
1,507,773,133
cpp
Accepted
523
43,008
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MAXN 500000 #define INF 0x3f3f3f3f #define pb push_back #define lc(x) (x<<1) #define rc(x) (x<<1|1) #define mn(x) t[x] #define mid (lef+rig>>1) #define rint register int #define gc() getchar() inline int read(rint ans = 0, rint sgn = ' ', rint ch = gc()) { for(; ch < '0' || ch > '9'; sgn = ch, ch = gc()); for(; ch >='0' && ch <='9';(ans*=10)+=ch-'0', ch = gc()); return sgn-'-'?ans:-ans; } int N, K, O, P[MAXN+5], Q[MAXN+5], t[(MAXN<<2)+5], in[MAXN+5]; vector<int> e[MAXN+5]; priority_queue<int,vector<int>,less<int> > q; #define Push(p) (mn(p) = min(mn(lc(p)),mn(rc(p)))) inline void Build(int p, int lef, int rig){lef-rig ? Build(lc(p),lef,mid), Build(rc(p),mid+1,rig), Push(p) : mn(p) = INF;} inline void Edit(int p, int lef, int rig, int x, int v){lef-rig ? x <= mid ? Edit(lc(p),lef,mid,x,v) : Edit(rc(p),mid+1,rig,x,v), Push(p) : mn(p) = v;} inline int Query(int p, int lef, int rig, int L, int R) { if(L == lef && R == rig) return mn(p); if(R <= mid) return Query(lc(p),lef,mid,L,R); if(L > mid) return Query(rc(p),mid+1,rig,L,R); return min(Query(lc(p),lef,mid,L,mid),Query(rc(p),mid+1,rig,mid+1,R)); } int main() { O = N = read(), K = read(); for(rint i = 1; i <= N; P[i] = read(), Q[P[i]] = i, i++); Build(1,1,N); for(rint i = N, p; i >= 1; Edit(1,1,N,Q[i],i), i--) p = Query(1,1,N,max(Q[i]-K+1,1),Q[i]), p <= N ? e[Q[p]].pb(Q[i]), ++in[Q[i]] : 0, p = Query(1,1,N,Q[i],min(Q[i]+K-1,N)), p <= N ? e[Q[p]].pb(Q[i]), ++in[Q[i]] : 0; for(rint i = 1; i <= N; !in[i] ? q.push(i),0 : 0, i++); for(rint p, i, _sz; !q.empty(); ) for(p = q.top(), q.pop(), P[p] = O--, i = 0, _sz = (int)e[p].size(); i < _sz; i++) !--in[e[p][i]] ? q.push(e[p][i]),0 : 0; for(rint i = 1; i <= N; printf("%d\n",P[i]), i++); return 0; }
codenet
-1
1,901
s846991042
p04052
1,564,257,222
cpp
Accepted
436
44,672
#include<bits/stdc++.h> using namespace std; #define next Next #define gc getchar #define mid (l+r)/2 /*char buf[1<<21],*p1=buf,*p2=buf; inline int gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}*/ inline int read() { int ret=0,f=0;char c=gc(); while(!isdigit(c)){if(c=='-')f=1;c=gc();} while(isdigit(c)){ret=ret*10+c-48;c=gc();} if(f)return -ret;return ret; } const int N=1e6+10; const int inf=0x3f3f3f3f; int du[N],x[N],p[N],ans[N],top,tot,n,k; priority_queue <int,vector<int>,greater<int> >q; int head[N]; struct node{ int too,next; }edge[N*2]; void add(int a,int b) { edge[++top].too=b;edge[top].next=head[a];head[a]=top; } int mi[N*4]; void change(int nod,int l,int r,int x,int val) { if(l==r){mi[nod]=val;return;} if(x<=mid) change(nod*2,l,mid,x,val); else change(nod*2+1,mid+1,r,x,val); mi[nod]=min(mi[nod*2],mi[nod*2+1]); } int query(int nod,int l,int r,int L,int R) { if(mi[nod]==inf)return inf; if(l==L&&r==R)return mi[nod]; if(R<=mid)return query(nod*2,l,mid,L,R); else if(L>mid)return query(nod*2+1,mid+1,r,L,R); else return min(query(nod*2,l,mid,L,mid),query(nod*2+1,mid+1,r,mid+1,R)); } int main() { n=read();k=read(); for(int i=1;i<=n;i++)p[read()]=i; memset(mi,0x3f,sizeof(mi)); for(int i=n;i;i--) { int x=query(1,1,n,p[i],min(n,p[i]+k-1)); if(x!=inf)add(p[i],p[x]),++du[p[x]]; x=query(1,1,n,max(1,p[i]-k+1),p[i]); if(x!=inf) add(p[i],p[x]),++du[p[x]]; change(1,1,n,p[i],i); } for(int i=1;i<=n;i++)if(!du[i])q.push(i); while(!q.empty()) { int now=q.top(); ans[now]=++tot; q.pop(); for(int i=head[now];i;i=edge[i].next) { int v=edge[i].too; --du[v]; if(!du[v])q.push(v); } } for(int i=1;i<=n;i++)printf("%d\n",ans[i]); return 0; }
codenet
-1
1,904
s850370675
p04052
1,507,565,733
cpp
Accepted
485
26,480
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<ctime> #define maxn 500010 using namespace std; int n,k; int a[maxn]; int t[maxn]; void file(){ freopen("permutation.in","r",stdin); freopen("permutation.out","w",stdout); } int tr[maxn<<2]; struct node{ int v,nxt; }e[maxn<<2]; int head[maxn],cnt; void add(int u,int v){ e[++cnt].v=v;e[cnt].nxt=head[u];head[u]=cnt; } int in[maxn]; void pushup(int u){ tr[u]=max(tr[u<<1],tr[u<<1|1]); } void insert(int u,int l,int r,int val,int pos){ if(l==r){ tr[u]=val;return ; } int mid=(l+r)>>1; if(pos<=mid)insert(u<<1,l,mid,val,pos); else insert(u<<1|1,mid+1,r,val,pos); pushup(u); } int query(int u,int l,int r,int ql,int qr){ if(ql<=l && qr>=r)return tr[u]; int mid=(l+r)>>1; int res=0; if(ql<=mid)res=max(res,query(u<<1,l,mid,ql,qr)); if(qr>mid)res=max(res,query(u<<1|1,mid+1,r,ql,qr)); return res; } priority_queue<int, vector<int>, greater<int> >q; int ans[maxn]; void gettop(){ for(int i=1;i<=n;i++)if(in[i]==0)q.push(i); for(int i=1;i<=n;i++){ if(q.empty())return ; int u=q.top();q.pop(); ans[i]=u; for(int j=head[u];j;j=e[j].nxt){ int v=e[j].v; in[v]--; if(in[v]==0)q.push(v); } } } inline int read(){ int x=0,f=1;char ch=0; while(ch<'0' || ch>'9'){ if(ch=='-')f=-1;ch=getchar(); } while(ch>='0' && ch<='9'){ x=(x<<3)+(x<<1)+ch-'0';ch=getchar(); } return x*f; } int main(){ //file(); n=read();k=read(); for(int i=1;i<=n;i++){ a[i]=read(); t[a[i]]=i; } // cerr<<clock()/1000<<endl; for(int i=1;i<=n;i++){//printf("*\n"); int pos=query(1,1,n,t[i]+1,t[i]+k-1); insert(1,1,n,i,t[i]); add(t[pos],t[i]); if(pos!=0 && t[pos]!=0){ in[t[i]]++; } } // cerr<<clock()/1000<<endl; gettop(); // cerr<<clock()/1000<<endl; for(int i=1;i<=n;i++){ a[ans[i]]=i; } for(int i=1;i<=n;i++)printf("%d\n",a[i]); // cerr<<clock()/1000<<endl; return 0; }
codenet
-1
1,904
s204433976
p04052
1,486,998,279
cpp
Accepted
662
21,488
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<vector> #include<set> #include<map> #include<bitset> #include<cmath> #include<string> #include<queue> #define ls (t<<1) #define rs ((t<<1)+1) #define mid ((l+r)>>1) #define fi first #define se second #define mk make_pair #define pb push_back #define N 500005 #define M 200005 #define seed 23333 using namespace std; priority_queue<int>Q; int i,j,m,n,p[N],k,Max[N],st[N],ed[N],tree[N*4],Good[N],tot,ans[N]; void build_tree(int l,int r,int t) { if (l==r) tree[t]=l; else { build_tree(l,mid,ls); build_tree(mid+1,r,rs); tree[t]=p[tree[ls]]>p[tree[rs]]?tree[ls]:tree[rs]; } } void FindMax(int &x,int ll,int rr,int l,int r,int t) { if (ll<=l&&r<=rr) { if (p[tree[t]]>p[x]) x=tree[t]; } else { if (ll<=mid) FindMax(x,ll,rr,l,mid,ls); if (rr>mid) FindMax(x,ll,rr,mid+1,r,rs); } } void upd(int ll,int l,int r,int t) { if (l==r) return; if (ll<=mid) upd(ll,l,mid,ls); else upd(ll,mid+1,r,rs); tree[t]=p[tree[ls]]>p[tree[rs]]?tree[ls]:tree[rs]; } int check(int x) { if (!p[x]) return 0; int g=0; FindMax(g,max(1,x-k+1),min(n,x+k-1),1,n,1); if (g==x) return 1; return 0; } int main() { scanf("%d%d",&n,&k); for (i=1;i<=n;++i) scanf("%d",&p[i]); build_tree(1,n,1); for (i=1;i<=n;i=ed[tot]+1) { st[++tot]=i; ed[tot]=min(n,i+k-1); } for (i=1;i<=tot;++i) { FindMax(Max[i],st[i],ed[i],1,n,1); if (check(Max[i])) Good[i]=1,Q.push(Max[i]); } for (i=n;i;--i) { int x=Q.top(); Q.pop(); ans[x]=i; int bl=(x-1)/k+1; p[Max[bl]]=0; Good[bl]=0; upd(Max[bl],1,n,1); FindMax(Max[bl],st[bl],ed[bl],1,n,1); if (check(Max[bl])) Good[bl]=1,Q.push(Max[bl]); if (!Good[bl-1]&&check(Max[bl-1])) Good[bl-1]=1,Q.push(Max[bl-1]); if (!Good[bl+1]&&check(Max[bl+1])) Good[bl+1]=1,Q.push(Max[bl+1]); } for (i=1;i<=n;++i) printf("%d\n",ans[i]); }
codenet
-1
1,909
s681084975
p04052
1,555,779,198
cpp
Accepted
981
60,288
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<queue> using namespace std; typedef pair<int,int> PI; typedef long long LL; const int N=500005; vector<int> vec[N]; int a[N]; int n,k; struct qq { int l,r; int s1,s2; PI c; }tr[N*2];int num; void upd (int now) { int s1=tr[now].s1,s2=tr[now].s2; tr[now].c=max(tr[s1].c,tr[s2].c); } void bt (int l,int r) { int now=++num; tr[now].l=l;tr[now].r=r; tr[now].c={0,0}; if (l==r) return ; int mid=(l+r)>>1; tr[now].s1=num+1;bt(l,mid); tr[now].s2=num+1;bt(mid+1,r); } void change (int now,int x,int c) { if (tr[now].l==tr[now].r) {tr[now].c={c,x};return ;} int mid=(tr[now].l+tr[now].r)>>1; int s1=tr[now].s1,s2=tr[now].s2; if (x<=mid) change(s1,x,c); else change(s2,x,c); upd(now); } PI query (int now,int l,int r) { if (l>r) return {0,0}; if (tr[now].l==l&&tr[now].r==r) return tr[now].c; int s1=tr[now].s1,s2=tr[now].s2; int mid=(tr[now].l+tr[now].r)>>1; if (r<=mid) return query(s1,l,r); else if (l>mid) return query(s2,l,r); else return max(query(s1,l,mid),query(s2,mid+1,r)); } int du[N]; priority_queue<int> q; int ans[N]; int main() { scanf("%d%d",&n,&k); for (int u=1;u<=n;u++) {int x;scanf("%d",&x);a[x]=u;} bt(1,n); for (int u=1;u<=n;u++) { change(1,a[u],u);PI t; //printf("modify:%d %d\n",a[u],u); t=query(1,max(1,a[u]-k+1),a[u]-1);if (t.first!=0) {vec[a[u]].push_back(t.second);du[t.second]++;/*printf("%d %d\n",a[u],t.second);*/} t=query(1,a[u]+1,min(n,a[u]+k-1));if (t.first!=0) {vec[a[u]].push_back(t.second);du[t.second]++;/*printf("%d %d\n",a[u],t.second);*/} } for (int u=1;u<=n;u++) if (du[u]==0) q.push(u); for (int u=n;u>=1;u--) { int x=q.top();q.pop(); ans[x]=u; int siz=vec[x].size(); for (int i=0;i<siz;i++) { du[vec[x][i]]--; if (du[vec[x][i]]==0) q.push(vec[x][i]); } } for (int u=1;u<=n;u++) printf("%d\n",ans[u]); return 0; }
codenet
-1
1,910
s011206748
p04052
1,550,537,318
cpp
Accepted
234
27,764
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define SZ(x) ((int)x.size()) #define L(i,u) for (register int i=head[u]; i; i=nxt[i]) #define rep(i,a,b) for (register int i=(a); i<=(b); i++) #define per(i,a,b) for (register int i=(a); i>=(b); i--) using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int,int> Pii; typedef vector<int> Vi; template<class T> inline void read(T &x){ x=0; char c=getchar(); int f=1; while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f; } template<class T> inline void umin(T &x, T y){x=x<y?x:y;} template<class T> inline void umax(T &x, T y){x=x>y?x:y;} inline ui R() { static ui seed=416; return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13; } const int N = 550000,inf=0x3f3f3f3f; int n,a[N],ans[N],k,e[N][2],sz[N],m,v[N<<2]; inline void getinv(int *a, int *b){ static int c[N]; rep(i,1,n)c[a[i]]=i;rep(i,1,n)b[i]=c[i]; } int find(int l, int r){ l=max(l,1);r=min(r,n);if(l>r)return 0; l=l+m-1,r=r+m+1;int res=inf; while((l>>1)!=(r>>1)){ if(!(l&1))res=min(res,v[l+1]); if(r&1)res=min(res,v[r-1]); l>>=1;r>>=1; } return res<inf?res:0; } int main() { read(n);read(k);rep(i,1,n)read(a[i]);getinv(a,a); for(m=1;m<=n+1;m<<=1);memset(v,0x3f3f3f3f,sizeof(v)); per(i,n,1){ int t=find(a[i]+1,a[i]+k-1);if(t)e[a[i]][sz[a[i]]++]=a[t]; t=find(a[i]-k+1,a[i]-1);if(t)e[a[i]][sz[a[i]]++]=a[t]; v[m+a[i]]=i;for(int j=m+a[i]>>1;j;j>>=1)v[j]=min(v[j<<1],v[j<<1|1]); } static int deg[N];priority_queue<int,Vi,greater<int> >heap; rep(i,1,n)rep(j,0,sz[i]-1)deg[e[i][j]]++;//,printf("e %d %d\n",i,e[i][j]); rep(i,1,n)if(!deg[i])heap.push(i);int now=0; while(!heap.empty()){ int u=heap.top();heap.pop();ans[++now]=u; rep(i,0,sz[u]-1){ if(!--deg[e[u][i]]){ heap.push(e[u][i]); } } } getinv(ans,ans); rep(i,1,n)printf("%d\n",ans[i]); return 0; }
codenet
-1
1,917
s177686050
p04052
1,548,455,644
cpp
Accepted
462
27,384
#include <bits/stdc++.h> using namespace std; const int Maxn = 500010; int N, K, A[ Maxn ], P[ Maxn ], Rec[ Maxn ]; int Tree[ Maxn << 2 ]; struct edge { int To, Next; }; edge Edge[ Maxn << 1 ]; int Start[ Maxn ], Used; int Degree[ Maxn ]; inline void AddEdge( int x, int y ) { Edge[ ++Used ] = ( edge ) { y, Start[ x ] }; Start[ x ] = Used; ++Degree[ y ]; return; } void Add( int Index, int L, int R, int Pos, int Key ) { Tree[ Index ] = max( Tree[ Index ], Key ); if( L == R ) return; int Mid = L + R >> 1; if( Pos <= Mid ) Add( Index << 1, L, Mid, Pos, Key ); if( Pos > Mid ) Add( Index << 1 | 1, Mid + 1, R, Pos, Key ); return; } int Query( int Index, int L, int R, int l, int r ) { if( l <= L && R <= r ) return Tree[ Index ]; int Mid = L + R >> 1; if( r <= Mid ) return Query( Index << 1, L, Mid, l, r ); if( l > Mid ) return Query( Index << 1 | 1, Mid + 1, R, l, r ); return max( Query( Index << 1, L, Mid, l, r ), Query( Index << 1 | 1, Mid + 1, R, l, r ) ); } struct Cmp { bool operator () ( int x, int y ) { return x > y; } }; priority_queue< int, vector< int >, Cmp > PQ; int main() { scanf( "%d%d", &N, &K ); for( int i = 1; i <= N; ++i ) scanf( "%d", &A[ i ] ); for( int i = 1; i <= N; ++i ) P[ A[ i ] ] = i; for( int i = 1; i <= N; ++i ) { int x; x = Query( 1, 1, N, max( 1, P[ i ] - K + 1 ), P[ i ] ); if( x ) AddEdge( P[ x ], P[ i ] ); x = Query( 1, 1, N, P[ i ], min( N, P[ i ] + K - 1 ) ); if( x ) AddEdge( P[ x ], P[ i ] ); Add( 1, 1, N, P[ i ], i ); } for( int i = 1; i <= N; ++i ) if( !Degree[ i ] ) PQ.push( i ); for( int i = 1; i <= N; ++i ) { Rec[ i ] = PQ.top(); PQ.pop(); for( int t = Start[ Rec[ i ] ]; t; t = Edge[ t ].Next ) { int v = Edge[ t ].To; --Degree[ v ]; if( !Degree[ v ] ) PQ.push( v ); } } for( int i = 1; i <= N; ++i ) A[ Rec[ i ] ] = i; for( int i = 1; i <= N; ++i ) printf( "%d\n", A[ i ] ); return 0; }
codenet
-1
1,919
s516546517
p04052
1,563,572,869
cpp
Accepted
492
28,908
#include <cctype> #include <algorithm> #include <cstring> #include <cstdio> #include <queue> using namespace std; inline int read(int f = 1, int x = 0, char ch = ' ') { while(!isdigit(ch = getchar())) if(ch == '-') f = -1; while(isdigit(ch)) x = x*10+ch-'0', ch = getchar(); return f*x; } const int N = 5e5+5, INF = 0x3f3f3f3f; struct Edge { int next, to; Edge(int next = 0, int to = 0):next(next), to(to) {}; }edge[N<<1]; int tot, head[N], in[N]; void add(int x, int y) { edge[++tot] = Edge(head[x], y); head[x] = tot; ++in[y]; } int val[N<<2]; void change(int x, int l, int r, int pos, int d) { val[x] = min(val[x], d); if(l == r) return; int mid = (l+r)>>1; if(pos <= mid) return change(x<<1, l, mid, pos, d); else return change(x<<1|1, mid+1, r, pos, d); } int query(int x, int l, int r, int ql, int qr) { if(ql > qr) return INF; if(ql <= l&&r <= qr) return val[x]; int ret = INF, mid = (l+r)>>1; if(ql <= mid) ret = min(ret, query(x<<1, l, mid, ql, qr)); if(qr > mid) ret = min(ret, query(x<<1|1, mid+1, r, ql, qr)); return ret; } int n, m, k, ans[N], p[N]; priority_queue<pair<int, int> > q; int main() { memset(val, 0x3f, sizeof(val)); n = read(), k = read(); for(int i = 1; i <= n; ++i) p[read()] = i; for(int x = n, y; x; --x) { y = query(1, 1, n, max(p[x]-k+1, 1), p[x]-1); if(y != INF) add(x, y); y = query(1, 1, n, p[x]+1, min(p[x]+k-1, n)); if(y != INF) add(x, y); change(1, 1, n, p[x], x); } for(int x = 1; x <= n; ++x) if(!in[x]) q.push(make_pair(-p[x], x)); while(q.size()) { int x = q.top().second; q.pop(); ++m; ans[p[x]] = m; for(int i = head[x]; i; i = edge[i].next) { int y = edge[i].to; if(--in[y] == 0) q.push(make_pair(-p[y], y)); } } for(int i = 1; i <= n; ++i) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,923
s164512618
p04052
1,565,272,406
cpp
Accepted
676
54,784
//minamoto #include<bits/stdc++.h> #define R register #define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i) #define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i) #define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v) template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;} template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;} using namespace std; const int N=1e6+5,inf=0x3f3f3f3f; struct eg{int v,nx;}e[N<<1];int head[N],tot; inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;} inline int min(R int x,R int y){return x<y?x:y;} struct node;typedef node* ptr; struct node{ ptr lc,rc;int mn; inline void upd(){mn=min(lc->mn,rc->mn);} }E[N<<2],*nd=E,*rt; priority_queue<int,vector<int>,greater<int> >q;int deg[N],pos[N],id[N],n,k; void build(ptr &p,int l,int r){ p=++nd;if(l==r)return p->mn=inf,void(); int mid=(l+r)>>1; build(p->lc,l,mid),build(p->rc,mid+1,r); p->upd(); } void update(ptr p,int l,int r,int x,int v){ if(l==r)return p->mn=v,void(); int mid=(l+r)>>1; x<=mid?update(p->lc,l,mid,x,v):update(p->rc,mid+1,r,x,v); p->upd(); } int res; void query(ptr p,int l,int r,int ql,int qr){ if(ql<=l&&qr>=r)return cmin(res,p->mn),void(); int mid=(l+r)>>1; if(ql<=mid)query(p->lc,l,mid,ql,qr); if(qr>mid)query(p->rc,mid+1,r,ql,qr); } int main(){ // freopen("testdata.in","r",stdin); scanf("%d%d",&n,&k); for(R int i=1,x;i<=n;++i)scanf("%d",&x),pos[x]=i; build(rt,1,n); for(R int i=n,l,r;i;--i){ l=max(pos[i]-k+1,1),r=pos[i]-1,res=inf; if(l<=r)query(rt,1,n,l,r);if(res!=inf)add(pos[i],pos[res]),++deg[pos[res]]; l=pos[i]+1,r=min(pos[i]+k-1,n),res=inf; if(l<=r)query(rt,1,n,l,r);if(res!=inf)add(pos[i],pos[res]),++deg[pos[res]]; update(rt,1,n,pos[i],i); } // fp(u,1,n)go(u)printf("%d %d\n",u,v); fp(i,1,n)if(!deg[i])q.push(i); for(R int u,i=1;i<=n;++i){ u=q.top(),q.pop(),id[u]=i; go(u)if(!--deg[v])q.push(v); } fp(i,1,n)printf("%d\n",id[i]); return 0; }
codenet
-1
1,934
s565627982
p04052
1,571,178,383
cpp
Accepted
566
31,224
#include<bits/stdc++.h> using namespace std; const int N=500010; int gi() { int f=0; char ch=getchar(); while(ch<'0' || ch>'9')ch=getchar(); while(ch>='0' && ch<='9')f=f*10+ch-48,ch=getchar(); return f; } int n,k,p[N],a[N]; struct node { int mn; }s[N<<2]; void pushup(int k) { s[k].mn=min(s[k<<1].mn,s[k<<1|1].mn); } void build(int l,int r,int k) { if(l==r) { s[k].mn=n+1; return; } int mid=l+r>>1; build(l,mid,k<<1); build(mid+1,r,k<<1|1); s[k].mn=n+1; } int head[N],cnt,ct; struct edge { int fr,to,next; bool operator < (const edge a)const { return to<a.to; } }e[N<<1]; void adde(int k) { e[k].next=head[e[k].fr]; head[e[k].fr]=k; } int query(int L,int R,int l,int r,int k) { if(l>r)return n+1; if(l<=L && R<=r) return s[k].mn; int mid=L+R>>1,res=n+1; if(l<=mid) res=min(res,query(L,mid,l,r,k<<1)); if(r>mid) res=min(res,query(mid+1,R,l,r,k<<1|1)); return res; } void modify(int l,int r,int p,int x,int k) { if(l==r) { s[k].mn=x; return; } int mid=l+r>>1; if(p<=mid) modify(l,mid,p,x,k<<1); else modify(mid+1,r,p,x,k<<1|1); pushup(k); } int in[N]; priority_queue<int,vector<int>,greater<int> >q; int id[N]; void topsort() { for(int i=1;i<=n;i++) for(int j=head[i];j;j=e[j].next) { int v=e[j].to; in[v]++; } for(int i=1;i<=n;i++) if(!in[i]) q.push(i); while(!q.empty()) { int k=q.top(); q.pop(); id[k]=++ct; for(int i=head[k];i;i=e[i].next) { int v=e[i].to; in[v]--; if(!in[v])q.push(v); } } } int main() { n=gi(); k=gi(); for(int i=1;i<=n;i++) p[i]=gi(),a[p[i]]=i; build(1,n,1); for(int i=n;i;i--) { int x=query(1,n,max(a[i]-k+1,1),a[i]-1,1); if(x<=n) e[++cnt].fr=a[i],e[cnt].to=a[x]; x=query(1,n,a[i]+1,min(a[i]+k-1,n),1); if(x<=n) e[++cnt].fr=a[i],e[cnt].to=a[x]; modify(1,n,a[i],i,1); } sort(e+1,e+cnt+1); for(int i=1;i<=cnt;i++) adde(i); topsort(); for(int i=1;i<=n;i++) printf("%d\n",id[i]); return 0; }
codenet
-1
1,934
s164512618
p04052
1,565,272,406
cpp
Accepted
676
54,784
//minamoto #include<bits/stdc++.h> #define R register #define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i) #define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i) #define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v) template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;} template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;} using namespace std; const int N=1e6+5,inf=0x3f3f3f3f; struct eg{int v,nx;}e[N<<1];int head[N],tot; inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;} inline int min(R int x,R int y){return x<y?x:y;} struct node;typedef node* ptr; struct node{ ptr lc,rc;int mn; inline void upd(){mn=min(lc->mn,rc->mn);} }E[N<<2],*nd=E,*rt; priority_queue<int,vector<int>,greater<int> >q;int deg[N],pos[N],id[N],n,k; void build(ptr &p,int l,int r){ p=++nd;if(l==r)return p->mn=inf,void(); int mid=(l+r)>>1; build(p->lc,l,mid),build(p->rc,mid+1,r); p->upd(); } void update(ptr p,int l,int r,int x,int v){ if(l==r)return p->mn=v,void(); int mid=(l+r)>>1; x<=mid?update(p->lc,l,mid,x,v):update(p->rc,mid+1,r,x,v); p->upd(); } int res; void query(ptr p,int l,int r,int ql,int qr){ if(ql<=l&&qr>=r)return cmin(res,p->mn),void(); int mid=(l+r)>>1; if(ql<=mid)query(p->lc,l,mid,ql,qr); if(qr>mid)query(p->rc,mid+1,r,ql,qr); } int main(){ // freopen("testdata.in","r",stdin); scanf("%d%d",&n,&k); for(R int i=1,x;i<=n;++i)scanf("%d",&x),pos[x]=i; build(rt,1,n); for(R int i=n,l,r;i;--i){ l=max(pos[i]-k+1,1),r=pos[i]-1,res=inf; if(l<=r)query(rt,1,n,l,r);if(res!=inf)add(pos[i],pos[res]),++deg[pos[res]]; l=pos[i]+1,r=min(pos[i]+k-1,n),res=inf; if(l<=r)query(rt,1,n,l,r);if(res!=inf)add(pos[i],pos[res]),++deg[pos[res]]; update(rt,1,n,pos[i],i); } // fp(u,1,n)go(u)printf("%d %d\n",u,v); fp(i,1,n)if(!deg[i])q.push(i); for(R int u,i=1;i<=n;++i){ u=q.top(),q.pop(),id[u]=i; go(u)if(!--deg[v])q.push(v); } fp(i,1,n)printf("%d\n",id[i]); return 0; }
codenet
-1
1,934
s115398506
p04052
1,523,298,745
cpp
Accepted
1,079
62,704
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<queue> #include<algorithm> using namespace std; int tp[505050],la[505050]; struct Node { Node *lch,*rch; int l,r; int ma; }*rt; int p[505050]; int t[505050]; int sn; bool cmp(int x,int y) { return p[x]<p[y]; } void work(int l,int r) { sn++; sort(t+l,t+r+1,cmp); for(int i=l;i<=r;i++) { la[t[i]]=tp[sn]; tp[sn]=t[i]; } } void update(Node *x) { x->ma=max(x->lch->ma,x->rch->ma); } Node *Build(int l,int r) { Node *ret=new Node; ret->l=l; ret->r=r; if(l==r) { ret->ma=p[l]; ret->lch=ret->rch=NULL; return ret; } int mid=(l+r)>>1; ret->lch=Build(l,mid); ret->rch=Build(mid+1,r); update(ret); return ret; } void modify(Node *x,int p) { if(x->l==x->r) { x->ma=0; return; } int mid=(x->l+x->r)>>1; if(p<=mid) modify(x->lch,p); else modify(x->rch,p); update(x); } int query(Node *x,int l,int r) { if(l<=x->l&&r>=x->r) return x->ma; int ret=0,mid=(x->l+x->r)>>1; if(l<=mid) ret=max(ret,query(x->lch,l,r)); if(mid<r) ret=max(ret,query(x->rch,l,r)); return ret; } int n,k; bool jud(int x) { if(query(rt,max(x-k+1,1),min(x+k-1,n))==p[x]) return true; return false; } bool inq[505050]; int ans[505050]; int vv; priority_queue <int> q; int main() { int now,ss; scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d",&p[i]); for(int i=1;i<=n;i++) t[i]=i; sn=0; memset(tp,0,sizeof(tp)); for(int i=1;i<=n;i+=k) work(i,min(i+k-1,n)); memset(inq,0,sizeof(inq)); for(int i=1;i<=sn;i++) { q.push(tp[i]); inq[tp[i]]=true; } rt=Build(1,n); inq[0]=true; vv=n; while(!q.empty()) { now=q.top(); inq[now]=false; q.pop(); if(jud(now)) { ans[now]=vv--; modify(rt,now); ss=(now+k-1)/k; tp[ss]=la[tp[ss]]; for(int i=ss-1;i<=ss+1;i++) if(!inq[tp[i]]) { q.push(tp[i]); inq[tp[i]]=true; } } } for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,936
s882322401
p04052
1,536,690,878
cpp
Accepted
664
34,164
#include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<ctime> #include<bitset> #include<cstdio> #include<cstdlib> #include<cstring> #include<complex> #include<iostream> #include<algorithm> #define N 500001 #define M 1<<20 #define INF 0x3f3f3f3f #define LL long long #define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v #define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) char ch;bool fs;void re(int& x) { while(ch=getchar(),ch<33); if(ch=='-')fs=1,x=0;else fs=0,x=ch-48; while(ch=getchar(),ch>33)x=x*10+ch-48; if(fs)x=-x; } using namespace std; priority_queue<int , vector<int> , greater<int> > que; int n,k,p[N],a[N],d[N],top,st[N]; int l[M],r[M],mn[M]; int cnt,head[N],nxt[N+N],to[N+N]; void upd(int k) { mn[k]=min(mn[k<<1],mn[k<<1|1]); } void build(int k,int a,int b) { l[k]=a,r[k]=b; if(a==b) { mn[k]=p[a]; return; } build(k<<1,a,a+b>>1); build(k<<1|1,(a+b>>1)+1,b); upd(k); } void change(int k,int a,int b) { if(l[k]==r[k]) { mn[k]=b; return; } if(a<=(l[k]+r[k]>>1))change(k<<1,a,b); else change(k<<1|1,a,b); upd(k); } int query(int k,int a,int b) { // cerr<<k<<" "<<a<<" "<<b<<endl; if(a>r[k] || b<l[k])return INF; if(a<=l[k] && b>=r[k])return mn[k]; return min(query(k<<1,a,b),query(k<<1|1,a,b)); } int main() { re(n),re(k); for(int i=1;i<=n;++i) re(p[i]),a[p[i]]=i; /* for(int i=1;i<=n;++i) printf("%d ",a[i]); puts("");*/ build(1,1,n); for(int i=1;i<=n;++i) { change(1,a[i],INF); int j=query(1,a[i]+1,a[i]+k-1); if(j<INF) { ++d[a[j]]; add_edge(a[i],a[j]); } j=query(1,a[i]-k+1,a[i]-1); if(j<INF) { ++d[a[j]]; add_edge(a[i],a[j]); } } for(int i=1;i<=n;++i) if(!d[i]) que.push(i); while(!que.empty()) { int u=que.top();que.pop(); st[u]=++top; for(int i=head[u];i;i=nxt[i]) { --d[to[i]]; if(!d[to[i]]) que.push(to[i]); } } for(int i=1;i<=n;++i) printf("%d\n",st[i]); }
codenet
-1
1,939
s733007787
p04052
1,597,119,328
cpp
Accepted
664
43,184
#include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<stack> #include<vector> #include<queue> #include<bitset> #include<set> using namespace std; #define LL long long #define DB double #define MOD 1000000007 #define Pr pair<int,int> #define X first #define Y second #define MAXN 500000 #define INF 1000000000 #define mem(x,p) memset(x,p,sizeof(x)) int read(){ int x=0,F=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')F=-1;c=getchar();} while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();} return x*F; } struct Seg_tree{ #define pl (x<<1) #define pr (x<<1|1) #define mid (l+r>>1) int mn[(MAXN<<2)+5]; void Build(int x,int l,int r){ mn[x]=INF; if(l==r)return ; Build(pl,l,mid),Build(pr,mid+1,r); } void Insert(int x,int l,int r,int pos,int v){ if(l==r){mn[x]=v;return ;} if(pos<=mid)Insert(pl,l,mid,pos,v); else Insert(pr,mid+1,r,pos,v); mn[x]=min(mn[pl],mn[pr]); } int Query(int x,int l,int r,int L,int R){ if(L>r||l>R)return INF; if(L<=l&&r<=R)return mn[x]; return min(Query(pl,l,mid,L,R),Query(pr,mid+1,r,L,R)); } }T; vector<int> G[MAXN+5]; int n,K,a[MAXN+5],ct; int rnk[MAXN+5],du[MAXN+5],ans[MAXN+5]; priority_queue<int> Q; void topsort(){ for(int i=1;i<=n;i++) for(int j=0;j<G[i].size();j++) du[G[i][j]]++; for(int i=1;i<=n;i++) if(!du[i])Q.push(i); while(!Q.empty()){ int x=Q.top();Q.pop(); rnk[++ct]=x; for(int i=0;i<G[x].size();i++){ int v=G[x][i]; if(!(--du[v]))Q.push(v); } } } int main(){ n=read(),K=read(); for(int i=1;i<=n;i++){ int x=read();a[x]=i; } reverse(a+1,a+n+1); T.Build(1,1,n); for(int i=n;i>=1;i--){ int p=T.Query(1,1,n,max(1,a[i]-K+1),a[i]); if(p!=INF)G[a[i]].push_back(a[p]); p=T.Query(1,1,n,a[i],min(n,a[i]+K-1)); if(p!=INF)G[a[i]].push_back(a[p]); T.Insert(1,1,n,a[i],i); } topsort(); reverse(rnk+1,rnk+n+1); for(int i=1;i<=n;i++)ans[rnk[i]]=i; for(int i=1;i<=n;i++)printf("%d\n",ans[i]); }
codenet
-1
1,940
s124080921
p04052
1,567,201,349
cpp
Accepted
432
26,224
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #define N 500005 using namespace std; struct Segment_Tree{ #define ls pos << 1 #define rs pos << 1 | 1 int val[N << 3]; void build(int pos,int l,int r){ if(l == r){ val[pos] = N; return ; } int mid = (l + r) >> 1; build(ls, l, mid); build(rs, mid + 1, r); val[pos] = min(val[ls], val[rs]); return ; } void update(int pos,int l,int r,int p, int w){ if(l == r){ val[pos] = w; return ; } int mid = (l + r) >> 1; if(mid >= p) update(ls, l, mid, p, w); else update(rs, mid + 1, r, p, w); val[pos] = min(val[ls], val[rs]); } int query(int pos,int l,int r,int nl,int nr){ if(l >= nl && r <= nr) return val[pos]; int mid = (l + r) >> 1, tmp = N; if(mid >= nl) tmp = min(tmp, query(ls, l, mid, nl, nr)); if(mid < nr) tmp = min(tmp, query(rs, mid + 1, r, nl, nr)); return tmp; } #undef ls #undef rs }T; struct edge{ int to, nxt; }e[N << 1]; int n, k, cnt, tot; int p[N], fir[N], ins[N], a[N], q[N]; void work(); void add(int ,int); void tuopu(); int main(){ scanf("%d%d", &n, &k); for(int i = 1; i <= n; ++i) scanf("%d", q + i), p[q[i]] = i; T.build(1, 1, n); for(int i = n; i; --i){ int x; x = T.query(1, 1, n, max(1, p[i] - k + 1), p[i]); if(x != N) add(p[i], p[x]); x = T.query(1, 1, n, p[i], min(n, p[i] + k - 1)); if(x != N) add(p[i], p[x]); T.update(1, 1, n, p[i], i); } tuopu(); for(int i = 1; i <= tot; ++i) p[a[i]] = i; for(int i = 1; i <= n; ++i) printf("%d\n", p[i]); return 0; } void add(int u,int v){ e[++cnt] = (edge){v, fir[u]}; fir[u] = cnt; ++ins[v]; return ; } void tuopu(){ priority_queue < int , vector < int > , greater < int > > Q; for(int i = 1; i <= n; ++i) if(!ins[i]) Q.push(i); while(!Q.empty()){ int u = Q.top(); Q.pop(); a[++tot] = u; for(int i = fir[u]; i; i = e[i].nxt){ int v = e[i].to; --ins[v]; if(!ins[v]) Q.push(v); } } return ; }
codenet
-1
1,940
s058431199
p04052
1,528,498,802
cpp
Accepted
639
43,392
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_map> #include <functional> #include <cstring> #include <cmath> #include <ctime> #include <cstdlib> using namespace std; typedef long long llong; typedef long double ld; typedef pair<int, int> pii; typedef pair<llong, llong> pll; const int MAXN = 5e5 + 1; int n, k; int arr[MAXN]; int rev[MAXN]; int ord[MAXN]; int ans[MAXN]; vector<int> edge[MAXN]; int seg[1 << 20]; void update(int i, int s, int e, int x, int v) { seg[i] = min(seg[i], v); if (s == e) return; int m = (s + e) / 2; if (x <= m) update(i << 1, s, m, x, v); else update(i << 1 | 1, m + 1, e, x, v); } int query(int i, int s, int e, int x, int y) { if (e < x || y < s) return n + 1; if (x <= s && e <= y) return seg[i]; int m = (s + e) / 2; return min(query(i << 1, s, m, x, y), query(i << 1 | 1, m + 1, e, x, y)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 1; i < (1 << 20); ++i) seg[i] = n + 1; for (int i = 1; i <= n; ++i) { cin >> arr[i]; rev[arr[i]] = i; } for (int i = n; --i; ) { update(1, 1, n, rev[i + 1], i + 1); int ret; if ((ret = query(1, 1, n, rev[i] + 1, rev[i] + k - 1)) <= n) { edge[rev[ret]].push_back(rev[i]); ++ord[rev[i]]; } if ((ret = query(1, 1, n, rev[i] - k + 1, rev[i] - 1)) <= n) { edge[rev[ret]].push_back(rev[i]); ++ord[rev[i]]; } } priority_queue<int> pq; for (int i = 1; i <= n; ++i) { if (ord[i] == 0) pq.push(i); } for (int i = n; i; --i) { int x = pq.top(); pq.pop(); ans[x] = i; for (int j : edge[x]) { if (--ord[j] == 0) pq.push(j); } } for (int i = 1; i <= n; ++i) { printf("%d\n", ans[i]); } return 0; }
codenet
-1
1,940
s500650593
p04052
1,562,602,840
cpp
Accepted
592
46,464
#include <bits/stdc++.h> using namespace std; #define N 500000 #define pb push_back #define INF 0x3f3f3f3f #define mid ((l + r) >> 1) #define lson (o << 1) #define rson (o << 1 | 1) int n, k, p[N + 5], q[N + 5], r[N + 5], minv[4 * N + 5], in[N + 5], tot; vector<int> G[N + 5]; void pushup(int o) { minv[o] = min(minv[lson], minv[rson]); } void insert(int o, int l, int r, int x, int k) { if (l == r) { minv[o] = k; return ; } if (x <= mid) insert(lson, l, mid, x, k); else insert(rson, mid + 1, r, x, k); pushup(o); } int query(int o, int l, int r, int L, int R) { if (L > R) return INF; if (L <= l && r <= R) return minv[o]; int ret = INF; if (L <= mid) ret = min(ret, query(lson, l, mid, L, R)); if (R > mid) ret = min(ret, query(rson, mid + 1, r, L, R)); return ret; } void add() { for (int i = n, p1, p2; i >= 1; --i) { p1 = query(1, 1, n, max(q[i] - k + 1, 1), q[i] - 1); p2 = query(1, 1, n, q[i] + 1, min(q[i] + k - 1, n)); if (p1 != INF) G[i].pb(p1), in[p1]++; if (p2 != INF) G[i].pb(p2), in[p2]++; insert(1, 1, n, q[i], i); } } struct Cmp { bool operator () (const int &a, const int &b) const { return q[a] > q[b]; } }; void topo() { priority_queue<int, vector<int>, Cmp> pq; for (int i = 1; i <= n; ++i) if (!in[i]) pq.push(i); while (!pq.empty()) { int u = pq.top(); pq.pop(); r[++tot] = q[u]; for (auto v : G[u]) { in[v]--; if (!in[v]) pq.push(v); } } for (int i = 1; i <= n; ++i) p[r[i]] = i; } int main() { scanf("%d%d", &n, &k); memset(minv, 0x3f, sizeof minv); for (int i = 1; i <= n; ++i) { scanf("%d", &p[i]); q[p[i]] = i; } add(); topo(); for (int i = 1; i <= n; ++i) printf("%d\n", p[i]); return 0; }
codenet
-1
1,941
s138247023
p04052
1,506,630,095
cpp
Accepted
386
25,472
#include <bits/stdc++.h> using namespace std; const int maxn=500005; int p[maxn]; int w[maxn<<2]; int x[maxn]; void init(int id,int l,int r) { if(l==r){ w[id]=p[l]; return ; } int mid=(l+r)>>1; init(id<<1,l,mid); init(id<<1|1,mid+1,r); w[id]=min(w[id<<1],w[id<<1|1]); } int Qw(int id,int l,int r,int tl,int tr) { if(l==tl&&r==tr) return w[id]; int mid=(l+r)>>1; if(tr<=mid) return Qw(id<<1,l,mid,tl,tr); else if(tl>mid) return Qw(id<<1|1,mid+1,r,tl,tr); else return min(Qw(id<<1,l,mid,tl,mid),Qw(id<<1|1,mid+1,r,mid+1,tr)); } void up(int id,int l,int r,int v) { if(l==r){ w[id]=1e8; return; } int mid=(l+r)>>1; if(v<=mid) up(id<<1,l,mid,v); else up(id<<1|1,mid+1,r,v); w[id]=min(w[id<<1],w[id<<1|1]); } int q[maxn],top; int sum[maxn<<2]; bool vis[maxn]; int ans[maxn]; void Debug(int id,int l,int r) { if(l==r){ printf("%d ",w[id]); return ; } int mid=(l+r)>>1; Debug(id<<1,l,mid); Debug(id<<1|1,mid+1,r); } int main() { int n,k; scanf("%d%d",&n,&k);k--; for(int i=1;i<=n;i++) { scanf("%d",&x[i]); p[x[i]]=i; } init(1,1,n); top=0; memset(sum,0,sizeof(sum)); memset(vis,0,sizeof(vis)); int cnt=1; for(int i=1;i<=n;i++) { if(vis[i]) continue; q[top++]=i; while(top) { int i=q[top-1],tmp; up(1,1,n,x[i]); int t=Qw(1,1,n,1,x[i]); // printf("%d top %d %d %d\n",top,t,i,x[i]); // Debug(1,1,n); // printf("\n"); //scanf("%d",&tmp); if(t<=i+k){ q[top++]=t; continue; } ans[cnt++]=i; top--; vis[i]=1; } } for(int i=1;i<=n;i++) { p[ans[i]]=i; } for(int i=1;i<=n;i++) printf("%d\n",p[i]); return 0; }
codenet
-1
1,944
s138247023
p04052
1,506,630,095
cpp
Accepted
386
25,472
#include <bits/stdc++.h> using namespace std; const int maxn=500005; int p[maxn]; int w[maxn<<2]; int x[maxn]; void init(int id,int l,int r) { if(l==r){ w[id]=p[l]; return ; } int mid=(l+r)>>1; init(id<<1,l,mid); init(id<<1|1,mid+1,r); w[id]=min(w[id<<1],w[id<<1|1]); } int Qw(int id,int l,int r,int tl,int tr) { if(l==tl&&r==tr) return w[id]; int mid=(l+r)>>1; if(tr<=mid) return Qw(id<<1,l,mid,tl,tr); else if(tl>mid) return Qw(id<<1|1,mid+1,r,tl,tr); else return min(Qw(id<<1,l,mid,tl,mid),Qw(id<<1|1,mid+1,r,mid+1,tr)); } void up(int id,int l,int r,int v) { if(l==r){ w[id]=1e8; return; } int mid=(l+r)>>1; if(v<=mid) up(id<<1,l,mid,v); else up(id<<1|1,mid+1,r,v); w[id]=min(w[id<<1],w[id<<1|1]); } int q[maxn],top; int sum[maxn<<2]; bool vis[maxn]; int ans[maxn]; void Debug(int id,int l,int r) { if(l==r){ printf("%d ",w[id]); return ; } int mid=(l+r)>>1; Debug(id<<1,l,mid); Debug(id<<1|1,mid+1,r); } int main() { int n,k; scanf("%d%d",&n,&k);k--; for(int i=1;i<=n;i++) { scanf("%d",&x[i]); p[x[i]]=i; } init(1,1,n); top=0; memset(sum,0,sizeof(sum)); memset(vis,0,sizeof(vis)); int cnt=1; for(int i=1;i<=n;i++) { if(vis[i]) continue; q[top++]=i; while(top) { int i=q[top-1],tmp; up(1,1,n,x[i]); int t=Qw(1,1,n,1,x[i]); // printf("%d top %d %d %d\n",top,t,i,x[i]); // Debug(1,1,n); // printf("\n"); //scanf("%d",&tmp); if(t<=i+k){ q[top++]=t; continue; } ans[cnt++]=i; top--; vis[i]=1; } } for(int i=1;i<=n;i++) { p[ans[i]]=i; } for(int i=1;i<=n;i++) printf("%d\n",p[i]); return 0; }
codenet
-1
1,944
s318206732
p04052
1,491,259,317
cpp
Accepted
389
45,696
#include <cctype> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> const int N = 1 << 19, INF = 0x3f3f3f3f; int nextInt() { char ch; while (!isdigit(ch = getchar())) {} int res = ch - '0'; while (isdigit(ch = getchar())) res = 10 * res + ch - '0'; return res; } class Seg { int data[N << 1]; public: void clear() { memset(data, 0x3f, sizeof data); } void modify(int p, int v) { data[p += N] = v; while (p >>= 1) data[p] = std::min(data[p << 1], data[p << 1 | 1]); } int query(int l, int r) { int res = INF; for (l = l + N - 1, r = r + N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (!(l & 1)) res = std::min(res, data[l + 1]); if (r & 1) res = std::min(res, data[r - 1]); } return res; } } seg; int n, k, p[N]; std::vector<int> adj[N]; int deg[N]; inline void link(int a, int b) { adj[a].push_back(b); ++deg[b]; } int main() { scanf("%d%d", &n, &k); static int pos[N]; for (int i = 1; i <= n; ++i) pos[p[i] = nextInt()] = i; seg.clear(); for (int i = 1; i <= n; ++i) { if (i > k) seg.modify(p[i - k], INF); int j = seg.query(p[i], n); if (j < INF) link(i, pos[j]); seg.modify(p[i], p[i]); } seg.clear(); for (int i = n; i > 0; --i) { if (i + k <= n) seg.modify(p[i + k], INF); int j = seg.query(p[i], n); if (j < INF) link(i, pos[j]); seg.modify(p[i], p[i]); } static int heap[N]; int top = 0; for (int i = 1; i <= n; ++i) if (!deg[i]) heap[++top] = -i; std::make_heap(heap + 1, heap + top + 1); static int ans[N]; for (int cur = 0; top;) { std::pop_heap(heap + 1, heap + top + 1); int a = -heap[top--]; ans[a] = ++cur; for (int i = 0; i < adj[a].size(); ++i) { int b = adj[a][i]; if (--deg[b] == 0) { heap[++top] = -b; std::push_heap(heap + 1, heap + top + 1); } } } for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,952
s847646172
p04052
1,597,701,221
cpp
Accepted
654
15,856
#include<bits/stdc++.h> #define ll long long #define ld long double #define db double #define pint pair<int,ll> #define mk make_pair #define pb push_back #define eb emplace_back #define ins insert #define fi first #define se second #define Rep(x,y,z) for(int x=y;x<=z;x++) #define Red(x,y,z) for(int x=y;x>=z;x--) using namespace std; const int MAXN=5e5+5; char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny; inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;} //inline char gc(){return getchar();} inline int read(){ int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc; for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny; } int n,k,Mx[MAXN<<2],a[MAXN],ans[MAXN],vis[MAXN];priority_queue<int>q; #define ls x<<1 #define rs x<<1|1 inline int comp(int x,int y){return a[x]>a[y]?x:y;} inline void pushup(int x){Mx[x]=comp(Mx[ls],Mx[rs]);} void Build(int x,int l,int r){ if(l==r){Mx[x]=l;return;}int mid=l+r>>1; Build(ls,l,mid),Build(rs,mid+1,r),pushup(x); } void Del(int x,int l,int r,int p){ if(l==r){Mx[x]=0;return;}int mid=l+r>>1; p<=mid?Del(ls,l,mid,p):Del(rs,mid+1,r,p),pushup(x); } int Ask(int x,int l,int r,int ql,int qr){ if(ql>qr)return 0;if(ql<=l&&r<=qr)return Mx[x];int mid=l+r>>1; if(qr<=mid)return Ask(ls,l,mid,ql,qr);else if(ql>mid)return Ask(rs,mid+1,r,ql,qr); else return comp(Ask(ls,l,mid,ql,mid),Ask(rs,mid+1,r,mid+1,qr)); } inline bool check(int x){return x&&!vis[x]&&Ask(1,1,n,x-k+1,x+k-1)==x;} int main(){ // freopen("std.in","r",stdin); // freopen("std.out","w",stdout); n=read(),k=read(); Rep(i,1,n)a[i]=read();a[0]=-1e9,Build(1,1,n); Rep(i,1,n)if(check(i))q.push(i),vis[i]=1; Red(i,n,1){ if(q.empty())break; int x=q.top(),p;q.pop(),ans[x]=i,Del(1,1,n,x); if(x>1)if(check(p=Ask(1,1,n,x-k+1,x-1)))vis[p]=1,q.push(p); if(x<n)if(check(p=Ask(1,1,n,x+1,x+k-1)))vis[p]=1,q.push(p); }Rep(i,1,n)cout<<ans[i]<<'\n'; return 0; }
codenet
-1
1,962
s983834357
p04052
1,569,628,067
cpp
Accepted
1,213
21,616
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <iostream> #include <algorithm> #include <iomanip> #include <fstream> #include <vector> #include <bitset> #include <queue> #include <stack> #include <map> #include <set> using namespace std; const int MAXN = 500005; int n,k; int a[MAXN]; int ans[MAXN]; bool mark[MAXN]; pair<int,int> node[MAXN << 2]; priority_queue<int> Q; void buildtree(int o,int l,int r) { if (l == r) { node[o] = make_pair(a[l],l); return; } int mid = l + r >> 1; buildtree(o << 1,l,mid); buildtree(o << 1 | 1,mid + 1,r); node[o] = max(node[o << 1],node[o << 1 | 1]); } void modify(int o,int l,int r,int p) { if (l == r) { node[o] = make_pair(0,0); return; } int mid = l + r >> 1; if (mid >= p) modify(o << 1,l,mid,p); else modify(o << 1 | 1,mid + 1,r,p); node[o] = max(node[o << 1],node[o << 1 | 1]); } pair<int,int> query(int o,int l,int r,int x,int y) { if (l >= x && r <= y) return node[o]; int mid = l + r >> 1; if (mid < x) return query(o << 1 | 1,mid + 1,r,x,y); if (mid + 1 > y) return query(o << 1,l,mid,x,y); return max(query(o << 1,l,mid,x,y),query(o << 1 | 1,mid + 1,r,x,y)); } bool check(int pos) { return !mark[pos] && query(1,1,n,max(1,pos - k + 1),min(n,pos + k - 1)).first == a[pos]; } void update(int id) { pair<int,int> MX = query(1,1,n,(id - 1) * k + 1,min(id * k,n)); if (MX.second && check(MX.second)) { mark[MX.second] = 1; Q.push(MX.second); } } int main() { scanf("%d%d",&n,&k); for (int i = 1;i <= n;i++) scanf("%d",&a[i]); buildtree(1,1,n); for (int i = 1;(i - 1) * k + 1 <= n;i++) update(i); for (int i = n;i >= 1;i--) { int cur = Q.top(); Q.pop(); ans[cur] = i; a[cur] = 0; modify(1,1,n,cur); int bel = (cur - 1) / k + 1; if (bel > 1) update(bel - 1); update(bel); if (bel * k + 1 <= n) update(bel + 1); } for (int i = 1;i <= n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,968
s057077117
p04052
1,571,174,494
cpp
Accepted
508
27,008
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout) inline int read() { int data = 0, w = 1; char ch = getchar(); while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar(); return data * w; } const int N(500010), INF(0x3f3f3f3f); struct edge { int next, to; } e[N << 1]; int head[N], e_num, n, K, cnt, a[N], p[N], deg[N], min[N << 2]; inline void add_edge(int from, int to) { e[++e_num] = (edge) {head[from], to}; head[from] = e_num, ++deg[to]; } void Insert(int p, int v, int x = 1, int l = 1, int r = n) { if (l == r) return (void) (min[x] = v); int mid = (l + r) >> 1, ls = x << 1, rs = ls | 1; if (p <= mid) Insert(p, v, ls, l, mid); else Insert(p, v, rs, mid + 1, r); min[x] = std::min(min[ls], min[rs]); } int Query(int ql, int qr, int x = 1, int l = 1, int r = n) { if (ql > qr) return INF; if (ql <= l && r <= qr) return min[x]; int mid = (l + r) >> 1, ls = x << 1, rs = ls | 1, ans = INF; if (ql <= mid) ans = Query(ql, qr, ls, l, mid); if (mid < qr) ans = std::min(ans, Query(ql, qr, rs, mid + 1, r)); return ans; } void bfs() { std::priority_queue<int, std::vector<int>, std::greater<int> > Q; for (int i = 1; i <= n; i++) if (!deg[i]) Q.push(i); while (!Q.empty()) { int x = Q.top(); Q.pop(); p[x] = ++cnt; for (int i = head[x]; i; i = e[i].next) if (!--deg[e[i].to]) Q.push(e[i].to); } } int main() { n = read(), K = read(), memset(min, 0x3f, sizeof min); for (int i = 1; i <= n; i++) a[read()] = i; for (int i = n, t; i; i--) { t = Query(a[i] + 1, std::min(a[i] + K, n)); if (t != INF) add_edge(a[i], a[t]); t = Query(std::max(1, a[i] - K + 1), a[i] - 1); if (t != INF) add_edge(a[i], a[t]); Insert(a[i], i); } for (int i = (bfs(), 1); i <= n; i++) printf("%d\n", p[i]); return 0; }
codenet
-1
1,969
s057077117
p04052
1,571,174,494
cpp
Accepted
508
27,008
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout) inline int read() { int data = 0, w = 1; char ch = getchar(); while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar(); return data * w; } const int N(500010), INF(0x3f3f3f3f); struct edge { int next, to; } e[N << 1]; int head[N], e_num, n, K, cnt, a[N], p[N], deg[N], min[N << 2]; inline void add_edge(int from, int to) { e[++e_num] = (edge) {head[from], to}; head[from] = e_num, ++deg[to]; } void Insert(int p, int v, int x = 1, int l = 1, int r = n) { if (l == r) return (void) (min[x] = v); int mid = (l + r) >> 1, ls = x << 1, rs = ls | 1; if (p <= mid) Insert(p, v, ls, l, mid); else Insert(p, v, rs, mid + 1, r); min[x] = std::min(min[ls], min[rs]); } int Query(int ql, int qr, int x = 1, int l = 1, int r = n) { if (ql > qr) return INF; if (ql <= l && r <= qr) return min[x]; int mid = (l + r) >> 1, ls = x << 1, rs = ls | 1, ans = INF; if (ql <= mid) ans = Query(ql, qr, ls, l, mid); if (mid < qr) ans = std::min(ans, Query(ql, qr, rs, mid + 1, r)); return ans; } void bfs() { std::priority_queue<int, std::vector<int>, std::greater<int> > Q; for (int i = 1; i <= n; i++) if (!deg[i]) Q.push(i); while (!Q.empty()) { int x = Q.top(); Q.pop(); p[x] = ++cnt; for (int i = head[x]; i; i = e[i].next) if (!--deg[e[i].to]) Q.push(e[i].to); } } int main() { n = read(), K = read(), memset(min, 0x3f, sizeof min); for (int i = 1; i <= n; i++) a[read()] = i; for (int i = n, t; i; i--) { t = Query(a[i] + 1, std::min(a[i] + K, n)); if (t != INF) add_edge(a[i], a[t]); t = Query(std::max(1, a[i] - K + 1), a[i] - 1); if (t != INF) add_edge(a[i], a[t]); Insert(a[i], i); } for (int i = (bfs(), 1); i <= n; i++) printf("%d\n", p[i]); return 0; }
codenet
-1
1,969
s031195569
p04052
1,581,708,692
cpp
Accepted
574
52,864
#include <bits/stdc++.h> using namespace std; #define N 2000010 int p[N], a[N], b[N]; struct node { int l, r, mid, minn; } s[N << 2]; void build(int di, int l, int r) { s[di].l = l, s[di].r = r, s[di].mid = l + r >> 1, s[di].minn = 1e9; if (l == r) return; build(di << 1, l, s[di].mid), build(di << 1 | 1, s[di].mid + 1, r); } void ins(int di, int x, int v) { if (s[di].l == s[di].r) { s[di].minn = v; return; } ins(x <= s[di].mid ? di << 1 : di << 1 | 1, x, v); s[di].minn = min(s[di << 1].minn, s[di << 1 | 1].minn); } int query(int di, int l, int r) { if (l <= s[di].l && s[di].r <= r) return s[di].minn; if (r <= s[di].mid) return query(di << 1, l, r); if (l > s[di].mid) return query(di << 1 | 1, l, r); return min(query(di << 1, l, r), query(di << 1 | 1, l, r)); } int hd[N], nx[N << 1], e[N << 1], Num; void adde(int x, int y) { nx[++Num] = hd[x], hd[x] = Num, e[Num] = y; } priority_queue<int> q; int n, k, In[N]; int main() { scanf("%d%d", &n, &k); for (int i = 1, x; i <= n; i++) { scanf("%d", &x); a[x] = i; } for (int i = 1; i <= n / 2; i++) swap(a[i], a[n - i + 1]); build(1, 1, n); for (int i = n; i >= 1; i--) { int x = query(1, max(1, a[i] - k + 1), a[i]); if (x > 0 && x <= n) adde(a[i], a[x]); x = query(1, a[i], min(n, a[i] + k - 1)); if (x > 0 && x <= n) adde(a[i], a[x]); ins(1, a[i], i); } for (int i = 1; i <= n; i++) for (int j = hd[i]; j; j = nx[j]) In[e[j]]++; for (int i = 1; i <= n; i++) if (!In[i]) q.push(i); int num = 0; while (!q.empty()) { int u = q.top(); q.pop(); b[++num] = u; for (int i = hd[u]; i; i = nx[i]) if (!(--In[e[i]])) q.push(e[i]); } for (int i = 1; i <= n / 2; i++) swap(b[i], b[n - i + 1]); for (int i = 1; i <= n; i++) p[b[i]] = i; for (int i = 1; i <= n; i++) printf("%d\n", p[i]); }
codenet
-1
1,975
s291273899
p04052
1,468,776,808
cpp
Accepted
591
54,400
#define DEB #include<bits/stdc++.h> #define REP(i,m) for(int i=0;i<(m);++i) #define REPN(i,m,in) for(int i=(in);i<(m);++i) #define ALL(t) (t).begin(),(t).end() #define CLR(a) memset((a),0,sizeof(a)) #define pb push_back #define mp make_pair #define fr first #define sc second using namespace std; #ifdef DEB #define dump(x) cerr << #x << " = " << (x) << endl #define prl cerr<<"called:"<< __LINE__<<endl template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;} #else #define dump(x) ; #define prl ; template<class T> void debug(T a,T b){ ;} #endif template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; } template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; } typedef long long int lint; typedef pair<int,int> pi; namespace std{ template<class S,class T> ostream &operator <<(ostream& out,const pair<S,T>& a){ out<<'('<<a.fr<<','<<a.sc<<')'; return out; } } const int INF=5e8; struct segtree{ int val[1100000]; int n; void init(int n_){ n=1; while(n<n_) n<<=1; } void set(int k,int a){ k+=n-1; val[k]=a; while(k>0){ k=(k-1)/2; val[k]=min(val[k*2+1],val[k*2+2]); } } int query(int a,int b,int i,int l,int r,int K){ if(b<=l || r<+a || val[i]>=K) return INF; if(r-l==1) return l; int md=(l+r)>>1; int L=query(a,b,i*2+1,l,md,K); if(L==INF) return query(a,b,i*2+2,md,r,K); return L; } bool alive(int v){ return val[v+n-1]<INF; } }; segtree seg; int n,K; int ar[500005],br[500005]; int res[500005],cnt,ans[500005]; set<int> alive; void rec(int v){ seg.set(v,INF); while(1){ int v2=seg.query(v+1,n,0,0,seg.n,ar[v]); if(v2>=v+K) break; rec(v2); } res[cnt++]=v; } int main(){ cin>>n>>K; REP(i,n){ scanf("%d",&ar[i]),--ar[i]; br[ar[i]]=i; } seg.init(n); REP(i,n) seg.set(i,ar[i]); REP(i,n) if(seg.alive(i)){ rec(i); } REP(i,n) ans[res[i]]=i; REP(i,n) printf("%d\n",ans[i]+1); return 0; }
codenet
-1
1,982
s291273899
p04052
1,468,776,808
cpp
Accepted
591
54,400
#define DEB #include<bits/stdc++.h> #define REP(i,m) for(int i=0;i<(m);++i) #define REPN(i,m,in) for(int i=(in);i<(m);++i) #define ALL(t) (t).begin(),(t).end() #define CLR(a) memset((a),0,sizeof(a)) #define pb push_back #define mp make_pair #define fr first #define sc second using namespace std; #ifdef DEB #define dump(x) cerr << #x << " = " << (x) << endl #define prl cerr<<"called:"<< __LINE__<<endl template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;} #else #define dump(x) ; #define prl ; template<class T> void debug(T a,T b){ ;} #endif template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; } template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; } typedef long long int lint; typedef pair<int,int> pi; namespace std{ template<class S,class T> ostream &operator <<(ostream& out,const pair<S,T>& a){ out<<'('<<a.fr<<','<<a.sc<<')'; return out; } } const int INF=5e8; struct segtree{ int val[1100000]; int n; void init(int n_){ n=1; while(n<n_) n<<=1; } void set(int k,int a){ k+=n-1; val[k]=a; while(k>0){ k=(k-1)/2; val[k]=min(val[k*2+1],val[k*2+2]); } } int query(int a,int b,int i,int l,int r,int K){ if(b<=l || r<+a || val[i]>=K) return INF; if(r-l==1) return l; int md=(l+r)>>1; int L=query(a,b,i*2+1,l,md,K); if(L==INF) return query(a,b,i*2+2,md,r,K); return L; } bool alive(int v){ return val[v+n-1]<INF; } }; segtree seg; int n,K; int ar[500005],br[500005]; int res[500005],cnt,ans[500005]; set<int> alive; void rec(int v){ seg.set(v,INF); while(1){ int v2=seg.query(v+1,n,0,0,seg.n,ar[v]); if(v2>=v+K) break; rec(v2); } res[cnt++]=v; } int main(){ cin>>n>>K; REP(i,n){ scanf("%d",&ar[i]),--ar[i]; br[ar[i]]=i; } seg.init(n); REP(i,n) seg.set(i,ar[i]); REP(i,n) if(seg.alive(i)){ rec(i); } REP(i,n) ans[res[i]]=i; REP(i,n) printf("%d\n",ans[i]+1); return 0; }
codenet
-1
1,982
s211267833
p04052
1,561,543,569
cpp
Accepted
952
31,232
#include <bits/stdc++.h> using namespace std; #define sz(x) ((int) ((x).size())) typedef long long ll; typedef long double ld; mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); uniform_int_distribution<int> unii(0, 1 << 30); int randNum() { return unii(rng); } struct treap { struct node { int size,prio,val,data; node *l=0,*r=0; } *root=0; int get(node *n) { return n?n->size:0; } int getData(node *n) { return n?n->data:(1<<30); } void upd(node *n) { if (n) { n->size=1+get(n->l)+get(n->r); n->data = min(n->val, min(getData(n->l), getData(n->r))); } } void split(node *n, node *&l, node *&r, int key, int add=0) { if (!n) return void(l=r=0); int nkey=add+get(n->l)+1; if (nkey<key) split(n->r,n->r,r,key,nkey),l=n; else split(n->l,l,n->l,key,add),r=n; upd(n); } void splitData(node *n, node *&l, node *&r, int key) { if (!n) return void(l=r=0); int nkey = min(n->val, getData(n->r)); if (nkey<=key) splitData(n->r,n->r,r,key),l=n; else splitData(n->l,l,n->l,key),r=n; upd(n); } void merge(node *l, node *r, node *&n) { if (!l||!r) n=l?l:r; else if (l->prio>r->prio) merge(l->r,r,l->r),n=l; else merge(l,r->l,r->l),n=r; upd(n); } void insert(int key, int val) { node *n=new node(); n->prio=randNum(),n->val=val; node *l,*r; splitData(root,l,r,key); merge(l,n,l); merge(l,r,root); } int get(int pos) { node *l,*n,*r; split(root,l,r,pos); split(r,n,r,2); int res=n?n->val:0; merge(l,n,l); merge(l,r,root); return res; } } tr; int n, k, p[600000], pos[600000]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); cin >> n >> k; for (int i = 0; i < n; i++) cin >> p[i], p[i]--, pos[p[i]] = i; for (int i = 0; i < n; i++) tr.insert(pos[i] + k - 1, pos[i]); for (int i = 0; i < n; i++) pos[i] = tr.get(i + 1), p[pos[i]] = i; for (int i = 0; i < n; i++) cout << p[i] + 1 << "\n"; }
codenet
-1
1,989
s052772906
p04052
1,509,416,983
cpp
Accepted
399
29,684
#include <bits/stdc++.h> #define lc (pos<<1) #define rc (pos<<1|1) using namespace std; int n,k,a[500005],p[500005]; inline int read() {int x=0; char c=getchar(); while (c<'0'||c>'9') c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return x; } inline void write(int ans) {if (ans==0) {putchar('0');return;} int t[21],cnt=0; while (ans) {t[++cnt]=ans%10;ans/=10;} for (int i=cnt;i>=1;i--) putchar('0'+t[i]); } inline int min(int a,int b) {return a<b?a:b;} inline int max(int a,int b) {return a>b?a:b;} int minn[2000005]; void build(int pos,int l,int r) {minn[pos]=23333333; if (l==r) return; int mid=(l+r)>>1; build(lc,l,mid); build(rc,mid+1,r); } void modify(int pos,int l,int r,int p,int x) {minn[pos]=min(minn[pos],x); if (l==r) return; int mid=(l+r)>>1; if (p<=mid) {modify(lc,l,mid,p,x);} else {modify(rc,mid+1,r,p,x);} } int ask(int pos,int l,int r,int lt,int rt) {if (l==lt&&r==rt) return minn[pos]; int mid=(l+r)>>1; if (rt<=mid) return ask(lc,l,mid,lt,rt); if (lt>=mid+1) return ask(rc,mid+1,r,lt,rt); return min(ask(lc,l,mid,lt,mid),ask(rc,mid+1,r,mid+1,rt)); } int head[500005],nxt[1000005],v[1000005],d[500005],tot=0; int g[500005],ans[500005],cnt=0; inline void add(int a,int b) {tot++;nxt[tot]=head[a];head[a]=tot;v[tot]=b;d[b]++;} priority_queue <int,vector<int>,greater<int> > q; int main (){ int n1,n2,i,j; n=read();k=read(); for (i=1;i<=n;i++) {p[i]=read();a[p[i]]=i;} build(1,1,n); for (i=n;i>=1;i--) {n1=23333333;n2=23333333; if (max(1,a[i]-k+1)<=a[i]-1) n1=ask(1,1,n,max(1,a[i]-k+1),a[i]-1); if (a[i]+1<=min(a[i]+k-1,n)) n2=ask(1,1,n,a[i]+1,min(a[i]+k-1,n)); if (n1!=23333333) add(a[i],a[n1]); if (n2!=23333333) add(a[i],a[n2]); modify(1,1,n,a[i],i); } for (i=1;i<=n;i++) {if (!d[i]) {q.push(i);} } while (!q.empty()) {int t=q.top();q.pop(); g[++cnt]=t; for (i=head[t];i;i=nxt[i]) {d[v[i]]--; if (!d[v[i]]) {q.push(v[i]);} } } for (i=1;i<=n;i++) {ans[g[i]]=i;} for (i=1;i<=n;i++) {write(ans[i]); putchar('\n'); } return 0; }
codenet
-1
1,989