1 solutions
-
0
删数 (delete)
算法 1
有能删的数的时候一定删除最右边的,这样不会让前边本来能删的变成不能删,模拟时间复杂度不超过 。
算法 2
对于 的问题,我们枚举 从 ,如果新添加的 且 ,那么在操作的过程中一定有某一时刻能删除 且不影响其它任何数,所以 就加 1。
这个做法同时能以 的时间复杂度过掉 的测试点。
算法 3
枚举 从 ,可以发现 越小,能删除的一定越多,用一个数据结构维护 取每一个地方的时候的答案,可以发现当 从 变到 的时候是该数据结构上一个前缀加 1。
每次我们就线段树二分找到 的最后位置 ,然后对 区间加 1。
时间复杂度 。
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int n,q,tt,pp; struct node { int l; int r; int id; }; bool cmp(node a,node b) { return a.r<b.r; } int a[1000005]; node qq[1000005]; int ans[1000005]; int tree[4000005],lazy[4000005]; void pushdown(int p,int l,int r) { if(lazy[p]) { lazy[p<<1]+=lazy[p]; lazy[p<<1|1]+=lazy[p]; tree[p<<1]+=lazy[p]; tree[p<<1|1]+=lazy[p]; lazy[p]=0; } } void update(int p,int l,int r,int ql,int qr) { if(ql==l&&qr==r) { tree[p]++; lazy[p]++; return; } pushdown(p,l,r); int mid=(l+r)/2; if(qr<=mid) update(p<<1,l,mid,ql,qr); else if(ql>mid) update(p<<1|1,mid+1,r,ql,qr); else { update(p<<1,l,mid,ql,mid); update(p<<1|1,mid+1,r,mid+1,qr); } tree[p]=max(tree[p<<1],tree[p<<1|1]); } int query(int p,int l,int r,int x) { if(l==r) return tree[p]; pushdown(p,l,r); int mid=(l+r)/2; if(x<=mid) return query(p<<1,l,mid,x); else return query(p<<1|1,mid+1,r,x); } int binarysearch(int p,int l,int r,int x) { if(l==r) return l; pushdown(p,l,r); int mid=(l+r)/2; if(tree[p<<1|1]>=x) return binarysearch(p<<1|1,mid+1,r,x); else return binarysearch(p<<1,l,mid,x); } int main() { freopen("delete.in","r",stdin); freopen("delete.out","w",stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; cin>>q; for(int i=0;i<q;i++) { cin>>qq[i].l>>qq[i].r; qq[i].id=i; } sort(qq,qq+q,cmp); pp=0; for(int i=1;i<=n;i++) { if(a[i]<=i&&a[i]+tree[1]>=i) { tt=binarysearch(1,1,n,i-a[i]); tt=min(tt,i); update(1,1,n,1,tt); } while(pp<q&&qq[pp].r==i) { ans[qq[pp].id]=query(1,1,n,qq[pp].l); pp++; } } for(int i=0;i<q;i++) cout<<ans[i]<<"\n"; return 0; };
- 1
Information
- ID
- 147
- Time
- 2000ms
- Memory
- 512MiB
- Difficulty
- 9
- Tags
- (None)
- # Submissions
- 20
- Accepted
- 4
- Uploaded By