Cod sursa(job #2910024)

Utilizator vlad2009Vlad Tutunaru vlad2009 Data 17 iunie 2022 17:50:57
Problema Cutii Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <fstream>
#include <algorithm>
#include <string.h>

using namespace std;

const int MAX_N = 3500;
short int aib[MAX_N + 1][MAX_N + 1];

struct cutie {
    int x;
    int y;
    int z;
};

bool operator < (const cutie &a, const cutie &b) {
    return a.x < b.x;
}

cutie a[MAX_N + 1];
int n, t;

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

int query(int x, int y) {
    short int answer = 0;
    for (int i = x; i >= 1; i -= i & -i) {
        for (int j = y; j >= 1; j -= j & -j) {
            answer = max(answer, aib[i][j]);
        }
    }
    return answer;
}

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

int main() {
    InParser fin("cutii.in");
    ofstream fout("cutii.out");
    fin >> n >> t;
    while (t--) {
        for (int i = 1; i <= n; i++) {
            fin >> a[i].x >> a[i].y >> a[i].z;
        }
        sort(a + 1, a + n + 1);
        memset(aib, 0, sizeof(aib));
        int answer = 0;
        for (int i = 1; i <= n; i++) {
            int tmp = query(a[i].y, a[i].z) + 1;
            answer = max(answer, tmp);
            update(a[i].y, a[i].z, tmp);
        }
        fout << answer << "\n";
    }
    return 0;
}