1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| constexpr int N = 1e4+5;
struct node{ int s,id; pii t; };
int n,q;
struct SGT{ node tr[N<<2]; inline void up(int p){ tr[p].s = tr[p<<1].s+tr[p<<1|1].s; tr[p].t = min(tr[p<<1].t,tr[p<<1|1].t); } void upd(int d,int l,int r,int p,node x){ if(d==l && l==r) tr[p] = x; else{ int mid = (l+r)>>1; if(mid>=d) upd(d,l,mid,p<<1,x); else upd(d,mid+1,r,p<<1|1,x); up(p); } } void add(int d,int l,int r,int p,int x){ if(d==l && l==r) tr[p].t.fst+=x; else{ int mid = (l+r)>>1; if(mid>=d) add(d,l,mid,p<<1,x); else add(d,mid+1,r,p<<1|1,x); up(p); } } int _place(int l,int r,int p){ if(l==r) return l; int mid = (l+r)>>1; if(tr[p<<1].s<(mid-l+1)) return _place(l,mid,p<<1); else return _place(mid+1,r,p<<1|1); } int place(){return tr[1].s==n?-1:_place(1,n,1);} node qmin(int l,int r,int p){ if(l==r) return tr[p]; int mid = (l+r)>>1; return tr[p<<1].t<tr[p<<1|1].t?qmin(l,mid,p<<1):qmin(mid+1,r,p<<1|1); } }sgt;
unordered_map<int,int> pl;
void solve(){ read(n),read(q); int ans = 0; for(int i=1;i<=q;i++){ int x;read(x); if(pl[x]){ ++ans; sgt.add(pl[x],1,n,1,1); } else{ int p = sgt.place(); if(p!=-1){ pl[x] = p; sgt.upd(p,1,n,1,{1,x,{0,i}}); } else{ node now = sgt.qmin(1,n,1); sgt.upd(pl[now.id],1,n,1,{1,x,{0,i}}); pl[x] = pl[now.id],pl[now.id] = 0; } } } cout<<ans; }
|