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
s456772272
p04052
1,558,744,698
cpp
Accepted
336
17,392
#include<bits/stdc++.h> using namespace std; const int N = 500005; int n, k, A[N], P[N], R[N], M[N], MX[N * 2]; priority_queue < int > Pq; inline void Set(int i, int val) { MX[i += n - 1] = val; for (; i; i >>= 1) MX[i >> 1] = max(MX[i], MX[i ^ 1]); } inline int Get(int l, int r) { int Mx = 0; l = max(l, 1); r = min(r, n + 1); l += n - 1; r += n - 1; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) Mx = max(Mx, MX[l ++]); if (r & 1) Mx = max(Mx, MX[-- r]); } return (Mx); } inline void Do(int block) { int i = block * k + 1; if (i < 0 || i > n || M[block]) return ; int Mx = Get(i, i + k); if (Mx && Get(R[Mx] - k + 1, R[Mx] + k) == Mx) M[block] = 1, Pq.push(R[Mx]); } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &P[i]), R[P[i]] = i, Set(i, P[i]); for (int i = 0; i <= (n - 1) / k; i++) Do(i); for (int i = n; i; i --) { int id = Pq.top(); Pq.pop(); A[id] = i; Set(id, 0); M[(id - 1) / k] = 0; Do((id - 1) / k); Do((id - 1) / k - 1); Do((id - 1) / k + 1); } for (int i = 1; i <= n; i++) printf("%d\n", A[i]); return 0; }
codenet
-1
1,269
s899168997
p04052
1,509,637,416
cpp
Accepted
498
66,048
#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int MaxN = 501234; int n, K, P[MaxN]; int seg[MaxN << 2]; vector<int> af[MaxN]; void modify(int p, int l, int r, int x, int v) { if(l == r) { seg[p] = v; } else { if(x <= (l + r >> 1)) modify(p << 1, l, l + r >> 1, x, v); else modify(p << 1 | 1, (l + r >> 1) + 1, r, x, v); seg[p] =max(seg[p << 1], seg[p << 1 | 1]); } } int query(int p, int l, int r, int a, int b) { if(r < l) return 0; if(r < a || l > b) return 0; if(a <= l && r <= b) return seg[p]; return max(query(p << 1, l, l + r >> 1, a, b), query(p << 1 | 1, (l + r >> 1) + 1, r, a, b)); } int pos[MaxN], ans[MaxN], an = -1; void getans(int x) { ans[++an] = x; sort(af[x].begin(), af[x].end()); for(int i = 0; i < af[x].size(); i++) getans(af[x][i]); } int main() { int i; scanf("%d %d", &n, &K); for(i = 1; i <= n; i++) scanf("%d", P + i); for(i = 1; i <= n; i++) pos[P[i]] = i; pos[n + 1] = n + 1; for(i = 1; i <= n; i++) { int prev = query(1, 1, n, pos[i] + 1, min(pos[i] + K - 1, n)); if(!prev) prev = n + 1; af[pos[prev]].push_back(pos[i]); modify(1, 1, n, pos[i], i); } getans(n + 1); for(i = 1; i <= n; i++) P[ans[i]] = i; for(i = 1; i <= n; i++) printf("%d\n", P[i]); return 0; }
codenet
-1
1,284
s568491015
p04052
1,545,968,815
cpp
Accepted
752
48,512
#include <bits/stdc++.h> using namespace std; const int N=500010; int n,k,a[N],p[N]; int T[N<<1]; int idx(int l,int r){return l+r|l!=r;} void update(int l,int r,int p,int num){ int x=idx(l,r),mid=(l+r)>>1; if(l==r){T[x]=num;return;} if(p<=mid)update(l,mid,p,num); else update(mid+1,r,p,num); T[x]=max(T[idx(l,mid)],T[idx(mid+1,r)]); } int query(int l,int r,int L,int R){ int x=idx(l,r),mid=(l+r)>>1,res=0; if(L<=l&&r<=R)return T[x]; if(L<=mid)res=max(res,query(l,mid,L,R)); if(mid<R)res=max(res,query(mid+1,r,L,R)); return res; } int d[N]; vector<int> v[N]; void topsort(){ set<int> s; for(int i=1;i<=n;i++)if(!d[i])s.insert(i); for(int i=1;i<=n;i++){ int x=p[i]=*s.begin(); s.erase(s.begin()); for(auto y:v[x])if(!(--d[y]))s.insert(y); } } int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); p[a[i]]=i; } for(int i=1;i<=n;i++){ int t=query(1,n,max(1,p[i]-k+1),p[i]); if(t)v[p[t]].push_back(p[i]),d[p[i]]++; t=query(1,n,p[i],min(n,p[i]+k-1)); if(t)v[p[t]].push_back(p[i]),d[p[i]]++; update(1,n,p[i],i); } topsort(); 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,288
s791296934
p04052
1,508,095,061
cpp
Accepted
1,008
62,208
#include <bits/stdc++.h> using namespace std; inline int readint() { char c;int tmp=0,x=1;c=getchar(); while(!isdigit(c)){if(c=='-') x=-1;c=getchar();} while(isdigit(c)){tmp=tmp*10+c-'0';c=getchar();} return tmp*x; } const int maxn=500000+5; int p[maxn],q[maxn],deg[maxn],ans[maxn],n,k; set<int > ss; typedef set<int >::iterator seto; vector<int > g[maxn]; void addedge(int from,int to) { g[from].push_back(to); deg[to]++; } priority_queue<int ,vector<int >,greater<int > > pq; int main() { n=readint();k=readint(); for(int i=1;i<=n;i++) q[p[i]=readint()]=i; seto ite; for(int i=1;i<=n;i++){ if(i-k>=1) ss.erase(p[i-k]); ite=(ss.insert(p[i])).first; if(++ite!=ss.end()) addedge(i,q[*ite]); } ss.clear(); for(int i=n;i>=1;i--){ if(i+k<=n) ss.erase(p[i+k]); ite=(ss.insert(p[i])).first; if(++ite!=ss.end()) addedge(i,q[*ite]); } for(int i=1;i<=n;i++) if(deg[i]==0) pq.push(i); int res=0; while(!pq.empty()){ int tp=pq.top();pq.pop(); ans[tp]=++res; for(int j=0;j<(int)g[tp].size();j++) { deg[g[tp][j]]--; if(deg[g[tp][j]]==0) pq.push(g[tp][j]); } } for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,288
s661218805
p04052
1,565,235,232
cpp
Accepted
977
56,576
#include<set> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 500500 #define ri register int using namespace std; int n,k; int a[N],p[N],ans[N],ans2[N],ru[N]; set<int> s; vector<int> to[N]; priority_queue<int> pq; void add_edge(int u,int v) { to[v].push_back(u); ru[u]++; } inline int read() { int ret=0,f=0; char ch=getchar(); while (ch<'0' || ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0' && ch<='9') ret*=10,ret+=(ch-'0'),ch=getchar(); return f?-ret:ret; } int main() { n=read(); k=read(); for (ri i=1;i<=n;i++) a[p[i]=read()]=i; for (ri i=1;i<=n+k-1;i++) { if (i<=n) s.insert(p[i]); if (i-k+1>=1) s.erase(p[i-k+1]); if (i+1<=n) { set<int> :: iterator cur=s.upper_bound(p[i+1]); if (cur!=s.end()) add_edge(i+1,a[*cur]); } if (i-k+1>=1) { set<int> :: iterator cur=s.upper_bound(p[i-k+1]); if (cur!=s.end()) add_edge(i-k+1,a[*cur]); } } for (ri i=1;i<=n;i++) if (!ru[i]) pq.push(i); int cc=0; while (!pq.empty()) { int x=pq.top(); pq.pop(); ans[++cc]=x; for (ri i=0;i<to[x].size();i++) { int y=to[x][i]; ru[y]--; if (!ru[y]) pq.push(y); } } for (ri i=1;i<=n;i++) ans2[ans[n-i+1]]=i; for (ri i=1;i<=n;i++) printf("%d\n",ans2[i]); return 0; }
codenet
-1
1,291
s635179964
p04052
1,518,719,221
cpp
Accepted
407
62,464
#include<stdio.h> #include<vector> #include<algorithm> #include<set> using namespace std; #define SIZE 524288 class segtree { public: int seg[SIZE * 2]; void init() { fill(seg, seg + SIZE * 2, -1); } void update(int a, int b) { a += SIZE; seg[a] = max(seg[a], b); for (;;) { a /= 2; if (a == 0)break; seg[a] = max(seg[a * 2], seg[a * 2 + 1]); } } int get(int beg, int end, int node, int lb, int ub) { if (ub < beg || end < lb)return -1; if (beg <= lb&&ub <= end)return seg[node]; return max(get(beg, end, node * 2, lb, (lb + ub) / 2), get(beg, end, node * 2 + 1, (lb + ub) / 2 + 1, ub)); } }; segtree tree; int d[505050]; vector<int>pat[505050]; int ans[505050]; int pt = 0; void dfs(int node) { sort(pat[node].begin(), pat[node].end()); for (int i = 0; i < pat[node].size(); i++)dfs(pat[node][i]); ans[node] = pt++; } int main() { int num, gen; scanf("%d%d", &num, &gen); gen--; for (int i = 0; i < num; i++) { int z; scanf("%d", &z); z--; d[z] = i; } tree.init(); for (int i = num - 1; i >= 0; i--) { int r = tree.get(d[i] - gen, d[i], 1, 0, SIZE - 1); if (r != -1)pat[d[num - 1 - r]].push_back(d[i]); else pat[num].push_back(d[i]); tree.update(d[i], num - 1 - i); } dfs(num); for (int i = 0; i < num; i++)printf("%d\n", ans[i] + 1); }
codenet
-1
1,305
s261605326
p04052
1,490,385,680
cpp
Accepted
1,193
53,504
#include<iostream> #include<vector> #include<algorithm> #include<cstring> #include<cstdio> #include<cmath> #include<cstdlib> #include<ctime> #include<queue> #include<set> using namespace std; const int N=2e6; int gi() { int w=0;bool q=1;char c=getchar(); while ((c<'0'||c>'9') && c!='-') c=getchar(); if (c=='-') q=0,c=getchar(); while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar(); return q? w:-w; } int p[N]; int head[N],nxt[N],to[N],d[N],tot,ans[N]; struct cmp{ inline bool operator () (const int &a,const int &b) const { return p[a]<p[b]; } }; struct cmp2{ inline bool operator () (const int &a,const int &b) const { return a>b; } }; inline void link(int a,int b) { d[to[++tot]=b]++,nxt[tot]=head[a],head[a]=tot; } int main() { int n=gi(),m=gi(),i,k,t; for (i=1;i<=n;i++) p[i]=gi(); set<int,cmp>s;set<int>::iterator it; priority_queue<int,vector<int>,cmp2>q; for (i=1;i<=n;i++) { if (i>m) s.erase(i-m); it=s.insert(i).first; if (++it!=s.end()) link(i,*it); } s.clear(); for (i=n;i;i--) { if (i+m<=n) s.erase(i+m); it=s.insert(i).first; if (++it!=s.end()) link(i,*it); } for (i=1;i<=n;i++) if (!d[i]) q.push(i); for (t=1;t<=n;t++) { k=q.top();q.pop(); ans[k]=t; for (i=head[k];i;i=nxt[i]) if (!--d[to[i]]) q.push(to[i]); } for (i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,315
s355382514
p04052
1,507,563,325
cpp
Accepted
1,366
61,824
#include<set> #include<queue> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=5e6+10; int a[maxn],b[maxn],c[maxn],deg[maxn],to[maxn],beg[maxn],nex[maxn],ans[maxn]; int e; set<int> st1,st2; priority_queue<int> q; inline int read(){ int x=0,flag=1; char ch=getchar(); while(!isdigit(ch) && ch!='-')ch=getchar(); if(ch=='-')flag=-1,ch=getchar(); while(isdigit(ch))x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); return x*flag; } inline void add(int x,int y){ to[++e]=y; nex[e]=beg[x]; beg[x]=e; } int main(){ int i,j,k,m,n; n=read();k=read(); for(i=1;i<=n;i++){ a[i]=read(); b[a[i]]=i; } for(i=1;i<k;i++)st1.insert(a[i]); for(i=1;i<=n;i++){ if(i+k-1<=n)st1.insert(a[i+k-1]); st1.erase(a[i-1]); st2.insert(a[i]); if(i>k)st2.erase(a[i-k]); set<int>::iterator cur=st1.upper_bound(a[i]); if(cur!=st1.end()){ add(b[*cur],i); deg[i]++; } cur=st2.upper_bound(a[i]); if(cur!=st2.end()){ add(b[*cur],i); deg[i]++; } } for(i=1;i<=n;i++) if(!deg[i])q.push(i); int cnt=0; while(!q.empty()){ int x=q.top(); q.pop(); ans[++cnt]=x; for(i=beg[x];i;i=nex[i]){ deg[to[i]]--; if(deg[to[i]]==0)q.push(to[i]); } } for(i=1;i<=n;i++)c[ans[i]]=n-i+1; for(i=1;i<=n;i++)printf("%d\n",c[i]); return 0; }
codenet
-1
1,319
s876317867
p04052
1,583,912,466
cpp
Accepted
1,094
50,432
// Do you knOW what it feels like? // To be TorTured by your own MinD? // I don't wanna feel the PAIN. // I BeG you to KILL me, pleASE... #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") //#pragma GCC optimize("Os") #define F first #define S second #define pb push_back #define SZ(x) (ll)(x.size()) #define all(x) x.begin(),x.end() #define mp make_pair typedef long long ll; typedef pair<ll,ll> pll; typedef long double ld; //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll maxn=5e5+10, lg=23, mod=1e9+7, inf=1e18; ll n,k; ll a[maxn],ans[maxn],tt=0; ll seg[maxn<<1]; void goz(ll i,ll x){ i--; seg[i+=n]=x; for(;i>1;i>>=1) seg[i>>1]=min(seg[i],seg[i^1]); } ll get(ll r,ll l=0){ ll mn=inf; for(l+=n,r+=n;l<r;l>>=1,r>>=1){ if(l&1) mn=min(mn,seg[l++]); if(r&1) mn=min(mn,seg[--r]); } return mn; } void sol(ll x){ while(1){ ll ag=get(a[x]-1); //cout<<x<<' '<<a[x]<<' '<<ag<<endl; if(ag>=x+k) break; sol(ag); } ans[x]=++tt; goz(a[x],inf); } int main(){ ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); memset(seg,69,sizeof(seg)); cin>>n>>k; for(int i=1;i<=n;i++) cin>>a[i], goz(a[i],i); for(int i=1;i<=n;i++){ if(!ans[i]) sol(i); cout<<ans[i]<<endl; } return 0; }
codenet
-1
1,323
s325716379
p04052
1,555,821,015
cpp
Accepted
488
25,716
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; priority_queue <int , vector <int >,greater <int > > Q; const int N=500005; #define mid ((l+r)>>1) int a[N],val[N<<2]; inline int query(int root,int l,int r,int L,int R){ if (r<L||l>R) return 0; if (L<=l&&r<=R) return val[root]; return max(query(root<<1,l,mid,L,R),query((root<<1)|1,mid+1,r,L,R)); } inline void update(int root,int l,int r,int x,int y){ if (l==r) {val[root]=y;return;} if (x<=mid) update(root<<1,l,mid,x,y); else update((root<<1)|1,mid+1,r,x,y); val[root]=max(val[root<<1],val[(root<<1)|1]); } int Head[N],Next[N<<1],Adj[N<<1],tot=0,d[N],ans[N]; inline void addedge(int u,int v){Next[++tot]=Head[u],Head[u]=tot,Adj[tot]=v,d[v]++;} int main (){ int n,k;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;i++){ int t=query(1,1,n,max(1,a[i]-k+1),a[i]); if (t) addedge(a[t],a[i]); t=query(1,1,n,a[i],min(n,a[i]+k-1)); if (t) addedge(a[t],a[i]); update(1,1,n,a[i],i); } for (int i=1;i<=n;i++) if (!d[i]) Q.push(i); int Time=0; while (!Q.empty()){ int x=Q.top();ans[x]=++Time;Q.pop(); for (int e=Head[x];e;e=Next[e]){ d[Adj[e]]--; if (!d[Adj[e]]) Q.push(Adj[e]); } } for (int i=1;i<=n;i++) printf ("%d\n",ans[i]); return 0; }
codenet
-1
1,329
s325716379
p04052
1,555,821,015
cpp
Accepted
488
25,716
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; priority_queue <int , vector <int >,greater <int > > Q; const int N=500005; #define mid ((l+r)>>1) int a[N],val[N<<2]; inline int query(int root,int l,int r,int L,int R){ if (r<L||l>R) return 0; if (L<=l&&r<=R) return val[root]; return max(query(root<<1,l,mid,L,R),query((root<<1)|1,mid+1,r,L,R)); } inline void update(int root,int l,int r,int x,int y){ if (l==r) {val[root]=y;return;} if (x<=mid) update(root<<1,l,mid,x,y); else update((root<<1)|1,mid+1,r,x,y); val[root]=max(val[root<<1],val[(root<<1)|1]); } int Head[N],Next[N<<1],Adj[N<<1],tot=0,d[N],ans[N]; inline void addedge(int u,int v){Next[++tot]=Head[u],Head[u]=tot,Adj[tot]=v,d[v]++;} int main (){ int n,k;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;i++){ int t=query(1,1,n,max(1,a[i]-k+1),a[i]); if (t) addedge(a[t],a[i]); t=query(1,1,n,a[i],min(n,a[i]+k-1)); if (t) addedge(a[t],a[i]); update(1,1,n,a[i],i); } for (int i=1;i<=n;i++) if (!d[i]) Q.push(i); int Time=0; while (!Q.empty()){ int x=Q.top();ans[x]=++Time;Q.pop(); for (int e=Head[x];e;e=Next[e]){ d[Adj[e]]--; if (!d[Adj[e]]) Q.push(Adj[e]); } } for (int i=1;i<=n;i++) printf ("%d\n",ans[i]); return 0; }
codenet
-1
1,329
s721199365
p04052
1,536,958,553
cpp
Accepted
532
24,820
#include<bits/stdc++.h> using namespace std; #define RI register int int read() { int q=0;char ch=' '; while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') q=q*10+ch-'0',ch=getchar(); return q; } const int N=500005; int n,K,tot,js; int tr[N<<2],a[N],h[N],ne[N<<1],to[N<<1],du[N],ans[N]; priority_queue<int,vector<int>,greater<int> >q; int query(int l,int r,int s,int t,int i) { if(l<=s&&t<=r) return tr[i]; int mid=(s+t)>>1,re=0; if(l<=mid) re=query(l,r,s,mid,i<<1); if(mid+1<=r) re=max(re,query(l,r,mid+1,t,(i<<1)|1)); return re; } void chan(int x,int s,int t,int i,int num) { if(s==t) {tr[i]=num;return;} int mid=(s+t)>>1; if(x<=mid) chan(x,s,mid,i<<1,num); else chan(x,mid+1,t,(i<<1)|1,num); tr[i]=max(tr[i<<1],tr[(i<<1)|1]); } void add(int x,int y) {to[++tot]=y,ne[tot]=h[x],h[x]=tot,++du[y];} void topsort() { for(RI i=1;i<=n;++i) if(!du[i]) q.push(i); while(!q.empty()) { int x=q.top();q.pop(),ans[x]=++js; for(RI i=h[x];i;i=ne[i]) { --du[to[i]]; if(!du[to[i]]) q.push(to[i]); } } } int main() { n=read(),K=read(); for(RI i=1;i<=n;++i) a[read()]=i; for(RI i=1;i<=n;++i) { int k=query(max(1,a[i]-K+1),a[i],1,n,1); if(k) add(a[k],a[i]); k=query(a[i],min(n,a[i]+K-1),1,n,1); if(k) add(a[k],a[i]); chan(a[i],1,n,1,i); } topsort(); for(RI i=1;i<=n;++i) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,341
s360367078
p04052
1,576,095,275
cpp
Accepted
920
17,264
#include<bits/stdc++.h> using namespace std; const int mn = 5e5+10; int p[mn],q[mn],val[mn<<2],ans[mn]; #define ls idx<<1 #define rs idx<<1|1 void build(int idx,int l,int r){ if(l==r){ val[idx]=p[l]; return; } int mid=(l+r)>>1; build(ls,l,mid);build(rs,mid+1,r); val[idx]=max(val[ls],val[rs]); } void update(int idx,int l,int r,int p,int v){ if(l==r){ val[idx]=v; return; } int mid = (l+r)>>1; if(p<=mid) update(ls,l,mid,p,v); else update(rs,mid+1,r,p,v); val[idx]=max(val[ls],val[rs]); } int query(int idx,int l,int r,int L,int R){ if(L<=l&&r<=R) return val[idx]; if(R<l||r<L) return -1; int mid=(l+r)>>1; return max(query(ls,l,mid,L,R),query(rs,mid+1,r,L,R)); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,k; cin>>n>>k; for(int i=1;i<=n;i++){ cin>>p[i]; q[p[i]]=i; } build(1,1,n); priority_queue<int> pq; for(int i=1;i<=n;i++) if(query(1,1,n,max(1,i-k+1),min(n,i+k-1))==p[i]) pq.emplace(i); for(int i=n;i>=1;i--){ int x=pq.top();pq.pop(); ans[x]=i; update(1,1,n,x,-1); int nr=query(1,1,n,x,min(n,x+k-1)),nl=query(1,1,n,max(1,x-k+1),x); if(nr!=-1&&query(1,1,n,max(1,q[nr]-k+1),min(q[nr]+k-1,n))==nr) pq.emplace(q[nr]); if(nl!=-1&&query(1,1,n,max(1,q[nl]-k+1),min(q[nl]+k-1,n))==nl) pq.emplace(q[nl]); } for(int i=1;i<=n;i++) cout<<ans[i]<<'\n'; }
codenet
-1
1,344
s458176618
p04052
1,578,888,565
cpp
Accepted
560
38,528
#include<bits/stdc++.h> using namespace std; #define N 2000010 int mn[N],d[N],h[N],v[N*2],nxt[N*2],ec,ans[N],ct,n,k,a[N],p[N]; void add(int x,int y){v[++ec]=y;nxt[ec]=h[x];h[x]=ec;} void bd(int o,int l,int r){ mn[o]=1e9; if(l==r)return; int md=(l+r)/2; bd(o*2,l,md); bd(o*2+1,md+1,r); } void mod(int o,int l,int r,int x,int y){ if(l==r){ mn[o]=y; return; } int md=(l+r)/2; if(x<=md)mod(o*2,l,md,x,y); else mod(o*2+1,md+1,r,x,y); mn[o]=min(mn[o*2],mn[o*2+1]); } int q(int o,int l,int r,int x,int y){ if(r<x||y<l)return 1e9; if(x<=l&&r<=y)return mn[o]; int md=(l+r)/2; return min(q(o*2,l,md,x,y),q(o*2+1,md+1,r,x,y)); } priority_queue<int>qu; void tp(){ for(int i=1;i<=n;i++) for(int j=h[i];j;j=nxt[j]) d[v[j]]++; for(int i=1;i<=n;i++) if(!d[i])qu.push(i); while(!qu.empty()){ int x=qu.top(); qu.pop(); ans[++ct]=x; for(int i=h[x];i;i=nxt[i]){ d[v[i]]--; if(!d[v[i]])qu.push(v[i]); } } } int main(){ cin>>n>>k; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); a[x]=i; } reverse(a+1,a+n+1); bd(1,1,n); for(int i=n;i;i--){ int x=q(1,1,n,max(1,a[i]-k+1),a[i]); if(x>0&&x<=n)add(a[i],a[x]); x=q(1,1,n,a[i],min(n,a[i]+k-1)); if(x>0&&x<=n)add(a[i],a[x]); mod(1,1,n,a[i],i); } tp(); reverse(ans+1,ans+n+1); for(int i=1;i<=n;i++)p[ans[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",p[i]); }
codenet
-1
1,349
s038403045
p04052
1,507,750,835
cpp
Accepted
909
48,384
#include<bits/stdc++.h> using namespace std; int read() { char s; int k=0,base=1; while((s=getchar())!='-'&&s!=EOF&&!(s>='0'&&s<='9')); if(s==EOF)exit(0); if(s=='-')base=-1,s=getchar(); while(s>='0'&&s<='9') { k=k*10+(s-'0'); s=getchar(); } return k*base; } void write(int x) { if(x<0) { putchar('-'); write(-x); } else { if(x/10)write(x/10); putchar(x%10+'0'); } } const int maxn=500010; const int maxm=1010010; int n,k,T; int rd[maxn]; int p[maxn],q[maxn]; int ans[maxn]; int id,to[maxm<<1],ne[maxm<<1],po[maxn]; inline void add(int x,int y) { id++; to[id]=y;ne[id]=po[x]; po[x]=id; rd[y]++; } set<int> s; set<int>::iterator it; priority_queue<int,vector<int>,greater<int> > Q; int main() { n=read();k=read(); for (int i=1;i<=n;i++) q[p[i]=read()]=i; for (int i=1;i<=n;i++) { if (i>k) s.erase(p[i-k]); it=s.insert(p[i]).first; if (++it!=s.end()) add(i,q[*it]); } for (int i=n;i>=1;i--) { if (i+k<=n) s.erase(p[i+k]); it=s.insert(p[i]).first; if (++it!=s.end()) add(i,q[*it]); } for (int i=1;i<=n;i++) { if (rd[i]==0) Q.push(i); } for (int i=1;i<=n;i++) { T=Q.top();Q.pop(); ans[T]=i; for (int j=po[T];j;j=ne[j]) { rd[to[j]]--; if (rd[to[j]]==0) Q.push(to[j]); } } // printf("-----------------------\n"); for (int i=1;i<=n;i++) { printf("%d\n",ans[i]); } return 0; }
codenet
-1
1,349
s450637970
p04052
1,567,893,239
cpp
Accepted
369
24,876
#include<bits/stdc++.h> using namespace std; #define pb push_back #define all(v) (v).begin(), (v).end() int n, k; int arr[500010]; int v[500010]; int ans[500010]; int f(int s, int e) { if(s >= e) return 0; vector<int> v1, v2, ret; int p = s+e>>1; //int p = rand() % (e-s+1) + s; int mx = v[p]; for(int i=p-1; i>=s; i--) { if(v[i] >= mx+k) { v2.pb(v[i]); } else { v1.pb(v[i]); mx = max(mx, v[i]); } } reverse(all(v1)); reverse(all(v2)); int mn = v[p]; for(int i=p+1; i<=e; i++) { if(v[i] <= mn - k) { v1.pb(v[i]); } else { v2.pb(v[i]); mn = min(mn, v[i]); } } for(int i=0; i<v1.size(); i++) { ret.pb(v1[i]); } ret.pb(v[p]); for(int i=0; i<v2.size(); i++) { ret.pb(v2[i]); } for(int i=s; i<=e; i++) { v[i] = ret[i-s]; } f(s, s+v1.size()-1); f(e - v2.size() + 1, e); } int main() { ios::sync_with_stdio(false); cin.tie(0); srand(time(NULL)); cin >> n >> k; for(int i=0; i<n; i++) { cin >> arr[i]; v[arr[i]] = i; } f(1, n); for(int i=1; i<=n; i++) { ans[v[i]] = i; } for(int i=0; i<n; i++) { cout << ans[i] << "\n"; } }
codenet
-1
1,352
s915865228
p04052
1,507,561,787
cpp
Accepted
368
67,968
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<vector> using namespace std; const int maxn=500010; vector<int>g[maxn]; int n,k; void read(int& x){ x=0; char c=getchar(); while(c<'0' || c>'9') c=getchar(); while(c>='0' && c<='9'){ x=x*10+c-'0'; c=getchar(); } } int p[maxn],P[maxn]; int ans[maxn]; struct Segment_Tree{ int Max[maxn<<2]; void modify(int o,int l,int r,int c,int pos){ if(l==r){ Max[o]=pos;return; } int mid=(l+r)>>1; if(c<=mid) modify(o<<1,l,mid,c,pos); else modify(o<<1|1,mid+1,r,c,pos); Max[o]=max(Max[o<<1],Max[o<<1|1]); } int query(int o,int l,int r,int ql,int qr){ if(ql<=l && qr>=r) return Max[o]; int mid=(l+r)>>1; int ret=0; if(ql<=mid) ret=query(o<<1,l,mid,ql,qr); if(qr>mid) ret=max(ret,query(o<<1|1,mid+1,r,ql,qr)); return ret; } }T; int cnt=-1; void dfs(int x){ ans[++cnt]=x; sort(g[x].begin(),g[x].end()); for(int i=0;i<g[x].size();i++) dfs(g[x][i]); } int main(){ read(n);read(k); for(int i=1;i<=n;i++){ read(p[i]); P[p[i]]=i; } P[n+1]=n+1; T.modify(1,1,n,P[1],1); for(int i=1;i<=n;i++){ int j=T.query(1,1,n,P[i]+1,min(n,P[i]+k-1)); if(!j) j=n+1; g[P[j]].push_back(P[i]); //printf("%d %d\n",P[j],P[i]); T.modify(1,1,n,P[i],i); } dfs(n+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,358
s123476090
p04052
1,507,669,101
cpp
Accepted
368
59,392
#include<bits/stdc++.h> const int N=500010; using namespace std; int head[N],tot=0,a[N],n,pos[N],ans[N],k,cnt=-1; vector<int>G[N]; int maxv[N<<2]; #define lson (o<<1) #define rson (o<<1|1) inline void pushup(int o){maxv[o]=max(maxv[lson],maxv[rson]);} inline void change(int o,int l,int r,int q,int v){ if(l==r)return void(maxv[o]=v); int mid=(l+r)>>1; if(q<=mid)change(lson,l,mid,q,v); if(q>mid)change(rson,mid+1,r,q,v); pushup(o); } inline int query(int o,int l,int r,int ql,int qr){ if(ql<=l&&r<=qr)return maxv[o]; int mid=(l+r)>>1,ans=0; if(ql<=mid)ans=max(ans,query(lson,l,mid,ql,qr)); if(qr>mid)ans=max(ans,query(rson,mid+1,r,ql,qr)); return ans; } inline int read(){ int f=1,x=0;char ch; do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9'); do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9'); return f*x; } inline void addedge(int u,int v){G[u].push_back(v);} inline void dfs(int u){ ans[++cnt]=u; sort(G[u].begin(),G[u].end()); for(int i=0;i<G[u].size();i++){ int v=G[u][i]; dfs(v); } } int main(){ n=read();k=read(); for(int i=1;i<=n;i++)a[i]=read(),pos[a[i]]=i; pos[n+1]=n+1; for(int i=1;i<=n;i++){ int pre=query(1,1,n,pos[i]+1,pos[i]+k-1); if(!pre)pre=n+1; addedge(pos[pre],pos[i]); change(1,1,n,pos[i],i); } dfs(n+1); for(int i=1;i<=n;i++)a[ans[i]]=i; for(int i=1;i<=n;i++)printf("%d\n",a[i]); return 0; }
codenet
-1
1,363
s084317390
p04052
1,540,569,726
cpp
Accepted
414
25,464
#include<cstdio> #include<queue> #define maxn 500005 using namespace std; int max(int a,int b){ return a>b?a:b; } int n,k,a[maxn],t[maxn<<2],head[maxn],cnt=0,d[maxn],ans[maxn]; struct edge{ int nxt,to; }e[maxn<<1]; priority_queue<int> q; void change(int p,int l,int r,int L,int R){ if(l==r){ t[p]=max(t[p],R); return; } int mid=(l+r)>>1; if(L<=mid) change(p<<1,l,mid,L,R); else change(p<<1|1,mid+1,r,L,R); t[p]=max(t[p<<1],t[p<<1|1]); } int Find(int p,int l,int r,int L,int R){ if(L<=l&&r<=R){ return t[p]; } int mid=(l+r)>>1; if(R<=mid) return Find(p<<1,l,mid,L,R); else if(L>mid) return Find(p<<1|1,mid+1,r,L,R); else return max(Find(p<<1,l,mid,L,mid),Find(p<<1|1,mid+1,r,mid+1,R)); } void add(int u,int v){ e[++cnt].nxt=head[u]; e[cnt].to=v; head[u]=cnt; d[v]++; } int main(){ scanf("%d%d",&n,&k); k--; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); a[x]=i; } for(int i=1;i<=n;i++){ int x=Find(1,1,n,a[i],a[i]+k); if(x>=1&&x<=n) add(a[x],a[i]); x=Find(1,1,n,a[i]-k,a[i]); if(x>=1&&x<=n) add(a[x],a[i]); change(1,1,n,a[i],i); } for(int i=1;i<=n;i++){ if(!d[i]) q.push(-i); } cnt=0; while(q.size()){ int u=-q.top(); q.pop(); for(int i=head[u];i;i=e[i].nxt){ int v=e[i].to; if(!(--d[v])){ q.push(-v); } } ans[u]=++cnt; } for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,375
s577416628
p04052
1,571,910,535
cpp
Accepted
649
40,704
#include <bits/stdc++.h> using namespace std; const int maxn = 500500; using ii = pair<int,int>; ii tr[maxn + maxn]; int n; void upd(int p, int v){ for(tr[p += n] = {v, p}; p > 1; p >>= 1) tr[p>>1] = max(tr[p], tr[p^1]); } ii qry(int l, int r){ ii mx = {-1, -1}; for(l += n, r += n; l < r; l >>= 1, r >>= 1){ if(l&1) mx = max(mx, tr[l++]); if(r&1) mx = max(mx, tr[--r]); } return mx; } int ans[maxn], val[maxn], cval[maxn];; int main(){ int k; cin >> n >> k; for(int i = n + n; i >= 1; i--) tr[i] = {-1, -1}; for(int i = 0; i < n; i++){ int x; cin >> x; x--; val[i] = cval[i] = x; upd(i, x); } set<int> do_it; auto is_max = [&](int p){ int l = max(p - k + 1, 0); int r = min(p + k, n); if(p == qry(l, r).second && val[p] >= 0){ return true; } return false; }; auto push_block = [&](int pw){ if(pw < 0) return; if(pw * k >= n) return; int l = pw * k; int r = min(l + k, n); int p = qry(l, r).second; if(is_max(p)){ do_it.insert(p); } }; for(int i = 0; i < n; i += k){ push_block(i / k); } for(int i = n; i >= 1; i--){ if(do_it.empty()) break; int pick = *prev(do_it.end()); do_it.erase(prev(do_it.end())); upd(pick, -1); val[pick] = -1; int blk = pick / k; ans[pick + 1] = i; push_block(blk - 1); push_block(blk); push_block(blk + 1); } for(int i = 1; i <= n; i++) cout << ans[i] << "\n"; return 0; }
codenet
-1
1,382
s850382623
p04052
1,491,586,449
cpp
Accepted
1,527
62,720
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define all(c) c.begin(),c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) using namespace std; template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";} template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;} const int MN=500000; int N,K; int a[MN],b[MN]; vector<int> G[MN]; int deg[MN]; int ans[MN]; void add_edge(int a,int b){ // printf("%d->%d\n",a,b); G[a].pb(b); deg[b]++; } int main(){ cin>>N>>K; rep(i,N){ cin>>a[i]; a[i]--; b[a[i]]=i; } typedef pair<int,int> P; set<P> st; rep(i,N){ P p(a[i],i); st.insert(p); if(i>=K){ st.erase(P(a[i-K],i-K)); } auto it = st.lower_bound(p); if(it!=st.begin()){ it--; int l = it->sc; add_edge(i,l); it++; } it++; if(it!=st.end()){ int r = it->sc; add_edge(r,i); } } priority_queue<int> que; rep(i,N) if(deg[i]==0) que.push(i); rep(t,N){ int v = que.top();que.pop(); ans[v]=N-1-t; for(int u:G[v]){ deg[u]--; if(deg[u]==0) que.push(u); } } rep(i,N) cout<<ans[i]+1<<endl; }
codenet
-1
1,382
s586747268
p04052
1,534,456,887
cpp
Accepted
419
38,652
#include<bits/stdc++.h> #define inf 1000000000 #define N 2000005 using namespace std; int n,K,kk,cnt,a[N],p[N],d[N],head[N],val[N],ru[N]; struct Edge{int nxt,to;}e[N]; inline void link(int x,int y){ru[y]++;e[++kk].nxt=head[x];e[kk].to=y;head[x]=kk;} void insert(int k,int l,int r,int x,int y){ if (l==r){d[k]=y;return;} int mid=(l+r)>>1; if (x<=mid) insert(k*2,l,mid,x,y); else insert(k*2+1,mid+1,r,x,y); d[k]=max(d[k*2],d[k*2+1]); } int query1(int k,int l,int r,int x,int y){ if (x>y) return 0; if (x<=l&&y>=r) return d[k]; int mid=(l+r)>>1; if (y<=mid) return query1(k*2,l,mid,x,y); else if (x>mid) return query1(k*2+1,mid+1,r,x,y); else return max(query1(k*2,l,mid,x,mid),query1(k*2+1,mid+1,r,mid+1,y)); } priority_queue<int> Q; inline void topsort(){ for (int i=1;i<=n;i++) if (!ru[i]) Q.push(-i); while (!Q.empty()){ int u=-Q.top();Q.pop(); cnt++;val[u]=cnt; for (int i=head[u];i;i=e[i].nxt){ int v=e[i].to;ru[v]--; if (!ru[v]) Q.push(-v); } } } 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 pos1=query1(1,1,n,min(n,p[i]+1),min(n,p[i]+K-1)); if (pos1) link(p[pos1],p[i]); pos1=query1(1,1,n,max(1,p[i]-K+1),max(1,p[i]-1)); if (pos1) link(p[pos1],p[i]); insert(1,1,n,p[i],i); } topsort(); for (int i=1;i<=n;i++) printf("%d\n",val[i]); return 0; }
codenet
-1
1,386
s727520046
p04052
1,591,740,574
cpp
Accepted
1,025
15,728
#include<bits/stdc++.h> #define re register #define inc(i,j,k) for(re int i=j;i<=k;i++) #define dec(i,j,k) for(re int i=j;i>=k;i--) #define lc o<<1 #define rc o<<1|1 const int N=5e5+5; using namespace std; inline int read(){ int x=0; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x; } int n,k,p[N],maxx[N<<2],ans[N]; int gmax(int a,int b){return p[a]>p[b]?a:b;} priority_queue<int> q; bool in[N]; void build(int o,int l,int r){ if(l==r) return maxx[o]=l,void(); int mid=(l+r)>>1; build(lc,l,mid),build(rc,mid+1,r); maxx[o]=gmax(maxx[lc],maxx[rc]); } void del(int o,int l,int r,int x){ if(l==r){p[l]=0;return;} int mid=(l+r)>>1; if(x<=mid) del(lc,l,mid,x); else del(rc,mid+1,r,x); maxx[o]=gmax(maxx[lc],maxx[rc]); } int query(int o,int l,int r,int x,int y){ if(y<l||r<x) return 0; if(x<=l&&r<=y) return maxx[o]; int mid=(l+r)>>1,res=0; if(x<=mid) res=gmax(res,query(lc,l,mid,x,y)); if(mid<y) res=gmax(res,query(rc,mid+1,r,x,y)); return res; } void putin(int x){ if(in[x]) return; if(query(1,1,n,x-k+1,x+k-1)==x) q.push(x),in[x]=1; } int main(){ n=read(),k=read(); inc(i,1,n) p[i]=read(); in[0]=1;build(1,1,n); inc(i,1,n) putin(i); dec(i,n,1){ int u=q.top();q.pop(); ans[u]=i; del(1,1,n,u); putin(query(1,1,n,u-k+1,u-1)); putin(query(1,1,n,u+1,u+k-1)); } inc(i,1,n) printf("%d\n",ans[i]); }
codenet
-1
1,396
s727520046
p04052
1,591,740,574
cpp
Accepted
1,025
15,728
#include<bits/stdc++.h> #define re register #define inc(i,j,k) for(re int i=j;i<=k;i++) #define dec(i,j,k) for(re int i=j;i>=k;i--) #define lc o<<1 #define rc o<<1|1 const int N=5e5+5; using namespace std; inline int read(){ int x=0; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x; } int n,k,p[N],maxx[N<<2],ans[N]; int gmax(int a,int b){return p[a]>p[b]?a:b;} priority_queue<int> q; bool in[N]; void build(int o,int l,int r){ if(l==r) return maxx[o]=l,void(); int mid=(l+r)>>1; build(lc,l,mid),build(rc,mid+1,r); maxx[o]=gmax(maxx[lc],maxx[rc]); } void del(int o,int l,int r,int x){ if(l==r){p[l]=0;return;} int mid=(l+r)>>1; if(x<=mid) del(lc,l,mid,x); else del(rc,mid+1,r,x); maxx[o]=gmax(maxx[lc],maxx[rc]); } int query(int o,int l,int r,int x,int y){ if(y<l||r<x) return 0; if(x<=l&&r<=y) return maxx[o]; int mid=(l+r)>>1,res=0; if(x<=mid) res=gmax(res,query(lc,l,mid,x,y)); if(mid<y) res=gmax(res,query(rc,mid+1,r,x,y)); return res; } void putin(int x){ if(in[x]) return; if(query(1,1,n,x-k+1,x+k-1)==x) q.push(x),in[x]=1; } int main(){ n=read(),k=read(); inc(i,1,n) p[i]=read(); in[0]=1;build(1,1,n); inc(i,1,n) putin(i); dec(i,n,1){ int u=q.top();q.pop(); ans[u]=i; del(1,1,n,u); putin(query(1,1,n,u-k+1,u-1)); putin(query(1,1,n,u+1,u+k-1)); } inc(i,1,n) printf("%d\n",ans[i]); }
codenet
-1
1,396
s298482141
p04052
1,588,222,127
cpp
Accepted
395
43,008
#include <bits/stdc++.h> using namespace std; #define mid ((s + e) / 2) #define makefast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define comp(x) lower_bound(vc.begin(), vc.end(), x) - vc.begin() const int N = 5e5 + 7; int n, k; int p[N], q[N]; int seg[N * 50]; bool mrk[N]; int cpk; set <int> s; void upd(int p, int val) { p += (1 << 20); seg[p] = val; p /= 2; while (p) { seg[p] = max(seg[p * 2], seg[p * 2 + 1]); p /= 2; } } int get(int l, int r) { l += (1 << 20); r += (1 << 20); int res = max(seg[l], seg[r]); while (l / 2 != r / 2) { if (l % 2 == 0) res = max(res, seg[l + 1]); if (r % 2 == 1) res = max(res, seg[r - 1]); l /= 2; r /= 2; } return res; } void get(int pk) { int l = pk * k; int r = min(n, pk * k + k - 1); int mx = get(l, r); if (mx < 0 || mrk[q[mx]]) return; if (get(q[mx] - k + 1, q[mx] + k - 1) == mx) { s.insert(q[mx]); mrk[q[mx]] = 1; } } int res[N]; int main() { makefast; cin >> n >> k; cpk = (n + k - 1) / k; for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; q[p[i]] = i; upd(i, p[i]); } for (int i = 0; i < cpk; i++) get(i); for (int i = 1; i <= n; i++) { int p = *s.rbegin(); s.erase(p); res[p + 1] = n - i + 1; upd(p, -1); for (int i = (p / k) - 1; i <= (p / k) + 1; i++) if (0 <= i && i < cpk) get(i); } for (int i = 1; i <= n; i++) cout << res[i] << '\n'; return 0; }
codenet
-1
1,398
s951824990
p04052
1,507,568,117
cpp
Accepted
445
57,344
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> #include<vector> #include<queue> using namespace std; #define REP(i,st,ed) for(int i=st,i##end=ed;i<=i##end;++i) #define DREP(i,st,ed) for(int i=st,i##end=ed;i>=i##end;--i) const int maxn=500005,inf=0x3f3f3f3f; struct seg_tree{ int Max[maxn<<2]; void push_up(int p,int u,int v){ Max[p]=max(Max[u],Max[v]); } int query(int p,int l,int r,int L,int R){ if((L<=l)&&(R>=r))return Max[p]; int mid=(l+r)>>1,u=p<<1,v=u+1,res=0; if(L<=mid)res=query(u,l,mid,L,R); if(R>mid)res=max(res,query(v,mid+1,r,L,R)); return res; } void update(int p,int l,int r,int pos,int val){ if(l==r){ Max[p]=val; return; } int mid=(l+r)>>1,u=p<<1,v=u+1; if(pos<=mid)update(u,l,mid,pos,val); else update(v,mid+1,r,pos,val); push_up(p,u,v); } }T; int n,m,a[maxn],p[maxn],s[maxn],tot; vector<int>E[maxn]; priority_queue<int,vector<int>,greater<int> >Q; void dfs(int u){ REP(i,0,E[u].size()-1)Q.push(E[u][i]); while(!Q.empty()){ int v=Q.top(); s[++tot]=v; Q.pop(),dfs(v); } } int main(){ scanf("%d%d",&n,&m); REP(i,1,n) scanf("%d",&a[i]),p[a[i]]=i; if(m==1){ REP(i,1,n) printf("%d\n",i); return 0; } REP(i,1,n){ E[p[T.query(1,1,n,p[i]+1,p[i]+m-1)]].push_back(p[i]); T.update(1,1,n,p[i],i); } dfs(0); REP(i,1,n) a[s[i]]=i; REP(i,1,n) printf("%d\n",a[i]); return 0; }
codenet
-1
1,411
s175769514
p04052
1,532,264,736
cpp
Accepted
1,847
54,528
#include <cstdio> #include <iostream> #include <set> using namespace std; const int N = 510000; int n, k, ind[N]; int a[N], b[N]; set<int>f, g; struct Edge { int to; Edge *next; }*h[N], e[N<<2]; int _; inline void add(int u, int v) { Edge *tmp = &e[++_]; tmp -> to = v; tmp -> next = h[u]; h[u] = tmp; } inline char nc() { static char buf[100000], *p1, *p2; return p1==p2&&(p2=(p1=buf)+fread(buf, 1, 100000, stdin)), p1==p2?EOF:*p1++; } int read() { int x = 0; char ch = nc(); while(!isdigit(ch)) ch = nc(); while(isdigit(ch)) x = x*10+ch-48, ch = nc(); return x; } int main() { n = read(), k = read(); for(int i = 1; i <= n; i ++ ) b[a[i]=read()] = i; for(int i = 1; i < k; i ++ ) f.insert(a[i]); for(int i = 1; i <= n; i ++ ) { if(i+k-1 < n) f.insert(a[i+k-1]); f.erase(a[i-1]); g.insert(a[i]); if(i > k) g.erase(a[i-k]); set<int>::iterator cur; cur = f.upper_bound(a[i]); if(cur != f.end()) add(b[*cur], i), ind[i]++; cur = g.upper_bound(a[i]); if(cur != g.end()) { add(b[*cur], i), ind[i]++; } } f.clear(); for(int i = 1; i <= n; i ++ ) if(!ind[i]) f.insert(-i); int top = 0; while(f.size()) { int u = -(*f.begin()); a[++top] = u; f.erase(f.begin()); for(Edge *i = h[u]; i; i = i->next) { if(--ind[i->to] == 0) { f.insert(-i->to); } } } for(int i = 1; i <= n; i ++ ) { b[a[i]] = n-i+1; } for(int i = 1; i <= n; i ++ ) cout << b[i] << endl; }
codenet
-1
1,420
s491381274
p04052
1,591,258,209
cpp
Accepted
1,047
15,600
#include <cstdio> #include <algorithm> #include <queue> const int Inf = 0x3f3f3f3f; const int MN = 500005, MS = 1 << 20 | 7; int N, K, P[MN], Ans[MN]; #define li (i << 1) #define ri (li | 1) #define mid ((l + r) >> 1) #define ls li, l, mid #define rs ri, mid + 1, r int mxp[MS]; void Build(int i, int l, int r) { if (l == r) return mxp[i] = l, void(); Build(ls), Build(rs); mxp[i] = P[mxp[li]] > P[mxp[ri]] ? mxp[li] : mxp[ri]; } void Del(int i, int l, int r, int p) { if (l == r) return mxp[i] = 0, void(); p <= mid ? Del(ls, p) : Del(rs, p); mxp[i] = P[mxp[li]] > P[mxp[ri]] ? mxp[li] : mxp[ri]; } int Qur(int i, int l, int r, int a, int b) { if (r < a || b < l) return 0; if (a <= l && r <= b) return mxp[i]; int v1 = Qur(ls, a, b), v2 = Qur(rs, a, b); return P[v1] > P[v2] ? v1 : v2; } int inq[MN]; std::priority_queue<int> pq; inline void check(int id) { if (inq[id]) return ; if (Qur(1, 1, N, id - K + 1, id + K - 1) == id) pq.push(id), inq[id] = 1; } int main() { scanf("%d%d", &N, &K); for (int i = 1; i <= N; ++i) scanf("%d", &P[i]); P[0] = -Inf; Build(1, 1, N); for (int i = 1; i <= N; ++i) check(i); for (int i = N; i >= 1; --i) { int u = pq.top(); pq.pop(); Ans[u] = i; Del(1, 1, N, u); int pos; if ((pos = Qur(1, 1, N, u - K + 1, u - 1))) check(pos); if ((pos = Qur(1, 1, N, u + 1, u + K - 1))) check(pos); } for (int i = 1; i <= N; ++i) printf("%d\n", Ans[i]); return 0; }
codenet
-1
1,426
s809487634
p04052
1,573,341,445
cpp
Accepted
639
28,916
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #define N 500010 #define P pair<int,int> #define MP make_pair #define fi first #define se second using namespace std; P tree[N<<2]; void change(int u,int l,int r,P x) { tree[u]=max(tree[u],x); if(l==r) return; int mid=(l+r)>>1; if(x.se<=mid) change(u<<1,l,mid,x); else change(u<<1|1,mid+1,r,x); } P answer(int u,int l,int r,int L,int R) { if(L<=l && r<=R) return tree[u]; if(R<L) return MP(0,0); int mid=(l+r)>>1; P ans=MP(0,0); if(L<=mid) ans=max(ans,answer(u<<1,l,mid,L,R)); if(R>mid) ans=max(ans,answer(u<<1|1,mid+1,r,L,R)); return ans; } int nxt[N<<1],to[N<<1],head[N],in[N],cnt; int p[N]; void add(int u,int v) { nxt[++cnt]=head[u]; to[cnt]=v; in[v]++; head[u]=cnt; } priority_queue<int,vector<int>,greater<int> > q; int ans[N]; int main() { int n,k; scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { int a; scanf("%d",&a); p[a]=i; } for(int i=1;i<=n;i++) { change(1,1,n,MP(i,p[i])); P l=answer(1,1,n,max(1,p[i]-k+1),p[i]-1),r=answer(1,1,n,p[i]+1,min(p[i]+k-1,n)); if(l.fi>0) add(l.se,p[i]); if(r.fi>0) add(r.se,p[i]); } for(int i=1;i<=n;i++) if(!in[i]) q.push(i); int tot=0; while(!q.empty()) { int u=q.top(); q.pop(); ans[u]=++tot; for(int i=head[u];i;i=nxt[i]) { int v=to[i]; in[v]--; if(!in[v]) q.push(v); } } for(int i=1;i<=tot;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,426
s852092011
p04052
1,576,253,858
cpp
Accepted
551
27,764
#include<bits/stdc++.h> using namespace std; const int maxn=5e5+5; const int INF=1e7+5; priority_queue<int,vector<int>,greater<int> > q; int n,k; int A[maxn],ans[maxn],ind[maxn]; int tot; int h[maxn],last[maxn<<1],to[maxn<<1]; int st[maxn<<2]; void build(int x,int l,int r) { st[x]=INF; if(l==r) return; int mid=(l+r)>>1; build(x<<1,l,mid),build(x<<1|1,mid+1,r); } void pushup(int x) { st[x]=min(st[x<<1],st[x<<1|1]); } void update(int x,int l,int r,int id,int k) { if(l==r){ st[x]=k; return; } int mid=(l+r)>>1; if(id<=mid) update(x<<1,l,mid,id,k); else update(x<<1|1,mid+1,r,id,k); pushup(x); } int query(int x,int l,int r,int ql,int qr) { if(r<ql||l>qr) return INF; if(l>=ql&&r<=qr) return st[x]; int mid=(l+r)>>1; return min(query(x<<1,l,mid,ql,qr),query(x<<1|1,mid+1,r,ql,qr)); } void add(int x,int y) { to[++tot]=y;last[tot]=h[x];h[x]=tot;++ind[y]; } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;++i){ int a=0; scanf("%d",&a); A[a]=i; } build(1,1,n); for(int i=n;i>=1;--i){ int l=query(1,1,n,A[i],A[i]+k-1),r=query(1,1,n,A[i]-k+1,A[i]); if(l!=INF) add(A[i],A[l]); if(r!=INF) add(A[i],A[r]); update(1,1,n,A[i],i); } for(int i=1;i<=n;++i) if(!ind[i]) q.push(i); int cnt=0; while(q.size()){ int u=q.top(); q.pop(); ans[u]=++cnt; for(int i=h[u];i;i=last[i]){ int v=to[i]; if(!--ind[v]) q.push(v); } } for(int i=1;i<=n;++i) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,429
s387457937
p04052
1,588,218,682
cpp
Accepted
332
17,648
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, inf = 1e8; int n, nn, k, a[N], b[N], cnt, ans[N]; bool mark[N]; int seg[N << 2]; priority_queue<int> s; void upd(int p, int val) { p += nn, seg[p] = val, p /= 2; for (; p > 0; p /= 2) seg[p] = max(seg[p * 2], seg[p * 2 + 1]); return; } int get(int l, int r) { l = max(l, 0), r = min(r, n); int rtr = -inf; for (l += nn, r += nn; l < r; l /= 2, r /= 2) { if (l % 2) rtr = max(rtr, seg[l++]); if (r % 2) rtr = max(rtr, seg[--r]); } return rtr; } void add(int plc) { if (plc >= cnt || plc < 0) return; int mx = get(k * plc, k * (plc + 1)); // cout << "plc = " << plc << " : " << mx << endl; if (mx < 0) return; if (!mark[b[mx]] && get(b[mx] - k + 1, b[mx] + k) == mx) s.push(b[mx]), mark[b[mx]] = true; // cout << "done" << endl; return; } void makeFind(int plc) { add(plc - 1); add(plc); add(plc + 1); } int main() { ios_base::sync_with_stdio (false), cin.tie(0), cout.tie(0); cin >> n >> k, nn = n, cnt = (n + k - 1) / k; while (__builtin_popcount(nn) != 1) nn++; for (int i = 0; i < n; i++) { cin >> a[i], a[i]--, b[a[i]] = i; upd(i, a[i]); } for (int i = 0; i < cnt; i++) add(i); // cout << "GO NEXT" << endl; for (int i = 0; i < n; i++) { int ls = s.top(); s.pop(); ans[ls] = n - i - 1; upd(ls, -inf); int plc = ls / k; makeFind(plc); } for (int i = 0; i < n; i++) cout << ans[i] + 1 << "\n"; return 0; }
codenet
-1
1,435
s829175822
p04052
1,558,743,986
cpp
Accepted
658
58,624
// KALAM # include<bits/stdc++.h> using namespace std; const int N = 500000 + 77; int n , k , cur = 1 , a[N] , P[N] , Mx[N << 2] , A[N]; bool M[N]; vector < int > adj[N]; void Update(int x , int y , int l = 1 , int r = n + 1 , int id = 1) { if(r - l < 2) { Mx[id] = y; return ; } int mid = ((l + r) >> 1); if(x < mid) Update(x , y , l , mid , id << 1); else Update(x , y , mid , r , id << 1 ^ 1); Mx[id] = max(Mx[id << 1] , Mx[id << 1 ^ 1]); } int Get(int ql , int qr , int l = 1 , int r = n + 1 , int id = 1) { if(qr <= l || r <= ql) return 0; if(ql <= l && r <= qr) return Mx[id]; int mid = ((l + r) >> 1); return max(Get(ql , qr , l , mid , id << 1) , Get(ql , qr , mid , r , id << 1 ^ 1)); } void dfs(int v) { M[v] = 1; for(int u : adj[v]) if(! M[u]) dfs(u); A[cur ++] = v; } int main() { scanf("%d %d" , & n , & k); for(int i = 1;i <= n;++ i) scanf("%d" , a + i) , P[a[i]] = i; for(int i = 1;i <= n;++ i) { int le = Get(max(P[i] - k + 1 , 1) , P[i]); int ri = Get(P[i] , min(P[i] + k , n + 1)); if(le) adj[P[i]].push_back(P[le]); if(ri) adj[P[i]].push_back(P[ri]); Update(P[i] , i); } for(int i = 1;i <= n;++ i) if(! M[i]) dfs(i); for(int i = 1;i <= n;++ i) a[A[i]] = i; for(int i = 1;i <= n;++ i) printf("%d\n" , a[i]); return 0; }
codenet
-1
1,436
s457401047
p04052
1,508,701,288
cpp
Accepted
615
45,056
#include<bits/stdc++.h> #define N 500010 #define ls (x<<1) #define rs (x<<1|1) #define RG register #define inf 0x3f3f3f3f using namespace std; vector<int> son[N]; priority_queue<int> que; int K,n,tmp,val,goal,p[N],q[N],du[N],tr[N<<2]; inline int query(RG int x,RG int l,RG int r,RG int ll,RG int rr){ if(l==ll&&r==rr) return tr[x]; RG int mid=(l+r)>>1; if(rr<=mid) return query(ls,l,mid,ll,rr); if(ll>mid) return query(rs,mid+1,r,ll,rr); return min(query(ls,l,mid,ll,mid),query(rs,mid+1,r,mid+1,rr)); } inline void add(RG int x,RG int l,RG int r){ if(l==r){tr[x]=val;return;} RG int mid=(l+r)>>1; if(goal<=mid) add(ls,l,mid); else add(rs,mid+1,r); tr[x]=min(tr[ls],tr[rs]); } int main(){ scanf("%d%d",&n,&K); memset(tr,inf,sizeof(tr)); for (RG int i=1;i<=n;++i){ scanf("%d",p+i); q[p[i]]=i; } for (RG int i=n;i;--i){ tmp=query(1,1,n,max(1,q[i]-K+1),q[i]); if(tmp<inf) son[q[tmp]].push_back(q[i]),++du[q[i]]; tmp=query(1,1,n,q[i],min(n,q[i]+K-1)); if(tmp<inf) son[q[tmp]].push_back(q[i]),++du[q[i]]; val=i;goal=q[i];add(1,1,n); } for (RG int i=1;i<=n;++i) if(!du[i]) que.push(i); for (RG int i=n;i;--i){ p[i]=que.top();que.pop(); for (RG int j=0;j<(int)son[p[i]].size();++j) if(!--du[son[p[i]][j]]) que.push(son[p[i]][j]); } for (RG int i=1;i<=n;++i) q[p[i]]=i; for (RG int i=1;i<=n;++i) printf("%d\n",q[i]); return 0; }
codenet
-1
1,440
s852642139
p04052
1,569,340,538
cpp
Accepted
173
13,424
#include <bits/stdc++.h> #define all(v) (v).begin(), (v).end() #define sortv(v) sort(all(v)) #define uniqv(v) (v).erase(unique(all(v)), (v).end()) #define pb push_back #define FI first #define SE second #define lb lower_bound #define ub upper_bound #define mp make_pair #define test 1 #define TEST if(test) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; const int MOD = 1000000007; // 998244353 const int INF = 2e9; const ll INFLL = 1e18; const int MAX_N = 500000; int N, K; int arr[MAX_N+1]; int idx[MAX_N+1]; int ans[MAX_N+1]; int mn[MAX_N+1]; vector<int> v; void pro(int x, int y){ if(x==y) return; int m = (x+y)/2; pro(x, m); pro(m+1, y); int i1 = x, i2 = m+1; mn[m] = idx[m]; for(int i=m-1; i>=x; i--){ mn[i] = min(mn[i+1], idx[i]); } while(1){ if(i1==m+1 && i2==y+1) break; if(i1==m+1){ v.pb(idx[i2]); i2++; }else if(i2==y+1){ v.pb(idx[i1]); i1++; }else{ if(mn[i1]>=idx[i2]+K){ v.pb(idx[i2]); i2++; }else{ v.pb(idx[i1]); i1++; } } } for(int i=0; i<v.size(); i++){ idx[x+i] = v[i]; //cout<<x+i<<" "<<v[i]<<endl; }//cout<<endl; while(!v.empty()) v.pop_back(); } int main(){ scanf("%d%d", &N, &K); for(int i=1; i<=N; i++){ int x; scanf("%d", &x); arr[i] = x; idx[x] = i; } pro(1, N); for(int i=1; i<=N; i++){ ans[idx[i]] = i; } for(int i=1; i<=N; i++){ printf("%d\n", ans[i]); } return 0; }
codenet
-1
1,446
s912406844
p04052
1,538,615,663
cpp
Accepted
1,236
44,544
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<set> using namespace std; const int MAXN=500000+10; int n,k,A[MAXN],P[MAXN],Indegree[MAXN],to[MAXN<<1],nex[MAXN<<1],beg[MAXN],Ans[MAXN],cnt,e; priority_queue<int> q; set<int> tmp1,tmp2; inline void read(int &x) { int data=0,w=1; char ch=0; while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar(); if(ch=='-')w=-1,ch=getchar(); while(ch>='0'&&ch<='9')data=(data<<3)+(data<<1)+(ch^'0'),ch=getchar(); x=data*w; } inline void insert(int x,int y) { to[++e]=y; nex[e]=beg[x]; beg[x]=e; } inline void toposort() { while(!q.empty()) { int x=q.top(); q.pop(); Ans[++cnt]=x; for(register int i=beg[x];i;i=nex[i]) { Indegree[to[i]]--; if(!Indegree[to[i]])q.push(to[i]); } } } int main() { read(n);read(k); for(register int i=1;i<=n;++i) { read(P[i]); A[P[i]]=i; } for(register int i=1;i<k-1;i++)tmp1.insert(P[i]); for(register int i=1;i<=n;i++) { if(i+k-1<=n)tmp1.insert(P[i+k-1]); tmp1.erase(P[i-1]); tmp2.insert(P[i]); if(i>k)tmp2.erase(P[i-k]); set<int>::iterator cur=tmp1.upper_bound(P[i]); if(cur!=tmp1.end())insert(A[*cur],i),Indegree[i]++; cur=tmp2.upper_bound(P[i]); if(cur!=tmp2.end())insert(A[*cur],i),Indegree[i]++; } for(register int i=1;i<=n;++i) if(!Indegree[i])q.push(i); toposort(); for(register int i=1;i<=n;++i)A[Ans[i]]=n-i+1; for(register int i=1;i<=n;++i)printf("%d\n",A[i]); return 0; }
codenet
-1
1,451
s516754860
p04052
1,507,687,347
cpp
Accepted
976
42,624
#include<cstdio> #include<cctype> #include<queue> #include<set> #define N 500005 #define For(i,j,k) for(int i=j;i<=k;i++) using namespace std; int read(){ int x=0,l=1; char ch=getchar(); while(!isdigit(ch)) {if (ch=='-') l=-1; ch=getchar();} while(isdigit(ch)) x=x*10+(ch^48),ch=getchar(); return x*l; } int l,pi[10]; void write(int x){ l=0; while(x>0) pi[++l]=x%10,x=x/10; while(l) putchar(48+pi[l--]); putchar(10); } struct node{ int key,pos; friend bool operator < (node a,node b){ return a.key<b.key; } }; int ans[N],a[N],v[N<<1],in[N],cnt,head[N],nex[N<<1]; void add(int x,int y){ in[y]++; v[++cnt]=y; nex[cnt]=head[x]; head[x]=cnt; } int main(){ int now=0,n=read(),k=read(); For(i,1,n) a[i]=read(); set<node> st; set<node>::iterator it; For(i,1,n){ if (i>=k+1) st.erase((node){a[i-k],i-k}); it=st.upper_bound((node){a[i],i}); if (it!=st.end()) add(i,(*it).pos); st.insert((node){a[i],i}); } st.clear(); For(i,1,k-1) st.insert((node){a[i],i}); For(i,1,n){ st.erase((node){a[i],i}); if (i+k-1<=n) st.insert((node){a[i+k-1],i+k-1}); it=st.upper_bound((node){a[i],i}); if (it!=st.end()) add(i,(*it).pos); st.erase((node){a[i-k+1]}); } priority_queue<int,vector<int>,greater<int> > q; For(i,1,n) if (!in[i]) q.push(i); while(!q.empty()){ int u=q.top(); q.pop(); ans[u]=++now; for(int i=head[u];i;i=nex[i]){ --in[v[i]]; if (!in[v[i]]) q.push(v[i]); } } For(i,1,n) write(*(ans+i)); return 0; }
codenet
-1
1,452
s958641178
p04052
1,557,728,476
cpp
Accepted
727
49,536
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int maxn = 5e5 + 20; int d[maxn] , a[maxn] , n , mx[maxn * 4]; vector<int> adj[maxn]; void update(int p , int val , int s = 0 , int e = n , int v = 1) { if(e - s < 2) { mx[v] = val; return; } int m = (s + e) / 2; if(p < m) update(p , val , s , m , 2 * v); else update(p , val , m , e , 2 * v + 1); mx[v] = max(mx[2 * v] , mx[2 * v + 1]); } int get(int l , int r , int s = 0 , int e = n , int v = 1) { if(l <= s && e <= r) return mx[v]; if(r <= s || e <= l) return -1; int m = (s + e) / 2; return max(get(l , r , s , m , 2 * v) , get(l , r , m , e , 2 * v + 1)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(mx , -1 , sizeof mx); int k; cin >> n >> k; for(int i = 0; i < n; i++) { int x; cin >> x; x--; a[x] = i; } for(int i = 0; i < n; i++) { int v = get(a[i] - k + 1 , a[i]); if(v >= 0) adj[a[v]].pb(a[i]) , d[a[i]]++; v = get(a[i] , a[i] + k); if(v >= 0) adj[a[v]].pb(a[i]) , d[a[i]]++; update(a[i] , i); } set<int> st; for(int i = 0; i < n; i++) if(!d[i]) st.insert(i); for(int _ = 0; _ < n; _++) { assert(!st.empty()); int v = *st.begin(); st.erase(v); for(auto u : adj[v]) { d[u]--; if(!d[u]) st.insert(u); } a[v] = _; } for(int i = 0; i < n; i++) cout << a[i] + 1 << " "; cout << endl; }
codenet
-1
1,457
s496727479
p04052
1,569,592,465
cpp
Accepted
502
25,588
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<queue> using namespace std; const int N=500005; int n,K,i,j,k,l,a[N],mn[N*5],head[N],adj[N*2],nxt[N*2],d[N],ans[N]; priority_queue<int,vector<int>,greater<int> > q; void addedge(int u,int v) { adj[++l]=v; nxt[l]=head[u]; head[u]=l; ++d[v]; } void built(int o,int l,int r) { mn[o]=1<<30; if(l==r) return; int mid=l+r>>1; built(o<<1,l,mid); built(o<<1|1,mid+1,r); } void update(int o,int l,int r,int x,int y) { if(l==r) { mn[o]=min(mn[o],y); return; } int mid=l+r>>1; if(x<=mid) update(o<<1,l,mid,x,y); else update(o<<1|1,mid+1,r,x,y); mn[o]=min(mn[o<<1],mn[o<<1|1]); } int query(int o,int l,int r,int x,int y) { if(l>=x&&r<=y) return mn[o]; int mid=l+r>>1,rtn=1<<30; if(x<=mid) rtn=query(o<<1,l,mid,x,y); if(y>mid) rtn=min(rtn,query(o<<1|1,mid+1,r,x,y)); return rtn; } int main() { scanf("%d%d",&n,&K); for(i=1;i<=n;++i) { scanf("%d",&j); a[j]=i; } built(1,1,n); for(i=n;i>=1;--i) { j=query(1,1,n,a[i],min(n,a[i]+K-1)); if(j<(1<<30)) addedge(a[i],a[j]); j=query(1,1,n,max(1,a[i]-K+1),a[i]); if(j<(1<<30)) addedge(a[i],a[j]); update(1,1,n,a[i],i); } for(i=1;i<=n;++i) if(!d[i]) q.push(i); k=0; while(!q.empty()) { i=q.top(); q.pop(); ans[i]=++k; for(j=head[i];j;j=nxt[j]) { --d[adj[j]]; if(!d[adj[j]]) q.push(adj[j]); } } for(i=1;i<=n;++i) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,468
s916590221
p04052
1,572,011,720
cpp
Accepted
429
29,684
#include <bits/stdc++.h> using namespace std; #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 N 500005 #define inf 666666666 int in[N],pos[N],p[N],ans[N],tot,n,k; priority_queue <int> q; int head[N],to[N<<1],nxt[N<<1],cnt; void add(int u,int v) { cnt++; to[cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; } #define ls id<<1 #define rs id<<1|1 int t[N<<2]; void change(int id,int l,int r,int p,int d) { if(l==r) { t[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); t[id]=min(t[ls],t[rs]); } int query(int id,int L,int R,int l,int r) { if(t[id]>=N) return inf; if(l==L&&r==R) return t[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() { int tmp; scanf("%d%d",&n,&k); rep(i,1,n) scanf("%d",&tmp),p[tmp]=i; memset(t,0x3f,sizeof(t)); dep(i,n,1) { int pos=query(1,1,n,p[i],min(n,p[i]+k-1)); if(pos!=inf) { add(p[i],p[pos]); in[p[pos]]++; } pos=query(1,1,n,max(1,p[i]-k+1),p[i]); if(pos!=inf) { add(p[i],p[pos]); in[p[pos]]++; } change(1,1,n,p[i],i); } rep(i,1,n) if(!in[i]) q.push(-i); while(!q.empty()) { int now=-q.top(); ans[now]=++tot; q.pop(); for(int i=head[now]; i; i=nxt[i]) { int v=to[i]; --in[v]; if(!in[v]) q.push(-v); } } rep(i,1,n) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,469
s463537063
p04052
1,468,727,579
cpp
Accepted
459
27,648
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stack> #include <queue> #include <set> #include <cmath> #include <map> using namespace std; #define MOD 1000000007 #define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; const int INF = 2100000000; struct segtree { static const int DEPTH = 19; static const int N = 1 << DEPTH; int val[2 * N]; segtree() { for (int i = 0; i < 2 * N; ++i) val[i] = INF; } void update(int p, int v) { p += N; val[p] = v; p >>= 1; while (p) { val[p] = min(val[p * 2], val[p * 2 + 1]); p >>= 1; } } int query(int L, int R) { L += N; R += N; int ret = INF; while (L < R) { if (L & 1) ret = min(ret, val[L++]); if (R & 1) ret = min(ret, val[--R]); L >>= 1; R >>= 1; } return ret; } }; segtree seg; int N, K, P[505050]; bool visited[505050]; int ans[505050], al; void decide(int idx) { for (;;) { int least = seg.query(0, P[idx]); if (least >= idx + K) break; decide(least); } ans[idx] = al++; visited[idx] = true; seg.update(P[idx], INF); } int main() { scanf("%d%d", &N, &K); for (int i = 0; i < N; ++i) { scanf("%d", P + i); --P[i]; visited[i] = false; seg.update(P[i], i); } al = 0; for (int i = 0; i < N; ++i) if (!visited[i]) decide(i); for (int i = 0; i < N; ++i) printf("%d\n", ans[i] + 1); return 0; }
codenet
-1
1,471
s130707422
p04052
1,551,658,597
cpp
Accepted
437
27,512
#include <bits/stdc++.h> using namespace std; #define rep(i,x,y) for(int i=(x);i<=(y);++i) int q[2001000],a[500010],p[500010]; inline void add(int l,int r,int o,int x,int y) { if(l==r){q[o]=y;return;} int mid=(l+r)/2; if(x<=mid)add(l,mid,2*o,x,y); else add(mid+1,r,2*o+1,x,y); q[o]=min(q[2*o],q[2*o+1]); } inline int query(int l,int r,int o,int x,int y){ if(x<=l&&r<=y)return q[o]; int mid=(l+r)/2,ret=1e9; if(x<=mid)ret=min(ret,query(l,mid,2*o,x,y)); if(y>mid)ret=min(ret,query(mid+1,r,2*o+1,x,y)); return ret; } struct Edge{ int v,Next; } G[1000010]; int head[500010],d[500010],tot; inline void add(int u,int v){ //cerr<<u<<" "<<v<<endl; G[++tot]=(Edge){v,head[u]}; head[u]=tot;d[v]++; } int main() { int n,k; scanf("%d%d",&n,&k); rep(i,1,n)scanf("%d",&a[i]); rep(i,1,n)p[a[i]]=i; memset(head,-1,sizeof(head)); rep(i,1,4*n)q[i]=1e9; for(int i=n;i>=1;--i){ //cerr<<i<<endl; int wt=query(1,n,1,p[i],min(n,p[i]+k-1)); if(wt!=1e9)add(p[i],p[wt]); wt=query(1,n,1,max(1,p[i]-k+1),p[i]); //if(wt!=1e9)cerr<<"pp "<<p[wt]<<endl; if(wt!=1e9)add(p[i],p[wt]); //cerr<<p[i]<<" " <<i<<endl; add(1,n,1,p[i],i); //cerr<<p[i]<<" " <<i<<endl; }//cerr<<233<<endl; priority_queue<int> Q; rep(i,1,n)if(!d[i]){ Q.push(-i); } int tt=0; while(!Q.empty()){ int x=-Q.top();Q.pop(); p[++tt]=x; for(int i=head[x];i!=-1;i=G[i].Next){ d[G[i].v]--; if(d[G[i].v]==0){ Q.push(-G[i].v); } } } rep(i,1,n)a[p[i]]=i; rep(i,1,n)printf("%d\n",a[i]); }
codenet
-1
1,485
s137153072
p04052
1,507,517,640
cpp
Accepted
349
35,060
#include<bits/stdc++.h> #define l(x) (x<<1) #define r(x) (x<<1)|1 #define pb push_back const int N = 500000 + 10; using namespace std; int n,m,e; int mx[N<<2],t[N],s[N],b[N],bgn[N<<1],to[N<<2],nxt[N<<2]; int v[N],snt; priority_queue<int, vector<int>, greater<int> > q; namespace seg { inline int ckmx(int a,int b) { return a>b?a:b; } void mt(int x){mx[x]=ckmx(mx[l(x)],mx[r(x)]);} void upd(int x,int l,int r,int p,int v) { if(l==r) {mx[x]=v; return;} int m=(l+r)>>1; if(p<=m) upd(l(x),l,m,p,v); else upd(r(x),m+1,r,p,v); mt(x); } int qury(int x,int l,int r,int ql,int qr) { if(ql<=l&&r<=qr) return mx[x]; else { int g=0,m=(l+r)>>1; if(ql<=m) g=ckmx(g,qury(l(x),l,m,ql,qr)); if(m<qr) g=ckmx(g,qury(r(x),m+1,r,ql,qr)); return g; } } } using namespace seg; namespace solver { int abs(int x) { return x > 0 ? x : -x;} void add(int x,int y) { to[++e]=y; nxt[e]=bgn[x]; bgn[x]=e; } void dfs(int x=0) { int sz=0; s[snt++]=x; for(int i=bgn[x]; i; i=nxt[i]) q.push(to[i]); while(!q.empty()) { int y=q.top(); q.pop(); dfs(y); } } void solve() { scanf("%d%d",&n,&m); int x(0); for(int i=1; i<=n; ++i) { scanf("%d",&x); b[i]=x;t[x]=i; } for(int i=1; i<=n; ++i) { int ps=qury(1,1,n,t[i]+1,t[i]+m-1); upd(1,1,n,t[i],i); add(t[ps],t[i]); } dfs(); for(int i=1; i<=n; ++i) b[s[i]]=i; for(int i=1; i<=n; ++i) printf("%d\n",b[i]); } } int main() { solver :: solve(); }
codenet
-1
1,490
s437595658
p04052
1,568,657,729
cpp
Accepted
600
48,640
#include<bits/stdc++.h> using namespace std; const int N=1000005; int f[N],a[N],l,b[N],tot,ne[N],fi[N],zz[N],in[N],ans[N],n,m; struct Tree{ int l,r,num; }T[N*4]; void up(int x){ if (x==1)return; if (b[f[x]]<b[f[x/2]]){ swap(f[x],f[x/2]); up(x/2); } } void down(int x){ int i=x; if (x*2<=l&&b[f[x]]>b[f[x*2]])i=x*2; if (x*2<l&&b[f[i]]>b[f[x*2+1]])i=x*2+1; if (i!=x){ swap(f[x],f[i]); down(i); } } void build(int x,int l,int r){ T[x].l=l;T[x].r=r;T[x].num=n+1; if (T[x].l==T[x].r)return; int mid=(l+r)/2; build(x*2,l,mid); build(x*2+1,mid+1,r); } void insert(int x,int y,int z){ T[x].num=min(T[x].num,z); if (T[x].l==T[x].r)return; int mid=(T[x].l+T[x].r)/2; if (y<=mid)insert(x*2,y,z); else insert(x*2+1,y,z); } int find(int x,int l,int r){ if (T[x].l>r||l>T[x].r)return n+1; if (T[x].l>=l&&T[x].r<=r)return T[x].num; return min(find(x*2,l,r),find(x*2+1,l,r)); } void jb(int x,int y){ ne[++tot]=fi[x]; fi[x]=tot; zz[tot]=y; in[y]++; } int main(){ scanf("%d%d",&n,&m); for (int i=1;i<=n;i++)scanf("%d",&a[i]),b[a[i]]=i; build(1,1,n); for (int i=n;i;i--){ int k1=find(1,b[i]-m+1,b[i]),k2=find(1,b[i],b[i]+m-1); if (k1<=n)jb(i,k1); if (k2<=n)jb(i,k2); insert(1,b[i],i); } for (int i=1;i<=n;i++) if (!in[i])f[++l]=i,up(l); for (int i=1;i<=n;i++){ int k=f[1];f[1]=f[l--];down(1); ans[b[k]]=i; for (int j=fi[k];j;j=ne[j]){ in[zz[j]]--; if (!in[zz[j]])f[++l]=zz[j],up(l); } } for (int i=1;i<=n;i++)printf("%d\n",ans[i]); return 0; }
codenet
-1
1,493
s689029552
p04052
1,580,338,541
cpp
Accepted
1,001
11,432
#include<bits/stdc++.h> #define itr(i, n) for(int i=1;i<=n;++i) using namespace std; const int SIZE = 5e5+1; vector<int> num_arr(SIZE), idx_arr(SIZE); stack<int> s, t; int memo[SIZE]; int n, k; void NIT(){ //Num Index Transform 数とインデックスを入れ替える itr(i, n) //両方とも要素を1からnにする idx_arr[num_arr[i]] = i; } void INT(){ //Index Num Transform itr(i, n) { num_arr[idx_arr[i]] = i; } } void divide_and_conquer_method(int left, int right){ if (left >= right) return; int median = (left + right) / 2; divide_and_conquer_method(left, median); divide_and_conquer_method(median+1, right); int Min = idx_arr[median]; s.push(Min); t.push(idx_arr[median]); for (int i = median-1; i >= left; --i){ Min = min(Min, idx_arr[i]); s.push(Min); t.push(idx_arr[i]); } int j = left; for (int i = median+1; i <= right; i++) { while (!(s.empty()) && s.top() - idx_arr[i] < k) { memo[j] = t.top(); ++j; s.pop(); t.pop(); } memo[j] = idx_arr[i]; j++; } while (!t.empty()) { memo[j] = t.top(); ++j; t.pop(); s.pop(); } for (int i = left; i <= right; i++){ idx_arr[i] = memo[i]; } } int main(){ cin >> n >> k; itr(i, n) cin >> num_arr[i]; NIT(); divide_and_conquer_method(1, n); INT(); itr(i, n){ cout << num_arr[i] << endl; } }
codenet
-1
1,501
s962868405
p04052
1,571,003,030
cpp
Accepted
774
45,184
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int maxn = 500010; int n, K, p[maxn], a[maxn], d[maxn], mn[maxn << 2]; vector<int> G[maxn]; priority_queue<int> q; #define mid (l + r >> 1) #define ls (k << 1) #define rs (k << 1 | 1) void modify(int k, int l, int r, int p, int v) { if (l == r) { mn[k] = v; return; } if (mid >= p) modify(ls, l, mid, p, v); else modify(rs, mid + 1, r, p, v); mn[k] = min(mn[ls], mn[rs]); } int qmin(int k, int l, int r, int ql, int qr) { if (l >= ql && r <= qr) return mn[k]; int ans = INT_MAX; if (mid >= ql) ans = qmin(ls, l, mid, ql, qr); if (mid < qr) ans = min(ans, qmin(rs, 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]), a[p[i]] = i; } memset(mn, 0x3f, sizeof(mn)); for (int i = n; i; i--) { int j = qmin(1, 1, n, a[i] - K + 1, a[i]); if (j <= n) G[a[j]].push_back(a[i]), d[a[i]]++; j = qmin(1, 1, n, a[i], a[i] + K - 1); if (j <= n) G[a[j]].push_back(a[i]), d[a[i]]++; modify(1, 1, n, a[i], i); } for (int i = 1; i <= n; i++) { if (!d[i]) q.push(i); } for (int i = n; i; i--) { int v = a[i] = q.top(); q.pop(); for (int u : G[v]) if (!--d[u]) q.push(u); } for (int i = 1; i <= n; i++) { p[a[i]] = i; } for (int i = 1; i <= n; i++) { printf("%d\n", p[i]); } return 0; }
codenet
-1
1,506
s962868405
p04052
1,571,003,030
cpp
Accepted
774
45,184
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int maxn = 500010; int n, K, p[maxn], a[maxn], d[maxn], mn[maxn << 2]; vector<int> G[maxn]; priority_queue<int> q; #define mid (l + r >> 1) #define ls (k << 1) #define rs (k << 1 | 1) void modify(int k, int l, int r, int p, int v) { if (l == r) { mn[k] = v; return; } if (mid >= p) modify(ls, l, mid, p, v); else modify(rs, mid + 1, r, p, v); mn[k] = min(mn[ls], mn[rs]); } int qmin(int k, int l, int r, int ql, int qr) { if (l >= ql && r <= qr) return mn[k]; int ans = INT_MAX; if (mid >= ql) ans = qmin(ls, l, mid, ql, qr); if (mid < qr) ans = min(ans, qmin(rs, 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]), a[p[i]] = i; } memset(mn, 0x3f, sizeof(mn)); for (int i = n; i; i--) { int j = qmin(1, 1, n, a[i] - K + 1, a[i]); if (j <= n) G[a[j]].push_back(a[i]), d[a[i]]++; j = qmin(1, 1, n, a[i], a[i] + K - 1); if (j <= n) G[a[j]].push_back(a[i]), d[a[i]]++; modify(1, 1, n, a[i], i); } for (int i = 1; i <= n; i++) { if (!d[i]) q.push(i); } for (int i = n; i; i--) { int v = a[i] = q.top(); q.pop(); for (int u : G[v]) if (!--d[u]) q.push(u); } for (int i = 1; i <= n; i++) { p[a[i]] = i; } for (int i = 1; i <= n; i++) { printf("%d\n", p[i]); } return 0; }
codenet
-1
1,506
s001474971
p04052
1,592,468,026
cpp
Accepted
1,047
17,644
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> #define PB push_back using namespace std; const int N = 5e5 + 500; const int OFF = (1 << 19); int n, k, P[N], Q[N], T[2 * OFF], sol[N]; inline int miin(int x, int y){ if(Q[x] < Q[y]) return x; return y; } void update(int i,int x){ Q[i] = x; T[OFF + i] = i; for(i = (i + OFF) / 2; i ; i /= 2) T[i] = miin(T[2 * i], T[2 * i + 1]); } int query(int i,int a,int b,int lo,int hi){ if(lo <= a && b <= hi) return T[i]; if(a > hi || b < lo) return n; return miin(query(2 * i, a, (a + b) / 2, lo, hi), query(2 * i + 1, (a + b) / 2 + 1, b, lo, hi)); } inline int lokal(int x){ return x == query(1, 0, OFF - 1, max(0, x - k + 1), min(n - 1, x + k - 1)); } priority_queue < pair < int, int > , vector < pair < int, int > >, less < pair < int, int > > > PQ; inline void buket(int x){ if(x < 0 || x * k >= n) return; int kan = query(1, 0, OFF - 1, x * k, min(x * k + k - 1, n - 1)); if(Q[kan] == n) return; if(lokal(kan)) PQ.push({kan, kan}); } int main(){ scanf("%d%d", &n, &k); for(int i = 0;i < n;i++){ scanf("%d", P + i); P[i]--; update(i, n - P[i] - 1); } update(n, n); for(int i = 0;i * k < n;i++) buket(i); int trn = n - 1; for(;!PQ.empty();){ int cur = PQ.top().second; PQ.pop(); if(Q[cur] == n) continue; sol[cur] = trn--; update(cur, n); buket(cur / k); buket(cur / k - 1); buket(cur / k + 1); } for(int i = 0;i < n;i++) printf("%d\n", sol[i] + 1); }
codenet
-1
1,511
s427445471
p04052
1,503,513,140
cpp
Accepted
892
21,760
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> pp; int n, k; const int M = 524288; int fen[500010]; int fens(int p){ int ret=0; while(p) ret+=fen[p], p&=(p-1); return ret; } void fenu(int p, int v){ while(p<=n) fen[p]+=v, p+=(p&-p); } int fenr(int l, int r){ l=max(l, 0); r=min(r, n); return fens(r)-(l?fens(l-1):0); } pp tree[M<<1]; int lazy[M<<1]; void uke(int p, int v){ tree[p].first += v; lazy[p] += v; } void pd(int p, int l, int r){ if(l==r || !lazy[p]) return; uke(p*2, lazy[p]); uke(p*2+1, lazy[p]); lazy[p]=0; } void upd(int pt, int v, int l=1, int r=n, int p=1){ pd(p, l, r); if(l==r){ tree[p]={v, l}; return; } int mid=(l+r)/2; if(pt<=mid) upd(pt, v, l, mid, p*2); else upd(pt, v, mid+1, r, p*2+1); tree[p]=min(tree[p*2], tree[p*2+1]); } void updr(int ul, int ur, int l=1, int r=n, int p=1){ pd(p, l, r); if(ul<=l && r<=ur){ uke(p, -1); return; } if(ur<l || r<ul) return; int mid=(l+r)/2; updr(ul, ur, l, mid, p*2); updr(ul, ur, mid+1, r, p*2+1); tree[p]=min(tree[p*2], tree[p*2+1]); } int a[500010]; int ans[500010]; int main() { scanf("%d%d",&n,&k); for(int i=1; i<=n; ++i){ int x; scanf("%d", &x); a[x]=i; } for(auto& x:tree) x={2e9, 0}; for(int i=1; i<=n; ++i){ int x=a[i]; int indeg = fenr(x-k+1, x+k-1); fenu(x, 1); upd(x, indeg); } for(int i=1; i<=n; ++i){ int x=tree[1].second; ans[x]=i; upd(x, 2e9); updr(max(1, x-k+1), min(n, x+k-1)); } for(int i=1; i<=n; ++i){ printf("%d\n", ans[i]); } return 0; }
codenet
-1
1,515
s733766607
p04052
1,542,563,664
cpp
Accepted
448
28,288
#include <bits/stdc++.h> using namespace std; #define X first #define Y second #define mid ((l+r)>>1) #define lc k<<1,l,mid #define rc k<<1|1,mid+1,r #define pb push_back typedef double db; typedef long long ll; typedef pair<int,int> P; const int MAXN=5e5+10,INF=1<<28; struct edge{int nxt,to;}e[MAXN<<2]; int head[MAXN],tot; int n,k,in[MAXN],a[MAXN],pos[MAXN],seg[MAXN<<2]; void add_edge(int x,int y) {e[++tot]=(edge){head[x],y};head[x]=tot;in[y]++;} void Update(int pos,int val,int k,int l,int r) { seg[k]=min(seg[k],val); if(l==r) return;//最后一层也要先更新再返回! if(pos<=mid) Update(pos,val,lc); else Update(pos,val,rc); } void Query(int &mn,int a,int b,int k,int l,int r) { if(a<=l&&r<=b) {mn=min(mn,seg[k]);return;} if(a<=mid) Query(mn,a,b,lc); if(b>mid) Query(mn,a,b,rc); } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d",&a[i]),pos[a[i]]=i; for(int i=0;i<=4*n;i++) seg[i]=INF; for(int i=n;i>=1;i--) { int nxt=INF,pre=INF; //注意询问必须保证与[1,n]相交 Query(nxt,pos[i],pos[i]+k-1,1,1,n); Query(pre,pos[i]-k+1,pos[i],1,1,n); if(nxt!=INF) add_edge(pos[i],pos[nxt]); if(pre!=INF) add_edge(pos[i],pos[pre]); Update(pos[i],i,1,1,n); } priority_queue<int,vector<int>,greater<int> > q; for(int i=1;i<=n;i++) if(!in[i]) q.push(i); for(int i=1;i<=n;i++) { int t=q.top();q.pop(); pos[i]=t; for(int j=head[t];j;j=e[j].nxt) if(!(--in[e[j].to])) q.push(e[j].to); } for(int i=1;i<=n;i++) a[pos[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",a[i]); return 0; } /* 6 2 2 3 5 6 4 1 */
codenet
-1
1,527
s938431272
p04052
1,588,231,146
cpp
Accepted
647
24,960
#include <bits/stdc++.h> using namespace std; const int MAXN = 500 * 1000 + 23, INF = 1000 * 1000 * 1000 + 23; int n, k, p[MAXN], fen[MAXN], inv[MAXN], od[MAXN], seg[MAXN << 2], ml[MAXN << 2]; inline void upd(int p) { for (p++; p <= MAXN; p += (p & -p)) fen[p]++; } inline int pget(int p) { int ret = 0; for (; p; p -= (p & -p)) ret += fen[p]; return ret; } inline int fget(int l, int r) { return pget(min(r, n)) - pget(max(l, 0)); } void add(int l, int r, int val, int v = 1, int st = 0, int ed = n) { if (l >= ed || st >= r) return; if (l <= st && ed <= r) { seg[v] += val; ml[v] += val; return; } int mid = ((st + ed) >> 1); add(l, r, val, v << 1, st, mid), add(l, r, val, (v << 1) | 1, mid, ed); seg[v] = min(seg[v << 1], seg[(v << 1) | 1]) + ml[v]; } int get(int v = 1, int st = 0, int ed = n) { if (ed - st == 1) return st; int mid = ((st + ed) >> 1); if (seg[(v << 1) | 1] <= seg[v << 1]) return get((v << 1) | 1, mid, ed); else return get(v << 1, st, mid); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> p[i]; inv[--p[i]] = i; } for (int i = n - 1; ~i; i--) { od[inv[i]] = fget(inv[i] - k + 1, inv[i] + k); upd(inv[i]); } for (int i = 0; i < n; i++) add(i, i + 1, od[i]); for (int i = n - 1; ~i; i--) { int x = get(); p[x] = i; add(x, x + 1, INF); add(max(x - k + 1, 0), min(n, x + k), -1); } for (int i = 0; i < n; i++) cout << p[i] + 1 << '\n'; return 0; }
codenet
-1
1,527
s733766607
p04052
1,542,563,664
cpp
Accepted
448
28,288
#include <bits/stdc++.h> using namespace std; #define X first #define Y second #define mid ((l+r)>>1) #define lc k<<1,l,mid #define rc k<<1|1,mid+1,r #define pb push_back typedef double db; typedef long long ll; typedef pair<int,int> P; const int MAXN=5e5+10,INF=1<<28; struct edge{int nxt,to;}e[MAXN<<2]; int head[MAXN],tot; int n,k,in[MAXN],a[MAXN],pos[MAXN],seg[MAXN<<2]; void add_edge(int x,int y) {e[++tot]=(edge){head[x],y};head[x]=tot;in[y]++;} void Update(int pos,int val,int k,int l,int r) { seg[k]=min(seg[k],val); if(l==r) return;//最后一层也要先更新再返回! if(pos<=mid) Update(pos,val,lc); else Update(pos,val,rc); } void Query(int &mn,int a,int b,int k,int l,int r) { if(a<=l&&r<=b) {mn=min(mn,seg[k]);return;} if(a<=mid) Query(mn,a,b,lc); if(b>mid) Query(mn,a,b,rc); } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d",&a[i]),pos[a[i]]=i; for(int i=0;i<=4*n;i++) seg[i]=INF; for(int i=n;i>=1;i--) { int nxt=INF,pre=INF; //注意询问必须保证与[1,n]相交 Query(nxt,pos[i],pos[i]+k-1,1,1,n); Query(pre,pos[i]-k+1,pos[i],1,1,n); if(nxt!=INF) add_edge(pos[i],pos[nxt]); if(pre!=INF) add_edge(pos[i],pos[pre]); Update(pos[i],i,1,1,n); } priority_queue<int,vector<int>,greater<int> > q; for(int i=1;i<=n;i++) if(!in[i]) q.push(i); for(int i=1;i<=n;i++) { int t=q.top();q.pop(); pos[i]=t; for(int j=head[t];j;j=e[j].nxt) if(!(--in[e[j].to])) q.push(e[j].to); } for(int i=1;i<=n;i++) a[pos[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",a[i]); return 0; } /* 6 2 2 3 5 6 4 1 */
codenet
-1
1,527
s001715423
p04052
1,526,438,862
cpp
Accepted
1,424
46,592
#include<bits/stdc++.h> using namespace std; #define rep(i,j,k) for(int i=(j),i##_end=(k);i<=i##_end;++i) const int MXN = 5e5 + 5; const int INF = 0x7fffffff; int n,a[MXN],k,p[MXN],mn[MXN<<2],deg[MXN],A[MXN],cnt; vector<int> G[MXN]; inline void pushup(int k) { mn[k]=min(mn[k<<1],mn[k<<1|1]); } inline void update(int k,int l,int r,int p,int x) { if(l==r) { mn[k]=x; return; } // cerr<<"k = "<<k<<endl; int mid=l+r>>1; if(p<=mid) update(k<<1,l,mid,p,x); else update(k<<1|1,mid+1,r,p,x); pushup(k); } inline int query(int k,int l,int r,int L,int R) { if(L>R) return INF; if(l>=L&&r<=R) { return mn[k]; } // fprintf(stderr,"k = %d , l = %d , r = %d L = %d R = %d\n",k,l,r,L,R); if(!k) exit(0); int mid=l+r>>1; if(L>mid) return query(k<<1|1,mid+1,r,L,R); else if(R<=mid) return query(k<<1,l,mid,L,R); else return min(query(k<<1,l,mid,L,mid),query(k<<1|1,mid+1,r,mid+1,R)); } int main() { ios::sync_with_stdio(false); cin>>n>>k; rep(i,1,n) cin>>a[i]; rep(i,1,n) p[a[i]]=i; rep(i,0,MXN*4-1) mn[i]=INF; for(int i=n;i;--i) { int ret=query(1,1,n,p[i]+1,min(p[i]+k-1,n)); if(ret!=INF) G[p[i]].push_back(p[ret]),deg[p[ret]]++; ret = INF; ret = query(1,1,n,max(1,p[i]-k+1),p[i]-1); if(ret!=INF) G[p[i]].push_back(p[ret]),deg[p[ret]]++; update(1,1,n,p[i],i); } priority_queue<int,vector<int>,greater<int> > Q; rep(i,1,n) if(!deg[i]) Q.push(i); while(!Q.empty()) { int now = Q.top(); Q.pop(); A[now]=++cnt; for(auto w:G[now]) { if(!--deg[w]) Q.push(w); } } rep(i,1,n) cout<<A[i]<<endl; }
codenet
-1
1,528
s395061063
p04052
1,589,905,695
cpp
Accepted
443
19,072
#include<cstdio> #include<cstring> #define N 1048576 inline int min(int x,int y){return x<y?x:y;} inline int max(int x,int y){return x<y?y:x;} char G[3500000],*H=G; inline int read() { int x=0; while(*H<48||57<*H)H++; while(47<*H&&*H<58)x=(x<<1)+(x<<3)+(*H++^48); return x; } int S[6],T; inline void print(int x) { int y; do y=x/10,S[T++]=x-y*10,x=y;while(x); while(T)*H++=S[--T]^48; } int a[N],b[N],g[N],h[N],i,j,k,m,n,o; inline void up(int u){g[u]=min(g[u<<1],g[u<<1|1]);} inline void add(int u,int v){g[u]+=v,h[u]+=v;} inline void down(int u){if(h[u])add(u<<1,h[u]),add(u<<1|1,h[u]),h[u]=0;} inline void push(int u){for(j=o;j;j--)down(u>>j);} inline void add(int u,int v,int w) { for(push(u+=m-1),push(v+=m+1);u^v^1;up(u>>=1),up(v>>=1)) { if(~u&1)add(u^1,w); if(v&1)add(v^1,w); } while(u>>=1)up(u); } inline int query() { int u=1; for(;u<m;u=u<<1|(g[u<<1]>0))down(u); return u^m; } inline void add(int u){while(u<=n)h[u]++,u+=u&-u;} inline void del(int u){while(u<=n)h[u]--,u+=u&-u;} inline int sum(int u) { int x=0; while(u)x+=h[u],u^=u&-u; return x; } int main() { fread(G,1,3500000,stdin),n=read(),k=read(); for(i=1;i<=n;i++)b[a[i]=read()]=i; for(m=1;m<n+2;m<<=1)o++; for(i=1;i<=n;i++)g[m|b[i]]=sum(min(n,b[i]+k-1))-sum(max(0,b[i]-k)),add(b[i]); for(i=g[m]=n+1,memset(h+1,0,n<<2);i<m;i++)g[m|i]=n+1; for(i=m,H=G;--i;)up(i); for(i=1;i<=n;i++)a[b[i]=query()]=i,add(b[i],b[i],n+1),add(max(1,b[i]-k+1),min(n,b[i]+k-1),-1); for(i=1;i<=n;*H++='\n',i++)print(a[i]); return 0&fwrite(G,1,H-G,stdout); }
codenet
-1
1,529
s786401248
p04052
1,537,129,857
cpp
Accepted
463
27,764
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; #define MAXN 500010 int a[MAXN],n,k,ans[MAXN]; int t[MAXN*4]; int Query(int i,int l,int r,int L,int R) { if(L>R) return t[0]; if(L<=l&&r<=R) return t[i]; int mid=(l+r)/2; if(R<=mid) return Query(i<<1,l,mid,L,R); if(L>mid) return Query(i<<1|1,mid+1,r,L,R); return min(Query(i<<1,l,mid,L,R),Query(i<<1|1,mid+1,r,L,R)); } void Change(int i,int l,int r,int p,int w) { if(l==r) { t[i]=w; return; } int mid=(l+r)/2; if(p<=mid) Change(i<<1,l,mid,p,w); else Change(i<<1|1,mid+1,r,p,w); t[i]=min(t[i<<1|1],t[i<<1]); } int Adj[MAXN],V[MAXN*2],nxt[MAXN*2],c,d[MAXN]; void AddEdge(int u,int v) { d[v]++;c++;V[c]=v,nxt[c]=Adj[u];Adj[u]=c; } priority_queue<int ,vector<int>,greater<int> >Q; void TopSort() { for(int i=1;i<=n;i++) if(!d[i]) Q.push(i); int tot=0; while(!Q.empty()) { int u=Q.top();Q.pop(); ans[u]=++tot; for(int i=Adj[u];i;i=nxt[i]) if(!--d[V[i]]) Q.push(V[i]); } } int main() { // freopen("swap.in","r",stdin); // freopen("swap.out","w",stdout); scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); a[x]=i; } if(k<=1) { for(int i=1;i<=n;i++) printf("%d\n",i); return 0; } memset(t,0x3f3f3f3f,sizeof t); for(int i=n;i;i--) { int x=Query(1,1,n,a[i]+1,min(a[i]+k-1,n)); if(x<1e9) AddEdge(a[i],a[x]); x=Query(1,1,n,max(a[i]-k+1,1),a[i]-1); if(x<1e9) AddEdge(a[i],a[x]); Change(1,1,n,a[i],i); } TopSort(); for(int i=1;i<=n;i++) printf("%d\n",ans[i]); }
codenet
-1
1,530
s897220081
p04052
1,542,489,072
cpp
Accepted
631
44,416
#include <bits/stdc++.h> using namespace std; const int N = 500005; int n, m, a[N]; int seg[N * 4]; void update(int k, int x, int i = 1, int l = 1, int r = n) { if (l == r) { seg[i] = x; return; } int mid = (l + r) >> 1; if (k <= mid) update(k, x, i << 1, l, mid); else update(k, x, i << 1 | 1, mid + 1, r); seg[i] = max(seg[i << 1], seg[i << 1 | 1]); } int query(int lf, int rg, int i = 1, int l = 1, int r = n) { if (lf > rg) return 0; if (lf <= l && r <= rg) return seg[i]; int res = 0; int mid = (l + r) >> 1; if (lf <= mid) res = max(res, query(lf, rg, i << 1, l, mid)); if (rg > mid) res = max(res, query(lf, rg, i << 1 | 1, mid + 1, r)); return res; } int ind[N], lst[N], ans[N]; vector<int> G[N]; priority_queue<int> que; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[x] = i; } for (int i = 1; i <= n; i++) { int j; j = query(max(1, a[i] - m + 1), a[i] - 1); if (j) G[a[i]].push_back(a[j]), ind[a[j]]++; j = query(a[i] + 1, min(n, a[i] + m - 1)); if (j) G[a[i]].push_back(a[j]), ind[a[j]]++; update(a[i], i); } for (int u = 1; u <= n; u++) if (!ind[u]) que.push(u); int cnt = 0; while (!que.empty()) { int u = que.top(); lst[++cnt] = u; que.pop(); for (int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i]; ind[v]--; if (!ind[v]) que.push(v); } } reverse(lst + 1, lst + 1 + n); for (int i = 1; i <= n; i++) ans[lst[i]] = i; for (int i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,536
s897220081
p04052
1,542,489,072
cpp
Accepted
631
44,416
#include <bits/stdc++.h> using namespace std; const int N = 500005; int n, m, a[N]; int seg[N * 4]; void update(int k, int x, int i = 1, int l = 1, int r = n) { if (l == r) { seg[i] = x; return; } int mid = (l + r) >> 1; if (k <= mid) update(k, x, i << 1, l, mid); else update(k, x, i << 1 | 1, mid + 1, r); seg[i] = max(seg[i << 1], seg[i << 1 | 1]); } int query(int lf, int rg, int i = 1, int l = 1, int r = n) { if (lf > rg) return 0; if (lf <= l && r <= rg) return seg[i]; int res = 0; int mid = (l + r) >> 1; if (lf <= mid) res = max(res, query(lf, rg, i << 1, l, mid)); if (rg > mid) res = max(res, query(lf, rg, i << 1 | 1, mid + 1, r)); return res; } int ind[N], lst[N], ans[N]; vector<int> G[N]; priority_queue<int> que; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[x] = i; } for (int i = 1; i <= n; i++) { int j; j = query(max(1, a[i] - m + 1), a[i] - 1); if (j) G[a[i]].push_back(a[j]), ind[a[j]]++; j = query(a[i] + 1, min(n, a[i] + m - 1)); if (j) G[a[i]].push_back(a[j]), ind[a[j]]++; update(a[i], i); } for (int u = 1; u <= n; u++) if (!ind[u]) que.push(u); int cnt = 0; while (!que.empty()) { int u = que.top(); lst[++cnt] = u; que.pop(); for (int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i]; ind[v]--; if (!ind[v]) que.push(v); } } reverse(lst + 1, lst + 1 + n); for (int i = 1; i <= n; i++) ans[lst[i]] = i; for (int i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,536
s409429183
p04052
1,507,586,144
cpp
Accepted
484
65,920
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> using namespace std; int n,k,cnt=-1; int p[500010],mx[2000010],pos[500010],ans[500010]; vector<int>edge[500010]; void modify(int x,int l,int r,int pos,int v) { if(l==r) { mx[x]=v; return; } int mid=(l+r)>>1; if(pos<=mid) modify(x<<1,l,mid,pos,v); else modify(x<<1|1,mid+1,r,pos,v); mx[x]=max(mx[x<<1],mx[x<<1|1]); } int query(int x,int l,int r,int a,int b) { if(a<=l&&r<=b) return mx[x]; int mid=(l+r)>>1,res=0; if(a<=mid) res=max(res,query(x<<1,l,mid,a,b)); if(b>mid) res=max(res,query(x<<1|1,mid+1,r,a,b)); return res; } void dfs(int x) { ans[++cnt]=x; sort(edge[x].begin(),edge[x].end()); for(int i=0; i<(int)edge[x].size(); i++) dfs(edge[x][i]); } inline int read() { int x=0; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x; } int main() { n=read(),k=read(); for(int i=1; i<=n; i++) { p[i]=read(); pos[p[i]]=i; } pos[n+1]=n+1; for(int i=1; i<=n; i++) { int id=query(1,1,n,pos[i]+1,pos[i]+k-1); if(!id) id=n+1; edge[pos[id]].push_back(pos[i]); modify(1,1,n,pos[i],i); } dfs(n+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,539
s130755816
p04052
1,537,131,689
cpp
Accepted
670
47,104
#include<bits/stdc++.h> #define pb push_back using namespace std; const int N=5e5+10; const int inf=1e9; int gi() { int x=0,o=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*o; } int n,k,a[N],du[N],b[N],ans[N]; vector<int> E[N]; int tr[N<<2]; #define ls (o<<1) #define rs (o<<1|1) void update(int o,int l,int r,int p,int v) { if(l==r) { tr[o]=min(tr[o],v);return; } int mid=(l+r)>>1; p<=mid?update(ls,l,mid,p,v):update(rs,mid+1,r,p,v); tr[o]=min(tr[ls],tr[rs]); } int query(int o,int l,int r,int L,int R) { if(L>R) return inf; if(L<=l&&r<=R) return tr[o]; int mid=(l+r)>>1,ret=inf; if(L<=mid) ret=min(ret,query(ls,l,mid,L,R)); if(R>mid) ret=min(ret,query(rs,mid+1,r,L,R)); return ret; } int main() { cin>>n>>k; for(int i=1;i<=n;i++) a[gi()]=i; memset(tr,0x3f,sizeof(tr)); for(int i=n;i;i--) { int p=query(1,1,n,a[i]+1,min(n,a[i]+k-1)); if(p<=n) E[a[p]].pb(a[i]),du[a[i]]++; p=query(1,1,n,max(1,a[i]-k+1),a[i]-1); if(p<=n) E[a[p]].pb(a[i]),du[a[i]]++; update(1,1,n,a[i],i); } priority_queue<int> q; int now=n; for(int i=1;i<=n;i++) if(!du[i]) q.push(i); while(!q.empty()) { int u=q.top();q.pop();b[now--]=u; for(int v:E[u]) if(!--du[v]) q.push(v); } for(int i=1;i<=n;i++) ans[b[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0; } //orzgzy //鸡贼明年进队超稳
codenet
-1
1,541
s194209485
p04052
1,580,773,333
cpp
Accepted
505
27,764
#include <bits/stdc++.h> #define segc int M = (L + R - 1) >> 1, lc = id << 1, rc = lc | 1 const int N = 500054, M = N * 2; int n, K, E = 0; int a[N], b[N]; int to[M], first[N], next[M], deg[N]; int x[N * 4]; std::priority_queue <int> pq; inline void up(int &x, const int y) {x < y ? x = y : 0;} inline int min(const int x, const int y) {return x < y ? x : y;} inline int max(const int x, const int y) {return x < y ? y : x;} inline void addedge(int u, int v) {to[++E] = v, next[E] = first[u], first[u] = E, ++deg[v];} void adj(int id, int L, int R, int h, int v) { if (L == R) {x[id] = v; return;} segc; h <= M ? adj(lc, L, M, h, v) : adj(rc, M + 1, R, h, v); x[id] = max(x[lc], x[rc]); } int range(int id, int L, int R, int ql, int qr) { if (ql > qr) return 0; if (ql <= L && R <= qr) return x[id]; segc, s = 0; if (ql <= M) up(s, range(lc, L, M, ql, qr)); if (qr > M) up(s, range(rc, M + 1, R, ql, qr)); return s; } int main() { int i, j, x, y; scanf("%d%d", &n, &K), --K; for (i = 1; i <= n; ++i) scanf("%d", a + i), b[a[i]] = i; for (i = 1; i <= n; ++i) { if (x = range(1, 1, n, max(b[i] - K, 1), b[i] - 1)) addedge(b[i], b[x]); if (x = range(1, 1, n, b[i] + 1, min(b[i] + K, n))) addedge(b[i], b[x]); adj(1, 1, n, b[i], i); } for (i = 1; i <= n; ++i) if (!deg[i]) pq.emplace(i); for (i = n; i; --i) { b[i] = x = pq.top(), pq.pop(); for (j = first[x]; j; j = next[j]) if (!--deg[y = to[j]]) pq.emplace(y); } for (i = 1; i <= n; ++i) a[b[i]] = i; for (i = 1; i <= n; ++i) printf("%d\n", a[i]); return 0; }
codenet
-1
1,550
s866398113
p04052
1,590,352,833
cpp
Accepted
336
40,704
// High above the clouds there is a rainbow... #include<bits/stdc++.h> #define F first #define S second #define PB push_back #define sz(s) int((s).size()) #define bit(n,k) (((n)>>(k))&1) using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; const int maxn=5e5+10,inf=1e9; int n,pos[maxn]; vector<int>v[maxn]; int val[2*maxn]; void add(int pos,int x){ pos+=n; val[pos]=x; for(pos>>=1; pos>0 ; pos>>=1) val[pos]= min(val[2*pos], val[2*pos+1]); } int ask(int f,int s){ int ans=inf; for(f+=n,s+=n; f<s; f>>=1, s>>=1){ if(f&1) ans=min(ans,val[f++]); if(s&1) ans=min(ans,val[--s]); } return ans; } priority_queue<int,vector<int>,greater<int> >pq; int SZ[maxn]; void adde(int a,int b){ v[a].PB(b); SZ[b]++; } int ANS[maxn]; int main(){ ios_base::sync_with_stdio(false);cin.tie(0); int k;cin>>n>>k; for(int i=0;i<n;i++){ int x;cin>>x; pos[--x]=i; } fill(val,val+2*maxn,inf); for(int i=n-1;i>=0;i--){ int num=ask(pos[i],min(pos[i]+k,n)); if(num!=inf) adde(pos[i],pos[num]); num=ask( max(int(0),pos[i]-k+1), pos[i] ); if(num!=inf) adde(pos[i],pos[num]); add(pos[i],i); } for(int i=0;i<n;i++){ if(SZ[i]==0) pq.push(i); } int C=0; while(sz(pq)){ int u=pq.top(); pq.pop(); ANS[u]=C++; for(int y:v[u]){ if(--SZ[y]==0) pq.push(y); } } for(int i=0;i<n;i++){ cout<<ANS[i]+1<<"\n"; } return 0; } // Deathly mistakes: // * Read the problem curfully. // * Check maxn. // * Overflows.
codenet
-1
1,561
s866398113
p04052
1,590,352,833
cpp
Accepted
336
40,704
// High above the clouds there is a rainbow... #include<bits/stdc++.h> #define F first #define S second #define PB push_back #define sz(s) int((s).size()) #define bit(n,k) (((n)>>(k))&1) using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; const int maxn=5e5+10,inf=1e9; int n,pos[maxn]; vector<int>v[maxn]; int val[2*maxn]; void add(int pos,int x){ pos+=n; val[pos]=x; for(pos>>=1; pos>0 ; pos>>=1) val[pos]= min(val[2*pos], val[2*pos+1]); } int ask(int f,int s){ int ans=inf; for(f+=n,s+=n; f<s; f>>=1, s>>=1){ if(f&1) ans=min(ans,val[f++]); if(s&1) ans=min(ans,val[--s]); } return ans; } priority_queue<int,vector<int>,greater<int> >pq; int SZ[maxn]; void adde(int a,int b){ v[a].PB(b); SZ[b]++; } int ANS[maxn]; int main(){ ios_base::sync_with_stdio(false);cin.tie(0); int k;cin>>n>>k; for(int i=0;i<n;i++){ int x;cin>>x; pos[--x]=i; } fill(val,val+2*maxn,inf); for(int i=n-1;i>=0;i--){ int num=ask(pos[i],min(pos[i]+k,n)); if(num!=inf) adde(pos[i],pos[num]); num=ask( max(int(0),pos[i]-k+1), pos[i] ); if(num!=inf) adde(pos[i],pos[num]); add(pos[i],i); } for(int i=0;i<n;i++){ if(SZ[i]==0) pq.push(i); } int C=0; while(sz(pq)){ int u=pq.top(); pq.pop(); ANS[u]=C++; for(int y:v[u]){ if(--SZ[y]==0) pq.push(y); } } for(int i=0;i<n;i++){ cout<<ANS[i]+1<<"\n"; } return 0; } // Deathly mistakes: // * Read the problem curfully. // * Check maxn. // * Overflows.
codenet
-1
1,561
s663285849
p04052
1,587,877,272
cpp
Accepted
621
27,384
#include <bits/stdc++.h> using namespace std; const int maxn = 500010; int n, p[maxn], ip[maxn], mx[maxn << 2], res[maxn], k, _; int deg[maxn], l[maxn], e; priority_queue<int> pq; struct Edge { int v, x; } E[maxn << 1]; void update(int p, int v, int l, int r, int rt) { mx[rt] = max(mx[rt], v); if (l == r) return; int m = (l + r) >> 1; if (p <= m) update(p, v, l, m, rt<<1); else update(p, v, m+1, r, rt<<1|1); mx[rt] = max(mx[rt<<1], mx[rt<<1|1]); } int query(int L, int R, int l, int r, int rt) { if (L <= l && r <= R) return mx[rt]; int m = (l + r) >> 1; int ret = 0; if (L <= m) ret = max(ret, query(L, R, l, m, rt<<1)); if (R > m) ret = max(ret, query(L, R, m+1, r, rt<<1|1)); return ret; } inline void addEdge(int u, int v) { E[e].v = v, E[e].x = l[u], l[u] = e++; } int main() { memset(l, -1, sizeof(l)); scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); ip[p[i]] = i; } for (int i = 1; i <= n; i++) { { int x = query(max(1, ip[i] - k + 1), ip[i], 1, n, 1); if (x) { addEdge(ip[i], ip[x]); ++ deg[ip[x]]; } } { int x = query(ip[i], min(n, ip[i] + k - 1), 1, n, 1); if (x) { addEdge(ip[i], ip[x]); ++ deg[ip[x]]; } } update(ip[i], i, 1, n, 1); } for (int i = 1; i <= n; i++) if (!deg[i]) pq.push(i); while (!pq.empty()) { int u = pq.top(); pq.pop(); res[u] = n - (_ ++); for (int p = l[u]; p >= 0; p = E[p].x) { int v = E[p].v; -- deg[v]; if (!deg[v]) pq.push(v); } } for (int i = 1; i <= n; i++) printf("%d\n", res[i]); return 0; }
codenet
-1
1,562
s947986686
p04052
1,538,966,611
cpp
Accepted
1,050
42,624
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<queue> #define next Next #define begin Begin using namespace std; typedef long long ll; const int maxn=500000+10; inline int read(){ int x; char c; int f=1; while((c=getchar())!='-' && (c<'0' || c>'9')); if(c=='-') c=getchar(),f=-1; x=c^'0'; while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0'); return x*f; } priority_queue<int> q; inline ll readll(){ ll x; char c; ll f=1; while((c=getchar())!='-' && (c<'0' || c>'9')); if(c=='-') c=getchar(),f=-1; x=c^'0'; while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0'); return x*f; } int begin[maxn],next[maxn<<1],to[maxn<<1],e; void add(int x,int y){ to[++e]=y; next[e]=begin[x]; begin[x]=e; } int d[maxn]; int p[maxn],a[maxn]; set<int> st1,st2; int main(){ int n=read(),k=read(); for(int i=1;i<=n;i++){ a[i]=read(); p[a[i]]=i; } for(int i=1;i<k-1;i++) st1.insert(a[i]); for(int i=1;i<=n;i++){ if(i+k-1<=n) st1.insert(a[i+k-1]); st1.erase(a[i-1]); st2.insert(a[i]); if(i>k) st2.erase(a[i-k]); set<int>::iterator it=st1.upper_bound(a[i]); if(it!=st1.end()) add(p[*it],i),d[i]++; it=st2.upper_bound(a[i]); if(it!=st2.end()) add(p[*it],i),d[i]++; } for(int i=1;i<=n;i++) if(!d[i]) q.push(i); int ans=0; while(!q.empty()){ int u=q.top(); q.pop(); a[u]=n-ans; ans++; for(int i=begin[u];i;i=next[i]){ d[to[i]]--; if(!d[to[i]]) q.push(to[i]); } } for(int i=1;i<=n;i++) printf("%d\n",a[i]); return 0; }
codenet
-1
1,564
s660943116
p04052
1,567,219,338
cpp
Accepted
630
44,544
#include<bits/stdc++.h> using namespace std; #define M 500005 int n,k,cnt; int a[M],p[M],deg[M],ans[M],minn[M<<2]; vector <int> vec[M]; priority_queue <int> q; void tpsort(){ for(int i=1; i<=n; i++) if(!deg[i]) q.push(-i); while(!q.empty()){ int x=-q.top(); q.pop(); a[++cnt]=x; for(int i=0; i<vec[x].size(); i++) if(--deg[vec[x][i]]==0) q.push(-vec[x][i]); } } void build(int k,int l,int r){ minn[k]=0x3f3f3f; if(l==r) return ; int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); } void modify(int k,int l,int r,int x,int v){ if(l==r) { minn[k]=min(minn[k],v);return ;} int mid=(l+r)>>1; if(x<=mid) modify(k<<1,l,mid,x,v); if(x>mid) modify(k<<1|1,mid+1,r,x,v); minn[k]=min(minn[k<<1],minn[k<<1|1]); } int query(int k,int l,int r,int x,int y){ if(l>=x&&r<=y) return minn[k]; int mid=(l+r)>>1; int res=0x3f3f3f; if(x<=mid) res=min(res,query(k<<1,l,mid,x,y)); if(y>mid) res=min(res,query(k<<1|1,mid+1,r,x,y)); return res; } void d(){ puts("HH");} int main() { cin >> 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 ;i--){ // 倒序 (并不知道为什么 int t1=query(1,1,n,max(1,p[i]-k+1),p[i]); if(t1<=n) vec[p[i]].push_back(p[t1]), deg[p[t1]]++; int t2=query(1,1,n,p[i],min(n,p[i]+k-1)); if(t2<=n) vec[p[i]].push_back(p[t2]), deg[p[t2]]++; // cout<<i<<" "<<t1<<" "<<t2<<endl; modify(1,1,n,p[i],i); // 在p[i]位置加入i } //for(int i=1; i<=n; i++) cout<<deg[i]<<" "; tpsort(); //拓扑排序 for(int i=1; i<=n; i++) ans[a[i]]=i; for(int i=1; i<=n; i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,565
s158846279
p04052
1,600,265,141
cpp
Accepted
952
15,900
#include<bits/stdc++.h> #define ls (u<<1) #define rs (u<<1|1) #define mid ((l+r)>>1) using namespace std; int n,m; struct node{ int next,to; }a[1500005]; int head[500005],cnt,v[500005],p[500005],minn[2000005],da[500005]; void add(int e,int r){ a[++cnt].next=head[e];a[cnt].to=r;head[e]=cnt; } priority_queue<int>q; inline int read(){ char c=getchar();int s=0; while(c<'0'||c>'9')c=getchar(); while(c>='0'&&c<='9'){ s=s*10+c-'0';c=getchar(); }return s; } inline void update(int u){ if(v[minn[ls]]>v[minn[rs]])minn[u]=minn[ls]; else minn[u]=minn[rs]; } void build(int u,int l,int r){ if(l==r){return (void)(minn[u]=l);} build(ls,l,mid); build(rs,mid+1,r); update(u); } void add(int u,int l,int r,int x){ if(l==r){return (void)(minn[u]=0);} if(x<=mid)add(ls,l,mid,x); else add(rs,mid+1,r,x); update(u); } int query(int u,int l,int r,int L,int R){ if(L>R)return 0; if(L<=l&&R>=r){return minn[u];} int jly=0; int jlx=0; if(L<=mid)jlx=query(ls,l,mid,L,R); if(R>mid)jly=query(rs,mid+1,r,L,R); if(v[jlx]>v[jly])return jlx; else return jly; } int bj[500005]; void pd(int u){ if(bj[u])return ; if(query(1,1,n,max(1,u-m+1),min(u+m-1,n))==u)return (void)(q.push(u),bj[u]=1); } int main(){ n=read();m=read(); for(int i=1;i<=n;i++)v[i]=read(); build(1,1,n);minn[0]=-19191981;bj[0]=1; for(int i=n;i>=1;i--){ pd(i); } for(int i=n;i>=1;i--){ int u=q.top(); q.pop(); add(1,1,n,u); da[u]=i; int x=query(1,1,n,max(1,u-m+1),u-1); pd(x); int y=query(1,1,n,u+1,min(u+m-1,n)); pd(y); } for(int i=1;i<=n;i++){ printf("%d\n",da[i]); } }
codenet
-1
1,567
s974853515
p04052
1,583,866,163
cpp
Accepted
1,652
21,104
#include<bits/stdc++.h> using namespace std; using PII = pair<int, int>; constexpr int maxn = 500000; int P[maxn + 1], A[maxn + 1]; PII T[maxn << 2]; #define ls (v << 1) #define rs (ls | 1) #define tm ((tl + tr) >> 1) void update(int v, int tl, int tr, int p, int x){ if(tl == tr) T[v] = {x, p}; else{ if(p <= tm) update(ls, tl, tm, p, x); if(p > tm) update(rs, tm + 1, tr, p, x); T[v] = max(T[ls], T[rs]); } } PII get_max(int v, int tl, int tr, int L, int R){ if(tl >= L and tr <= R) return T[v]; PII res = {0, -1}; if(L <= tm) res = max(res, get_max(ls, tl, tm, L, R)); if(R > tm) res = max(res, get_max(rs, tm + 1, tr, L, R)); return res; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int N, K; cin >> N >> K; for(int i = 1; i <= N; i += 1){ cin >> P[i]; update(1, 1, N, i, P[i]); } priority_queue<int> q; auto add = [&](const int& i){ if(i <= 0 || (i - 1) * K >= N) return; PII p = get_max(1, 1, N, (i - 1) * K + 1, i * K); if(p.first == 0) return; if(get_max(1, 1, N, p.second - K + 1, p.second + K - 1) != p) return; q.push(p.second); }; for(int i = 1; i <= N; i += K) add((i + K - 1) / K); for(int i = N; i; i -= 1){ while(A[q.top()]) q.pop(); int p = q.top(); A[p] = i; update(1, 1, N, p, 0); for(int j = -1; j <= 1; j += 1) add((p + K - 1) / K + j); } for(int i = 1; i <= N; i += 1) cout << A[i] << "\n"; return 0; }
codenet
-1
1,572
s929013700
p04052
1,533,362,235
cpp
Accepted
576
17,876
#include <bits/stdc++.h> using namespace std; #define SZ(v) ((int)(v).size()) #define ALL(v) (v).begin(),(v).end() #define one first #define two second typedef long long ll; typedef pair<int, int> pi; const int INF = 0x3f2f1f0f; const ll LINF = 1ll * INF * INF; const int MAX_N = 5e5 + 100; struct IDX{ int P; vector<pi> val; IDX(int n = 10) { for(P=1; P<n; P<<=1); val = vector<pi>(2*P, pi(0, 0)); } void update(int v, pi k) { val[v+=P] = k; while(v>>=1) val[v] = max(val[v*2], val[v*2+1]); } pi getMax(int a, int b) { pi res = pi(-1, 0); for(a+=P, b+=P; a<=b; a>>=1, b>>=1) { if(a%2==1) res = max(res, val[a++]); if(b%2==0) res = max(res, val[b--]); } return res; } }; int N, K, Nr[MAX_N]; int Ans[MAX_N]; IDX idx; int main() { cin >> N >> K; idx = IDX(N); for(int i=0; i<N; i++) scanf("%d", &Nr[i]); for(int i=0; i<N; i++) idx.update(i, pi(Nr[i], i)); vector<bool> vis(N, false); priority_queue<int> Q; for(int i=0; i<N; i++) { int v, ix; tie(v, ix) = idx.getMax(max(0, i-K+1), min(N-1, i+K-1)); if(ix == i) Q.emplace(i), vis[i] = true; } int cnt = 0; while(!Q.empty()) { int v = Q.top(); Q.pop(); Ans[v] = N - cnt; cnt++; idx.update(v, pi(-1, v)); Nr[v] = -1; int g = v/K; for(int k=max(0, g-1); k<=min((N-1)/K, g+1); k++) { int w, wix; tie(w, wix) = idx.getMax(k*K, min(N-1, (k+1)*K)); if(vis[wix]) continue; int same; tie(ignore, same) = idx.getMax(max(0, wix-K+1), min(N-1, wix+K-1)); if(same == wix) Q.emplace(wix), vis[wix] = true; } } for(int i=0; i<N; i++) printf("%d\n", Ans[i]); return 0; }
codenet
-1
1,584
s413491795
p04052
1,555,683,238
cpp
Accepted
527
27,384
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #define M 500010 #define ls node<<1 #define rs node<<1|1 #define inf 1e9 using namespace std; int n,num,k,cnt; int p[M],a[M],dig[M],ans[M],head[M],val[M<<2]; struct edge{int next,to;}e[M<<1]; void add(int from,int to) { e[++num]=(edge){head[from],to}; head[from]=num; } priority_queue<int,vector<int>,greater<int> >Q; void build(int node,int l,int r) { val[node]=inf; if(l==r) return; int mid=(l+r)>>1; build(ls,l,mid); build(rs,mid+1,r); } void change(int node,int l,int r,int p,int v) { val[node]=min(val[node],v); if(l==r) return; int mid=(l+r)>>1; if(p<=mid) change(ls,l,mid,p,v); else change(rs,mid+1,r,p,v); } int query(int node,int l,int r,int l1,int r1) { if(l1>r1) return inf; if(l1<=l&&r1>=r) return val[node]; int mid=(l+r)>>1,ans=inf; if(l1<=mid) ans=min(ans,query(ls,l,mid,l1,r1)); if(r1>mid) ans=min(ans,query(rs,mid+1,r,l1,r1)); return ans; } 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++) a[p[i]]=i; build(1,1,n); for(int i=n;i>=1;i--) { int res; res=query(1,1,n,max(1,a[i]-k+1),a[i]-1); if(res!=inf) add(a[i],a[res]),dig[a[res]]++; res=query(1,1,n,a[i]+1,min(a[i]+k-1,n)); if(res!=inf) add(a[i],a[res]),dig[a[res]]++; change(1,1,n,a[i],i); } for(int i=1;i<=n;i++) if(!dig[i]) Q.push(i); while(!Q.empty()) { int x=Q.top();Q.pop();ans[x]=++cnt; for(int i=head[x];i;i=e[i].next) { int to=e[i].to; if((--dig[to])==0) Q.push(to); } } for(int i=1;i<=cnt;i++) printf("%d\n",ans[i]); return 0; }
codenet
-1
1,585
s566240600
p04052
1,468,769,420
cpp
Accepted
3,986
41,088
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- template<class V,int NV> class SegTree_1 { public: vector<V> val; static V const def=1<<25; V comp(V l,V r){ return min(l,r);}; SegTree_1(){val=vector<V>(NV*2,def);}; V getval(int x,int y,int l=0,int r=NV,int k=1) { // x<=i<y if(r<=x || y<=l) return def; if(x<=l && r<=y) return val[k]; return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1)); } void update(int entry, V v) { entry += NV; val[entry]=v; while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1]); } }; SegTree_1<int,1<<20> seg; int N,K; int P[501010]; int R[501010]; int vis[505050]; int fix; void go(int i) { while(1) { int a = seg.getval(0,P[i]); if(a>=i+K) break; go(a); } vis[i]=1; R[i] = ++fix; seg.update(P[i],1<<30); } void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>K; FOR(i,N) { cin>>P[i]; P[i]--; seg.update(P[i],i); } FOR(i,N) if(vis[i]==0) go(i); FOR(i,N) cout<<R[i]<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); solve(); return 0; }
codenet
-1
1,596
s566240600
p04052
1,468,769,420
cpp
Accepted
3,986
41,088
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- template<class V,int NV> class SegTree_1 { public: vector<V> val; static V const def=1<<25; V comp(V l,V r){ return min(l,r);}; SegTree_1(){val=vector<V>(NV*2,def);}; V getval(int x,int y,int l=0,int r=NV,int k=1) { // x<=i<y if(r<=x || y<=l) return def; if(x<=l && r<=y) return val[k]; return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1)); } void update(int entry, V v) { entry += NV; val[entry]=v; while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1]); } }; SegTree_1<int,1<<20> seg; int N,K; int P[501010]; int R[501010]; int vis[505050]; int fix; void go(int i) { while(1) { int a = seg.getval(0,P[i]); if(a>=i+K) break; go(a); } vis[i]=1; R[i] = ++fix; seg.update(P[i],1<<30); } void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>K; FOR(i,N) { cin>>P[i]; P[i]--; seg.update(P[i],i); } FOR(i,N) if(vis[i]==0) go(i); FOR(i,N) cout<<R[i]<<endl; } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); solve(); return 0; }
codenet
-1
1,596
s214149730
p04052
1,508,450,073
cpp
Accepted
607
27,008
#include<queue> #include<cstdio> #include<cstring> #define inf 2e8 #define maxn 500010 #define min(x,y) (x)<(y)?(x):(y) using namespace std; int to[maxn*2],nxt[maxn*2],mn[maxn*4]; int a[maxn],b[maxn],d[maxn]; int first[maxn]; int n,k,cnt; priority_queue<int>q; void add(int x,int y) { to[++cnt]=y; nxt[cnt]=first[x]; first[x]=cnt; d[y]++; } void change(int x,int l,int r,int p,int v) { if(l==r) { mn[x]=v; return; } int mid=(l+r)>>1; if(p<=mid) change(x<<1,l,mid,p,v); else change(x<<1|1,mid+1,r,p,v); mn[x]=min(mn[x<<1],mn[x<<1|1]); } int query(int x,int l,int r,int s,int t) { if(s<=l&&r<=t) return mn[x]; int mid=(l+r)>>1,ans=inf; if(s<=mid) ans=min(ans,query(x<<1,l,mid,s,t)); if(t>mid) ans=min(ans,query(x<<1|1,mid+1,r,s,t)); return ans; } int main() { int x; scanf("%d%d",&n,&k); memset(mn,15,sizeof(mn)); for(int i=1;i<=n;i++) scanf("%d",&b[i]),a[b[i]]=i; for(int i=n;i;i--) { x=query(1,1,n,a[i]-k+1,a[i]); if(x<=n) add(a[x],a[i]); x=query(1,1,n,a[i],a[i]+k-1); if(x<=n) add(a[x],a[i]); change(1,1,n,a[i],i); } for(int i=1;i<=n;i++) if(!d[i]) q.push(i); for(int i=n;i;i--) { b[i]=x=q.top(); q.pop(); for(int j=first[x];j;j=nxt[j]) if((--d[to[j]])==0) q.push(to[j]); } for(int i=1;i<=n;i++) a[b[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",a[i]); return 0; }
codenet
-1
1,598
s462627808
p04052
1,527,261,565
cpp
Accepted
628
32,896
#include<bits/stdc++.h> #define MN 500000 #define N 524288 using namespace std; 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*10+ch-'0';ch=getchar();} return x*f; } int n,K,a[MN+5],t[N*2+5],b[MN+5],c[MN+5]; struct Tree{int l,r,val,x;}T[MN*3+5]; inline void Renew(int x,int v){for(t[x+=N]+=v;x>>=1;)t[x]=t[x<<1]+t[x<<1|1];} inline int Query(int l,int r) { int res=0; for(l+=N-1,r+=N+1;l^r^1;l>>=1,r>>=1) { if(~l&1) res+=t[l+1]; if( r&1) res+=t[r-1]; } return res; } void Build(int x,int l,int r) { if((T[x].l=l)==(T[x].r=r)) {T[x].x=b[l];return;} int mid=l+r>>1; Build(x<<1,l,mid);Build(x<<1|1,mid+1,r); T[x].x=min(T[x<<1].x,T[x<<1|1].x); } inline void Mark(int x,int v){T[x].x+=v;T[x].val+=v;} inline void pushdown(int x){if(T[x].val)Mark(x<<1,T[x].val),Mark(x<<1|1,T[x].val),T[x].val=0;} int Find(int x) { if(T[x].l==T[x].r) return T[x].l; pushdown(x); return Find(x<<1|(T[x<<1|1].x<=0)); } void Mod(int x,int l,int r,int v) { if(T[x].l==l&&T[x].r==r) return Mark(x,v); int mid=T[x].l+T[x].r>>1; pushdown(x); if(r<=mid) Mod(x<<1,l,r,v); else if(l>mid) Mod(x<<1|1,l,r,v); else Mod(x<<1,l,mid,v),Mod(x<<1|1,mid+1,r,v); T[x].x=min(T[x<<1].x,T[x<<1|1].x); } int main() { n=read();K=read(); for(int i=1;i<=n;++i) a[read()]=i; for(int i=n;i;--i) b[a[i]]=Query(max(1,a[i]-K+1),min(n,a[i]+K-1)),Renew(a[i],1); Build(1,1,n); for(int i=n;i;--i) { int x=Find(1);c[x]=i; Mod(1,x,x,1e6);Mod(1,max(1,x-K+1),min(n,x+K-1),-1); } for(int i=1;i<=n;++i) printf("%d\n",c[i]); return 0; }
codenet
-1
1,599
s289175974
p04052
1,570,675,860
cpp
Accepted
1,051
14,964
#include<bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define mp make_pair typedef long long LL; typedef unsigned long long uLL; typedef pair<int,int> pii; const int maxn=500111; #define ls (t<<1) #define rs ((t<<1)|1) int p[maxn],q[maxn]; namespace sgt { int mx[1100111]; void modify(int p,int v,int tl,int tr,int t) { if(tl==tr) { mx[t]=v; return; } int mid=(tl+tr)>>1; if(p<=mid)modify(p,v,tl,mid,ls); else modify(p,v,mid+1,tr,rs); mx[t]=max(mx[ls],mx[rs]); } int query(int l,int r,int tl,int tr,int t) { if(l<=tl&&tr<=r)return mx[t]; int mid=(tl+tr)>>1,ret=0; if(l<=mid)ret=max(ret,query(l,r,tl,mid,ls)); if(r>mid)ret=max(ret,query(l,r,mid+1,tr,rs)); return ret; } } int n,k,ans[maxn]; bool inq[maxn]; int test(int v) { int val=sgt::query(max(1,v-k+1),min(n,v+k-1),1,n,1); if(val==p[v])return true; else return false; } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",p+i); q[p[i]]=i; } if(k==1) { for(int i=1;i<=n;i++)printf("%d\n",i); return 0; } priority_queue<int> pq; for(int i=1;i<=n;i++)sgt::modify(i,p[i],1,n,1); for(int i=1;i<=n;i++)if(test(i)) { pq.push(i); inq[i]=1; } for(int i=n;i>=1;i--) { int v=pq.top();pq.pop(); ans[v]=i;sgt::modify(v,0,1,n,1); int mx=q[sgt::query(max(1,v-k+1),v,1,n,1)]; if(mx&&!inq[mx]&&test(mx)) { inq[mx]=1; pq.push(mx); } mx=q[sgt::query(v,min(n,v+k-1),1,n,1)]; if(mx&&!inq[mx]&&test(mx)) { inq[mx]=1; pq.push(mx); } } for(int i=1;i<=n;i++)printf("%d\n",ans[i]); return 0; }
codenet
-1
1,600
s396702267
p04052
1,507,514,123
cpp
Accepted
351
22,260
#include<bits/stdc++.h> using namespace std; template<class T> inline bool chkmax(T& a,T b){return a<b?a=b,1:0;} template<class T> inline bool chkmin(T& a,T b){return a>b?a=b,1:0;} template<typename T> inline T& read(T& x){ static char c; bool flag=0; while(!isdigit(c=getchar())) if(c=='-') flag=1; for(x=c-'0';isdigit(c=getchar());(x*=10)+=c-'0'); if(flag) x=-x; return x; } const int maxn=500010; int n,k,t[maxn],p[maxn],ind[maxn]; int head[maxn],nxt[maxn],to[maxn],e; void ae(int x,int y){ to[++e]=y; nxt[e]=head[x]; head[x]=e; } namespace SGT{ #define ls h<<1 #define rs h<<1|1 #define mid ((l+r)>>1) #define lc l,mid #define rc mid+1,r int T[maxn*3]; void pushup(int h){ T[h]=max(T[ls],T[rs]); } void update(int h,int l,int r,int p,int x){ if(l==r){T[h]=x; return;} if(p<=mid) update(ls,lc,p,x); else update(rs,rc,p,x); pushup(h); } int query(int h,int l,int r,int L,int R){ if(L<=l&&r<=R) return T[h]; int ret=0; if(L<=mid) chkmax(ret,query(ls,lc,L,R)); if(R>mid) chkmax(ret,query(rs,rc,L,R)); return ret; } } using namespace SGT; priority_queue<int,vector<int>,greater<int> > q; int main(){ read(n); read(k); for(int i=1;i<=n;i++) t[read(p[i])]=i; for(int i=1;i<=n;i++){ int pre=query(1,1,n,t[i]+1,t[i]+k-1); update(1,1,n,t[i],i); if(pre) ae(t[pre],t[i]),++ind[t[i]]; } for(int i=1;i<=n;i++) if(!ind[i]) q.push(i); for(int i=1;i<=n;i++){ int u=q.top(); q.pop(); t[i]=u; for(int j=head[u];j;j=nxt[j]){ int v=to[j]; if(!--ind[v]) q.push(v); } } for(int i=1;i<=n;i++) p[t[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",p[i]); return 0; }
codenet
-1
1,602
s396702267
p04052
1,507,514,123
cpp
Accepted
351
22,260
#include<bits/stdc++.h> using namespace std; template<class T> inline bool chkmax(T& a,T b){return a<b?a=b,1:0;} template<class T> inline bool chkmin(T& a,T b){return a>b?a=b,1:0;} template<typename T> inline T& read(T& x){ static char c; bool flag=0; while(!isdigit(c=getchar())) if(c=='-') flag=1; for(x=c-'0';isdigit(c=getchar());(x*=10)+=c-'0'); if(flag) x=-x; return x; } const int maxn=500010; int n,k,t[maxn],p[maxn],ind[maxn]; int head[maxn],nxt[maxn],to[maxn],e; void ae(int x,int y){ to[++e]=y; nxt[e]=head[x]; head[x]=e; } namespace SGT{ #define ls h<<1 #define rs h<<1|1 #define mid ((l+r)>>1) #define lc l,mid #define rc mid+1,r int T[maxn*3]; void pushup(int h){ T[h]=max(T[ls],T[rs]); } void update(int h,int l,int r,int p,int x){ if(l==r){T[h]=x; return;} if(p<=mid) update(ls,lc,p,x); else update(rs,rc,p,x); pushup(h); } int query(int h,int l,int r,int L,int R){ if(L<=l&&r<=R) return T[h]; int ret=0; if(L<=mid) chkmax(ret,query(ls,lc,L,R)); if(R>mid) chkmax(ret,query(rs,rc,L,R)); return ret; } } using namespace SGT; priority_queue<int,vector<int>,greater<int> > q; int main(){ read(n); read(k); for(int i=1;i<=n;i++) t[read(p[i])]=i; for(int i=1;i<=n;i++){ int pre=query(1,1,n,t[i]+1,t[i]+k-1); update(1,1,n,t[i],i); if(pre) ae(t[pre],t[i]),++ind[t[i]]; } for(int i=1;i<=n;i++) if(!ind[i]) q.push(i); for(int i=1;i<=n;i++){ int u=q.top(); q.pop(); t[i]=u; for(int j=head[u];j;j=nxt[j]){ int v=to[j]; if(!--ind[v]) q.push(v); } } for(int i=1;i<=n;i++) p[t[i]]=i; for(int i=1;i<=n;i++) printf("%d\n",p[i]); return 0; }
codenet
-1
1,602
s235293787
p04052
1,534,823,300
cpp
Accepted
448
30,848
#include <iostream> #include <set> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXN = 500005; struct Edge{ int to,next; }e[MAXN<<1]; int p[MAXN],q[MAXN],n,head[MAXN],in[MAXN],ord[MAXN],val[MAXN],k; set<int>ele; typedef set<int>::iterator SI; priority_queue<int>pq; int mx[MAXN<<2]; int ql,qr,qval,qpos; void update(int i,int l,int r) { if(l==r)return void(mx[i]=qval); int mid=(l+r)>>1,lc=i<<1,rc=lc|1; if(qpos<=mid)update(lc,l,mid); else update(rc,mid+1,r); mx[i]=max(mx[lc],mx[rc]); } int query(int i,int l,int r) { // cout<<i<<" "<<l<<" "<<r<<endl; if(l>=ql&&r<=qr)return mx[i]; int mid=(l+r)>>1,lc=i<<1,rc=lc|1; if(qr<=mid)return query(lc,l,mid); else if(ql>mid)return query(rc,mid+1,r); else return max(query(lc,l,mid),query(rc,mid+1,r)); } int cnt; inline void insert(int u,int v) { e[++cnt].to=v,e[cnt].next=head[u],head[u]=cnt; } 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){ ql=max(q[i]-k+1,1),qr=q[i]; // cout<<ql<<" "<<qr<<endl; int x=query(1,1,n); if(x)insert(q[i],q[x]),++in[q[x]]; ql=q[i],qr=min(q[i]+k-1,n); // cout<<ql<<" "<<qr<<endl; x=query(1,1,n); if(x)insert(q[i],q[x]),++in[q[x]]; qpos=q[i],qval=i; // cout<<qpos<<" "<<qval<<endl; update(1,1,n); } for(int i=1;i<=n;++i) if(!in[i])pq.push(i); for(int i=n;i;--i){ ord[i]=pq.top(); pq.pop(); for(int j=head[ord[i]];j;j=e[j].next){ int v=e[j].to; if(!--in[v]){ pq.push(v); } } } for(int i=1;i<=n;++i)val[ord[i]]=i; for(int i=1;i<=n;++i)printf("%d\n",val[i]); return 0; }
codenet
-1
1,606
s998752662
p04052
1,570,581,391
cpp
Accepted
751
46,964
#include<bits/stdc++.h> typedef long long ll; ll gi(){ ll x=0,f=1; char ch=getchar(); while(!isdigit(ch))f^=ch=='-',ch=getchar(); while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f?x:-x; } int P[500010]; struct segtree{ int A[2000010],tag[2000010]; #define mid ((l+r)>>1) void Upd(int x,int o){tag[x]=A[x]=o;} void down(int x){if(tag[x])Upd(x<<1,tag[x]),Upd(x<<1|1,tag[x]),tag[x]=0;} void update(int x,int l,int r,int L,int R,int d){ if(L<=l&&r<=R)return Upd(x,d); down(x); if(L<=mid)update(x<<1,l,mid,L,R,d); if(mid<R)update(x<<1|1,mid+1,r,L,R,d); } int query(int x,int l,int r,int p){ if(l==r)return A[x]; down(x); if(p<=mid)return query(x<<1,l,mid,p); else return query(x<<1|1,mid+1,r,p); } #undef mid }A,B; int fir[500010],dis[1000010],nxt[1000010],id,in[500010]; void link(int a,int b){++in[b];nxt[++id]=fir[a],fir[a]=id,dis[id]=b;} int p[500010],tpn[500010]; struct cmp{bool operator()(int a,int b){return P[a]>P[b];}}; std::priority_queue<int,std::vector<int>,cmp>que; int main(){ #ifdef XZZSB freopen("in.in","r",stdin); freopen("out.out","w",stdout); #endif int n=gi(),K=gi()-1; for(int i=1;i<=n;++i)P[gi()]=i; for(int i=n,_;i;--i){ if(_=A.query(1,1,n,P[i]))link(i,_); if(_=B.query(1,1,n,P[i]))link(i,_); A.update(1,1,n,P[i],std::min(n,P[i]+K),i); B.update(1,1,n,std::max(1,P[i]-K),P[i],i); } for(int i=1;i<=n;++i)if(!in[i])que.push(i); for(int _=1,x;_<=n;++_){ tpn[_]=P[x=que.top()];que.pop(); for(int i=fir[x];i;i=nxt[i])if(!--in[dis[i]])que.push(dis[i]); } for(int i=1;i<=n;++i)p[tpn[i]]=i; for(int i=1;i<=n;++i)printf("%d\n",p[i]); return 0; }
codenet
-1
1,613
s704261737
p04052
1,584,592,330
cpp
Accepted
1,038
11,392
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #include <random> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define sz(x) ((int)(x).size()) typedef vector<int> vi; typedef long long ll; typedef pair<int, int> pii; typedef double db; mt19937 mrand(random_device{}()); const ll mod = 1000000007; int rnd(int x) { return mrand() % x; } ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a%mod; a = a * a%mod; }return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } // head const int MAXN = 500000+5; int N,K; int P[MAXN]; int pos[MAXN]; int temp[MAXN]; int minp[MAXN]; void solve(int l, int r) { if(l==r) return; int mid = (l+r)>>1; solve(l,mid); solve(mid+1,r); rep(i,l,r+1) temp[i] = pos[i]; minp[mid] = temp[mid]; per(i,l,mid) minp[i] = min(minp[i+1], temp[i]); int p = l; int i = l, j = mid+1; while(i<=mid && j<=r) { if(minp[i]-temp[j]<K) pos[p++] = temp[i++]; else pos[p++] = temp[j++]; } while(i<=mid) pos[p++] = temp[i++]; while(j<=r) pos[p++] = temp[j++]; } int main() { ios::sync_with_stdio(false); cin.tie(0); #ifdef RG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin>>N>>K; rep(i,0,N) cin>>P[i]; rep(i,0,N) pos[P[i]] = i; solve(1,N); rep(i,1,N+1) P[pos[i]] = i; rep(i,0,N) cout<<P[i]<<endl; return 0; }
codenet
-1
1,619
s905187106
p04052
1,468,863,057
cpp
Accepted
2,536
19,308
#include <bits/stdc++.h> #define FO(i,a,b) for (int i = (a); i < (b); i++) #define sz(v) int(v.size()) using namespace std; int n, k; int o[500005], a[500005]; int rt[1000005], rtt[1000005], T; bool rpv[500005]; bool qs(int l, int r) { l = max(l,0); r = min(r,n); for (l += n, r += n; l < r; l /= 2, r /= 2) { if (l&1) if (rtt[l++] == T) return true; if (r&1) if (rtt[--r] == T) return true; } return false; } void up(int i) { for (i += n; i; i /= 2) rtt[i] = T; } void go(int i, int j) { if (i == j) return; else { int pv = i + rand() % (j-i); T++; up(a[pv]); for (int t = pv-1; t >= i; t--) { rpv[t] = qs(a[t]-k+1, a[t]+k); if (rpv[t]) up(a[t]); } T++; up(a[pv]); for (int t = pv+1; t < j; t++) { rpv[t] = qs(a[t]-k+1, a[t]+k); if (rpv[t]) up(a[t]); } vector<int> l, r; FO(t,i,pv) { if (rpv[t] || a[t] < a[pv]) l.push_back(a[t]); else r.push_back(a[t]); } FO(t,pv+1,j) { if (rpv[t] || a[t] > a[pv]) r.push_back(a[t]); else l.push_back(a[t]); } int apv = a[pv]; int t = i; for (int x : l) a[t++] = x; int md = t; a[t++] = apv; for (int x : r) a[t++] = x; assert(t == j); go(i,md); go(md+1,j); } } int main() { scanf("%d %d", &n, &k); FO(i,0,n) { scanf("%d", o+i); o[i]--; a[o[i]] = i; } go(0,n); FO(i,0,n) o[a[i]] = i+1; FO(i,0,n) printf("%d\n", o[i]); }
codenet
-1
1,620
s776419343
p04052
1,554,760,387
cpp
Accepted
641
52,352
#include <bits/stdc++.h> using namespace std; template <class I> I chmin(I &x, I y) { return x > y ? x = y : x; } const int MaxN(500003); const int INF(0x3f3f3f3f); int p[MaxN], q[MaxN], d[MaxN]; vector<int> G[MaxN]; #define lc rt << 1 #define rc rt << 1 | 1 int Seg[MaxN << 2]; void Build(int rt, int l, int r) { Seg[rt] = INF; if(l == r) return; int mid = (l + r) >> 1; Build(lc, l, mid); Build(rc, mid + 1, r); } void Modify(int rt, int l, int r, int pos, int v) { chmin(Seg[rt], v); if(l == r) return; int mid = (l + r) >> 1; if(pos <= mid) Modify(lc, l, mid, pos, v); else Modify(rc, mid + 1, r, pos, v); } int Query(int rt, int l, int r, int ql, int qr) { if(ql <= l && r <= qr) return Seg[rt]; int mid = (l + r) >> 1; int res = INF; if(ql <= mid) chmin(res, Query(lc, l, mid, ql, qr)); if(qr > mid) chmin(res, Query(rc, mid + 1, r, ql, qr)); return res; } #undef lc #undef rc int main() { int n, K; 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 = n, x; i; i--) { x = Query(1, 1, n, max(q[i] - K + 1, 1), q[i]); if(x != INF) G[q[i]].push_back(q[x]), ++d[q[x]]; x = Query(1, 1, n, q[i], min(q[i] + K - 1, n)); if(x != INF) G[q[i]].push_back(q[x]), ++d[q[x]]; Modify(1, 1, n, q[i], i); } set<int> Q; for(int i = 1; i <= n; i++) if(!d[i]) Q.insert(i); for(int i = 1; i <= n; i++) { int u = q[i] = *Q.begin(); Q.erase(Q.begin()); for(auto v : G[u]) if(!--d[v]) Q.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,622
s339811848
p04052
1,596,625,709
cpp
Accepted
664
43,336
#include<bits/stdc++.h> using namespace std; const int N = 5e5+7; int tr[4*N]; void update(int u, int st, int en, int idx, int x) { if (st == en) tr[u] = x; else { int mid = (st+en)/2; if (idx <= mid) update(2*u, st, mid, idx, x); else update(2*u+1, mid+1, en, idx, x); tr[u] = max(tr[2*u], tr[2*u+1]); } } int query(int u, int st, int en, int l, int r) { if (r<st || en<l) return 0; else if (l<=st && en<=r) return tr[u]; else { int mid = (st+en)/2; return max(query(2*u, st, mid, l, r), query(2*u+1, mid+1, en, l, r)); } } int a[N], b[N], out[N]; vector<int> in[N]; void add(int u, int v) { out[u]++; in[v].push_back(u); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin>>n>>k; for (int i=1; i<=n; i++) { cin>>a[i]; b[a[i]] = i; } for (int i=1; i<=n; i++) { int l1 = query(1,1,n,b[i]-k+1,b[i]); int l2 = query(1,1,n,b[i],b[i]+k-1); if (l1) add(b[l1], b[i]); if (l2) add(b[l2], b[i]); update(1,1,n,b[i],i); } vector<int> ans; priority_queue<int> pq; for (int i=1; i<=n; i++) if (out[i] == 0) pq.push(i); while (pq.size()) { int u = pq.top(); pq.pop(); ans.push_back(u); for (int v: in[u]) { out[v]--; if (out[v] == 0) pq.push(v); } } reverse(ans.begin(), ans.end()); for (int i=1; i<=n; i++) a[ans[i-1]] = i; for (int i=1; i<=n; i++) cout<<a[i]<<"\n"; }
codenet
-1
1,626
s555477133
p04052
1,490,666,342
cpp
Accepted
734
42,624
// <swap.cpp> - Mon Mar 27 20:17:23 2017 // This file is created by XuYike's black technology automatically. // Copyright (C) 2015 ChangJun High School, Inc. // I don't know what this program is. #include <iostream> #include <vector> #include <set> #include <queue> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> using namespace std; typedef long long lol; #define next NEXT template<typename T> inline void gg(T &res){ res=0;T fh=1;char ch=getchar(); while((ch>'9'||ch<'0')&&ch!='-')ch=getchar(); if(ch=='-')fh=-1,ch=getchar(); while(ch>='0'&&ch<='9')res=res*10+ch-'0',ch=getchar(); res*=fh; } inline int gi(){int x;gg(x);return x;} inline lol gl(){lol x;gg(x);return x;} const int MAXN=500010; const int MAXM=1000010; const int INF=1e9; int bt,b[MAXN],next[MAXM],to[MAXM],d[MAXN]; inline void add(int x,int y){next[++bt]=b[x];b[x]=bt;to[bt]=y;d[y]++;} int p[MAXN],q[MAXN]; set <int> s; set <int>::iterator it; priority_queue <int,vector<int>,greater<int> > r; int main(){ int n=gi(),k=gi(); for(int i=1;i<=n;i++)q[p[i]=gi()]=i; for(int i=1;i<=n;i++){ if(i>k)s.erase(p[i-k]); it=s.insert(p[i]).first; if(++it!=s.end())add(i,q[*it]); } s.clear(); for(int i=n;i;i--){ if(i+k<=n)s.erase(p[i+k]); it=s.insert(p[i]).first; if(++it!=s.end())add(i,q[*it]); } for(int i=1;i<=n;i++)if(!d[i])r.push(i); for(int i=1;i<=n;i++){ int x=r.top();r.pop(); p[x]=i; for(int j=b[x];j;j=next[j])if(!--d[to[j]])r.push(to[j]); } for(int i=1;i<=n;i++)printf("%d\n",p[i]); return 0; }
codenet
-1
1,629
s272182120
p04052
1,588,167,841
cpp
Accepted
543
17,776
//Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God; #include<bits/stdc++.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; const int inf = N; int n, k, cnt; int a[N]; int ans[N]; int ind[N]; int fen[N * 4]; bool mark[N]; priority_queue<int> s; void add(int ind, int val) { for (fen[ind += n] = val; ind >>= 1;) { fen[ind] = max(fen[ind << 1], fen[ind << 1 | 1]); } } int get(int l, int r) { l = max(l, 0); r = min(r, n); int ans = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) { ans = max(ans, fen[l++]); } if (r & 1) { ans = max(ans, fen[--r]); } } return ans; } void isval(int x) { int mx = get(x * k, x * k + k); if (mx == -inf) { return; } int me = ind[mx]; if (a[me] >= get(me - k + 1, me + k) && mark[me] == false) { mark[me] = true; s.push(me); } } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; ind[a[i]] = i; add(i, a[i]); } cnt = (n + k - 1) / k; for (int i = 0; i < cnt; i++) { isval(i); } for (int i = n; i; i--) { int x = s.top(); s.pop(); ans[x] = i; add(x, -inf); x /= k; if (x) { isval(x - 1); } isval(x); if (x + 1 < cnt) { isval(x + 1); } } for (int i = 0; i < n; i++) { cout << ans[i] << ' '; } }
codenet
-1
1,633
s334468625
p04052
1,581,296,773
cpp
Accepted
740
44,544
// 解题常用步骤:转化 找性质 推式子 // 本题第一步转化起到了简化的作用 #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std; const int N = 5e5 + 10, inf = 0x3f3f3f3f; int n, K, a[N], x, ans[N]; int mn[N << 2], deg[N]; priority_queue<int, vector<int>, greater<int> >q; // 用 pq 来求最小拓扑序 vector<int> e[N]; void upd(int x) { mn[x] = min(mn[x << 1], mn[x << 1 | 1]); } void modify(int x, int l, int r, int pos, int v) { if (l == r) { mn[x] = v; return; } int mid = (l + r) >> 1; if (pos <= mid) modify(x << 1, l, mid, pos, v); else modify(x << 1 | 1, mid + 1, r, pos, v); upd(x); } int query(int x, int l, int r, int lx, int rx) { if (rx < l || lx > r) return inf; if (lx <= l && r <= rx) return mn[x]; int mid = (l + r) >> 1; return min(query(x << 1, l, mid, lx, rx), query(x << 1 | 1, mid + 1, r, lx, rx)); } int main() { cin >> n >> K; rep(i, 1, n) scanf("%d", &x), a[x] = i; memset(mn, 0x3f, sizeof(mn)); for (int i = n, j; i >= 1; i--) { if ((j = query(1, 1, n, a[i] + 1, a[i] + K - 1)) != inf) ++deg[a[j]], e[a[i]].push_back(a[j]); if ((j = query(1, 1, n, a[i] - K + 1, a[i] - 1)) != inf) ++deg[a[j]], e[a[i]].push_back(a[j]); modify(1, 1, n, a[i], i); } rep(i, 1, n) if (!deg[i]) q.push(i); int tot = 0; while (q.size()) { int x = q.top(); q.pop(); a[++tot] = x; for (int i = 0; i < e[x].size(); i++) { int y = e[x][i]; if (!(--deg[y])) q.push(y); } } rep(i, 1, n) ans[a[i]] = i; rep(i, 1, n) printf("%d\n", ans[i]); return 0; }
codenet
-1
1,635
s746620924
p04052
1,517,179,497
cpp
Accepted
722
54,400
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<queue> #include<set> #define MN 500000 using namespace std; 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*10+ch-'0',ch=getchar(); return x*f; } int n,K,a[MN+5],b[MN+5],head[MN+5],cnt=0,in[MN+5],c[MN+5],d[MN+5]; struct edge{int to,next;}e[MN*4+5]; inline void ins(int f,int t) { if(f<t) swap(f,t); f=b[f];t=b[t];++in[t]; e[++cnt]=(edge){t,head[f]};head[f]=cnt; } priority_queue<int> q;set<int> st; int main() { n=read();K=read(); for(int i=1;i<=n;++i) b[a[i]=read()]=i; for(int i=1;i<n+K;++i) { // cout<<i<<"begin\n"; set<int>::iterator it,it2; if(i<=n) { // cout<<"Insert"<<a[i]<<endl; st.insert(a[i]); set<int>::iterator it=st.find(a[i]),it2; if(it!=st.begin()) ins(a[i],*(--(it2=it))); if((++(it2=it))!=st.end()) ins(a[i],*it2); } if(i>=K) { // cout<<"erase"<<a[i-K+1]<<endl; it=st.find(a[i-K+1]); if(it!=st.begin()) ins(a[i-K+1],*(--(it2=it))); if((++(it2=it))!=st.end()) ins(a[i-K+1],*it2); st.erase(it); } } for(int i=1;i<=n;++i) if(!in[i]) q.push(i); for(int i=1;i<=n;++i) { int x=q.top();c[n+1-i]=x;q.pop(); for(int i=head[x];i;i=e[i].next) if(!--in[e[i].to]) q.push(e[i].to); } for(int i=1;i<=n;++i) d[c[i]]=i; for(int i=1;i<=n;++i) printf("%d\n",d[i]); return 0; }
codenet
-1
1,644
s010900682
p04052
1,580,400,867
cpp
Accepted
1,067
24,944
#include<bits/stdc++.h> using namespace std; int read(){ int a = 0; char c = getchar(); while(!isdigit(c)) c = getchar(); while(isdigit(c)){a = a * 10 + c - 48; c = getchar();} return a; } const int _ = 2e6 + 7; int N , K , P[_] , tmp[_]; priority_queue < int > q; #define mid ((l + r) >> 1) #define lch (x << 1) #define rch (x << 1 | 1) int mn[_] , mrk[_]; void mark(int x , int v){mn[x] += v; mrk[x] += v;} void down(int x){if(mrk[x]){mark(lch , mrk[x]); mark(rch , mrk[x]); mrk[x] = 0;}} void ins(int x , int l , int r , int t){ if(l == r){mn[x] = 0; return;} down(x); mn[x] = 0; mid >= t ? ins(lch , l , mid , t) : ins(rch , mid + 1 , r , t); } void add(int x , int l , int r , int L , int R , int v){ if(l >= L && r <= R) return mark(x , v); down(x); if(mid >= L) add(lch , l , mid , L , R , v); if(mid < R) add(rch , mid + 1 , r , L , R , v); mn[x] = min(mn[lch] , mn[rch]); } void find(int x , int l , int r){ if(mn[x]) return; if(l == r){mn[x] = 1e9; q.push(l); return;} down(x); find(lch , l , mid); find(rch , mid + 1 , r); mn[x] = min(mn[lch] , mn[rch]); } int main(){ N = read(); K = read(); for(int i = 1 ; i <= N ; ++i) P[read()] = i; for(int i = 1 ; i <= N ; ++i){ add(1 , 1 , N , max(1 , P[i] - K + 1) , min(N , P[i] + K - 1) , 1); ins(1 , 1 , N , P[i]); } find(1 , 1 , N); vector < int > ans; while(!q.empty()){ int t = q.top(); q.pop(); ans.push_back(t); add(1 , 1 , N , max(1 , t - K + 1) , min(N , t + K - 1) , -1); find(1 , 1 , N); } reverse(ans.begin() , ans.end()); for(int i = 0 ; i < 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,645
s766051582
p04052
1,523,547,464
cpp
Accepted
707
44,544
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5, inf = 0x3f3f3f3f; int n, k; int mn[maxn * 4], p[maxn], in[maxn], ans[maxn]; vector<int> G[maxn]; int read() { int x = 0, f = 1; char c = getchar(); while(!isdigit(c)) { if(c == '-') { f = -1; } c = getchar(); } while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x * f; } void update(int l, int r, int x, int p, int d) { if(l == r) { mn[x] = d; return; } int mid = (l + r) >> 1; if(p <= mid) { update(l, mid, x << 1, p, d); } else { update(mid + 1, r, x << 1 | 1, p, d); } mn[x] = min(mn[x << 1], mn[x << 1 | 1]); } int query(int l, int r, int x, int a, int b) { if(l > b || r < a) { return inf; } if(l >= a && r <= b) { return mn[x]; } int mid = (l + r) >> 1; return min(query(l, mid, x << 1, a, b), query(mid + 1, r, x << 1 | 1, a, b)); } int main() { n = read(); k = read(); memset(mn, inf, sizeof(mn)); for(int i = 1; i <= n; ++i) { p[read()] = i; } for(int i = n; i; --i) { int t = query(1, n, 1, p[i] + 1, min(p[i] + k, n)); if(t != inf) { G[p[i]].push_back(p[t]); ++in[p[t]]; } t = query(1, n, 1, max(1, p[i] - k + 1), p[i] - 1); if(t != inf) { G[p[i]].push_back(p[t]); ++in[p[t]]; } update(1, n, 1, p[i], i); } 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(); ans[u] = ++ans[0]; q.pop(); for(int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if(!--in[v]) { q.push(v); } } } for(int i = 1; i <= n; ++i) { printf("%d\n", ans[i]); } return 0; }
codenet
-1
1,654
s500771838
p04052
1,507,584,499
cpp
Accepted
446
27,764
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #define For(i, j, k) for(int i = j; i <= k; i++) #define Forr(i, j, k) for(int i = j; i >= k; i--) using namespace std; const int N = 500010; struct Segment_Tree{ int Min[N << 2]; #define lc (o << 1) #define rc (o << 1 | 1) #define M ((L + R) >> 1) Segment_Tree(){ memset(Min, 0x3f, sizeof Min); } void modify(int o, int L, int R, int x, int v){ if(L == R) Min[o] = v; else{ if(x <= M) modify(lc, L, M, x, v); else modify(rc, M + 1, R, x, v); Min[o] = min(Min[lc], Min[rc]); } } int query(int o, int L, int R, int l, int r){ if(l <= L && R <= r) return Min[o]; else{ int ret = 1e9; if(l <= M) ret = query(lc, L, M, l, r); if(r > M) ret = min(ret, query(rc, M + 1, R, l, r)); return ret; } } }T; int n, k; int Begin[N], Next[N << 1], to[N << 1], deg[N], e; inline void Add(int u, int v){ to[++e] = v, Next[e] = Begin[u], Begin[u] = e, ++deg[v]; } int P[N], A[N]; priority_queue<int> q; void work(){ For(i, 1, n) if(!deg[i]) q.push(i); Forr(i, n, 1){ int s = q.top(); q.pop(); A[s] = i; for(int j = Begin[s]; j; j = Next[j]){ int u = to[j]; deg[u]--; if(!deg[u]) q.push(u); } } For(i, 1, n) printf("%d\n", A[i]); } int main(){ scanf("%d%d", &n, &k); For(i, 1, n){ int x; scanf("%d", &x); P[x] = i; } Forr(i, n, 1){ int x = T.query(1, 1, n, P[i], P[i] + k - 1); if(x <= n) Add(P[x], P[i]); if(k > 1 && P[i] > 1){ x = T.query(1, 1, n, P[i] - k + 1, P[i] - 1); if(x <= n) Add(P[x], P[i]); } T.modify(1, 1, n, P[i], i); } work(); return 0; }
codenet
-1
1,657
s577325111
p04052
1,566,266,531
cpp
Accepted
496
31,352
#include<bits/stdc++.h> using namespace std; const int N=5e5+5; int n,k,pos[N],a[N]; const int INF=0x3f3f3f3f; struct SegT{ int t[N<<2]; void build(int x,int l,int r){ int mid=(l+r)>>1; t[x]=INF;if(l==r)return ; build(x<<1,l,mid);build(x<<1|1,mid+1,r); } void chg(int x,int l,int r,int pos,int sum){ int mid=(l+r)>>1; t[x]=min(t[x],sum); if(l==r)return; if(pos>mid)chg(x<<1|1,mid+1,r,pos,sum); else chg(x<<1,l,mid,pos,sum); } int qry(int x,int l,int r,int ql,int qr){ if(ql>qr)return INF; int mid=(l+r)>>1,ans=INF; if(ql<=l&&qr>=r)return t[x]; if(ql<=mid)ans=min(ans,qry(x<<1,l,mid,ql,qr)); if(qr>mid)ans=min(ans,qry(x<<1|1,mid+1,r,ql,qr)); return ans; } }T; int hed[N],to[N*3],nxt[N*3],cnt,d[N]; inline void adde(int u,int v){swap(u,v);++cnt;to[cnt]=v,nxt[cnt]=hed[u];hed[u]=cnt;d[v]++;} priority_queue<int>Q; int ans[N],top; int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); pos[a[i]]=i; } T.build(1,1,n); for(int i=n;i;i--){ int p=pos[i]; int R=T.qry(1,1,n,p+1,min(n,p+k-1)); int L=T.qry(1,1,n,max(1,p-k+1),p-1); if(R<=n)adde(pos[i],pos[R]); if(L<=n)adde(pos[i],pos[L]); T.chg(1,1,n,pos[i],i); } for(int i=1;i<=n;i++)if(!d[i])Q.push(i); int top=0; while(!Q.empty()){ int u=Q.top();Q.pop(); ans[u]=++top; for(int i=hed[u];i;i=nxt[i]){ int v=to[i];d[v]--; if(!d[v])Q.push(v); } } for(int i=1;i<=n;i++)printf("%d\n",n+1-ans[i]); }
codenet
-1
1,661
s290440679
p04052
1,567,295,432
cpp
Accepted
161
13,312
#include <bits/stdc++.h> #define f first #define s second #define MOD 1000000007 #define PMOD 998244353 #define pb(x) push_back(x) using namespace std; typedef long long int ll; typedef pair<int,int> pii; typedef pair<ll,ll> plii; typedef pair<int, pii> piii; const int INF = 1e9+10; const ll LINF = 1LL*INF*INF; const int MAXN = 5e5+10; const int MAXM = 5e3+10; priority_queue<int> pq; vector<vector<int> > graph; queue<int> que; int A[MAXN]; int P[MAXN]; int res[MAXN]; int tmp[MAXN]; int mnv[MAXN]; void merge_sort(int st,int en,int k) { if(st==en)return; int mid = (st+en)>>1; merge_sort(st,mid,k); merge_sort(mid+1,en,k); for(int i=st;i<=en;i++)tmp[i] = P[i]; mnv[mid] = tmp[mid]; for(int i=mid-1;i>=st;i--) mnv[i] = min(mnv[i+1],tmp[i]); int i = st,j = mid+1; int idx = st; while(i<=mid&&j<=en){ if(mnv[i]<tmp[j]+k){ P[idx] = tmp[i]; idx++; i++; } else { P[idx] = tmp[j]; idx++; j++; } } if(i<=mid){ for(;i<=mid;i++) P[idx++] = tmp[i]; } else { for(;j<=en;j++) P[idx++] = tmp[j]; } return; } int main() { int n,m,k,a,b,x,y,q; int sum = 0; int cnt = 0; int mx = 0; int mn = INF; int cur = 0, idx = -1; int tc; ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>k; for(int i=1;i<=n;i++) cin>>A[i]; for(int i=1;i<=n;i++) P[A[i]] = i; merge_sort(1,n,k); for(int i=1;i<=n;i++) res[P[i]] = i; for(int i=1;i<=n;i++) cout<<res[i]<<"\n"; return 0; }
codenet
-1
1,662
s290440679
p04052
1,567,295,432
cpp
Accepted
161
13,312
#include <bits/stdc++.h> #define f first #define s second #define MOD 1000000007 #define PMOD 998244353 #define pb(x) push_back(x) using namespace std; typedef long long int ll; typedef pair<int,int> pii; typedef pair<ll,ll> plii; typedef pair<int, pii> piii; const int INF = 1e9+10; const ll LINF = 1LL*INF*INF; const int MAXN = 5e5+10; const int MAXM = 5e3+10; priority_queue<int> pq; vector<vector<int> > graph; queue<int> que; int A[MAXN]; int P[MAXN]; int res[MAXN]; int tmp[MAXN]; int mnv[MAXN]; void merge_sort(int st,int en,int k) { if(st==en)return; int mid = (st+en)>>1; merge_sort(st,mid,k); merge_sort(mid+1,en,k); for(int i=st;i<=en;i++)tmp[i] = P[i]; mnv[mid] = tmp[mid]; for(int i=mid-1;i>=st;i--) mnv[i] = min(mnv[i+1],tmp[i]); int i = st,j = mid+1; int idx = st; while(i<=mid&&j<=en){ if(mnv[i]<tmp[j]+k){ P[idx] = tmp[i]; idx++; i++; } else { P[idx] = tmp[j]; idx++; j++; } } if(i<=mid){ for(;i<=mid;i++) P[idx++] = tmp[i]; } else { for(;j<=en;j++) P[idx++] = tmp[j]; } return; } int main() { int n,m,k,a,b,x,y,q; int sum = 0; int cnt = 0; int mx = 0; int mn = INF; int cur = 0, idx = -1; int tc; ios_base::sync_with_stdio(false); cin.tie(NULL); cin>>n>>k; for(int i=1;i<=n;i++) cin>>A[i]; for(int i=1;i<=n;i++) P[A[i]] = i; merge_sort(1,n,k); for(int i=1;i<=n;i++) res[P[i]] = i; for(int i=1;i<=n;i++) cout<<res[i]<<"\n"; return 0; }
codenet
-1
1,662
s348635026
p04052
1,571,174,093
cpp
Accepted
674
64,256
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<map> #include<iostream> using namespace std; #define ll long long #define REP(a,b,c) for(int a=b;a<=c;a++) #define re register #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout) inline int gi(){ int f=1,sum=0;char ch=getchar(); while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return f*sum; } const int N=1000010; int p[N],a[N],n,k,d[N],tot,ans[N]; vector<int>G[N]; priority_queue<int,vector<int>,greater<int> >q; void Add(int u,int v){G[u].push_back(v);d[v]++;} int t[N<<1]; void modify(int o,int l,int r,int pos,int v){ if(l==r){t[o]=v;return;} int mid=(l+r)>>1; if(pos<=mid)modify(o<<1,l,mid,pos,v); else modify(o<<1|1,mid+1,r,pos,v); t[o]=min(t[o<<1],t[o<<1|1]); } int query(int o,int l,int r,int posl,int posr){ if(posl>posr)return t[0]; if(posl<=l && r<=posr)return t[o]; int mid=(l+r)>>1,ret=t[0]; if(posl<=mid)ret=min(ret,query(o<<1,l,mid,posl,posr)); if(mid<posr)ret=min(ret,query(o<<1|1,mid+1,r,posl,posr)); return ret; } int main(){ n=gi();k=gi(); for(int i=1;i<=n;i++)p[i]=gi(),a[p[i]]=i; memset(t,63,sizeof(t)); for(int i=n;i;i--){ int x=query(1,1,n,a[i]+1,min(n,a[i]+k-1)); if(x<t[0])Add(a[i],a[x]); x=query(1,1,n,max(1,a[i]-k+1),a[i]-1); if(x<t[0])Add(a[i],a[x]); modify(1,1,n,a[i],i); } for(int i=1;i<=n;i++)if(!d[i])q.push(i); while(!q.empty()){ int u=q.top();q.pop();ans[u]=++tot; for(int v:G[u]){d[v]--;if(!d[v])q.push(v);} } for(int i=1;i<=n;i++)printf("%d\n",ans[i]); return 0; }
codenet
-1
1,668
s341765635
p04052
1,534,124,841
cpp
Accepted
610
26,240
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #define N (500000+1000) #define INF (10000000) using namespace std; struct Edge{int to,next;}edge[N<<2]; int n,k,a[N],pos[N],Segt[N<<2]; int head[N],num_edge,Ind[N]; priority_queue<int,vector<int>,greater<int> >q; void add(int u,int v) { edge[++num_edge].to=v; edge[num_edge].next=head[u]; head[u]=num_edge; Ind[v]++; } void Build(int now,int l,int r) { if (l==r){Segt[now]=INF; return;} int mid=(l+r)>>1; Build(now<<1,l,mid); Build(now<<1|1,mid+1,r); Segt[now]=min(Segt[now<<1],Segt[now<<1|1]); } int Query(int now,int l,int r,int l1,int r1) { if (r<l1 || l>r1) return INF; if (l1<=l && r<=r1) return Segt[now]; int mid=(l+r)>>1; return min(Query(now<<1,l,mid,l1,r1),Query(now<<1|1,mid+1,r,l1,r1)); } void Update(int now,int l,int r,int x,int k) { if (l==r) {Segt[now]=k; return;} int mid=(l+r)>>1; if (x<=mid) Update(now<<1,l,mid,x,k); else Update(now<<1|1,mid+1,r,x,k); Segt[now]=min(Segt[now<<1],Segt[now<<1|1]); } void Toposort() { int cnt=0; for (int i=1; i<=n; ++i) if (!Ind[i]) q.push(i); while (!q.empty()) { int x=q.top(); q.pop(); a[x]=++cnt; for (int i=head[x]; i; i=edge[i].next) { Ind[edge[i].to]--; if (!Ind[edge[i].to]) q.push(edge[i].to); } } for(int i=1; i<=n; ++i) printf("%d\n",a[i]); } 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=n; i>=1; --i) { int x=Query(1,1,n,pos[i]+1,min(pos[i]+k-1,n)); if (x!=INF) add(pos[i],pos[x]); int y=Query(1,1,n,max(pos[i]-k+1,1),pos[i]-1); if (y!=INF) add(pos[i],pos[y]); Update(1,1,n,pos[i],i); } Toposort(); }
codenet
-1
1,669
s237065533
p04052
1,518,713,005
cpp
Accepted
1,136
46,592
#include<bits/stdc++.h> #define fo(i,a,b) for(i=a;i<=b;i++) #define fd(i,a,b) for(i=a;i>=b;i--) #define min(a,b) (a<b?a:b) #define max(a,b) (a>b?a:b) typedef long long ll; const int maxn=5e5+5; using namespace std; int q[maxn],p[maxn],k,i,j,n,inf,y,a[maxn],d[maxn]; int l[maxn],r[maxn],tree[maxn<<2],now; struct ar{int x,y;}; bool operator <(ar a,ar b){ return a.y==b.y?a.x<b.x:a.y<b.y; } set<ar>w; inline int read(){ int n=0;char c=getchar(); for(;c<'0'||c>'9';c=getchar()); for(;c>='0'&&c<='9';c=getchar()) n=n*10+c-48; return n; } void find(int k,int l,int r,int a,int b){ if (now<=tree[k]) return; if (l==a&&r==b) { now=tree[k]; return; } int m=(l+r)>>1; if (b<=m) find(k<<1,l,m,a,b);else if (a>m) find(k<<1|1,m+1,r,a,b);else find(k<<1,l,m,a,m),find(k<<1|1,m+1,r,m+1,b); } void ins(int k,int l,int r,int x){ tree[k]=min(tree[k],y); if (l==r)return; int m=(l+r)>>1; if (x<=m) ins(k<<1,l,m,x);else ins(k<<1|1,m+1,r,x); } int main(){ n=read(),k=read(); fo(i,1,n) p[i]=read(),q[p[i]]=i; memset(tree,127/3,sizeof(tree)); inf=tree[0]; fd(i,n,1){ int x=q[i]; now=inf,find(1,1,n,max(1,x-k+1),x); if (now!=inf) l[x]=q[now],d[l[x]]++; now=inf,find(1,1,n,x,min(x+k-1,n)); if (now!=inf) r[x]=q[now],d[r[x]]++; y=i,ins(1,1,n,x); } fo(i,1,n) w.insert((ar){i,d[i]}); fo(i,1,n) { ar now=*w.begin(); w.erase(w.begin()); a[i]=now.x; if (i==n) continue; if (l[now.x]) { int x=l[now.x]; w.erase(w.find((ar){x,d[x]})); d[x]--; w.insert((ar){x,d[x]}); } if (r[now.x]) { int x=r[now.x]; w.erase(w.find((ar){x,d[x]})); d[x]--; w.insert((ar){x,d[x]}); } } fo(i,1,n) p[a[i]]=i; fo(i,1,n) printf("%d\n",p[i]); }
codenet
-1
1,672
s067279074
p04052
1,514,360,916
cpp
Accepted
1,403
75,632
#include<vector> #include<queue> #include<functional> #include<algorithm> #include<set> #include<iostream> using namespace std; const int MX = 1<<19; const int MM = 1000000007; typedef long long ll; typedef pair<int,int> pii; struct Tree{ pii t[MX*2]; void update(int A[MX]){ for(int i = 1; i < MX; i++) t[i+MX] = pii(A[i], i); for(int i = MX-1; i >= 1; i--){ t[i] = min(t[i*2], t[i*2+1]); } } void update(int x, int v){ x += MX; t[x] = pii(v, x-MX); while(x > 1){ x /= 2; t[x] = min(t[x*2], t[x*2+1]); } } pii read(int s, int e){ pii r = pii(1e9, -1); s += MX, e += MX; while(s <= e){ if( s&1 ) r = min(r, t[s++]); if(~e&1 ) r = min(r, t[e--]); s /= 2, e /= 2; } return r; } } tree; vector<int> G[MX]; int A[MX], B[MX], C[MX], D[MX]; int N, K; void make_dag(int s, int e, int v = -1){ if( s > e ) return; pii t = tree.read(s, e); if( t.second == -1 ) return; if( v != -1 && v+K > t.first) G[t.first].push_back(v); make_dag(t.second+1, e, v); } int main() { scanf("%d%d", &N, &K); for(int i = 1; i <= N; i++){ scanf("%d", B+i); A[B[i]] = i; } tree.update(A); for(int i = 1; i <= N; i++){ make_dag(1, B[i]-1, i); tree.update(B[i], 1e9); } int cnt[MX] = {}; for(int i = 1; i <= N; i++){ for(int c : G[i]){ cnt[c]++; } } priority_queue<int, vector<int>, greater<int>> Q; vector<int> ans; for(int i = 1; i <= N; i++) if( cnt[i] == 0 ) Q.push(i); while(Q.size()){ int p = Q.top(); Q.pop(); ans.push_back(p); for(int c : G[p]){ cnt[c]--; if( cnt[c] == 0 ) Q.push(c); } } for(int i = 0; i < ans.size(); i++) D[ans[i]] = i+1; for(int i = 1; i <= N; i++) printf("%d\n", D[i]); }
codenet
-1
1,674
s672460545
p04052
1,507,504,183
cpp
Accepted
347
65,920
#include<bits/stdc++.h> using namespace std; const int MAXN = 500010; template<typename T> inline T read() { T x(0), f(1); char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch=='-') f=-1; for(; isdigit(ch); ch = getchar()) x = (x*10)+(ch^48); return x * f; } int n, K, p[MAXN]; int P[MAXN]; namespace Seg_T{ #define ls(p) (p<<1) #define rs(p) (p<<1|1) #define mid ((l+r)>>1) int Max[MAXN<<2]; inline int query(int p, int l, int r, int x, int y) { if(x > y) return 0; if(l == x && r == y) return Max[p]; if(y <= mid) return query(ls(p), l, mid, x, y); if(x > mid) return query(rs(p), mid+1, r, x, y); return max(query(ls(p), l, mid, x, mid), query(rs(p), mid+1, r, mid+1, y)); } inline void modify(int p, int l, int r, int c, int v) { if(l == r) { Max[p] = v; return; } if(c <= mid) modify(ls(p), l, mid, c, v); else modify(rs(p), mid+1, r, c, v); Max[p] = max(Max[ls(p)], Max[rs(p)]); } } int e, ans[MAXN], cnt; vector<int> G[MAXN]; inline void Add(int u, int v) { //printf("%d %d\n", u, v); G[u].push_back(v); } void dfs(int u) { ans[++cnt] = u; sort(G[u].begin(), G[u].end()); for(int i = 0; i < (int)G[u].size(); i++) dfs(G[u][i]); } int main() { n = read<int>(), K = read<int>(); for(int i = 1; i <= n; i++) { p[i] = read<int>(); P[p[i]] = i; } P[n+1] = n+1; Add(n+1, P[1]); Seg_T::modify(1, 1, n, P[1], 1); for(int i = 2; i <= n; i++) { int j = Seg_T::query(1, 1, n, P[i]+1, min(n, P[i]+K-1)); if(!j) j = n+1; Add(P[j], P[i]); Seg_T::modify(1, 1, n, P[i], i); } dfs(n+1); for(int i = 2; i <= cnt; i++) p[ans[i]] = i-1; for(int i = 1; i <= n; i++) printf("%d\n", p[i]); return 0; }
codenet
-1
1,676
s114794697
p04052
1,527,467,765
cpp
Accepted
1,153
34,816
#include <bits/stdc++.h> #include <string> using namespace std; /******************************************* ****************** BY ********************** ******* FUTURE GOOGLE EMPLOYEE ************* ****** KARTHICK KUPPURAJ M ***************** ********************************************/ #define ms(s, n) memset(s, n, sizeof(s)) #define pb push_back #define pf push_front #define mp make_pair #define f first #define s second #define prec(n) fixed<<setprecision(n) #define sll(x) scanf("%lld",&x); #define pll(x) printf("%lld",x); #define pn printf("\n"); #define ps printf(" "); #define pc(c) printf("%c",c); typedef long long int ll; #define EPS 1e-9 const int MOD = (int) 1e9 + 7; const int INF = (int) 1e9; const ll LINF = (ll) 1e18; set<pair<int, int>> st; const int MAX = 5e5 + 2; int p[MAX],t[MAX]; struct Node { int l, r; } a[MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; st.insert(mp(n + 1, n + 1)); for (int i = 1; i <= n; ++i) { cin >> p[i]; st.insert(mp(p[i], i)); if (i - k > 0) { st.erase(mp(p[i - k], i - k)); } t[i] = (*st.upper_bound(mp(p[i],i))).second; //cout<<t[i]<<" "; } int end = n + 1; int open = n + 2; a[open].r = end; a[end].l = open; for (int i = 1; i <= n; ++i) { a[i].r = t[i]; a[i].l = a[t[i]].l; a[a[t[i]].l].r = i; a[t[i]].l = i; } for (int i = a[open].r, c = 1; i != end; i = a[i].r, c++) { p[i] = c; } for (int i = 1; i <= n; ++i) { cout << p[i] << endl; } return 0; }
codenet
-1
1,683
s114794697
p04052
1,527,467,765
cpp
Accepted
1,153
34,816
#include <bits/stdc++.h> #include <string> using namespace std; /******************************************* ****************** BY ********************** ******* FUTURE GOOGLE EMPLOYEE ************* ****** KARTHICK KUPPURAJ M ***************** ********************************************/ #define ms(s, n) memset(s, n, sizeof(s)) #define pb push_back #define pf push_front #define mp make_pair #define f first #define s second #define prec(n) fixed<<setprecision(n) #define sll(x) scanf("%lld",&x); #define pll(x) printf("%lld",x); #define pn printf("\n"); #define ps printf(" "); #define pc(c) printf("%c",c); typedef long long int ll; #define EPS 1e-9 const int MOD = (int) 1e9 + 7; const int INF = (int) 1e9; const ll LINF = (ll) 1e18; set<pair<int, int>> st; const int MAX = 5e5 + 2; int p[MAX],t[MAX]; struct Node { int l, r; } a[MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; st.insert(mp(n + 1, n + 1)); for (int i = 1; i <= n; ++i) { cin >> p[i]; st.insert(mp(p[i], i)); if (i - k > 0) { st.erase(mp(p[i - k], i - k)); } t[i] = (*st.upper_bound(mp(p[i],i))).second; //cout<<t[i]<<" "; } int end = n + 1; int open = n + 2; a[open].r = end; a[end].l = open; for (int i = 1; i <= n; ++i) { a[i].r = t[i]; a[i].l = a[t[i]].l; a[a[t[i]].l].r = i; a[t[i]].l = i; } for (int i = a[open].r, c = 1; i != end; i = a[i].r, c++) { p[i] = c; } for (int i = 1; i <= n; ++i) { cout << p[i] << endl; } return 0; }
codenet
-1
1,683
s965711807
p04052
1,567,310,337
cpp
Accepted
375
21,876
#include <bits/stdc++.h> using namespace std; #define ge getchar() #define Re read() inline int read() { int x = 0, ch; while(!isdigit(ch = ge)) ; while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = ge; return x; } template<typename T> inline void chkmin(T&x, T y) { x = x < y ? x : y; } const int MAXN = 500000; int a[MAXN + 1]; int d[MAXN + 1]; int to[MAXN + 1][2]; int T[MAXN << 2]; int id[MAXN + 1]; priority_queue<int, vector<int>, greater<int> >q; #define ls (nd << 1) #define rs (nd << 1 | 1) inline void Modify(int nd, int l, int r, int p, int v) { T[nd] = v; if(l == r) return ; int mid = (l + r) >> 1; if(p <= mid) Modify(ls, l, mid, p, v); else Modify(rs, mid + 1, r, p, v); } inline int Query(int nd, int l, int r, int L, int R) { if(L <= l && r <= R) return T[nd]; int mid = (l + r) >> 1, res = 2147483647; if(L <= mid) chkmin(res, Query(ls, l, mid, L, R)); if(mid < R) chkmin(res, Query(rs, mid + 1, r, L, R)); return res; } #undef ls #undef rs int main() { int n = Re, k = Re; for(int i = 1; i <= n; i++) a[Re] = i; memset(T, 63, sizeof(T)); for(int i = n; i >= 1; i--) { int L = Query(1, 1, n, max(1, a[i] - k + 1), a[i]); int R = Query(1, 1, n, a[i], min(n, a[i] + k - 1)); if(L <= n && a[i] - a[L] < k) d[a[L]]++, to[a[i]][0] = a[L]; if(R <= n && a[R] - a[i] < k) d[a[R]]++, to[a[i]][1] = a[R]; Modify(1, 1, n, a[i], i); } int x, u, ct = 0; for(int i = 1; i <= n; i++) if(!d[i]) q.push(i); while(!q.empty()) { id[x = q.top()] = ++ct; q.pop(); u = to[x][0]; if(u) { d[u]--; if(!d[u]) q.push(u); } u = to[x][1]; if(u) { d[u]--; if(!d[u]) q.push(u); } } for(int i = 1; i <= n; i++) printf("%d\n", id[i]); return 0; }
codenet
-1
1,688
s965711807
p04052
1,567,310,337
cpp
Accepted
375
21,876
#include <bits/stdc++.h> using namespace std; #define ge getchar() #define Re read() inline int read() { int x = 0, ch; while(!isdigit(ch = ge)) ; while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = ge; return x; } template<typename T> inline void chkmin(T&x, T y) { x = x < y ? x : y; } const int MAXN = 500000; int a[MAXN + 1]; int d[MAXN + 1]; int to[MAXN + 1][2]; int T[MAXN << 2]; int id[MAXN + 1]; priority_queue<int, vector<int>, greater<int> >q; #define ls (nd << 1) #define rs (nd << 1 | 1) inline void Modify(int nd, int l, int r, int p, int v) { T[nd] = v; if(l == r) return ; int mid = (l + r) >> 1; if(p <= mid) Modify(ls, l, mid, p, v); else Modify(rs, mid + 1, r, p, v); } inline int Query(int nd, int l, int r, int L, int R) { if(L <= l && r <= R) return T[nd]; int mid = (l + r) >> 1, res = 2147483647; if(L <= mid) chkmin(res, Query(ls, l, mid, L, R)); if(mid < R) chkmin(res, Query(rs, mid + 1, r, L, R)); return res; } #undef ls #undef rs int main() { int n = Re, k = Re; for(int i = 1; i <= n; i++) a[Re] = i; memset(T, 63, sizeof(T)); for(int i = n; i >= 1; i--) { int L = Query(1, 1, n, max(1, a[i] - k + 1), a[i]); int R = Query(1, 1, n, a[i], min(n, a[i] + k - 1)); if(L <= n && a[i] - a[L] < k) d[a[L]]++, to[a[i]][0] = a[L]; if(R <= n && a[R] - a[i] < k) d[a[R]]++, to[a[i]][1] = a[R]; Modify(1, 1, n, a[i], i); } int x, u, ct = 0; for(int i = 1; i <= n; i++) if(!d[i]) q.push(i); while(!q.empty()) { id[x = q.top()] = ++ct; q.pop(); u = to[x][0]; if(u) { d[u]--; if(!d[u]) q.push(u); } u = to[x][1]; if(u) { d[u]--; if(!d[u]) q.push(u); } } for(int i = 1; i <= n; i++) printf("%d\n", id[i]); return 0; }
codenet
-1
1,688