QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#424963#7859. BladestormlmeowdnML 8ms50736kbC++143.6kb2024-05-29 20:15:172024-05-29 20:15:18

Judging History

你现在查看的是最新测评结果

  • [2024-05-29 20:15:18]
  • 评测
  • 测评结果:ML
  • 用时:8ms
  • 内存:50736kb
  • [2024-05-29 20:15:17]
  • 提交

answer

//Shirasu Azusa 2024.5
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x)  {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x)  {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x)  {return (x==0?-1:__builtin_ctzll(x));}

#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;  
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} 
  return x*w;
}

const int N=2e6+5;

int n,ls[N],rs[N],fa[N],sz[N],tag[N],f[N],g[N],rt,ans[N],a[N],k;
ull w[N];
vi e[N];

void add(int p,int z) {tag[p]+=z;}
void psd(int p) {tag[ls[p]]+=tag[p],tag[rs[p]]+=tag[p],g[p-1]+=tag[p],tag[p]=0;}
void psu(int p) {fa[ls[p]]=fa[rs[p]]=p,fa[p]=0,sz[p]=sz[ls[p]]+sz[rs[p]]+1;}
int merge(int x,int y) {
  if(!x||!y) return x+y; psd(x), psd(y);
  if(w[x]>w[y]) return rs[x]=merge(rs[x],y), psu(x), x;
  else return ls[y]=merge(x,ls[y]), psu(y), y;
}
void split(int x,int k,int &l,int &r) {
  if(!x) {l=r=0; return;} psd(x);
  if(sz[ls[x]]+1>k) r=x, split(ls[x],k,l,ls[x]), psu(x);
  else l=x, split(rs[x],k-sz[ls[x]]-1,rs[x],r), psu(x);
}
int getrk(int x) {
  int res=sz[ls[x]]+1;
  while(fa[x]) {
    if(x==rs[fa[x]]) res+=sz[ls[fa[x]]]+1;
    x=fa[x];
  }
  return res;
}
int qry(int x) {
  int res=g[x]; x++;
  while(x) res+=tag[x], x=fa[x];
  return res;
}
void chgfa(int x,int y,int z) { //fa=x -> tag-z,fa=y
  //cout<<"change father from "<<x<<" to "<<y<<", delta="<<z<<endl;
  int px=getrk(x+1), qx=getrk(x+n+k+2);
  //cout<<"  "<<px<<" "<<qx<<endl;
  if(px+1==qx) return;
  int a,b,c; split(rt,px,a,b), split(b,qx-px-1,b,c);
  rt=merge(a,c); add(b,z);
  //cout<<"QWQ "<<a<<" "<<c<<endl;
  int py=getrk(y+1); split(rt,py,a,c);
  rt=merge(merge(a,b),c);
}
void dfs(int u) {
  rt=merge(rt,u+1);
  for(int v:e[u]) dfs(v);
  rt=merge(rt,u+n+k+2);
}
void print() {
  rep(i,1,2*n+4) {
    cout<<"info of "<<i<<", "<<ls[i]<<" "<<rs[i]<<" "<<sz[i]<<" "<<fa[i]<<" "<<tag[i]<<endl;
  }
}

void work() {
  n=read(), k=read(); mt19937_64 rnd; rt=0;
  rep(i,1,3*n) w[i]=rnd(), e[i].clear(), sz[i]=1;
  rep(i,1,n) a[i]=read();
  rep(i,n,n+k-1) f[i]=n+k, e[f[i]].eb(i);
  rep(i,0,n-1) f[i]=i+k, e[f[i]].eb(i);
  per(i,n+k-1,0) g[i]=g[f[i]]+1;
  //rep(i,0,n) cout<<g[i]<<" "; puts("");
  dfs(n+k);
  //print();
  set<int> q; rep(i,1,n) q.insert(i); q.insert(n+k), q.insert(0);
  per(i,n,1) {
    ans[i]=qry(0)-1; if(i==1) break;
    int x=a[i]; auto it=q.lower_bound(x);
    int l=*prev(it), r=*next(it); q.erase(it);
    //cout<<"WORK "<<l<<" "<<x<<" "<<r<<endl;
    int nw=qry(r); if(l+k<=x) chgfa(x,r,nw-qry(x));
    rep(i,max(x+1,l+k),min(r-1,x-1+k)) chgfa(i,r,nw-qry(i));
    //print();
  }
  rep(i,1,n) printf("%lld ",ans[i]); puts("");
  rep(i,1,3*n) w[i]=ls[i]=rs[i]=tag[i]=f[i]=g[i]=fa[i]=ans[i]=sz[i]=a[i]=ans[i]=0, e[i].clear();
}

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: 8ms
memory: 50736kb

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
Memory 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
...

output:


result: