三维偏序
贴个三维偏序的模板吧。注意清空bit的时候操作数不要和序列长线性相关。如果上一个题插入的数是无序的话,大概这么做就行了。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #define maxn 200000 7 #define inf 2147483647 8 using namespace std; 9 struct point{ 10 int x,y,z,s; 11 }a[maxn],q1[maxn],q2[maxn]; 12 int f[maxn]; 13 int n,m; 14 struct bit 15 { 16 int b[maxn]; 17 void clear(int x){ 18 for (int i=x;i;i-=(i&-i)) b[i]=0; 19 } 20 void add(int x,int z){ 21 for (int i=x;i;i-=(i&-i)) b[i]=max(b[i],z); 22 } 23 int ask(int x){ 24 int tmp=0; 25 for (int i=x;i<=n;i+=(i&-i)) tmp=max(b[i],tmp); 26 return tmp; 27 } 28 }s; 29 30 bool cmp1(point a,point b) 31 { return a.x>b.x;} 32 33 bool cmp2(point a,point b) 34 { return a.y>b.y;} 35 36 void solve(int l,int r){ 37 if (l==r) return ; 38 int mid=(l+r)>>1; 39 solve(l,mid); 40 int t1=0,t2=0; 41 for (int i=l;i<=mid;i++) q1[++t1]=a[i]; 42 for (int i=mid+1;i<=r;i++) q2[++t2]=a[i]; 43 sort(q1+1,q1+t1+1,cmp2); 44 sort(q2+1,q2+t2+1,cmp2); 45 int i=1; 46 for (int j=1;j<=t2;j++){ 47 for (;q1[i].y>q2[j].y&&i<=t1;i++) 48 s.add(q1[i].z,f[q1[i].s]); 49 f[q2[j].s]=max(f[q2[j].s],s.ask(q2[j].z)+1); 50 } 51 for (int i=1;i<=t1;i++) 52 s.clear(q1[i].z); 53 solve(mid+1,r); 54 } 55 56 int main() 57 { 58 //freopen("3sort.in","r",stdin); 59 scanf("%d",&n); 60 for (int i=1;i<=n;i++) 61 scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z),a[i].s=i; 62 sort(a+1,a+n+1,cmp1); 63 for (int i=1;i<=n;i++) f[i]=1; 64 solve(1,n); 65 int ans=0; 66 for (int i=1;i<=n;i++) ans=max(ans,f[i]); 67 printf("%d\n",ans); 68 return 0; 69 }
这个写的是从大到小的...
AC without art, no better than WA !