QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#288018 | #7859. Bladestorm | ucup-team1447# | TL | 0ms | 15728kb | C++14 | 4.8kb | 2023-12-21 15:55:34 | 2023-12-21 15:55:34 |
Judging History
answer
// what is matter? never mind.
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define int long long
#define ull unsigned long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 500005
#define inf 0x3f3f3f3f
int n,par[maxn],a[maxn],res[maxn],k;
int mx[maxn];
int val[maxn],sum[maxn];
int ch[maxn][2],fa[maxn];
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
bool nrt(int p){
return rs(fa[p])==p||ls(fa[p])==p;
}
bool chk(int p){
return rs(fa[p])==p;
}
void up(int p){
sum[p]=sum[ls(p)]+sum[rs(p)]+val[p];
}
void down(int p){
// if(rev[p]){
// if(ls(p))pr(ls(p));
// if(rs(p))pr(rs(p));
// rev[p]=0;
// }
}
void downall(int p){
// if(nrt(p)) downall(fa[p]);
// down(p);
}
void rot(int x){
int y=fa[x],z=fa[y],k=chk(x),s=ch[x][!k];
fa[x]=z; if(nrt(y)) ch[z][chk(y)]=x;
if(s)fa[s]=y; ch[y][k]=s;
ch[x][!k]=y; fa[y]=x;
up(y),up(x);
}
void splay(int x){
downall(x);
while(nrt(x)){
if(nrt(fa[x]))rot(chk(x)==chk(fa[x])?fa[x]:x);
rot(x);
}
}
void acc(int x){
for(int y=0;x;x=fa[y=x])
splay(x),rs(x)=y,up(x);
}
int find(int l,int r){
int res=l;
while(l){
if(l<=r)res=l,l=ch[l][0];
else l=ch[l][1];
}
// cout<<"res "<<res<<"\n";
splay(res);
return sum[rs(res)]+val[res];
}
void dfs(int u){
if(!u)return;
dfs(ls(u));
cout<<u<<" ";
dfs(rs(u));
}
int ask(int l,int r){
// cout<<"ask "<<l<<" "<<r<<"\n";
acc(l),splay(l);
// dfs(l);cout<<" dfs\n";
return find(l,r);
}
void setfa(int x,int f,int v){
// cout<<"setfa "<<x<<" "<<f<<" "<<v<<"\n";
acc(x),splay(x);
fa[ls(x)]=0,ls(x)=0;
val[x]=v;
up(x);
fa[x]=f;
}
set<int>s;
void change(int l,int r,int x){
// cout<<"mdf "<<l<<" "<<r<<" "<<x<<"\n";
for(auto it=s.lower_bound(l);it!=s.end();it=s.erase(it)){
if((*it)>=r)break;
setfa(*it,*it+1,0);
}
setfa(r,x,1);
s.insert(r);
}
void work()
{
n=read(),k=read();
For(i,1,n)a[i]=read(),mx[i]=max(mx[i-1],a[i]);
For(i,1,n)par[i]=min(n+1,i+k); par[n+1]=0;
For(i,1,n)val[i]=sum[i]=1,fa[i]=par[i];
val[n+1]=sum[n+1]=0,par[n+1]=0;
For(i,0,n+1) ch[i][0]=ch[i][1]=0;
s.clear();
For(i,1,n)s.insert(i);
set<int>st;
For(i,1,n)st.insert(i); st.insert(0); st.insert(n+1);
Rep(i,n,1){
// int now=1,ans=0;
// while(now<=mx[i])now=par[now],++ans;
// res[i]=ans;
res[i]=ask(1,mx[i]);
auto it=st.lower_bound(a[i]);
auto itl=prev(it);
auto itr=next(it);
int l=(*itl)+1,r=a[i],to=(*itr)+1;
to=min(to,n+1);
// For(j,l,r) par[j]=max(par[j],to);
st.erase(it);
r=min(r,to-k-1);
if(l<=r) change(l,r,to);
}
For(i,1,n)cout<<res[i]<<" ";cout<<"\n";
}
signed main()
{
int T=read();
while(T--)work();
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15728kb
input:
3 7 2 4 7 3 6 1 2 5 11 3 10 7 4 9 6 8 5 1 3 2 11 9 2 1 2 3 7 8 9 4 5 6
output:
1 2 3 3 4 4 4 1 2 3 3 3 3 3 4 4 4 4 1 1 2 3 4 4 4 5 5
result:
ok 3 lines
Test #2:
score: -100
Time Limit Exceeded
input:
40319 1 1 1 2 1 1 2 2 1 2 1 2 2 1 2 2 2 2 1 3 1 1 2 3 3 1 1 3 2 3 1 2 1 3 3 1 2 3 1 3 1 3 1 2 3 1 3 2 1 3 2 1 2 3 3 2 1 3 2 3 2 2 1 3 3 2 2 3 1 3 2 3 1 2 3 2 3 2 1 3 3 1 2 3 3 3 1 3 2 3 3 2 1 3 3 3 2 3 1 3 3 3 1 2 3 3 3 2 1 4 1 1 2 3 4 4 1 1 2 4 3 4 1 1 3 2 4 4 1 1 3 4 2 4 1 1 4 2 3 4 1 1 4 3 2 4 1 ...