Pagini recente » Cod sursa (job #1742104) | Cod sursa (job #901495) | Cod sursa (job #1513011) | Cod sursa (job #1893444) | Cod sursa (job #1655613)
#include <algorithm>
#include <fstream>
#include <cstring>
#include <cstdio>
#define NMAX 4000
using namespace std;
struct type {
int x , y , z;
} a[NMAX];
bool comp(type a , type b) {
return a.x < b.x || (a.x == b.x && a.y > b.y) || (a.x == b.x && a.y == b.y && a.z > b.z);
}
int aib[NMAX][NMAX];
int n , T;
int query(int x , int y);
int lsb(int x);
void update(int x , int y);
void read(int &x) {
int sign = 1;
char ch;
x = 0;
while(!isdigit(ch = getchar())) {
if(ch == '-') {
sign = -1;
}
else {
sign = 1;
}
}
do {
x = x * 10 + ch - '0';
}while(isdigit(ch = getchar()));
x *= sign;
}
int main() {
freopen("cutii.in" , "r" , stdin);
freopen("cutii.out" , "w" , stdout);
read(n) , read(T);
while(T--) {
int ans = 0;
for(int i = 1 ; i <= n ; ++i) {
read(a[i].x);
read(a[i].y);
read(a[i].z);
}
memset(aib , 0 , sizeof(aib));
sort(a + 1 , a + n + 1 , comp);
for(int i = 1 ; i <= n ; ++i) {
ans += query(a[i].y - 1 , a[i].z - 1);
update(a[i].y , a[i].z);
}
printf("%d\n" , ans);
}
return 0;
}
int lsb(int x) {
return x ^ ((x - 1) & x);
}
int query(int x , int y) {
int sol = 0;
for(int i = x ; i >= 1 ; i -= lsb(i)) {
for(int j = y ; j >= 1 ; j -= lsb(j)) {
sol += aib[i][j];
}
}
return sol;
}
void update(int x , int y) {
for(int i = x ; i <= n ; i += lsb(i)) {
for(int j = y ; j <= n ; j += lsb(j)) {
++aib[i][j];
}
}
}