Cod sursa(job #3321299)

Utilizator angelaAngela Visuian angela Data 9 noiembrie 2025 08:50:30
Problema Cutii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
 
ifstream fin("cutii.in");
ofstream fout("cutii.out");
 
struct Cutie {
    int x, y, z;
    bool operator < (const Cutie &c) const 
    {
		if (z != c.z) return z < c.z;
		if (x != c.x) return x < c.x;
		return y < c.y;
    }
};
 
const int MAX_N = 3500;
int aib[MAX_N + 10][MAX_N + 10];  // aib[x][y] - nr max de cutii cu _x < x si _y < y 
                                  // care pot fi incuibate una in alta
 
void Update(int cx, int cy, int val) 
{
    for (int x = cx; x <= MAX_N; x += x & -x) 
		for (int y = cy; y <= MAX_N; y += y & -y) 
            aib[x][y] = max(aib[x][y], val);
}
 
int Query(int cx, int cy) 
{
    int ans = 0;
    for (int x = cx; x > 0; x -= x & -x) 
        for (int y = cy; y > 0; y -= y & -y) 
            ans = max(ans, aib[x][y]);
    return ans;
}
 
void Reset(int cx, int cy) 
{
    for (int x = cx; x <= MAX_N; x += x & -x) 
        for (int y = cy; y <= MAX_N; y += y & -y) 
            aib[x][y] = 0;
}
 
int Solve(int n) 
{
    vector<Cutie> C;
    int x, y, z;
 
    for (int i = 1; i <= n; i++) 
    {
        fin >> x >> y >> z;
        C.emplace_back(x, y, z);
    }
    sort(C.begin(), C.end()); // O(n * log2(n)) sortare dupa z
     
    int ans = 0;
    for (const auto& c : C)  // O(n * log^2(n)  
    {
        int d = Query(c.x - 1, c.y - 1) + 1;
        Update(c.x, c.y, d);
        ans = max(ans, d);
    }
     
    for (const auto& c : C)  // O(n * log2(n) * log2(n))
        Reset(c.x, c.y);
 
    return ans;
}
 
int main() 
{
    int n, t;
    fin >> n >> t; 
     
    for(int i = 1; i <= t; i++) 
        fout << Solve(n) << '\n';

    return 0;
}