## 【NOI六连测】[D1T1] 比赛

A[x] < A[y]
B[x] > B[y]
C[x] > C[y]


BIT + 线段树：http://paste.ubuntu.com/18236197/
CDQ分治：http://paste.ubuntu.com/18236240/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;

const int MAXN = 200000+9;

int n,a[MAXN],b[MAXN],c[MAXN],ord[MAXN];
LL vout;

namespace Fenwick_Tree{
#define BIT Fenwick_Tree
#define low(x) (x&(-x))
int arr[MAXN],sum;

inline void init(){
memset(arr,0,sizeof(arr));
sum = 0;}

inline void modify(int pos){
for (int i=pos;i<=n;i+=low(i))
arr[i] += 1; sum++;
}

inline int query(int pos){
int ret = 0;
for (int i=pos;i;i-=low(i))
ret += arr[i];
return sum-ret;
}
};

inline void itv(int *A, int *B){
BIT::init();
for (int i=1;i<=n;i++) ord[A[i]] = i;
for (int j=1,i=ord[j];j<=n;i=ord[++j])
vout += BIT::query(B[i]),
BIT::modify(B[i]);
}

int main(){
freopen("contest.in","r",stdin);
freopen("contest.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d%d%d",&a[i],&b[i],&c[i]);
itv(a,b); itv(b,c); itv(a,c);
printf("%lld\n",vout/2LL);
return 0;
}