Cod sursa(job #1655617)

Utilizator cristina_borzaCristina Borza cristina_borza Data 18 martie 2016 09:34:59
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#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);

    scanf("%d%d" , &n , &T);
    while(T--) {
        int ans = 0;
        for(int i = 1 ; i <= n ; ++i) {
            scanf("%d%d%d" , &a[i].x , &a[i].y , &a[i].z);
            //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];
        }
    }
}