N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of the i-th lady by Bi, her intellect by Ii and her richness by Ri. Then i-th lady is a probable self-murderer if there is some j-th lady that Bi < Bj, Ii < Ij, Ri < Rj. Find the number of probable self-murderers.
The first line contains one integer N (1 ≤ N ≤ 500000). The second line contains N integer numbers Bi, separated by single spaces. The third and the fourth lines contain sequences Ii and Ri in the same format. It is guaranteed that 0 ≤ Bi, Ii, Ri ≤ 109.
Output the answer to the problem.
3 1 4 2 4 3 2 2 5 3
1
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 #define lson l,m,rt<<1 5 #define rson m+1,r,rt<<1|1 6 #define maxn 500005 7 struct node 8 { 9 int s; 10 }setree[maxn<<2]; 11 struct op{ 12 int a,b,c; 13 }mes[maxn]; 14 int sorted[maxn]; 15 struct{ 16 int num,c; 17 }mess[maxn]; 18 int binsearch(int l,int r,int key) 19 { 20 int m=(l+r)>>1; 21 if(sorted[m]==key) 22 return m; 23 if(key<sorted[m]) 24 return binsearch(l,m-1,key); 25 return binsearch(m+1,r,key); 26 } 27 void build(int l,int r,int rt) 28 { 29 setree[rt].s=0; 30 if(l==r) 31 return ; 32 int m=(l+r)>>1; 33 build(lson); 34 build(rson); 35 } 36 void pushup(int rt) 37 { 38 setree[rt].s=max(setree[rt<<1].s,setree[rt<<1|1].s); 39 } 40 void update(int l,int r,int rt,int num,int c) 41 { 42 if(l==r){ 43 if(setree[rt].s<c) 44 setree[rt].s=c; 45 return; 46 } 47 int m=(l+r)>>1; 48 if(num<=m) 49 update(lson,num,c); 50 else 51 update(rson,num,c); 52 pushup(rt); 53 } 54 int query(int l,int r,int rt,int L,int R) 55 { 56 if(L<=l&&r<=R) 57 return setree[rt].s; 58 int m=(l+r)>>1; 59 int ans=0; 60 if(L<=m) 61 ans=max(query(lson,L,R),ans); 62 if(R>m) 63 ans=max(ans,query(rson,L,R)); 64 return ans; 65 } 66 bool cmp(struct op x1,struct op x2) 67 { 68 return x1.a>x2.a; 69 } 70 void updateing(int k,int n) 71 { 72 for(int i=0;i<=k;i++) 73 update(0,n-1,1,mess[i].num,mess[i].c); 74 } 75 int main() 76 { 77 int n; 78 while(~scanf("%d",&n)){ 79 80 for(int i=0;i<n;i++) 81 scanf("%d",&mes[i].a); 82 for(int i=0;i<n;i++){ 83 scanf("%d",&mes[i].b); 84 sorted[i]=mes[i].b; 85 } 86 for(int i=0;i<n;i++) 87 scanf("%d",&mes[i].c); 88 89 sort(mes,mes+n,cmp); 90 sort(sorted,sorted+n); 91 92 int k=1,ans=0; 93 for(int i=1;i<n;i++) 94 if(sorted[i]!=sorted[i-1]) 95 sorted[k++]=sorted[i]; 96 97 build(0,k-1,1); 98 int num=binsearch(0,k-1,mes[0].b); 99 int cnt=0; 100 mess[0].num=num; 101 mess[0].c=mes[0].c; 102 for(int i=1;i<n;i++){ 103 num=binsearch(0,k-1,mes[i].b); 104 if(mes[i].a!=mes[i-1].a){ 105 updateing(cnt,k); 106 cnt=0; 107 mess[cnt].num=num; 108 mess[cnt].c=mes[i].c; 109 } 110 else{ 111 mess[++cnt].num=num; 112 mess[cnt].c=mes[i].c; 113 } 114 if(num<=k-2&&mes[i].c<query(0,k-1,1,num+1,k-1)) 115 ans++; 116 } 117 printf("%d\n",ans); 118 } 119 return 0; 120 }