Cod sursa(job #2055063)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 2 noiembrie 2017 19:58:43
Problema Cutii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include<fstream>
#include<algorithm>
using namespace std;
ifstream fi("cutii.in");
ofstream fo("cutii.out");
typedef struct box{int x,y,z;} BOX;
int n,i,t,T,F[3501][3501],rez;
BOX A[3501];

bool cmp(BOX a, BOX b)
{
    return a.x<b.x;
}

void update(int x, int y, int val)
{
    int y1;
    while(x<=n)
    {
        y1=y;
        while(y1<=n)
        {
            F[x][y1]+=val;
            y1=y1+(y1&(y1^(y1-1)));
        }
        x=x+(x&(x^(x-1)));
    }
}

int query(int x, int y)
{
    int rez=0,y1;
    while(x>=1)
    {
        y1=y;
        while(y1>=1)
        {
            rez+=F[x][y1];
            y1=y1-(y1&(y1^(y1-1)));
        }
        x=x-(x&(x^(x-1)));
    }
    return rez;
}

int main()
{
    fi>>n>>T;
    for(t=1; t<=T; t++)
    {
        rez=0;
        for(i=1; i<=n; i++)
            fi>>A[i].x>>A[i].y>>A[i].z;
        sort(A+1,A+n+1,cmp);
        //acum x-urile sunt in ordine crescatoare, deci trb sa verif doar y si z
        for(i=1; i<=n; i++)
        {
            rez+=query(A[i].y,A[i].z);
            update(A[i].y,A[i].z,1);
        }
        fo<<rez<<"\n";
        for(i=1; i<=n; i++)
            update(A[i].y,A[i].z,-1);
    }
    fi.close();
    fo.close();
    return 0;
}