Cod sursa(job #2774133)

Utilizator popoviciAna16Popovici Ana popoviciAna16 Data 9 septembrie 2021 20:28:59
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

ifstream fin ("cutii.in");
ofstream fout ("cutii.out");

struct etwas
{
    int x, y, z;

    bool operator < (const etwas &alt) const
    {
        return x < alt.x;
    }

} a[3501];

int aib[3501][3501];

void update (int x, int y, int val, int n)
{
    int i, j;
    for (i = x; i<=n; i = i + (i & (-i)))
        for (j = y; j<=n; j = j + (j & (-j)))
            aib[i][j] = max(aib[i][j], val);
}

int intr (int x, int y)
{
    int i, j, rasp = 0;
    for (i = x; i>0; i = i - (i & (-i)))
        for (j = y; j>0; j = j - (j & (-j)))
            rasp = max(rasp, aib[i][j]);
    return rasp;
}

void loeschen (int x, int y, int n)
{
    int i, j;
    for (i = x; i<=n; i = i + (i & (-i)))
        for (j = y; j<=n; j = j + (j & (-j)))
            aib[i][j] = 0;
}

int main()
{
    int t, n, i, val;

    fin >> n >> t;
    for (;t;t--)
    {
        for (i = 1; i<=n; i++)
            fin >> a[i].x >> a[i].y >> a[i].z;

        sort(a+1, a+n+1);
        for (i = 1; i<=n; i++)
        {
            val = 1 + intr (a[i].y-1, a[i].z-1);
            update (a[i].y, a[i].z, val, n);
        }

        fout << intr (n, n) << '\n';

        for (i = 1; i<=n; i++)
            loeschen (a[i].y, a[i].z, n);
    }
    return 0;
}