Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 52669 | Accepted: 15347 |
Description
Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
Source
线段树+离散化,还是比较好想的。逆序处理每个区间,询问是否有空位,如果有空位就覆盖整个区间,同时更新答案。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i>=n;i--) #define LL long long #define pa pair<int,int> #define MAXN 20005 using namespace std; struct tree_type { int l,r,sum,tag; }t[MAXN*4]; struct sort_type { int data,pos; }a[MAXN]; int c,n,f[MAXN],ans; int read() { int ret=0,flag=1; char ch=getchar(); while (ch<'0'||ch>'9') { if (ch=='-') flag=-1; ch=getchar(); } while (ch>='0'&&ch<='9') { ret=ret*10+ch-'0'; ch=getchar(); } return ret*flag; } void pushup(int k) { t[k].sum=t[k<<1].sum+t[k<<1|1].sum; } void pushdown(int k) { if (!t[k].tag) return; t[k<<1].tag=t[k<<1|1].tag=1; t[k<<1].sum=t[k<<1|1].sum=0; t[k].tag=0; } void build(int k,int x,int y) { t[k].l=x; t[k].r=y; t[k].tag=0; t[k].sum=y-x+1; if (x==y) return; int mid=(x+y)>>1; build(k<<1,x,mid); build(k<<1|1,mid+1,y); } int getsum(int k,int x,int y) { if (t[k].l==x&&t[k].r==y) return t[k].sum; pushdown(k); int mid=(t[k].l+t[k].r)>>1; if (mid>=y) return getsum(k<<1,x,y); else if (mid+1<=x) return getsum(k<<1|1,x,y); else return getsum(k<<1,x,mid)+getsum(k<<1|1,mid+1,y); } void cover(int k,int x,int y) { if (t[k].l==x&&t[k].r==y) { t[k].tag=1; t[k].sum=0; return; } pushdown(k); int mid=(t[k].l+t[k].r)>>1; if (mid>=y) cover(k<<1,x,y); else if (mid+1<=x) cover(k<<1|1,x,y); else cover(k<<1,x,mid),cover(k<<1|1,mid+1,y); pushup(k); } bool cmp(sort_type x,sort_type y) { return x.data<y.data; } int main() { // freopen("input.in","r",stdin); c=read(); while(c--) { ans=0; n=read(); F(i,0,n-1) { a[i<<1].data=read(); a[i<<1].pos=i<<1; a[i<<1|1].data=read(); a[i<<1|1].pos=i<<1|1; } sort(a,a+2*n,cmp); F(i,0,2*n-1) { if (i>0&&a[i].data==a[i-1].data) f[a[i].pos]=f[a[i-1].pos]; else f[a[i].pos]=i+1; } build(1,1,2*n); D(i,n-1,0) if (getsum(1,f[i<<1],f[i<<1|1])) { ans++; cover(1,f[i<<1],f[i<<1|1]); } printf("%d\n",ans); } }